p2: widen document fence for space and zero-width

Strip U+200B–U+200D, U+2060, U+FEFF before fencing. Match
(?i)<\s*/?\s*document so </ document> cannot sit outside the frame.
This commit is contained in:
2026-09-23 00:09:14 -07:00
parent ff87a04f94
commit a992216ec0
2 changed files with 17 additions and 2 deletions
+6 -2
View File
@@ -577,11 +577,15 @@ func min(a, b int) int {
return b
}
var documentTag = regexp.MustCompile(`(?i)</?document`)
var (
documentTag = regexp.MustCompile(`(?i)<\s*/?\s*document`)
zwChars = regexp.MustCompile("[\u200b\u200c\u200d\u2060\ufeff]")
)
func fenceDocument(s string) string {
s = zwChars.ReplaceAllString(s, "")
return documentTag.ReplaceAllStringFunc(s, func(m string) string {
if len(m) >= 2 && m[1] == '/' {
if strings.Contains(m, "/") {
return "&lt;/document"
}
return "&lt;document"
+11
View File
@@ -109,6 +109,17 @@ func TestDocumentTagStaysInFrame(t *testing.T) {
}
}
func TestFenceSpaceAndZW(t *testing.T) {
zw := "hello </documen\u200bt>\n"
out := fenceDocument(zw + "</ document>")
if strings.Contains(out, "</document>") || strings.Contains(strings.ToLower(out), "</ document>") {
t.Fatalf("unfenced: %q", out)
}
if !strings.Contains(out, "&lt;/document") {
t.Fatalf("missing escape: %q", out)
}
}
func TestFallbackOnTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)