blob: 5b9f143a72b6ae0694fc743aef0ac10a76b5f608 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.filter.security.misc;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.http.filter.DiscFilterRequest;
import com.yahoo.jdisc.http.filter.util.FilterTestUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author mpolden
* @author bjorncs
*/
public class LocalhostFilterTest {
@Test
void filter() {
// Reject from non-loopback
assertUnauthorized(createRequest("1.2.3.4", null));
// Allow requests from loopback addresses
assertSuccess(createRequest("127.0.0.1", null));
assertSuccess(createRequest("127.127.0.1", null));
assertSuccess(createRequest("0:0:0:0:0:0:0:1", null));
// Allow requests originating from self
assertSuccess(createRequest("1.3.3.7", "1.3.3.7"));
}
private static DiscFilterRequest createRequest(String remoteAddr, String localAddr) {
return FilterTestUtils.newRequestBuilder()
.withUri("http://%s:8080/".formatted(localAddr))
.withRemoteAddress(remoteAddr, 12345)
.build();
}
private static void assertUnauthorized(DiscFilterRequest request) {
LocalhostFilter filter = new LocalhostFilter();
RequestHandlerTestDriver.MockResponseHandler handler = new RequestHandlerTestDriver.MockResponseHandler();
filter.filter(request, handler);
assertEquals(Response.Status.UNAUTHORIZED, handler.getStatus());
}
private static void assertSuccess(DiscFilterRequest request) {
LocalhostFilter filter = new LocalhostFilter();
RequestHandlerTestDriver.MockResponseHandler handler = new RequestHandlerTestDriver.MockResponseHandler();
filter.filter(request, handler);
assertNull(handler.getResponse());
}
}
|