SaferSlingPostValidatorImpl.java
15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.text.Text
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.Resource
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.foundation.security.impl;
import com.day.cq.wcm.foundation.forms.attachments.AttachmentDataSource;
import com.day.cq.wcm.foundation.security.AttachmentTypeBlacklistService;
import com.day.cq.wcm.foundation.security.SaferSlingPostValidator;
import com.day.text.Text;
import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%security.saferslingpostvalidator.name", description="%security.saferslingpostvalidator.description", metatype=1, configurationPid="com.day.cq.wcm.foundation.security.SaferSlingPostValidatorImpl")
@Service(serviceFactory=1, value={SaferSlingPostValidator.class})
@Properties(value={@Property(name="service.description", value={"Safer Sling Post Validator"}, propertyPrivate=1)})
public class SaferSlingPostValidatorImpl
implements SaferSlingPostValidator {
private static final String OPERATION_PARAMETER = ":operation";
private static final String TYPEHINT_SUFFIX = "@TypeHint";
private static final String RESOURCETYPE_PARAMETER = "sling:resourceType";
private static final String RESOURCESUPERTYPE_PARAMETER = "sling:resourceSuperType";
private static final String JCR_PRIMARYTYPE = "jcr:primaryType";
private static final String JCR_MIXINTYPES = "jcr:mixinTypes";
private static final String BINARY = "Binary";
protected final Logger logger;
@Property(value={"jcr:description", "jcr:title", "jcr:lastModified", "jcr:primaryType", "jcr:mixinTypes", "sling:resourceType", "sling:resourceSuperType", "cq:tags"}, label="%security.saferslingpostvalidator.whitelist.name", description="%security.saferslingpostvalidator.whitelist.description")
private static final String PARAMETER_WHITELIST = "parameter.whitelist";
@Property(value={}, label="%security.saferslingpostvalidator.prefix.name", description="%security.saferslingpostvalidator.prefix.description", cardinality=Integer.MAX_VALUE)
private static final String PARAMETER_WHITELIST_PREFIXES = "parameter.whitelist.prefixes";
@Property(value={}, label="%security.saferslingpostvalidator.binary.name", description="%security.saferslingpostvalidator.binary.description", cardinality=Integer.MAX_VALUE)
private static final String BINARY_PARAMETER_WHITELIST = "binary.parameter.whitelist";
@Property(value={"@TypeHint", "@DefaultValue", "@UseDefaultWhenMissing", "@IgnoreBlanks", "@ValueFrom", "@Delete", "@Patch"}, label="%security.saferslingpostvalidator.modifier.name", description="%security.saferslingpostvalidator.modifier.description")
private static final String MODIFIER_WHITELIST = "modifier.whitelist";
@Property(value={"delete", "nop"}, label="%security.saferslingpostvalidator.operation.name", description="%security.saferslingpostvalidator.operation.description")
private static final String OPERATION_WHITELIST = "operation.whitelist";
@Property(value={}, label="%security.saferslingpostvalidator.operation.prefix.name", description="%security.saferslingpostvalidator.operation.prefix.description", cardinality=Integer.MAX_VALUE)
private static final String OPERATION_WHITELIST_PREFIXES = "operation.whitelist.prefixes";
@Property(value={"cq:CalendarEvent", "nt:unstructured", "nt:folder", "nt:file", "nt:resource", "sling:Folder", "sling:OrderedFolder", "Binary", "Boolean", "Date", "Double", "Long", "Name", "Path", "String", "String[]"}, label="%security.saferslingpostvalidator.typehint.name", description="%security.saferslingpostvalidator.typehint.description")
private static final String TYPEHINT_WHITELIST = "typehint.whitelist";
@Property(value={}, label="%security.saferslingpostvalidator.resourcetype.name", description="%security.saferslingpostvalidator.resourcetype.description", cardinality=Integer.MAX_VALUE)
private static final String RESOURCETYPE_WHITELIST = "resourcetype.whitelist";
@Reference
private AttachmentTypeBlacklistService attachmentTypeBlacklist;
private Set<String> parameterWhiteList;
private Set<String> parameterWhiteListPrefixes;
private Set<String> binaryParameterWhiteList;
private Set<String> modifierWhiteList;
private Set<String> operationWhiteList;
private Set<String> operationWhiteListPrefixes;
private Set<String> typeHintWhiteList;
private Set<String> resourceTypeWhiteList;
public SaferSlingPostValidatorImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.parameterWhiteList = new HashSet<String>();
this.parameterWhiteListPrefixes = new HashSet<String>();
this.binaryParameterWhiteList = new HashSet<String>();
this.modifierWhiteList = new HashSet<String>();
this.operationWhiteList = new HashSet<String>();
this.operationWhiteListPrefixes = new HashSet<String>();
this.typeHintWhiteList = new HashSet<String>();
this.resourceTypeWhiteList = new HashSet<String>();
}
@Activate
protected void activate(ComponentContext ctx) {
Object o = ctx.getProperties().get("parameter.whitelist");
if (o instanceof String[]) {
this.parameterWhiteList = new HashSet<String>();
this.parameterWhiteList.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("parameter.whitelist.prefixes")) instanceof String[]) {
this.parameterWhiteListPrefixes = new HashSet<String>();
this.parameterWhiteListPrefixes.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("binary.parameter.whitelist")) instanceof String[]) {
this.binaryParameterWhiteList = new HashSet<String>();
this.binaryParameterWhiteList.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("modifier.whitelist")) instanceof String[]) {
this.modifierWhiteList = new HashSet<String>();
this.modifierWhiteList.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("operation.whitelist")) instanceof String[]) {
this.operationWhiteList = new HashSet<String>();
this.operationWhiteList.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("operation.whitelist.prefixes")) instanceof String[]) {
this.operationWhiteListPrefixes = new HashSet<String>();
this.operationWhiteListPrefixes.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("typehint.whitelist")) instanceof String[]) {
this.typeHintWhiteList = new HashSet<String>();
this.typeHintWhiteList.addAll(Arrays.asList((String[])o));
}
if ((o = ctx.getProperties().get("resourcetype.whitelist")) instanceof String[]) {
this.resourceTypeWhiteList = new HashSet<String>();
this.resourceTypeWhiteList.addAll(Arrays.asList((String[])o));
}
}
@Override
public /* varargs */ boolean reject(SlingHttpServletRequest request, String ... whitelistPatterns) {
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String name;
Object o = paramNames.nextElement();
if (!(o instanceof String)) {
return true;
}
String fullName = (String)o;
if (this.parameterWhiteList.contains(fullName)) continue;
if (fullName.startsWith("../") || fullName.endsWith("/..") || fullName.contains("/../") || fullName.startsWith("/")) {
return true;
}
String prename = this.leafName(fullName);
int atidx = prename.lastIndexOf(64);
if (atidx >= 0) {
String modifier = prename.substring(atidx);
name = prename.substring(0, atidx);
if (this.checkModifier(request, name, fullName, modifier)) {
return true;
}
} else {
name = prename;
}
if (":operation".equals(name) && this.checkOperation(request, name, fullName)) {
return true;
}
if (("jcr:primaryType".equals(name) || "jcr:mixinTypes".equals(name)) && this.checkJCRTypes(request, name, fullName)) {
return true;
}
if (("sling:resourceType".equals(name) || "sling:resourceSuperType".equals(name)) && this.checkResourceTypes(request, name, fullName)) {
return true;
}
if (name.contains(":") && !name.startsWith(":") && !this.parameterWhiteList.contains(name) && this.checkParameterPrefixes(name) && this.checkParameterPatterns(name, whitelistPatterns)) {
return true;
}
if (":applyTo".equals(name) && this.validateApplyTo(request)) {
return true;
}
if (!this.validateUploads(request, name)) continue;
return true;
}
return false;
}
private boolean checkParameterPrefixes(String name) {
for (String prefix : this.parameterWhiteListPrefixes) {
if (!name.startsWith(prefix)) continue;
return false;
}
return true;
}
private /* varargs */ boolean checkParameterPatterns(String name, String ... patterns) {
if (patterns == null || patterns.length == 0) {
return false;
}
for (String patternVal : patterns) {
try {
Pattern p = Pattern.compile(patternVal);
Matcher m = p.matcher(name);
if (!m.matches()) continue;
return false;
}
catch (PatternSyntaxException e) {
this.logger.warn("invalid pattern [{}] provided to SaferSlingPostValidator: ", (Object)patternVal, (Object)e);
}
}
return true;
}
private String leafName(String name) {
int slashidx = name.lastIndexOf(47);
if (slashidx >= 0) {
return name.substring(slashidx + 1);
}
return name;
}
private boolean checkResourceTypes(SlingHttpServletRequest request, String name, String fullName) {
String[] values = request.getParameterValues(fullName);
if (values != null) {
if (values.length > 1) {
return true;
}
if (!this.resourceTypeWhiteList.contains(values[0])) {
return true;
}
} else {
return true;
}
return false;
}
private boolean checkJCRTypes(SlingHttpServletRequest request, String name, String fullName) {
String[] values = request.getParameterValues(fullName);
if (values != null) {
for (String type : values) {
if (this.typeHintWhiteList.contains(type)) continue;
return true;
}
}
return false;
}
private boolean checkOperation(SlingHttpServletRequest request, String name, String fullName) {
String[] values = request.getParameterValues(fullName);
if (values != null) {
if (values.length > 1) {
return true;
}
if (!"".equals(values[0]) && !this.operationWhiteList.contains(values[0])) {
for (String prefix : this.operationWhiteListPrefixes) {
if (!values[0].startsWith(prefix)) continue;
return false;
}
return true;
}
}
return false;
}
private boolean checkModifier(SlingHttpServletRequest request, String name, String fullName, String modifier) {
if (!"".equals(modifier) && !this.modifierWhiteList.contains(modifier)) {
return true;
}
if (this.checkTypeHint(request, name, fullName, modifier)) {
return true;
}
return false;
}
private boolean checkTypeHint(SlingHttpServletRequest request, String name, String fullName, String modifier) {
String[] values;
if ("@TypeHint".equals(modifier) && (values = request.getParameterValues(fullName)) != null) {
if (values.length > 1) {
return true;
}
String type = values[0];
if (!"".equals(type) && !this.typeHintWhiteList.contains(type)) {
return true;
}
if ("Binary".equals(type) && name.contains(".") && !this.binaryParameterWhiteList.contains(name)) {
return true;
}
}
return false;
}
private boolean validateApplyTo(SlingHttpServletRequest request) {
String[] paths = request.getParameterValues(":applyTo");
if (paths == null) {
return false;
}
Integer depth = (Integer)request.getAttribute(SaferSlingPostValidator.POST_DEPTH_ATTRIBUTE);
for (String path : paths) {
if (path == null || !this.validateApplyToPath(path, request.getResource().getPath(), depth != null ? depth : 0)) continue;
return true;
}
return false;
}
private boolean validateApplyToPath(String applyToPath, String resourcePath, int depth) {
String canonPath = Text.makeCanonicalPath((String)applyToPath);
if (!canonPath.startsWith(resourcePath)) {
return true;
}
String testPath = canonPath.substring(resourcePath.length());
if (testPath.length() > 0 && testPath.charAt(0) == '/') {
testPath = testPath.substring(1);
}
if (testPath.length() == 0) {
return false;
}
int testDepth = testPath.split("/").length;
if (testDepth > depth) {
return true;
}
return false;
}
private boolean validateUploads(SlingHttpServletRequest request, String name) {
RequestParameter[] rps = request.getRequestParameters(name);
if (rps == null) {
return false;
}
for (RequestParameter rp : rps) {
AttachmentDataSource ads;
if (rp.isFormField() || !this.attachmentTypeBlacklist.reject(ads = new AttachmentDataSource(rp))) continue;
return true;
}
return false;
}
protected void bindAttachmentTypeBlacklist(AttachmentTypeBlacklistService attachmentTypeBlacklistService) {
this.attachmentTypeBlacklist = attachmentTypeBlacklistService;
}
protected void unbindAttachmentTypeBlacklist(AttachmentTypeBlacklistService attachmentTypeBlacklistService) {
if (this.attachmentTypeBlacklist == attachmentTypeBlacklistService) {
this.attachmentTypeBlacklist = null;
}
}
}