Skip to content

Commit

Permalink
Ensures custom headers supplied to RemoveHopByHopHeadersFilter are co…
Browse files Browse the repository at this point in the history
…mpared case-insensitive

Fixes gh-2645
  • Loading branch information
spencergibb committed Mar 13, 2024
1 parent f7de687 commit d76a48d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;

@ConfigurationProperties("spring.cloud.gateway.filter.remove-hop-by-hop")
Expand All @@ -51,7 +53,8 @@ public Set<String> getHeaders() {
}

public void setHeaders(Set<String> headers) {
this.headers = headers;
Assert.notNull(headers, "headers may not be null");
this.headers = headers.stream().map(String::toLowerCase).collect(Collectors.toSet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.junit.jupiter.api.Test;

import org.springframework.http.HttpHeaders;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.gateway.filter.headers.RemoveHopByHopHeadersFilter.HEADERS_REMOVED_ON_REQUEST;
Expand Down Expand Up @@ -52,6 +54,23 @@ public void caseInsensitive() {
testFilter(MockServerWebExchange.from(builder));
}

@Test
public void caseInsensitiveCustom() {
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("http://localhost/get");

HEADERS_REMOVED_ON_REQUEST
.forEach(header -> builder.header(StringUtils.capitalize(header.toLowerCase()), header + "1"));

LinkedHashSet<String> customHeaders = new LinkedHashSet<>();
HEADERS_REMOVED_ON_REQUEST.forEach(header -> {
String newHeader = header.charAt(0) + StringUtils.capitalize(header.substring(1));
customHeaders.add(newHeader);
});
RemoveHopByHopHeadersFilter filter = new RemoveHopByHopHeadersFilter();
filter.setHeaders(customHeaders);
testFilter(filter, MockServerWebExchange.from(builder));
}

@Test
public void removesHeadersListedInConnectionHeader() {
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("http://localhost/get");
Expand All @@ -64,7 +83,11 @@ public void removesHeadersListedInConnectionHeader() {
}

private void testFilter(MockServerWebExchange exchange, String... additionalHeaders) {
RemoveHopByHopHeadersFilter filter = new RemoveHopByHopHeadersFilter();
testFilter(new RemoveHopByHopHeadersFilter(), exchange, additionalHeaders);
}

private void testFilter(RemoveHopByHopHeadersFilter filter, MockServerWebExchange exchange,
String... additionalHeaders) {
HttpHeaders headers = filter.filter(exchange.getRequest().getHeaders(), exchange);

Set<String> toRemove = new HashSet<>(HEADERS_REMOVED_ON_REQUEST);
Expand Down

0 comments on commit d76a48d

Please sign in to comment.