mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-22 09:18:05 +01:00
Refactor remote address/X-Forwarded-For handling
This commit is contained in:
@@ -35,8 +35,7 @@ class HttpServletRequestUtilIntegrationTest {
|
||||
// The Grizzly test container does not match the Jetty container used in real deployments, and JettyTestContainerFactory
|
||||
// in jersey-test-framework-provider-jetty doesn’t easily support @Context HttpServletRequest, so this test runs a
|
||||
// full Jetty server in a separate process
|
||||
private final DropwizardAppExtension<TestConfiguration> EXTENSION = new DropwizardAppExtension<>(
|
||||
TestApplication.class);
|
||||
private final DropwizardAppExtension<Configuration> EXTENSION = new DropwizardAppExtension<>(TestApplication.class);
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"127.0.0.1", "0:0:0:0:0:0:0:1"})
|
||||
@@ -72,13 +71,11 @@ class HttpServletRequestUtilIntegrationTest {
|
||||
|
||||
}
|
||||
|
||||
public static class TestApplication extends Application<TestConfiguration> {
|
||||
public static class TestApplication extends Application<Configuration> {
|
||||
|
||||
@Override
|
||||
public void run(final TestConfiguration configuration, final Environment environment) throws Exception {
|
||||
public void run(final Configuration configuration, final Environment environment) throws Exception {
|
||||
environment.jersey().register(new TestController());
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestConfiguration extends Configuration {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
import org.whispersystems.textsecuregcm.filters.RemoteAddressFilter;
|
||||
import javax.annotation.Priority;
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerRequestFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Adds the request property set by {@link RemoteAddressFilter} for test scenarios that depend on it, but do not have
|
||||
* access to a full {@code HttpServletRequest} pipline
|
||||
*/
|
||||
@Priority(Integer.MIN_VALUE) // highest priority, since other filters might depend on it
|
||||
public class TestRemoteAddressFilterProvider implements ContainerRequestFilter {
|
||||
|
||||
private final String ip;
|
||||
|
||||
public TestRemoteAddressFilterProvider(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(final ContainerRequestContext requestContext) throws IOException {
|
||||
requestContext.setProperty(RemoteAddressFilter.REMOTE_ADDRESS_ATTRIBUTE_NAME, ip);
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.whispersystems.textsecuregcm.filters.RemoteAddressFilter;
|
||||
import org.whispersystems.textsecuregcm.mappers.CompletionExceptionMapper;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
import org.whispersystems.websocket.WebSocketResourceProvider;
|
||||
@@ -173,9 +174,9 @@ class LoggingUnhandledExceptionMapperTest {
|
||||
|
||||
ApplicationHandler applicationHandler = new ApplicationHandler(resourceConfig);
|
||||
WebsocketRequestLog requestLog = mock(WebsocketRequestLog.class);
|
||||
WebSocketResourceProvider<TestPrincipal> provider = new WebSocketResourceProvider<>("127.0.0.1", applicationHandler,
|
||||
requestLog, new TestPrincipal("foo"), new ProtobufWebSocketMessageFactory(), Optional.empty(),
|
||||
Duration.ofMillis(30000));
|
||||
WebSocketResourceProvider<TestPrincipal> provider = new WebSocketResourceProvider<>("127.0.0.1",
|
||||
RemoteAddressFilter.REMOTE_ADDRESS_ATTRIBUTE_NAME, applicationHandler, requestLog, new TestPrincipal("foo"),
|
||||
new ProtobufWebSocketMessageFactory(), Optional.empty(), Duration.ofMillis(30000));
|
||||
|
||||
RemoteEndpoint remoteEndpoint = mock(RemoteEndpoint.class);
|
||||
doAnswer(answer -> {
|
||||
|
||||
Reference in New Issue
Block a user