From 9e79859ceae9c8ff7a24497e1a2aaa9fdcc0b039 Mon Sep 17 00:00:00 2001 From: TiM Date: Mon, 13 Apr 2026 12:57:25 +1200 Subject: [PATCH] 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. --- .../apigatewayv2/ApiGatewayV2Service.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index 3cb3f76db2..2963b55b69 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -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 ────────────────────────────