|
31 | 31 | import org.apache.struts2.RequestUtils; |
32 | 32 | import org.apache.struts2.ServletActionContext; |
33 | 33 | import org.apache.struts2.StrutsConstants; |
34 | | -import org.apache.struts2.StrutsException; |
35 | 34 | import org.apache.struts2.util.PrefixTrie; |
36 | 35 |
|
37 | 36 | import javax.servlet.http.HttpServletRequest; |
@@ -117,6 +116,10 @@ public class DefaultActionMapper implements ActionMapper { |
117 | 116 | protected boolean allowSlashesInActionNames = false; |
118 | 117 | protected boolean alwaysSelectFullNamespace = false; |
119 | 118 | protected PrefixTrie prefixTrie = null; |
| 119 | + |
| 120 | + protected Pattern allowedNamespaceNames = Pattern.compile("[a-zA-Z0-9._/\\-]*"); |
| 121 | + protected String defaultNamespaceName = "/"; |
| 122 | + |
120 | 123 | protected Pattern allowedActionNames = Pattern.compile("[a-zA-Z0-9._!/\\-]*"); |
121 | 124 | protected String defaultActionName = "index"; |
122 | 125 |
|
@@ -202,6 +205,16 @@ public void setAlwaysSelectFullNamespace(String alwaysSelectFullNamespace) { |
202 | 205 | this.alwaysSelectFullNamespace = BooleanUtils.toBoolean(alwaysSelectFullNamespace); |
203 | 206 | } |
204 | 207 |
|
| 208 | + @Inject(value = StrutsConstants.STRUTS_ALLOWED_NAMESPACE_NAMES, required = false) |
| 209 | + public void setAllowedNamespaceNames(String allowedNamespaceNames) { |
| 210 | + this.allowedNamespaceNames = Pattern.compile(allowedNamespaceNames); |
| 211 | + } |
| 212 | + |
| 213 | + @Inject(value = StrutsConstants.STRUTS_DEFAULT_NAMESPACE_NAME, required = false) |
| 214 | + public void setDefaultNamespaceName(String defaultNamespaceName) { |
| 215 | + this.defaultNamespaceName = defaultNamespaceName; |
| 216 | + } |
| 217 | + |
205 | 218 | @Inject(value = StrutsConstants.STRUTS_ALLOWED_ACTION_NAMES, required = false) |
206 | 219 | public void setAllowedActionNames(String allowedActionNames) { |
207 | 220 | this.allowedActionNames = Pattern.compile(allowedActionNames); |
@@ -389,10 +402,28 @@ protected void parseNameAndNamespace(String uri, ActionMapping mapping, Configur |
389 | 402 | } |
390 | 403 | } |
391 | 404 |
|
392 | | - mapping.setNamespace(namespace); |
| 405 | + mapping.setNamespace(cleanupNamespaceName(namespace)); |
393 | 406 | mapping.setName(cleanupActionName(name)); |
394 | 407 | } |
395 | 408 |
|
| 409 | + /** |
| 410 | + * Checks namespace name against allowed pattern if not matched returns default namespace |
| 411 | + * |
| 412 | + * @param rawNamespace name extracted from URI |
| 413 | + * @return safe namespace name |
| 414 | + */ |
| 415 | + protected String cleanupNamespaceName(final String rawNamespace) { |
| 416 | + if (allowedNamespaceNames.matcher(rawNamespace).matches()) { |
| 417 | + return rawNamespace; |
| 418 | + } else { |
| 419 | + LOG.warn( |
| 420 | + "{} did not match allowed namespace names {} - default namespace {} will be used!", |
| 421 | + rawNamespace, allowedActionNames, defaultActionName |
| 422 | + ); |
| 423 | + return defaultNamespaceName; |
| 424 | + } |
| 425 | + } |
| 426 | + |
396 | 427 | /** |
397 | 428 | * Checks action name against allowed pattern if not matched returns default action name |
398 | 429 | * |
|
0 commit comments