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:
@@ -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 "</document"
|
||||
}
|
||||
return "<document"
|
||||
|
||||
@@ -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, "</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)
|
||||
|
||||
Reference in New Issue
Block a user