-
Notifications
You must be signed in to change notification settings - Fork 0
Bugfix/unmatched path returns 404 #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ out/ | |
| .env.edge | ||
|
|
||
| .DS_Store | ||
| tmp/ | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
58 changes: 58 additions & 0 deletions
58
src/main/java/au/org/aodn/oceancurrent/configuration/TrailingSlashNormalizationFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package au.org.aodn.oceancurrent.configuration; | ||
|
|
||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletRequestWrapper; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import lombok.NonNull; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Lets a trailing slash resolve to the same handler, e.g. | ||
| * {@code /metadata/latest-dates/sixDaySst-sst/} behaves like | ||
| * {@code /metadata/latest-dates/sixDaySst-sst}. | ||
| * | ||
| * <p>Spring Boot 3 dropped trailing-slash matching by default, but upstream proxies | ||
| * (e.g. AWS Amplify) can append one. We strip it by wrapping the request and | ||
| * continuing the same chain — no redirect (which could loop if the proxy re-adds | ||
| * the slash) and no deprecated path-match config. Running first means routing and | ||
| * security both see the trimmed path. Swagger, API-docs and actuator paths are left alone. | ||
| */ | ||
| @Component | ||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| public class TrailingSlashNormalizationFilter extends OncePerRequestFilter { | ||
|
|
||
| // Framework-managed paths whose trailing-slash handling we must not interfere with: | ||
| // springdoc UI/docs/webjars and the actuator base-path (management.endpoints.web.base-path). | ||
| private static final List<String> EXCLUDED_PREFIXES = | ||
| List.of("/swagger-ui", "/v3/api-docs", "/webjars", "/manage"); | ||
|
|
||
| @Override | ||
| protected boolean shouldNotFilter(@NonNull HttpServletRequest request) { | ||
| String withinContext = request.getRequestURI().substring(request.getContextPath().length()); | ||
| if (withinContext.length() <= 1 || !withinContext.endsWith("/")) { | ||
| return true; | ||
| } | ||
| return EXCLUDED_PREFIXES.stream().anyMatch(withinContext::startsWith); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(@NonNull HttpServletRequest request, | ||
| @NonNull HttpServletResponse response, | ||
| @NonNull FilterChain filterChain) throws ServletException, IOException { | ||
| String trimmedUri = request.getRequestURI().substring(0, request.getRequestURI().length() - 1); | ||
| filterChain.doFilter(new HttpServletRequestWrapper(request) { | ||
| @Override | ||
| public String getRequestURI() { | ||
| return trimmedUri; | ||
| } | ||
| }, response); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...est/java/au/org/aodn/oceancurrent/configuration/TrailingSlashNormalizationFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package au.org.aodn.oceancurrent.configuration; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.mock.web.MockFilterChain; | ||
| import org.springframework.mock.web.MockHttpServletRequest; | ||
| import org.springframework.mock.web.MockHttpServletResponse; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class TrailingSlashNormalizationFilterTest { | ||
|
|
||
| private final TrailingSlashNormalizationFilter filter = new TrailingSlashNormalizationFilter(); | ||
|
|
||
| @Test | ||
| void trailingSlash_isTrimmedFromWrappedRequest() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/metadata/latest-dates/sixDaySst-sst/"); | ||
|
|
||
| assertEquals("/metadata/latest-dates/sixDaySst-sst", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void pathWithoutTrailingSlash_isLeftUnchanged() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/metadata/latest-dates/sixDaySst-sst"); | ||
|
|
||
| assertEquals("/metadata/latest-dates/sixDaySst-sst", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void swaggerUiPath_isNotTrimmed() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/swagger-ui/"); | ||
|
|
||
| assertEquals("/swagger-ui/", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void apiDocsPath_isNotTrimmed() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/v3/api-docs/"); | ||
|
|
||
| assertEquals("/v3/api-docs/", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void trailingSlash_onImageListPath_isTrimmed() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/metadata/image-list/sixDaySst-sst/"); | ||
|
|
||
| assertEquals("/metadata/image-list/sixDaySst-sst", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void trailingSlash_withQueryString_trimsPathAndKeepsQuery() throws Exception { | ||
| MockHttpServletRequest request = | ||
| new MockHttpServletRequest("GET", "/metadata/image-list/sixDaySst-sst/"); | ||
| request.setQueryString("region=NW"); | ||
| request.setParameter("region", "NW"); | ||
| MockFilterChain chain = new MockFilterChain(); | ||
|
|
||
| filter.doFilter(request, new MockHttpServletResponse(), chain); | ||
|
|
||
| HttpServletRequest forwarded = (HttpServletRequest) Objects.requireNonNull(chain.getRequest()); | ||
| assertEquals("/metadata/image-list/sixDaySst-sst", forwarded.getRequestURI()); | ||
| assertEquals("region=NW", forwarded.getQueryString()); | ||
| assertEquals("NW", forwarded.getParameter("region")); | ||
| } | ||
|
|
||
| @Test | ||
| void actuatorPath_isNotTrimmed() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/manage/health/"); | ||
|
|
||
| assertEquals("/manage/health/", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void rootPath_isLeftUnchanged() throws Exception { | ||
| HttpServletRequest forwarded = filterAndCapture("/"); | ||
|
|
||
| assertEquals("/", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void contextRoot_withContextPath_isLeftUnchanged() throws Exception { | ||
| // With context-path /api/v1, a request to the context root (/api/v1/) must not be | ||
| // trimmed to /api/v1, since within the context that is the root path "/". | ||
| HttpServletRequest forwarded = filterAndCaptureWithContext("/api/v1", "/api/v1/"); | ||
|
|
||
| assertEquals("/api/v1/", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| @Test | ||
| void trailingSlash_withContextPath_isTrimmed() throws Exception { | ||
| HttpServletRequest forwarded = | ||
| filterAndCaptureWithContext("/api/v1", "/api/v1/metadata/image-list/sixDaySst-sst/"); | ||
|
|
||
| assertEquals("/api/v1/metadata/image-list/sixDaySst-sst", forwarded.getRequestURI()); | ||
| } | ||
|
|
||
| private HttpServletRequest filterAndCapture(String requestUri) throws Exception { | ||
| return filterAndCaptureWithContext("", requestUri); | ||
| } | ||
|
|
||
| private HttpServletRequest filterAndCaptureWithContext(String contextPath, String requestUri) throws Exception { | ||
| MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); | ||
| request.setContextPath(contextPath); | ||
| MockHttpServletResponse response = new MockHttpServletResponse(); | ||
| MockFilterChain chain = new MockFilterChain(); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| return (HttpServletRequest) Objects.requireNonNull(chain.getRequest()); | ||
| } | ||
|
weited marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.