|
16 | 16 | */
|
17 | 17 | package org.apache.logging.log4j.core.lookup;
|
18 | 18 |
|
19 |
| -import static org.junit.Assert.assertNull; |
| 19 | +import static org.junit.Assert.fail; |
20 | 20 |
|
| 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; |
21 | 29 | import org.junit.BeforeClass;
|
| 30 | +import org.junit.Rule; |
22 | 31 | import org.junit.Test;
|
| 32 | +import org.zapodot.junit.ldap.EmbeddedLdapRule; |
| 33 | +import org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder; |
23 | 34 |
|
24 | 35 | /**
|
25 | 36 | * JndiLookupTest
|
26 | 37 | */
|
27 | 38 | public class JndiRestrictedLookupTest {
|
28 | 39 |
|
| 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"; |
29 | 46 | private static final String DOMAIN = "apache.org";
|
30 | 47 |
|
| 48 | + @Rule |
| 49 | + public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance() |
| 50 | + .usingDomainDsn(DOMAIN_DSN) |
| 51 | + .importingLdifs("JndiRestrictedLookup.ldif") |
| 52 | + .build(); |
| 53 | + |
31 | 54 | @BeforeClass
|
32 | 55 | public static void beforeClass() {
|
33 | 56 | System.setProperty("log4j2.enableJndiLookup", "true");
|
34 | 57 | }
|
35 | 58 |
|
| 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 | + |
36 | 112 | @Test
|
37 | 113 | public void testDnsLookup() throws Exception {
|
38 | 114 | final StrLookup lookup = new JndiLookup();
|
39 | 115 | 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 | + } |
41 | 168 | }
|
42 | 169 | }
|
0 commit comments