mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(apigatewayv2): fix route matching for path-parameter routes (#368)
routeKeyMatchesPath() used String.replace() on Pattern.quote() output,
which performs literal string matching. Path-parameter placeholders like
{id} and {proxy+} were wrapped in \Q...\E by Pattern.quote(), making
them invisible to the replace() calls. All parameterized routes silently
failed to match.
Rewritten to iterate over placeholders with a regex Matcher, quoting
only the literal segments between them. {param} maps to [^/]+ and
{proxy+} maps to .+ as before.
This commit is contained in:
1 file changed
+13
-6
+13
-6
@@ -185,12 +185,19 @@ public class ApiGatewayV2Service {
|
||||
String pattern = routeKey.substring(space + 1);
|
||||
if (!method.equalsIgnoreCase(httpMethod)) return false;
|
||||
|
||||
// Convert path template to regex: {param} → [^/]+, {proxy+} → .+
|
||||
String regex = "^" + Pattern.quote(pattern)
|
||||
.replace("\\{proxy+\\}", "\\E.+\\Q")
|
||||
.replace("\\{", "\\E[^/]+\\Q")
|
||||
.replaceAll("\\\\Q\\\\E", "") + "$";
|
||||
return path.matches(regex);
|
||||
// Build regex from path template: {proxy+} -> .+, {param} -> [^/]+
|
||||
// Quote literal segments to avoid regex injection from path patterns
|
||||
StringBuilder regex = new StringBuilder("^");
|
||||
java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\{([^}]*)}").matcher(pattern);
|
||||
int last = 0;
|
||||
while (m.find()) {
|
||||
regex.append(Pattern.quote(pattern.substring(last, m.start())));
|
||||
regex.append(m.group(1).endsWith("+") ? ".+" : "[^/]+");
|
||||
last = m.end();
|
||||
}
|
||||
regex.append(Pattern.quote(pattern.substring(last)));
|
||||
regex.append("$");
|
||||
return path.matches(regex.toString());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Integration CRUD ────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user