Skip to content

Commit 2930c77

Browse files
author
poorna
committed
Check for score overflow
1 parent 6db482a commit 2930c77

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/main/java/co/cask/http/HttpResourceHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@
4747
public final class HttpResourceHandler implements HttpHandler {
4848

4949
private static final Logger LOG = LoggerFactory.getLogger(HttpResourceHandler.class);
50+
// Limit the number of parts of the path so that match score calculation during runtime does not overflow
51+
private static final int MAX_PATH_PARTS = 25;
5052

51-
private final PatternPathRouterWithGroups<HttpResourceModel> patternRouter = PatternPathRouterWithGroups.create();
53+
private final PatternPathRouterWithGroups<HttpResourceModel> patternRouter =
54+
PatternPathRouterWithGroups.create(MAX_PATH_PARTS);
5255
private final Iterable<HttpHandler> handlers;
5356
private final Iterable<HandlerHook> handlerHooks;
5457
private final URLRewriter urlRewriter;
@@ -327,6 +330,11 @@ public HttpMethodInfo getDestinationMethod(HttpRequest request, HttpResponder re
327330
* @return weighted score
328331
*/
329332
private long getWeightedMatchScore(Iterable<String> requestUriParts, Iterable<String> destUriParts) {
333+
// The score calculated below is a base 5 number
334+
// The score will have one digit for one part of the URI
335+
// This will allow for 27 parts in the path since log (Long.MAX_VALUE) to base 5 = 27.13
336+
// We limit the number of parts in the path to 25 using MAX_PATH_PARTS constant above to avoid overflow during
337+
// score calculation
330338
long score = 0;
331339
for (Iterator<String> rit = requestUriParts.iterator(), dit = destUriParts.iterator();
332340
rit.hasNext() && dit.hasNext(); ) {

src/main/java/co/cask/http/PatternPathRouterWithGroups.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ public final class PatternPathRouterWithGroups<T> {
4040
// non-greedy wild card match.
4141
private static final Pattern WILD_CARD_PATTERN = Pattern.compile("\\*\\*");
4242

43+
private final int maxPathParts;
4344
private final List<ImmutablePair<Pattern, RouteDestinationWithGroups>> patternRouteList;
4445

45-
public static <T> PatternPathRouterWithGroups<T> create() {
46-
return new PatternPathRouterWithGroups<>();
46+
public static <T> PatternPathRouterWithGroups<T> create(int maxPathParts) {
47+
return new PatternPathRouterWithGroups<>(maxPathParts);
4748
}
4849

4950
/**
5051
* Initialize PatternPathRouterWithGroups.
5152
*/
52-
public PatternPathRouterWithGroups() {
53+
public PatternPathRouterWithGroups(int maxPathParts) {
54+
this.maxPathParts = maxPathParts;
5355
this.patternRouteList = Lists.newArrayList();
5456
}
5557

@@ -69,6 +71,10 @@ public void add(final String source, final T destination) {
6971

7072

7173
String [] parts = path.split("/");
74+
if (parts.length - 1 > maxPathParts) {
75+
throw new IllegalArgumentException(String.format("Number of parts of path %s exceeds allowed limit %s",
76+
source, maxPathParts));
77+
}
7278
StringBuilder sb = new StringBuilder();
7379
List<String> groupNames = Lists.newArrayList();
7480

src/test/java/co/cask/http/PathRouterTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class PathRouterTest {
3131
@Test
3232
public void testPathRoutings() {
3333

34-
PatternPathRouterWithGroups<String> pathRouter = PatternPathRouterWithGroups.create();
34+
PatternPathRouterWithGroups<String> pathRouter = PatternPathRouterWithGroups.create(25);
3535
pathRouter.add("/foo/{baz}/b", "foobarb");
3636
pathRouter.add("/foo/bar/baz", "foobarbaz");
3737
pathRouter.add("/baz/bar", "bazbar");
@@ -167,4 +167,10 @@ public void testPathRoutings() {
167167
Assert.assertEquals("wildcard-foo-id-2", routes.get(0).getDestination());
168168
Assert.assertEquals(ImmutableMap.of("id", "id1"), routes.get(0).getGroupNameValues());
169169
}
170+
171+
@Test(expected = IllegalArgumentException.class)
172+
public void testMaxPathParts() throws Exception {
173+
PatternPathRouterWithGroups<String> pathRouter = PatternPathRouterWithGroups.create(5);
174+
pathRouter.add("/1/2/3/4/5/6", "max-path-parts");
175+
}
170176
}

0 commit comments

Comments
 (0)