Skip to content

Commit bac23ba

Browse files
committed
Port JndiRestrictedLookupTest changes from main
1 parent cf4b39e commit bac23ba

File tree

1 file changed

+129
-2
lines changed

1 file changed

+129
-2
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JndiRestrictedLookupTest.java

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,154 @@
1616
*/
1717
package org.apache.logging.log4j.core.lookup;
1818

19-
import static org.junit.Assert.assertNull;
19+
import static org.junit.Assert.fail;
2020

21+
import java.io.Serializable;
22+
import javax.naming.Context;
23+
import javax.naming.NamingException;
24+
import javax.naming.Reference;
25+
import javax.naming.Referenceable;
26+
import javax.naming.StringRefAddr;
27+
import org.apache.logging.log4j.message.Message;
28+
import org.apache.logging.log4j.util.Strings;
2129
import org.junit.BeforeClass;
30+
import org.junit.Rule;
2231
import org.junit.Test;
32+
import org.zapodot.junit.ldap.EmbeddedLdapRule;
33+
import org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder;
2334

2435
/**
2536
* JndiLookupTest
2637
*/
2738
public class JndiRestrictedLookupTest {
2839

40+
private static final String LDAP_URL = "ldap://127.0.0.1:";
41+
private static final String RESOURCE = "JndiExploit";
42+
private static final String TEST_STRING = "TestString";
43+
private static final String TEST_MESSAGE = "TestMessage";
44+
private static final String LEVEL = "TestLevel";
45+
private static final String DOMAIN_DSN = "dc=apache,dc=org";
2946
private static final String DOMAIN = "apache.org";
3047

48+
@Rule
49+
public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance()
50+
.usingDomainDsn(DOMAIN_DSN)
51+
.importingLdifs("JndiRestrictedLookup.ldif")
52+
.build();
53+
3154
@BeforeClass
3255
public static void beforeClass() {
3356
System.setProperty("log4j2.enableJndiLookup", "true");
3457
}
3558

59+
@Test
60+
@SuppressWarnings("BanJNDI")
61+
public void testBadUriLookup() throws Exception {
62+
final int port = embeddedLdapRule.embeddedServerPort();
63+
final Context context = embeddedLdapRule.context();
64+
context.bind("cn=" + RESOURCE + "," + DOMAIN_DSN, new Fruit("Test Message"));
65+
final StrLookup lookup = new JndiLookup();
66+
final String result = lookup.lookup(
67+
LDAP_URL + port + "/" + "cn=" + RESOURCE + "," + DOMAIN_DSN + "?Type=A Type&Name=1100110&Char=!");
68+
if (result != null) {
69+
fail("Lookup returned an object");
70+
}
71+
}
72+
73+
@Test
74+
@SuppressWarnings("BanJNDI")
75+
public void testReferenceLookup() throws Exception {
76+
final int port = embeddedLdapRule.embeddedServerPort();
77+
final Context context = embeddedLdapRule.context();
78+
context.bind("cn=" + RESOURCE + "," + DOMAIN_DSN, new Fruit("Test Message"));
79+
final StrLookup lookup = new JndiLookup();
80+
final String result = lookup.lookup(LDAP_URL + port + "/" + "cn=" + RESOURCE + "," + DOMAIN_DSN);
81+
if (result != null) {
82+
fail("Lookup returned an object");
83+
}
84+
}
85+
86+
@Test
87+
@SuppressWarnings("BanJNDI")
88+
public void testSerializableLookup() throws Exception {
89+
final int port = embeddedLdapRule.embeddedServerPort();
90+
final Context context = embeddedLdapRule.context();
91+
context.bind("cn=" + TEST_STRING + "," + DOMAIN_DSN, "Test Message");
92+
final StrLookup lookup = new JndiLookup();
93+
final String result = lookup.lookup(LDAP_URL + port + "/" + "cn=" + TEST_STRING + "," + DOMAIN_DSN);
94+
if (result != null) {
95+
fail("LDAP is enabled");
96+
}
97+
}
98+
99+
@Test
100+
@SuppressWarnings("BanJNDI")
101+
public void testBadSerializableLookup() throws Exception {
102+
final int port = embeddedLdapRule.embeddedServerPort();
103+
final Context context = embeddedLdapRule.context();
104+
context.bind("cn=" + TEST_MESSAGE + "," + DOMAIN_DSN, new SerializableMessage("Test Message"));
105+
final StrLookup lookup = new JndiLookup();
106+
final String result = lookup.lookup(LDAP_URL + port + "/" + "cn=" + TEST_MESSAGE + "," + DOMAIN_DSN);
107+
if (result != null) {
108+
fail("Lookup returned an object");
109+
}
110+
}
111+
36112
@Test
37113
public void testDnsLookup() throws Exception {
38114
final StrLookup lookup = new JndiLookup();
39115
final String result = lookup.lookup("dns:/" + DOMAIN);
40-
assertNull("DNS data returend", result);
116+
if (result != null) {
117+
fail("No DNS data returned");
118+
}
119+
}
120+
121+
static class Fruit implements Referenceable {
122+
String fruit;
123+
124+
public Fruit(final String f) {
125+
fruit = f;
126+
}
127+
128+
public Reference getReference() throws NamingException {
129+
130+
return new Reference(
131+
Fruit.class.getName(),
132+
new StringRefAddr("fruit", fruit),
133+
JndiExploit.class.getName(),
134+
null); // factory location
135+
}
136+
137+
public String toString() {
138+
return fruit;
139+
}
140+
}
141+
142+
static class SerializableMessage implements Serializable, Message {
143+
private final String message;
144+
145+
SerializableMessage(final String message) {
146+
this.message = message;
147+
}
148+
149+
@Override
150+
public String getFormattedMessage() {
151+
return message;
152+
}
153+
154+
@Override
155+
public String getFormat() {
156+
return Strings.EMPTY;
157+
}
158+
159+
@Override
160+
public Object[] getParameters() {
161+
return null;
162+
}
163+
164+
@Override
165+
public Throwable getThrowable() {
166+
return null;
167+
}
41168
}
42169
}

0 commit comments

Comments
 (0)