fix(session): reject edge control characters

This commit is contained in:
kyinhub
2026-07-26 09:06:59 -07:00
committed by sususu98
parent c702c9ac21
commit c1f16b706e
2 changed files with 12 additions and 4 deletions

View File

@@ -42,15 +42,15 @@ type canonicalPart struct {
// NormalizeExplicitID validates an explicit client-provided session identifier.
// It preserves opaque printable values while rejecting oversized or control-bearing IDs.
func NormalizeExplicitID(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" || len(raw) > 256 {
return ""
}
for _, r := range raw {
if unicode.IsControl(r) {
return ""
}
}
raw = strings.TrimSpace(raw)
if raw == "" || len(raw) > 256 {
return ""
}
return raw
}

View File

@@ -244,6 +244,14 @@ func TestEnrichDerivesAfterInvalidSessionIdentity(t *testing.T) {
name: "oversized prompt cache key",
payload: []byte(`{"prompt_cache_key":"` + strings.Repeat("x", 257) + `",` + baseMessages + `}`),
},
{
name: "trailing control character prompt cache key",
payload: []byte(`{"prompt_cache_key":"tenant\n",` + baseMessages + `}`),
},
{
name: "leading control character prompt cache key",
payload: []byte(`{"prompt_cache_key":"\ttenant",` + baseMessages + `}`),
},
{
name: "control character session header",
payload: []byte(`{` + baseMessages + `}`),