Skip to content

Commit ca479af

Browse files
author
Kz Ho
committed
Keep writeStringSlowPath & writeStringSlowPathWithHTMLEscaped func the same
1 parent de82dbd commit ca479af

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

stream_str.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,34 @@ func writeStringSlowPath(stream *Stream, i int, s string, valLen int) {
362362
start = i
363363
continue
364364
}
365-
i++
366-
continue
365+
c, size := utf8.DecodeRuneInString(s[i:])
366+
if c == utf8.RuneError && size == 1 {
367+
if start < i {
368+
stream.WriteRaw(s[start:i])
369+
}
370+
stream.WriteRaw(`\ufffd`)
371+
i++
372+
start = i
373+
continue
374+
}
375+
// U+2028 is LINE SEPARATOR.
376+
// U+2029 is PARAGRAPH SEPARATOR.
377+
// They are both technically valid characters in JSON strings,
378+
// but don't work in JSONP, which has to be evaluated as JavaScript,
379+
// and can lead to security holes there. It is valid JSON to
380+
// escape them, so we do so unconditionally.
381+
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
382+
if c == '\u2028' || c == '\u2029' {
383+
if start < i {
384+
stream.WriteRaw(s[start:i])
385+
}
386+
stream.WriteRaw(`\u202`)
387+
stream.writeByte(hex[c&0xF])
388+
i += size
389+
start = i
390+
continue
391+
}
392+
i += size
367393
}
368394
if start < len(s) {
369395
stream.WriteRaw(s[start:])

0 commit comments

Comments
 (0)