|
| 1 | +package org.tron.core.services.http; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.junit.Assert.assertFalse; |
| 7 | + |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.util.Map; |
| 10 | +import org.junit.BeforeClass; |
| 11 | +import org.junit.Test; |
| 12 | +import org.tron.core.services.ratelimiter.adapter.DefaultBaseQqsAdapter; |
| 13 | +import org.tron.core.services.ratelimiter.adapter.GlobalPreemptibleAdapter; |
| 14 | +import org.tron.core.services.ratelimiter.adapter.IPQPSRateLimiterAdapter; |
| 15 | +import org.tron.core.services.ratelimiter.adapter.IRateLimiter; |
| 16 | +import org.tron.core.services.ratelimiter.adapter.QpsRateLimiterAdapter; |
| 17 | + |
| 18 | +/** |
| 19 | + * Security test: verifies that RateLimiterServlet uses a strict whitelist |
| 20 | + * instead of Class.forName(), preventing arbitrary class loading (RCE) |
| 21 | + * via a tampered config file. |
| 22 | + */ |
| 23 | +public class RateLimiterServletWhitelistTest { |
| 24 | + |
| 25 | + private static Map<String, Class<? extends IRateLimiter>> allowedAdapters; |
| 26 | + |
| 27 | + @SuppressWarnings("unchecked") |
| 28 | + @BeforeClass |
| 29 | + public static void loadWhitelist() throws Exception { |
| 30 | + Field f = RateLimiterServlet.class.getDeclaredField("ALLOWED_ADAPTERS"); |
| 31 | + f.setAccessible(true); |
| 32 | + allowedAdapters = (Map<String, Class<? extends IRateLimiter>>) f.get(null); |
| 33 | + } |
| 34 | + |
| 35 | + // --- whitelist completeness --- |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testWhitelistContainsAllLegitimateAdapters() { |
| 39 | + assertNotNull("GlobalPreemptibleAdapter must be whitelisted", |
| 40 | + allowedAdapters.get("GlobalPreemptibleAdapter")); |
| 41 | + assertNotNull("QpsRateLimiterAdapter must be whitelisted", |
| 42 | + allowedAdapters.get("QpsRateLimiterAdapter")); |
| 43 | + assertNotNull("IPQPSRateLimiterAdapter must be whitelisted", |
| 44 | + allowedAdapters.get("IPQPSRateLimiterAdapter")); |
| 45 | + assertNotNull("DefaultBaseQqsAdapter must be whitelisted", |
| 46 | + allowedAdapters.get("DefaultBaseQqsAdapter")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testWhitelistMapsToCorrectClasses() { |
| 51 | + assertTrue(allowedAdapters.get("GlobalPreemptibleAdapter") |
| 52 | + .isAssignableFrom(GlobalPreemptibleAdapter.class)); |
| 53 | + assertTrue(allowedAdapters.get("QpsRateLimiterAdapter") |
| 54 | + .isAssignableFrom(QpsRateLimiterAdapter.class)); |
| 55 | + assertTrue(allowedAdapters.get("IPQPSRateLimiterAdapter") |
| 56 | + .isAssignableFrom(IPQPSRateLimiterAdapter.class)); |
| 57 | + assertTrue(allowedAdapters.get("DefaultBaseQqsAdapter") |
| 58 | + .isAssignableFrom(DefaultBaseQqsAdapter.class)); |
| 59 | + } |
| 60 | + |
| 61 | + // --- security: malicious class names must be rejected --- |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testArbitraryClassNameIsRejected() { |
| 65 | + // Simulates a tampered config: attacker supplies a fully-qualified class name |
| 66 | + assertNull("Arbitrary class name must not be in whitelist", |
| 67 | + allowedAdapters.get("com.evil.MaliciousAdapter")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testPathTraversalStyleNameIsRejected() { |
| 72 | + assertNull("Path-traversal style name must not be in whitelist", |
| 73 | + allowedAdapters.get("../../../../evil.Payload")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testEmptyStrategyIsRejected() { |
| 78 | + assertNull("Empty strategy must not be in whitelist", |
| 79 | + allowedAdapters.get("")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testNullStrategyIsRejected() { |
| 84 | + assertNull("Null strategy must not be in whitelist", |
| 85 | + allowedAdapters.get(null)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testRuntimeClassIsRejected() { |
| 90 | + // Attacker tries to load java.lang.Runtime to exec arbitrary commands |
| 91 | + assertNull("java.lang.Runtime must not be in whitelist", |
| 92 | + allowedAdapters.get("java.lang.Runtime")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testProcessBuilderIsRejected() { |
| 97 | + assertNull("java.lang.ProcessBuilder must not be in whitelist", |
| 98 | + allowedAdapters.get("java.lang.ProcessBuilder")); |
| 99 | + } |
| 100 | + |
| 101 | + // --- whitelist is immutable (cannot be tampered at runtime) --- |
| 102 | + |
| 103 | + @Test(expected = UnsupportedOperationException.class) |
| 104 | + public void testWhitelistIsImmutable() { |
| 105 | + // The map must be unmodifiable to prevent runtime injection |
| 106 | + allowedAdapters.put("Injected", DefaultBaseQqsAdapter.class); |
| 107 | + } |
| 108 | + |
| 109 | + // --- all whitelisted entries implement IRateLimiter --- |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testAllWhitelistedClassesImplementIRateLimiter() { |
| 113 | + for (Map.Entry<String, Class<? extends IRateLimiter>> entry : allowedAdapters.entrySet()) { |
| 114 | + assertTrue( |
| 115 | + entry.getKey() + " must implement IRateLimiter", |
| 116 | + IRateLimiter.class.isAssignableFrom(entry.getValue()) |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // --- whitelist size is exactly what we expect (no accidental additions) --- |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testWhitelistSizeIsExact() { |
| 125 | + assertFalse("Whitelist must not be empty", allowedAdapters.isEmpty()); |
| 126 | + assertTrue("Whitelist must contain exactly 4 adapters", allowedAdapters.size() == 4); |
| 127 | + } |
| 128 | +} |
0 commit comments