MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / TestValidQueryText

Function TestValidQueryText

internal/server/middleware_query_test.go:383–432  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

381}
382
383func TestValidQueryText(t *testing.T) {
384 t.Parallel()
385 cases := []struct {
386 name string
387 in string
388 want bool
389 }{
390 {"empty", "", true},
391 {"plain", "search=hello&status=open", true},
392 {"escaped utf8 value", "search=caf%C3%A9", true},
393 {"literal utf8 value", "search=café", true},
394 {"escaped emoji", "search=%F0%9F%9A%80", true},
395 {"plus", "search=two+words", true},
396 {"escaped separators", "search=a%26b%3Dc", true},
397 {"escaped key", "caf%C3%A9=x", true},
398 {"no equals", "search", true},
399
400 {"escaped lone 0xff value", "search=bad-%FF-x", false},
401 {"escaped lone 0xff value, lowercase", "search=bad-%ff-x", false},
402 {"raw lone 0xff value", "search=bad-\xff-x", false},
403 {"escaped NUL value", "search=bad-%00-x", false},
404 {"raw NUL value", "search=bad-\x00-x", false},
405 {"escaped truncated sequence", "search=bad-%C3(-x", false},
406 {"escaped surrogate half", "search=%ED%A0%80", false},
407 {"escaped overlong", "search=%C0%AF", false},
408 {"bad key", "bad-%FF-key=x", false},
409 {"bad value in a later pair", "status=open&search=bad-%FF-x", false},
410 {"bad second value of a repeated key", "search=fine&search=bad-%FF-x", false},
411
412 // A malformed escape is dropped by url.ParseQuery, and by
413 // r.URL.Query(), so no handler can see it. The pairs that DO parse
414 // still decide the answer — both directions.
415 {"malformed escape, rest fine", "search=100%off&status=open", true},
416 {"malformed escape, rest bad", "search=100%off&status=bad-%FF-x", false},
417
418 // The raw check is STRICTER than the reachable set, deliberately: the
419 // bad byte here sits in a pair url.ParseQuery drops, so no handler
420 // could see it, and the request is refused anyway. Pinned because it
421 // is documented behaviour, not an accident — see validQueryText.
422 {"raw NUL inside a dropped pair", "search=ok&ignored=\x00%", false},
423 {"raw 0xff inside a dropped pair", "search=ok&ignored=\xff%", false},
424 }
425 for _, c := range cases {
426 t.Run(c.name, func(t *testing.T) {
427 if got := validQueryText(c.in); got != c.want {
428 t.Errorf("validQueryText(%q) = %v, want %v", c.in, got, c.want)
429 }
430 })
431 }
432}
433
434// TestValidQueryTextFastPathIsNeverMorePermissive is the differential check
435// behind the '%' fast path.

Callers

nothing calls this directly

Calls 2

validQueryTextFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected