edgeguard: an escaped parameter name still meets its query rule
url.ParseQuery decodes names as well as values, so "%73=%2Findex%2F%5Cthink" is the parameter "s" and the ThinkPHP probe, but the raw-text candidate check never found "s=" and the rule never ran (final review I3). When a rule's name is not found plainly and some parameter name in the query holds a "%", the query is parsed anyway. An escape in a value, the ordinary case, does not count, so ordinary queries stay allocation-free.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+50
−6
internal/edgeguard/match.go+38 −6internal/edgeguard/match_test.go+12 −0
diff --git a/internal/edgeguard/match.go b/internal/edgeguard/match.go| index 7d7e37097..8059f406c 100644 |
| --- a/internal/edgeguard/match.go |
| +++ b/internal/edgeguard/match.go |
| @@ -338,9 +338,10 @@ func stripParams(p string) string { |
| } |
| // hasParam reports whether rawQuery has a parameter called name, judged on |
| -// the raw text. nameEq is the name followed by "=": it must be at the start or right after "&". ";" is not treated |
| -// as a separator, because url.ParseQuery does not treat it as one. A name |
| -// that is percent-encoded in the query is not found. |
| +// the raw text. nameEq is the name followed by "=": it must be at the start |
| +// or right after "&". ";" is not treated as a separator, because |
| +// url.ParseQuery does not treat it as one. A name that is percent-encoded in |
| +// the query is not found here; escapedName is the check for that. |
| func hasParam(rawQuery, nameEq string) bool { |
| for from := 0; ; { |
| i := strings.Index(rawQuery[from:], nameEq) |
| @@ -355,16 +356,47 @@ func hasParam(rawQuery, nameEq string) bool { |
| } |
| } |
| +// escapedName reports whether any parameter name in rawQuery holds a "%". |
| +// url.ParseQuery decodes names as well as values, so "%73=..." is the |
| +// parameter "s", which hasParam cannot see on the raw text. It looks at the |
| +// names only: an escape in a value, the ordinary case, does not count. |
| +func escapedName(rawQuery string) bool { |
| + for rawQuery != "" { |
| + var pair string |
| + pair, rawQuery, _ = strings.Cut(rawQuery, "&") |
| + name, _, _ := strings.Cut(pair, "=") |
| + if strings.IndexByte(name, '%') >= 0 { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| // scanQuery offers the query parameter rules. The query is only parsed when |
| -// it has a parameter by a rule's name, so an ordinary request pays for a |
| -// substring search and nothing more. |
| +// it has a parameter by a rule's name, or a name with an escape in it that |
| +// might decode to one, so an ordinary request pays for a substring search |
| +// and nothing more. |
| func scanQuery(rawQuery string, b *best) { |
| var vals url.Values |
| parsed := false |
| + // escaped is escapedName(rawQuery), worked out the first time a rule's |
| + // name is not found plainly: 0 not yet, 1 no, 2 yes. |
| + escaped := 0 |
| for _, e := range idx.qparams { |
| - if e.pos < b.lo || e.pos >= b.pos || !hasParam(rawQuery, e.nameEq) { |
| + if e.pos < b.lo || e.pos >= b.pos { |
| continue |
| } |
| + if !hasParam(rawQuery, e.nameEq) { |
| + if escaped == 0 { |
| + escaped = 1 |
| + if strings.IndexByte(rawQuery, '%') >= 0 && escapedName(rawQuery) { |
| + escaped = 2 |
| + } |
| + } |
| + if escaped == 1 { |
| + continue |
| + } |
| + } |
| if !parsed { |
| // A parse error still returns the pairs that did parse. |
| vals, _ = url.ParseQuery(rawQuery) |
diff --git a/internal/edgeguard/match_test.go b/internal/edgeguard/match_test.go| index 3f6235871..2ad119708 100644 |
| --- a/internal/edgeguard/match_test.go |
| +++ b/internal/edgeguard/match_test.go |
| @@ -128,6 +128,14 @@ func TestCheckWellKnownExempt(t *testing.T) { |
| func TestCheckQueryParamBoundary(t *testing.T) { |
| runCases(t, []checkCase{ |
| {"/", `s=/index/\think`, "probe.phpframework.thinkphp"}, |
| + // url.ParseQuery decodes names as well as values, so a name spelled |
| + // with escapes is the same parameter. |
| + {"/", `%73=%2Findex%2F%5Cthink`, "probe.phpframework.thinkphp"}, |
| + {"/", `a=1&%73=%2Findex%2F%5Cthink&b=2`, "probe.phpframework.thinkphp"}, |
| + {"/", `utm_source=x%20y&%73=/index/\think`, "probe.phpframework.thinkphp"}, |
| + {"/", `%53=%2Findex%2F%5Cthink`, ""}, |
| + {"/", `%78s=%2Findex%2F%5Cthink`, ""}, |
| + {"/", `a=%73&b=/index/\think`, ""}, |
| {"/", `a=1&s=/index/\think`, "probe.phpframework.thinkphp"}, |
| {"/", `a=1&S=/index/\think`, ""}, |
| {"/", `xs=/index/\think`, ""}, |
| @@ -264,6 +272,7 @@ func TestCheckFragments(t *testing.T) { |
| {"/%2EEnv", "", ".Env", false, ""}, |
| {"/a/%2EGit/config", "", ".Git", false, ""}, |
| {"/", `a=1&s=%2Findex%2F%5CThink%5Capp`, `/index/\Think\app`, true, "s"}, |
| + {"/", `a=1&%73=%2Findex%2F%5CThink%5Capp`, `/index/\Think\app`, true, "s"}, |
| } |
| for _, c := range cases { |
| hit, ok := Check(c.path, c.rawQuery) |
| @@ -345,6 +354,9 @@ func TestCheckOrdinaryPathAllocs(t *testing.T) { |
| {"/blog/post", "utm_source=newsletter"}, |
| {"/blog/post", "page=2&sort=asc"}, |
| {"/blog/post", "session=abc"}, |
| + // An escape in a value is ordinary, and does not send the query to |
| + // the parser the way an escape in a name does. |
| + {"/search", "q=hello%20world&page=2"}, |
| {"/", ""}, |
| } { |
| if n := testing.AllocsPerRun(100, func() { Check(c.path, c.query) }); n != 0 { |