| 1 |
|
package com.atlassian.xwork.interceptors; |
| 2 |
|
|
| 3 |
|
import com.atlassian.xwork.RequireSecurityToken; |
| 4 |
|
import com.atlassian.xwork.VersionSupportForTests; |
| 5 |
|
import com.atlassian.xwork.XsrfTokenGenerator; |
| 6 |
|
import com.opensymphony.webwork.ServletActionContext; |
| 7 |
|
import com.opensymphony.webwork.WebWorkStatics; |
| 8 |
|
import com.opensymphony.xwork.Action; |
| 9 |
|
import com.opensymphony.xwork.ActionProxy; |
| 10 |
|
import com.opensymphony.xwork.ActionProxyFactory; |
| 11 |
|
import com.opensymphony.xwork.ActionSupport; |
| 12 |
|
import com.opensymphony.xwork.ValidationAware; |
| 13 |
|
import com.opensymphony.xwork.config.Configuration; |
| 14 |
|
import com.opensymphony.xwork.config.ConfigurationException; |
| 15 |
|
import com.opensymphony.xwork.config.ConfigurationManager; |
| 16 |
|
import com.opensymphony.xwork.config.ConfigurationProvider; |
| 17 |
|
import com.opensymphony.xwork.config.entities.ActionConfig; |
| 18 |
|
import com.opensymphony.xwork.config.entities.PackageConfig; |
| 19 |
|
import com.opensymphony.xwork.interceptor.Interceptor; |
| 20 |
|
|
| 21 |
|
import org.jmock.Mock; |
| 22 |
|
import org.jmock.MockObjectTestCase; |
| 23 |
|
|
| 24 |
|
import javax.servlet.http.HttpServletRequest; |
| 25 |
|
import javax.servlet.http.HttpServletResponse; |
| 26 |
|
|
| 27 |
|
import java.util.ArrayList; |
| 28 |
|
import java.util.Collections; |
| 29 |
|
import java.util.HashMap; |
| 30 |
|
import java.util.List; |
| 31 |
|
import java.util.Map; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (77) |
Complexity: 16 |
Complexity Density: 0.26 |
|
| 36 |
|
public class TestXsrfTokenInterceptor extends MockObjectTestCase |
| 37 |
|
{ |
| 38 |
|
private static final String VALID_TOKEN = "abcdef"; |
| 39 |
|
|
| 40 |
|
private XsrfTokenInterceptor.SecurityLevel securityLevel; |
| 41 |
|
private Mock mockServletRequest; |
| 42 |
|
private Mock mockServletResponce; |
| 43 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
| 44 |
|
public static class DummyAction implements Action |
| 45 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 46 |
10
|
@RequireSecurityToken(true)... |
| 47 |
|
public String withProtection() throws Exception |
| 48 |
|
{ |
| 49 |
10
|
return SUCCESS; |
| 50 |
|
} |
| 51 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 52 |
10
|
@RequireSecurityToken(false)... |
| 53 |
|
public String noProtection() throws Exception |
| 54 |
|
{ |
| 55 |
10
|
return SUCCESS; |
| 56 |
|
|
| 57 |
|
} |
| 58 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 59 |
14
|
public String execute() throws Exception... |
| 60 |
|
{ |
| 61 |
14
|
return SUCCESS; |
| 62 |
|
} |
| 63 |
|
} |
| 64 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
|
public static class ValidateableDummyAction extends ActionSupport |
| 66 |
|
{ |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 67 |
0
|
@RequireSecurityToken(true)... |
| 68 |
|
public String execute() throws Exception |
| 69 |
|
{ |
| 70 |
0
|
return SUCCESS; |
| 71 |
|
} |
| 72 |
|
} |
| 73 |
|
|
|
|
|
| 25% |
Uncovered Elements: 6 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
| 74 |
|
private static class ConstantTokenGenerator implements XsrfTokenGenerator |
| 75 |
|
{ |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 76 |
0
|
public String getToken(HttpServletRequest request, boolean create)... |
| 77 |
|
{ |
| 78 |
0
|
return VALID_TOKEN; |
| 79 |
|
} |
| 80 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 81 |
0
|
public String generateToken(HttpServletRequest request)... |
| 82 |
|
{ |
| 83 |
0
|
return VALID_TOKEN; |
| 84 |
|
} |
| 85 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 86 |
49
|
public boolean validateToken(HttpServletRequest request, String token)... |
| 87 |
|
{ |
| 88 |
49
|
return VALID_TOKEN.equals(token); |
| 89 |
|
} |
| 90 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 91 |
0
|
public String getXsrfTokenName()... |
| 92 |
|
{ |
| 93 |
0
|
return XsrfTokenInterceptor.REQUEST_PARAM_NAME; |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 97 |
|
private class ConfigurableInterceptor extends XsrfTokenInterceptor |
| 98 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 99 |
16
|
public ConfigurableInterceptor()... |
| 100 |
|
{ |
| 101 |
16
|
super(new ConstantTokenGenerator(), new VersionSupportForTests()); |
| 102 |
|
} |
| 103 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 104 |
6
|
protected SecurityLevel getSecurityLevel()... |
| 105 |
|
{ |
| 106 |
6
|
return securityLevel; |
| 107 |
|
} |
| 108 |
|
} |
|
|
|
| 86.4% |
Uncovered Elements: 3 (22) |
Complexity: 6 |
Complexity Density: 0.38 |
|
| 109 |
|
private class CustomConfigurationProvider implements ConfigurationProvider |
| 110 |
|
{ |
| 111 |
|
private List<Interceptor> interceptors; |
| 112 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 113 |
0
|
public void destroy()... |
| 114 |
|
{ |
| 115 |
|
} |
| 116 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 117 |
16
|
public void init(Configuration configuration) throws ConfigurationException... |
| 118 |
|
{ |
| 119 |
16
|
PackageConfig packageConfig = new PackageConfig(); |
| 120 |
16
|
interceptors = new ArrayList<Interceptor>(); |
| 121 |
16
|
interceptors.add(new ConfigurableInterceptor()); |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
16
|
packageConfig.addActionConfig("annotated-protected", makeActionConfig(DummyAction.class, "withProtection")); |
| 127 |
16
|
packageConfig.addActionConfig("annotated-notprotected", makeActionConfig(DummyAction.class, "noProtection")); |
| 128 |
16
|
packageConfig.addActionConfig("unannotated", makeActionConfig(DummyAction.class, "execute")); |
| 129 |
|
|
| 130 |
16
|
packageConfig.addActionConfig("configured-protected", makeActionConfig(DummyAction.class, "execute", "true")); |
| 131 |
16
|
packageConfig.addActionConfig("configured-notprotected", makeActionConfig(DummyAction.class, "execute", "false")); |
| 132 |
|
|
| 133 |
16
|
packageConfig.addActionConfig("override-annotation-notprotected", makeActionConfig(DummyAction.class, "withProtection", "false")); |
| 134 |
16
|
packageConfig.addActionConfig("override-annotation-protected", makeActionConfig(DummyAction.class, "noProtection", "true")); |
| 135 |
|
|
| 136 |
16
|
packageConfig.addActionConfig("validation-aware", makeActionConfig(ValidateableDummyAction.class, "execute")); |
| 137 |
|
|
| 138 |
16
|
configuration.addPackageConfig("defaultPackage", packageConfig); |
| 139 |
|
} |
| 140 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
64
|
private ActionConfig makeActionConfig(Class<? extends Action> actionClass, String methodName, String permittedMethodsParameter)... |
| 142 |
|
{ |
| 143 |
64
|
return makeActionConfig(actionClass, methodName, Collections.singletonMap(XsrfTokenInterceptor.CONFIG_PARAM_NAME, permittedMethodsParameter)); |
| 144 |
|
} |
| 145 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 146 |
128
|
private ActionConfig makeActionConfig(Class<? extends Action> actionClass, String methodName, Map<?,?> parameterMap)... |
| 147 |
|
{ |
| 148 |
128
|
return new ActionConfig(methodName, actionClass, parameterMap, new HashMap<String,Object>(), interceptors); |
| 149 |
|
} |
| 150 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 151 |
64
|
private ActionConfig makeActionConfig(Class<? extends Action> actionClass, String methodName)... |
| 152 |
|
{ |
| 153 |
64
|
return makeActionConfig(actionClass, methodName, new HashMap<String,Object>()); |
| 154 |
|
} |
| 155 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 156 |
0
|
public boolean needsReload()... |
| 157 |
|
{ |
| 158 |
0
|
return false; |
| 159 |
|
} |
| 160 |
|
} |
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 161 |
8
|
@Override... |
| 162 |
|
protected void setUp() throws Exception |
| 163 |
|
{ |
| 164 |
8
|
super.setUp(); |
| 165 |
|
|
| 166 |
8
|
ConfigurationManager.addConfigurationProvider(new CustomConfigurationProvider()); |
| 167 |
8
|
ConfigurationManager.getConfiguration().reload(); |
| 168 |
|
|
| 169 |
8
|
mockServletRequest = new Mock(HttpServletRequest.class); |
| 170 |
8
|
mockServletResponce = new Mock(HttpServletResponse.class); |
| 171 |
8
|
mockServletResponce.stubs().method("setStatus").with(eq(403)); |
| 172 |
8
|
ServletActionContext.setResponse((HttpServletResponse) mockServletResponce.proxy()); |
| 173 |
8
|
mockServletRequest.stubs().method("getHeader").with(eq(XsrfTokenInterceptor.OVERRIDE_HEADER_NAME)).will(returnValue(null)); |
| 174 |
8
|
ServletActionContext.setRequest((HttpServletRequest) mockServletRequest.proxy()); |
| 175 |
|
} |
| 176 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 177 |
8
|
@Override... |
| 178 |
|
protected void tearDown() throws Exception |
| 179 |
|
{ |
| 180 |
8
|
ServletActionContext.setRequest(null); |
| 181 |
8
|
ConfigurationManager.destroyConfiguration(); |
| 182 |
8
|
ConfigurationManager.clearConfigurationProviders(); |
| 183 |
|
|
| 184 |
8
|
mockServletRequest = null; |
| 185 |
8
|
mockServletResponce = null; |
| 186 |
8
|
super.tearDown(); |
| 187 |
|
} |
| 188 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 189 |
1
|
public void testWithValidation() throws Exception... |
| 190 |
|
{ |
| 191 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_IN; |
| 192 |
1
|
testInterceptor("validation-aware", "cheese", Action.INPUT, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 193 |
|
} |
| 194 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 195 |
1
|
public void testNoTokenOptOut() throws Exception... |
| 196 |
|
{ |
| 197 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_OUT; |
| 198 |
1
|
testOnlyUnprotectedActionsSucceedWithToken(null); |
| 199 |
1
|
testInterceptor("unannotated", null, ActionSupport.INPUT, XsrfTokenInterceptor.SECURITY_TOKEN_REQUIRED_ERROR_KEY); |
| 200 |
|
} |
| 201 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 202 |
1
|
public void testNoTokenOptIn() throws Exception... |
| 203 |
|
{ |
| 204 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_IN; |
| 205 |
1
|
testOnlyUnprotectedActionsSucceedWithToken(null); |
| 206 |
1
|
testInterceptor("unannotated", null, Action.SUCCESS, XsrfTokenInterceptor.SECURITY_TOKEN_REQUIRED_ERROR_KEY); |
| 207 |
|
} |
| 208 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 209 |
1
|
public void testCorrectTokenOptIn() throws Exception... |
| 210 |
|
{ |
| 211 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_IN; |
| 212 |
1
|
testAllActionsSucceedWithToken(VALID_TOKEN); |
| 213 |
1
|
testInterceptor("unannotated", VALID_TOKEN, Action.SUCCESS, null); |
| 214 |
|
} |
| 215 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 216 |
1
|
public void testCorrectTokenOptOut() throws Exception... |
| 217 |
|
{ |
| 218 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_OUT; |
| 219 |
1
|
testAllActionsSucceedWithToken(VALID_TOKEN); |
| 220 |
1
|
testInterceptor("unannotated", VALID_TOKEN, Action.SUCCESS, null); |
| 221 |
|
} |
| 222 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 223 |
1
|
public void testIncorrectTokenOptIn() throws Exception... |
| 224 |
|
{ |
| 225 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_IN; |
| 226 |
1
|
testOnlyUnprotectedActionsSucceedWithToken("cheese"); |
| 227 |
1
|
testInterceptor("unannotated", VALID_TOKEN, Action.SUCCESS, null); |
| 228 |
|
} |
| 229 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 230 |
1
|
public void testIncorrectTokenOptOut() throws Exception... |
| 231 |
|
{ |
| 232 |
1
|
securityLevel = XsrfTokenInterceptor.SecurityLevel.OPT_OUT; |
| 233 |
1
|
testOnlyUnprotectedActionsSucceedWithToken("cheese"); |
| 234 |
1
|
testInterceptor("unannotated", "cheese", Action.INPUT, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 235 |
|
} |
| 236 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 237 |
1
|
public void testCorrectTokenWithOverrideHeader() throws Exception... |
| 238 |
|
{ |
| 239 |
1
|
mockServletRequest.stubs().method("getHeader").with(eq(XsrfTokenInterceptor.OVERRIDE_HEADER_NAME)).will(returnValue(XsrfTokenInterceptor.OVERRIDE_HEADER_VALUE)); |
| 240 |
|
|
| 241 |
1
|
testAllActionsSucceedWithToken("badtoken"); |
| 242 |
|
} |
| 243 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 244 |
3
|
private void testAllActionsSucceedWithToken(String token) throws Exception... |
| 245 |
|
{ |
| 246 |
3
|
testInterceptor("annotated-protected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 247 |
3
|
testInterceptor("annotated-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 248 |
3
|
testInterceptor("configured-protected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 249 |
3
|
testInterceptor("configured-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 250 |
3
|
testInterceptor("override-annotation-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 251 |
3
|
testInterceptor("override-annotation-protected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 252 |
|
} |
| 253 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 254 |
4
|
private void testOnlyUnprotectedActionsSucceedWithToken(String token) throws Exception... |
| 255 |
|
{ |
| 256 |
4
|
testInterceptor("annotated-protected", token, Action.INPUT, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 257 |
4
|
testInterceptor("annotated-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 258 |
4
|
testInterceptor("configured-protected", token, Action.INPUT, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 259 |
4
|
testInterceptor("configured-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 260 |
4
|
testInterceptor("override-annotation-notprotected", token, Action.SUCCESS, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 261 |
4
|
testInterceptor("override-annotation-protected", token, Action.INPUT, XsrfTokenInterceptor.VALIDATION_FAILED_ERROR_KEY); |
| 262 |
|
} |
| 263 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 264 |
49
|
private void testInterceptor(String actionAlias, String token, String expectedResult, String messageErrorKey) throws Exception... |
| 265 |
|
{ |
| 266 |
49
|
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", actionAlias, makeContext(), false); |
| 267 |
49
|
mockServletRequest.stubs().method("getParameter").with(eq(XsrfTokenInterceptor.REQUEST_PARAM_NAME)).will(returnValue(token)); |
| 268 |
49
|
String result = proxy.execute(); |
| 269 |
49
|
assertEquals("Testing " + actionAlias + " with token " + token, expectedResult, result); |
| 270 |
|
|
| 271 |
49
|
if (Action.INPUT.equals(result) && proxy.getAction() instanceof ValidationAware) |
| 272 |
|
{ |
| 273 |
1
|
ValidationAware validateable = (ValidationAware) proxy.getAction(); |
| 274 |
1
|
assertTrue(validateable.hasActionErrors()); |
| 275 |
1
|
assertEquals(messageErrorKey, validateable.getActionErrors().toArray()[0]); |
| 276 |
|
} |
| 277 |
|
} |
| 278 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 279 |
49
|
private HashMap<String,Object> makeContext()... |
| 280 |
|
{ |
| 281 |
49
|
HashMap<String, Object> context = new HashMap<String, Object>(); |
| 282 |
49
|
context.put(WebWorkStatics.HTTP_REQUEST, mockServletRequest.proxy()); |
| 283 |
49
|
context.put(WebWorkStatics.HTTP_RESPONSE, mockServletResponce.proxy()); |
| 284 |
49
|
return context; |
| 285 |
|
} |
| 286 |
|
} |