Skip to content

Commit

Permalink
RewriteInterceptor now returns problem json upon encountering invalid…
Browse files Browse the repository at this point in the history
… URIs and test added for confirming interceptor error behaviours (#765)

Signed-off-by: t-burch <[email protected]>
  • Loading branch information
t-burch authored Oct 25, 2023
1 parent 6d7c821 commit 6fbe1d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import org.slf4j.*;

import java.io.*;
import java.net.URISyntaxException;
import java.util.*;
import java.util.regex.*;

import static com.predic8.membrane.core.exceptions.ProblemDetails.createProblemDetails;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.Set.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Type.*;
Expand Down Expand Up @@ -147,8 +149,10 @@ public Outcome handleRequest(Exchange exc) throws Exception {
ListIterator<String> it = exc.getDestinations().listIterator();
while ( it.hasNext() ) {
String dest = it.next();
String pathQuery = getPathQueryOrSetError(router.getUriFactory(), dest, exc);
if (pathQuery == null)
return RETURN;

String pathQuery = URLUtil.getPathQuery(router.getUriFactory(), dest);
int pathBegin = -1;
int authorityBegin = dest.indexOf("//");
if (authorityBegin != -1)
Expand Down Expand Up @@ -187,14 +191,32 @@ public Outcome handleRequest(Exchange exc) throws Exception {
if (mapping != null && mapping.do_ == REWRITE) {
String newDest = replace(exc.getRequest().getUri(), mapping);
if (newDest.contains("://")) {
newDest = URLUtil.getPathQuery(router.getUriFactory(), newDest);
newDest = getPathQueryOrSetError(router.getUriFactory(), newDest, exc);
if (newDest == null)
return RETURN;
}
exc.getRequest().setUri(newDest);
}

return CONTINUE;
}

private String getPathQueryOrSetError(URIFactory factory, String destination, Exchange exc) throws URISyntaxException {
String pathQuery;
try {
pathQuery = URLUtil.getPathQuery(factory, destination);
} catch(URISyntaxException ignore) {
exc.setResponse(
createProblemDetails(
400,
"/uri-parser",
"This URL does not follow the URI specification. Confirm the validity of the provided URL.")
);
return null;
}
return pathQuery;
}

private void logMappings() {
for (Mapping m : mappings) {
log.debug("[from:"+m.from+"],[to:"+m.to+"],[do:"+m.do_+"]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
package com.predic8.membrane.core.interceptor.rewrite;

import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;
import static com.predic8.membrane.core.interceptor.Outcome.RETURN;
import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import com.predic8.membrane.core.interceptor.Outcome;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -77,11 +79,27 @@ void testRewrite() throws Exception {
}

@Test
void storeSample() throws Exception {
void storeSample() throws Exception {
exc.setRequest(MessageUtil.getGetRequest("https://api.predic8.de/store/products/"));
assertEquals(CONTINUE, di.handleRequest(exc));
assertEquals(CONTINUE, rewriter.handleRequest(exc));
assertEquals("https://api.predic8.de/shop/v2/products/", exc.getDestinations().get(0));
}

@Test
void invalidURI() throws Exception {
exc.setRequest(MessageUtil.getGetRequest("/buy/banana/%"));
exc.setRule(sp);

assertEquals(CONTINUE, di.handleRequest(exc));
assertEquals(RETURN, rewriter.handleRequest(exc));
assertEquals(
"""
{
"type" : "http://membrane-api.io/error/uri-parser",
"title" : "This URL does not follow the URI specification. Confirm the validity of the provided URL."
}""",
exc.getResponse().getBodyAsStringDecoded()
);
}
}

0 comments on commit 6fbe1d8

Please sign in to comment.