Skip to content

Commit fbc780e

Browse files
committed
Adds proper handling of primitive types
1 parent 6eb8301 commit fbc780e

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

core/src/main/java/com/opensymphony/xwork2/ognl/SecurityMemberAccess.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,18 @@ protected boolean isPackageExcluded(Package targetPackage, Package memberPackage
133133
LOG.warn("The use of the default (unnamed) package is discouraged!");
134134
}
135135

136-
final String targetPackageName = targetPackage == null ? "" : targetPackage.getName();
137-
final String memberPackageName = memberPackage == null ? "" : memberPackage.getName();
136+
String targetPackageName = targetPackage == null ? "" : targetPackage.getName();
137+
String memberPackageName = memberPackage == null ? "" : memberPackage.getName();
138138

139139
for (Pattern pattern : excludedPackageNamePatterns) {
140140
if (pattern.matcher(targetPackageName).matches() || pattern.matcher(memberPackageName).matches()) {
141141
return true;
142142
}
143143
}
144144

145+
targetPackageName = targetPackageName + ".";
146+
memberPackageName = memberPackageName + ".";
147+
145148
for (String packageName: excludedPackageNames) {
146149
if (targetPackageName.startsWith(packageName) || targetPackageName.equals(packageName)
147150
|| memberPackageName.startsWith(packageName) || memberPackageName.equals(packageName)) {

core/src/main/resources/struts-default.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@
6060
<!-- this is simpler version of the above used with string comparison -->
6161
<constant name="struts.excludedPackageNames"
6262
value="
63-
java.lang.,
6463
ognl.,
65-
javax,
64+
javax.,
6665
freemarker.core.,
6766
freemarker.template.,
6867
freemarker.ext.rhino.,

core/src/test/java/com/opensymphony/xwork2/ognl/SecurityMemberAccessTest.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import junit.framework.TestCase;
2323

2424
import java.lang.reflect.Member;
25-
import java.util.Arrays;
2625
import java.util.Collections;
2726
import java.util.HashMap;
2827
import java.util.HashSet;
@@ -62,7 +61,7 @@ public void testClassExclusion() throws Exception {
6261
String propertyName = "stringField";
6362
Member member = FooBar.class.getDeclaredMethod("get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
6463

65-
Set<Class<?>> excluded = new HashSet<Class<?>>();
64+
Set<Class<?>> excluded = new HashSet<>();
6665
excluded.add(FooBar.class);
6766
sma.setExcludedClasses(excluded);
6867

@@ -108,7 +107,7 @@ public void testInterfaceInheritanceExclusion() throws Exception {
108107
String propertyName = "barLogic";
109108
Member member = BarInterface.class.getMethod(propertyName);
110109

111-
Set<Class<?>> excluded = new HashSet<Class<?>>();
110+
Set<Class<?>> excluded = new HashSet<>();
112111
excluded.add(BarInterface.class);
113112
sma.setExcludedClasses(excluded);
114113

@@ -126,7 +125,7 @@ public void testMiddleOfInheritanceExclusion1() throws Exception {
126125
String propertyName = "fooLogic";
127126
Member member = FooBar.class.getMethod(propertyName);
128127

129-
Set<Class<?>> excluded = new HashSet<Class<?>>();
128+
Set<Class<?>> excluded = new HashSet<>();
130129
excluded.add(BarInterface.class);
131130
sma.setExcludedClasses(excluded);
132131

@@ -158,7 +157,7 @@ public void testMiddleOfInheritanceExclusion4() throws Exception {
158157
String propertyName = "barLogic";
159158
Member member = BarInterface.class.getMethod(propertyName);
160159

161-
Set<Class<?>> excluded = new HashSet<Class<?>>();
160+
Set<Class<?>> excluded = new HashSet<>();
162161
excluded.add(FooBarInterface.class);
163162
sma.setExcludedClasses(excluded);
164163

@@ -173,7 +172,7 @@ public void testPackageExclusion() throws Exception {
173172
// given
174173
SecurityMemberAccess sma = new SecurityMemberAccess(false);
175174

176-
Set<Pattern> excluded = new HashSet<Pattern>();
175+
Set<Pattern> excluded = new HashSet<>();
177176
excluded.add(Pattern.compile("^" + FooBar.class.getPackage().getName().replaceAll("\\.", "\\\\.") + ".*"));
178177
sma.setExcludedPackageNamePatterns(excluded);
179178

@@ -191,7 +190,7 @@ public void testPackageNameExclusion() throws Exception {
191190
// given
192191
SecurityMemberAccess sma = new SecurityMemberAccess(false);
193192

194-
Set<String> excluded = new HashSet<String>();
193+
Set<String> excluded = new HashSet<>();
195194
excluded.add(FooBar.class.getPackage().getName());
196195
sma.setExcludedPackageNames(excluded);
197196

@@ -205,11 +204,11 @@ public void testPackageNameExclusion() throws Exception {
205204
assertFalse("stringField is accessible!", actual);
206205
}
207206

208-
public void testDefaultPackageExclusion() throws Exception {
207+
public void testDefaultPackageExclusion() {
209208
// given
210209
SecurityMemberAccess sma = new SecurityMemberAccess(false);
211210

212-
Set<Pattern> excluded = new HashSet<Pattern>();
211+
Set<Pattern> excluded = new HashSet<>();
213212
excluded.add(Pattern.compile("^" + FooBar.class.getPackage().getName().replaceAll("\\.", "\\\\.") + ".*"));
214213
sma.setExcludedPackageNamePatterns(excluded);
215214

@@ -220,11 +219,11 @@ public void testDefaultPackageExclusion() throws Exception {
220219
assertFalse("default package is excluded!", actual);
221220
}
222221

223-
public void testDefaultPackageExclusion2() throws Exception {
222+
public void testDefaultPackageExclusion2() {
224223
// given
225224
SecurityMemberAccess sma = new SecurityMemberAccess(false);
226225

227-
Set<Pattern> excluded = new HashSet<Pattern>();
226+
Set<Pattern> excluded = new HashSet<>();
228227
excluded.add(Pattern.compile("^$"));
229228
sma.setExcludedPackageNamePatterns(excluded);
230229

@@ -317,10 +316,10 @@ public void testAccessPrimitiveInt() throws Exception {
317316
public void testAccessPrimitiveDoubleWithNames() throws Exception {
318317
// given
319318
SecurityMemberAccess sma = new SecurityMemberAccess(false);
320-
sma.setExcludedPackageNames(TextParseUtil.commaDelimitedStringToSet("java.lang.,ognl,javax"));
319+
sma.setExcludedPackageNames(TextParseUtil.commaDelimitedStringToSet("ognl.,javax."));
321320

322321

323-
Set<Class<?>> excluded = new HashSet<Class<?>>();
322+
Set<Class<?>> excluded = new HashSet<>();
324323
excluded.add(Object.class);
325324
excluded.add(Runtime.class);
326325
excluded.add(System.class);
@@ -369,7 +368,7 @@ public void testAccessPrimitiveDoubleWithNames() throws Exception {
369368
public void testAccessPrimitiveDoubleWithPackageRegExs() throws Exception {
370369
// given
371370
SecurityMemberAccess sma = new SecurityMemberAccess(false);
372-
Set<Pattern> patterns = new HashSet<Pattern>();
371+
Set<Pattern> patterns = new HashSet<>();
373372
patterns.add(Pattern.compile("^java\\.lang\\..*"));
374373
sma.setExcludedPackageNamePatterns(patterns);
375374

@@ -386,7 +385,7 @@ public void testAccessPrimitiveDoubleWithPackageRegExs() throws Exception {
386385
public void testAccessMemberAccessIsAccessible() throws Exception {
387386
// given
388387
SecurityMemberAccess sma = new SecurityMemberAccess(false);
389-
Set<Class<?>> excluded = new HashSet<Class<?>>();
388+
Set<Class<?>> excluded = new HashSet<>();
390389
excluded.add(ognl.MemberAccess.class);
391390
sma.setExcludedClasses(excluded);
392391

@@ -404,7 +403,7 @@ public void testAccessMemberAccessIsAccessible() throws Exception {
404403
public void testAccessMemberAccessIsBlocked() throws Exception {
405404
// given
406405
SecurityMemberAccess sma = new SecurityMemberAccess(false);
407-
Set<Class<?>> excluded = new HashSet<Class<?>>();
406+
Set<Class<?>> excluded = new HashSet<>();
408407
excluded.add(SecurityMemberAccess.class);
409408
sma.setExcludedClasses(excluded);
410409

@@ -419,6 +418,21 @@ public void testAccessMemberAccessIsBlocked() throws Exception {
419418
assertFalse(accessible);
420419
}
421420

421+
public void testPackageNameExclusionAsCommaDelimited() {
422+
// given
423+
SecurityMemberAccess sma = new SecurityMemberAccess(false);
424+
425+
426+
sma.setExcludedPackageNames(TextParseUtil.commaDelimitedStringToSet("java.lang."));
427+
428+
// when
429+
boolean actual = sma.isPackageExcluded(String.class.getPackage(), null);
430+
actual &= sma.isPackageExcluded(null, String.class.getPackage());
431+
432+
// then
433+
assertTrue("package java.lang. is accessible!", actual);
434+
}
435+
422436
}
423437

424438
class FooBar implements FooBarInterface {

0 commit comments

Comments
 (0)