-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DoSFilterTest.java
181 lines (160 loc) · 5.96 KB
/
DoSFilterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.servlets;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Enumeration;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlets.DoSFilter.RateTracker;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class DoSFilterTest extends AbstractDoSFilterTest
{
public WorkDir workDir;
private static class RemoteAddressRequest extends Request
{
public RemoteAddressRequest(String remoteHost, int remotePort)
{
super(null, null);
setRemoteAddr(new InetSocketAddress(remoteHost, remotePort));
}
}
private static class NoOpFilterConfig implements FilterConfig
{
@Override
public String getFilterName()
{
return "noop";
}
@Override
public ServletContext getServletContext()
{
return null;
}
@Override
public String getInitParameter(String name)
{
return null;
}
@Override
public Enumeration<String> getInitParameterNames()
{
return Collections.emptyEnumeration();
}
}
@BeforeEach
public void setUp() throws Exception
{
startServer(workDir, DoSFilter.class);
}
@Test
public void testRemotePortLoadIdCreationIpv6() throws ServletException
{
final ServletRequest request = new RemoteAddressRequest("::192.9.5.5", 12345);
DoSFilter doSFilter = new DoSFilter();
doSFilter.init(new NoOpFilterConfig());
doSFilter.setRemotePort(true);
try
{
RateTracker tracker = doSFilter.getRateTracker(request);
assertThat("tracker.id", tracker.getId(),
anyOf(
is("[::192.9.5.5]:12345"), // short form
is("[0:0:0:0:0:0:c009:505]:12345") // long form
));
}
finally
{
doSFilter.stopScheduler();
}
}
@Test
public void testRemotePortLoadIdCreationIpv4() throws ServletException
{
final ServletRequest request = new RemoteAddressRequest("127.0.0.1", 12345);
DoSFilter doSFilter = new DoSFilter();
doSFilter.init(new NoOpFilterConfig());
doSFilter.setRemotePort(true);
try
{
RateTracker tracker = doSFilter.getRateTracker(request);
assertThat("tracker.id", tracker.getId(), is("127.0.0.1:12345"));
}
finally
{
doSFilter.stopScheduler();
}
}
@Test
public void testRateIsRateExceeded() throws InterruptedException
{
DoSFilter doSFilter = new DoSFilter();
doSFilter.setName("foo");
boolean exceeded = hitRateTracker(doSFilter, 0);
assertTrue(exceeded, "Last hit should have exceeded");
int sleep = 250;
exceeded = hitRateTracker(doSFilter, sleep);
assertFalse(exceeded, "Should not exceed as we sleep 300s for each hit and thus do less than 4 hits/s");
}
@Test
public void testWhitelist() throws Exception
{
DoSFilter filter = new DoSFilter();
filter.setName("foo");
filter.setWhitelist("192.168.0.1/32,10.0.0.0/8,4d8:0:a:1234:ABc:1F:b18:17,4d8:0:a:1234:ABc:1F:0:0/96");
assertTrue(filter.checkWhitelist("192.168.0.1"));
assertFalse(filter.checkWhitelist("192.168.0.2"));
assertFalse(filter.checkWhitelist("11.12.13.14"));
assertTrue(filter.checkWhitelist("10.11.12.13"));
assertTrue(filter.checkWhitelist("10.0.0.0"));
assertFalse(filter.checkWhitelist("0.0.0.0"));
assertTrue(filter.checkWhitelist("4d8:0:a:1234:ABc:1F:b18:17"));
assertTrue(filter.checkWhitelist("4d8:0:a:1234:ABc:1F:b18:0"));
assertFalse(filter.checkWhitelist("4d8:0:a:1234:ABc:1D:0:0"));
}
@Test
public void testUnresponsiveServer() throws Exception
{
String last = "GET /ctx/timeout/?sleep=" + 2 * _requestMaxTime + " HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
String responses = doRequests("", 0, 0, 0, last);
assertThat(responses, Matchers.containsString(" 503 "));
}
private boolean hitRateTracker(DoSFilter doSFilter, int sleep) throws InterruptedException
{
boolean exceeded = false;
ServletContext context = new ContextHandler.StaticContext();
RateTracker rateTracker = new RateTracker(context, doSFilter.getName(), "test2", DoSFilter.RateType.UNKNOWN, 4);
for (int i = 0; i < 5; i++)
{
Thread.sleep(sleep);
if (rateTracker.isRateExceeded(System.nanoTime()) != null)
exceeded = true;
}
return exceeded;
}
}