Skip to content

Commit

Permalink
Handle default (unnamed) package security check
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Feb 4, 2015
1 parent 312a271 commit 76ea79f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ protected boolean checkEnumAccess(Object target, Member member) {
}

protected boolean isPackageExcluded(Package targetPackage, Package memberPackage) {
if (LOG.isWarnEnabled() && (targetPackage == null || memberPackage == null)) {
LOG.warn("The use of the default (unnamed) package is discouraged!");
}

final String targetPackageName = targetPackage == null ? "" : targetPackage.getName();
final String memberPackageName = memberPackage == null ? "" : memberPackage.getName();
for (Pattern pattern : excludedPackageNamePatterns) {
if (pattern.matcher(targetPackage.getName()).matches() || pattern.matcher(memberPackage.getName()).matches()) {
if (pattern.matcher(targetPackageName).matches() || pattern.matcher(memberPackageName).matches()) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ public void testPackageExclusion() throws Exception {
// then
assertFalse("stringField is accessible!", actual);
}

public void testDefaultPackageExclusion() throws Exception {
// given
SecurityMemberAccess sma = new SecurityMemberAccess(false);

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

// when
boolean actual = sma.isPackageExcluded(null, null);

// then
assertFalse("default package is excluded!", actual);
}

public void testDefaultPackageExclusion2() throws Exception {
// given
SecurityMemberAccess sma = new SecurityMemberAccess(false);

Set<Pattern> excluded = new HashSet<Pattern>();
excluded.add(Pattern.compile("^$"));
sma.setExcludedPackageNamePatterns(excluded);

// when
boolean actual = sma.isPackageExcluded(null, null);

// then
assertTrue("default package isn't excluded!", actual);
}

public void testAccessEnum() throws Exception {
// given
Expand Down

0 comments on commit 76ea79f

Please sign in to comment.