fix(apigateway): decode the ~0 JSON Pointer escape in stage method-settings patch paths (#3526)

This commit is contained in:
Matheus André authored and GitHub committed 2026-09-13 00:29:55 -04:00
1 parent a6f1751dfb
commit bc44fc9106
2 files changed
+36 -1

No files matched your search

@@ -735,7 +735,7 @@ public class ApiGatewayService {
// AWS escapes the resource path's slashes as ~1 in the patch path ("/~1pets/GET/...")
// but reports the setting keyed by the plain path ("pets/GET"), so normalise both the
// escaped and unescaped spellings onto that one key.
String resourcePath = prefix.substring(0, lastSlash).replace("~1", "/");
String resourcePath = unescapeJsonPointer(prefix.substring(0, lastSlash));
if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1);
String httpMethod = prefix.substring(lastSlash + 1);
String methodKey = resourcePath + "/" + httpMethod;
@@ -747,6 +747,15 @@ public class ApiGatewayService {
}
}
/**
* Reverses RFC 6901 JSON Pointer escaping: {@code ~1} is a literal {@code /} and {@code ~0} a
* literal {@code ~}. The order matters: {@code ~1} must be decoded first so that {@code ~01}
* (an escaped literal "~1") is not turned into a slash.
*/
static String unescapeJsonPointer(String segment) {
return segment.replace("~1", "/").replace("~0", "~");
}
private void applyMethodSettingValue(MethodSetting setting, String settingKey, String value) {
if (value == null) return;
switch (settingKey) {
@@ -136,6 +136,32 @@ class ApiGatewayIntegrationReadBackTest {
.body("methodSettings.'widget/POST'.throttlingBurstLimit", equalTo(77));
}
@Test
void methodSettingsUnescapeTheJsonPointerTildeEscape() {
String deploymentId = given().contentType(ContentType.JSON).body("{}")
.when().post("/restapis/" + apiId + "/deployments")
.then().statusCode(201).extract().path("id");
given().contentType(ContentType.JSON)
.body("{\"stageName\":\"tilde\",\"deploymentId\":\"" + deploymentId + "\"}")
.when().post("/restapis/" + apiId + "/stages")
.then().statusCode(201);
// RFC 6901 escapes a literal "~" as ~0 and "/" as ~1. "/~1pets~0promo/GET/..." therefore
// addresses the resource "/pets~promo"; only ~1 used to be decoded, leaving a stray "~0".
// "~01" is an escaped literal "~1", not a slash, so decoding order matters too.
given().contentType(ContentType.JSON)
.body("{\"patchOperations\":["
+ "{\"op\":\"replace\",\"path\":\"/~1pets~0promo/GET/throttling/burstLimit\",\"value\":\"12\"},"
+ "{\"op\":\"replace\",\"path\":\"/~1v~01/POST/throttling/rateLimit\",\"value\":\"3.5\"}]}")
.when().patch("/restapis/" + apiId + "/stages/tilde")
.then().statusCode(200);
given().when().get("/restapis/" + apiId + "/stages/tilde")
.then().statusCode(200)
.body("methodSettings.'pets~promo/GET'.throttlingBurstLimit", equalTo(12))
.body("methodSettings.'v~1/POST'.throttlingRateLimit", equalTo(3.5f));
}
@Test
void anIntegrationWithNoTypeReportsRatherThanCrashing() {
given().contentType(ContentType.JSON).body("{\"authorizationType\":\"NONE\"}")