Skip to content

Commit

Permalink
still support simple named resources when protocol restrictions are p…
Browse files Browse the repository at this point in the history
…resent
  • Loading branch information
nahsra committed Jun 25, 2024
1 parent 8aea291 commit 1eb98ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/io/github/pixee/security/JNDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ private ProtocolLimitedContext(final Context context, final Set<UrlProtocol> all
public Object lookup(final String resource) throws NamingException {
Set<String> allowedProtocolPrefixes = allowedProtocols.stream().map(UrlProtocol::getKey).map(p -> p + ":").collect(Collectors.toSet());
String canonicalResource = resource.toLowerCase().trim();
if (allowedProtocolPrefixes.stream().anyMatch(canonicalResource::startsWith)) {
return context.lookup(resource);
if(canonicalResource.contains(":")) {
if (allowedProtocolPrefixes.stream().anyMatch(canonicalResource::startsWith)) {
return context.lookup(resource);
} else {
throw new SecurityException("Unexpected JNDI resource protocol: " + resource);
}
}
throw new SecurityException("Unexpected JNDI resource protocol: " + resource);
return context.lookup(resource);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/io/github/pixee/security/JNDITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void it_limits_resources_by_name() throws NamingException {
void it_limits_resources_by_protocol() throws NamingException {
JNDI.LimitedContext onlyJavaContext = JNDI.limitedContextByProtocol(context, J8ApiBridge.setOf(UrlProtocol.JAVA));
assertThat(onlyJavaContext.lookup("java:comp/env"), is(JAVA_OBJECT));

// confirm protocols protections dont restrict simple name lookups
assertThat(onlyJavaContext.lookup("simple_name"), is(NAMED_OBJECT));
assertThrows(SecurityException.class, () -> onlyJavaContext.lookup("ldap://localhost:1389/ou=system"));
assertThrows(SecurityException.class, () -> onlyJavaContext.lookup("rmi://localhost:1099/evil"));

Expand All @@ -58,6 +61,9 @@ void it_limits_resources_by_protocol() throws NamingException {
void default_limits_rmi_and_ldap() throws NamingException {
JNDI.LimitedContext defaultLimitedContext = JNDI.limitedContext(context);
assertThat(defaultLimitedContext.lookup("java:comp/env"), is(JAVA_OBJECT));

// confirm simple name lookups still work
assertThat(defaultLimitedContext.lookup("simple_name"), is(NAMED_OBJECT));
assertThrows(SecurityException.class, () -> defaultLimitedContext.lookup("rmi://localhost:1099/evil"));
assertThrows(SecurityException.class, () -> defaultLimitedContext.lookup("ldap://localhost:1389/ou=system"));
}
Expand Down

0 comments on commit 1eb98ad

Please sign in to comment.