From c60313de3f0797f987bc7a392d75aebd17020158 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 11 Jan 2018 16:36:35 -0500 Subject: [PATCH] Refine UriTemplate match pattern The match/matches methods of UriTemplate use a regex with (.*) in place of URI variables, which work fine except in the end where such a pattern can match greedily more than one segment. This commit updates the regex to use ([^/]*) instead since URI variables are only meant to be used within a single path segment. Issue: SPR-16169 --- .../org/springframework/web/util/UriTemplate.java | 2 +- .../springframework/web/util/UriTemplateTests.java | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index 1cda442eee..8751f27e9a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -212,7 +212,7 @@ public class UriTemplate implements Serializable { String variable = builder.toString(); int idx = variable.indexOf(':'); if (idx == -1) { - pattern.append("(.*)"); + pattern.append("([^/]*)"); variableNames.add(variable); } else { diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 99c642f06b..aacb193611 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -153,9 +153,7 @@ public class UriTemplateTests { assertEquals("Invalid match", expected, result); } - // SPR-13627 - - @Test + @Test // SPR-13627 public void matchCustomRegexWithNestedCurlyBraces() throws Exception { UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}"); Map result = template.match("/site.co.eu"); @@ -180,6 +178,12 @@ public class UriTemplateTests { assertEquals("Invalid match", expected, result); } + @Test // SPR-16169 + public void matchWithMultipleSegmentsAtTheEnd() { + UriTemplate template = new UriTemplate("/account/{accountId}"); + assertFalse(template.matches("/account/15/alias/5")); + } + @Test public void queryVariables() throws Exception { UriTemplate template = new UriTemplate("/search?q={query}"); @@ -195,9 +199,7 @@ public class UriTemplateTests { assertTrue(template.matches("/search?query=foo#bar")); } - // SPR-13705 - - @Test + @Test // SPR-13705 public void matchesWithSlashAtTheEnd() { UriTemplate uriTemplate = new UriTemplate("/test/"); assertTrue(uriTemplate.matches("/test/"));