| 1 |
|
package com.atlassian.xwork.interceptors; |
| 2 |
|
|
| 3 |
|
import com.opensymphony.xwork.interceptor.Interceptor; |
| 4 |
|
import com.opensymphony.xwork.ActionInvocation; |
| 5 |
|
import com.opensymphony.xwork.ActionSupport; |
| 6 |
|
import com.opensymphony.xwork.Action; |
| 7 |
|
import com.opensymphony.xwork.ValidationAware; |
| 8 |
|
import com.opensymphony.webwork.ServletActionContext; |
| 9 |
|
import com.atlassian.xwork.RequireSecurityToken; |
| 10 |
|
import com.atlassian.xwork.SimpleXsrfTokenGenerator; |
| 11 |
|
import com.atlassian.xwork.XsrfTokenGenerator; |
| 12 |
|
import com.atlassian.xwork.XWorkVersionSupport; |
| 13 |
|
|
| 14 |
|
import java.lang.reflect.Method; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
@see |
| 29 |
|
@see |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
|
|
|
| 95.7% |
Uncovered Elements: 2 (46) |
Complexity: 14 |
Complexity Density: 0.52 |
|
| 33 |
|
public class XsrfTokenInterceptor implements Interceptor |
| 34 |
|
{ |
| 35 |
|
public static final String REQUEST_PARAM_NAME = "atl_token"; |
| 36 |
|
public static final String CONFIG_PARAM_NAME = "RequireSecurityToken"; |
| 37 |
|
public static final String VALIDATION_FAILED_ERROR_KEY = "atlassian.xwork.xsrf.badtoken"; |
| 38 |
|
public static final String SECURITY_TOKEN_REQUIRED_ERROR_KEY = "atlassian.xwork.xsrf.notoken"; |
| 39 |
|
public static final String OVERRIDE_HEADER_NAME = "X-Atlassian-Token"; |
| 40 |
|
public static final String OVERRIDE_HEADER_VALUE = "no-check"; |
| 41 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 42 |
|
public static enum SecurityLevel |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
|
OPT_IN(false), |
| 46 |
|
|
| 47 |
|
OPT_OUT(true); |
| 48 |
|
|
| 49 |
|
private final boolean defaultProtection; |
| 50 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 51 |
2
|
SecurityLevel(boolean defaultProtection)... |
| 52 |
|
{ |
| 53 |
2
|
this.defaultProtection = defaultProtection; |
| 54 |
|
} |
| 55 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 56 |
6
|
public boolean getDefaultProtection()... |
| 57 |
|
{ |
| 58 |
6
|
return defaultProtection; |
| 59 |
|
} |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
private final XsrfTokenGenerator tokenGenerator; |
| 63 |
|
private final XWorkVersionSupport versionSupport; |
| 64 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
0
|
public XsrfTokenInterceptor(XWorkVersionSupport versionSupport)... |
| 66 |
|
{ |
| 67 |
0
|
this(new SimpleXsrfTokenGenerator(), versionSupport); |
| 68 |
|
} |
| 69 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 70 |
16
|
public XsrfTokenInterceptor(XsrfTokenGenerator tokenGenerator, XWorkVersionSupport versionSupport)... |
| 71 |
|
{ |
| 72 |
16
|
this.tokenGenerator = tokenGenerator; |
| 73 |
16
|
this.versionSupport = versionSupport; |
| 74 |
|
} |
| 75 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 76 |
49
|
public String intercept(ActionInvocation invocation) throws Exception... |
| 77 |
|
{ |
| 78 |
49
|
Method invocationMethod = versionSupport.extractMethod(invocation); |
| 79 |
49
|
String configParam = (String) invocation.getProxy().getConfig().getParams().get(CONFIG_PARAM_NAME); |
| 80 |
49
|
RequireSecurityToken annotation = invocationMethod.getAnnotation(RequireSecurityToken.class); |
| 81 |
|
|
| 82 |
49
|
boolean isProtected = methodRequiresProtection(configParam, annotation); |
| 83 |
49
|
String token = ServletActionContext.getRequest().getParameter(REQUEST_PARAM_NAME); |
| 84 |
49
|
boolean validToken = tokenGenerator.validateToken(ServletActionContext.getRequest(), token); |
| 85 |
|
|
| 86 |
49
|
if (isProtected && !validToken) |
| 87 |
|
{ |
| 88 |
15
|
if (token == null) |
| 89 |
|
{ |
| 90 |
7
|
addInvalidTokenError(versionSupport.extractAction(invocation), SECURITY_TOKEN_REQUIRED_ERROR_KEY); |
| 91 |
|
} |
| 92 |
|
else |
| 93 |
|
{ |
| 94 |
8
|
addInvalidTokenError(versionSupport.extractAction(invocation), VALIDATION_FAILED_ERROR_KEY); |
| 95 |
|
} |
| 96 |
15
|
ServletActionContext.getResponse().setStatus(403); |
| 97 |
15
|
return ActionSupport.INPUT; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
34
|
return invocation.invoke(); |
| 101 |
|
} |
| 102 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 103 |
49
|
private boolean methodRequiresProtection(String configParam, RequireSecurityToken annotation)... |
| 104 |
|
{ |
| 105 |
49
|
if (isOverrideHeaderPresent()) |
| 106 |
6
|
return false; |
| 107 |
43
|
if (configParam != null) |
| 108 |
24
|
return Boolean.valueOf(configParam); |
| 109 |
19
|
else if (annotation != null) |
| 110 |
13
|
return annotation.value(); |
| 111 |
|
else |
| 112 |
6
|
return getSecurityLevel().getDefaultProtection(); |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
@link |
| 119 |
|
|
| 120 |
|
@param |
| 121 |
|
@param |
| 122 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 123 |
15
|
protected void addInvalidTokenError(Action action, String errorMessageKey)... |
| 124 |
|
{ |
| 125 |
15
|
if (action instanceof ValidationAware) |
| 126 |
1
|
((ValidationAware)action).addActionError(internationaliseErrorMessage(action, errorMessageKey)); |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
@param |
| 135 |
|
@param |
| 136 |
|
@return |
| 137 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
1
|
protected String internationaliseErrorMessage(Action action, String messageKey)... |
| 139 |
|
{ |
| 140 |
1
|
return messageKey; |
| 141 |
|
} |
| 142 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
49
|
private boolean isOverrideHeaderPresent()... |
| 144 |
|
{ |
| 145 |
49
|
return OVERRIDE_HEADER_VALUE.equals(ServletActionContext.getRequest().getHeader(OVERRIDE_HEADER_NAME)); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 150 |
|
public void destroy()... |
| 151 |
|
{ |
| 152 |
|
} |
| 153 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 154 |
|
public void init()... |
| 155 |
|
{ |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
@link |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
@return |
| 164 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 165 |
|
protected SecurityLevel getSecurityLevel()... |
| 166 |
|
{ |
| 167 |
|
return SecurityLevel.OPT_IN; |
| 168 |
|
} |
| 169 |
|
} |