| 1 |
|
package com.atlassian.xwork.interceptors; |
| 2 |
|
|
| 3 |
|
import com.opensymphony.xwork.interceptor.Interceptor; |
| 4 |
|
import com.opensymphony.xwork.ActionInvocation; |
| 5 |
|
import com.opensymphony.webwork.ServletActionContext; |
| 6 |
|
import com.atlassian.xwork.PermittedMethods; |
| 7 |
|
import com.atlassian.xwork.HttpMethod; |
| 8 |
|
|
| 9 |
|
import javax.servlet.http.HttpServletRequest; |
| 10 |
|
import java.lang.reflect.Method; |
| 11 |
|
import java.util.List; |
| 12 |
|
import java.util.ArrayList; |
| 13 |
|
import java.util.Arrays; |
| 14 |
|
|
| 15 |
|
import org.apache.log4j.Logger; |
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
@link |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
@link |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@since |
| 42 |
|
|
|
|
|
| 95% |
Uncovered Elements: 2 (40) |
Complexity: 11 |
Complexity Density: 0.42 |
|
| 43 |
|
public abstract class RestrictHttpMethodInterceptor implements Interceptor |
| 44 |
|
{ |
| 45 |
|
private static final Logger log = Logger.getLogger(RestrictHttpMethodInterceptor.class); |
| 46 |
|
public static final String INVALID_METHOD_RESULT = "invalidmethod"; |
| 47 |
|
public static final String PERMITTED_METHODS_PARAM_NAME = "permittedMethods"; |
| 48 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 10 |
Complexity Density: 0.71 |
|
| 49 |
|
public static enum SecurityLevel { |
| 50 |
|
|
| 51 |
|
NONE |
| 52 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 53 |
24
|
@Override... |
| 54 |
|
public boolean isPermitted(String invocationMethodName, HttpMethod[] permittedMethods, String httpMethod) |
| 55 |
|
{ |
| 56 |
24
|
return true; |
| 57 |
|
} |
| 58 |
|
}, |
| 59 |
|
|
| 60 |
|
OPT_IN |
| 61 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 62 |
67
|
@Override... |
| 63 |
|
public boolean isPermitted(String invocationMethodName, HttpMethod[] permittedMethods, String httpMethod) |
| 64 |
|
{ |
| 65 |
67
|
if (permittedMethods.length == 0) |
| 66 |
13
|
return true; |
| 67 |
|
|
| 68 |
54
|
return methodMatches(httpMethod, permittedMethods); |
| 69 |
|
} |
| 70 |
|
}, |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
DEFAULT |
| 76 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 77 |
66
|
@Override... |
| 78 |
|
public boolean isPermitted(String invocationMethodName, HttpMethod[] permittedMethods, String httpMethod) |
| 79 |
|
{ |
| 80 |
66
|
if (permittedMethods.length == 0) |
| 81 |
|
{ |
| 82 |
13
|
if (invocationMethodName.equals("doDefault")) |
| 83 |
5
|
return methodMatches(httpMethod, HttpMethod.GET, HttpMethod.POST); |
| 84 |
|
else |
| 85 |
8
|
return methodMatches(httpMethod, HttpMethod.POST); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
53
|
return methodMatches(httpMethod, permittedMethods); |
| 89 |
|
} |
| 90 |
|
}, |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
STRICT |
| 95 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 96 |
65
|
@Override... |
| 97 |
|
public boolean isPermitted(String invocationMethodName, HttpMethod[] permittedMethods, String httpMethod) |
| 98 |
|
{ |
| 99 |
65
|
return methodMatches(httpMethod, permittedMethods); |
| 100 |
|
} |
| 101 |
|
}; |
| 102 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 103 |
185
|
private static boolean methodMatches(String httpMethod, HttpMethod... allowedMethods)... |
| 104 |
|
{ |
| 105 |
185
|
for (HttpMethod allowedMethod : allowedMethods) |
| 106 |
|
{ |
| 107 |
249
|
if (allowedMethod.matches(httpMethod)) |
| 108 |
104
|
return true; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
81
|
return false; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
public abstract boolean isPermitted(String invocationMethodName, HttpMethod[] permittedMethods, String httpMethod); |
| 115 |
|
} |
| 116 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
| 117 |
222
|
public final String intercept(ActionInvocation invocation) throws Exception... |
| 118 |
|
{ |
| 119 |
222
|
Method invocationMethod = invocation.getProxy().getConfig().getMethod(); |
| 120 |
222
|
String configParam = (String) invocation.getProxy().getConfig().getParams().get(PERMITTED_METHODS_PARAM_NAME); |
| 121 |
222
|
PermittedMethods annotation = invocationMethod.getAnnotation(PermittedMethods.class); |
| 122 |
222
|
HttpMethod[] permittedMethods = toPermittedMethodArray(configParam, annotation); |
| 123 |
|
|
| 124 |
222
|
String httpMethod = getHttpMethod(); |
| 125 |
|
|
| 126 |
222
|
if (log.isDebugEnabled()) |
| 127 |
0
|
log.debug("Checking HTTP method: " + getHttpMethod() + " permitted against " + fullMethodName(invocationMethod)); |
| 128 |
|
|
| 129 |
222
|
if (getSecurityLevel().isPermitted(invocationMethod.getName(), permittedMethods, httpMethod)) |
| 130 |
|
{ |
| 131 |
141
|
log.debug("Invocation proceeding"); |
| 132 |
141
|
return invocation.invoke(); |
| 133 |
|
} |
| 134 |
|
else |
| 135 |
|
{ |
| 136 |
|
|
| 137 |
81
|
log.info("Refusing HTTP method: " + httpMethod + " against " + fullMethodName(invocationMethod) + " (configured allowed methods: " + Arrays.toString(permittedMethods) + ")"); |
| 138 |
81
|
return INVALID_METHOD_RESULT; |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 142 |
222
|
private HttpMethod[] toPermittedMethodArray(String configParam, PermittedMethods annotation)... |
| 143 |
|
{ |
| 144 |
222
|
if (configParam != null && configParam.trim().length() > 0) |
| 145 |
|
{ |
| 146 |
78
|
String[] methodNames = configParam.trim().split("\\s*,\\s*"); |
| 147 |
78
|
List<HttpMethod> permittedMethods = new ArrayList<HttpMethod>(methodNames.length); |
| 148 |
78
|
for (String methodName : methodNames) |
| 149 |
|
{ |
| 150 |
162
|
try |
| 151 |
|
{ |
| 152 |
162
|
permittedMethods.add(HttpMethod.valueOf(methodName)); |
| 153 |
|
} |
| 154 |
|
catch (IllegalArgumentException e) |
| 155 |
|
{ |
| 156 |
18
|
log.error("XWork configuration error: " + methodName + " is not a recognised HTTP method (method names are case sensitive)."); |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| 160 |
78
|
return permittedMethods.toArray(new HttpMethod[permittedMethods.size()]); |
| 161 |
|
} |
| 162 |
144
|
else if (annotation != null) |
| 163 |
|
{ |
| 164 |
94
|
return annotation.value(); |
| 165 |
|
} |
| 166 |
|
else |
| 167 |
|
{ |
| 168 |
50
|
return new HttpMethod[0]; |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 172 |
81
|
private String fullMethodName(Method invocationMethod)... |
| 173 |
|
{ |
| 174 |
81
|
return invocationMethod.getDeclaringClass().getName() + "#" + invocationMethod.getName(); |
| 175 |
|
} |
| 176 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 177 |
222
|
private String getHttpMethod()... |
| 178 |
|
{ |
| 179 |
222
|
HttpServletRequest servletRequest = ServletActionContext.getRequest(); |
| 180 |
222
|
return servletRequest == null ? "" : servletRequest.getMethod(); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 184 |
|
public final void destroy()... |
| 185 |
|
{ |
| 186 |
|
} |
| 187 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 188 |
|
public final void init()... |
| 189 |
|
{ |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
@return |
| 198 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 199 |
|
protected SecurityLevel getSecurityLevel()... |
| 200 |
|
{ |
| 201 |
|
return SecurityLevel.DEFAULT; |
| 202 |
|
} |
| 203 |
|
} |