Unverified Commit 02d19fb5 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

Optimize HTML escaping for safe JSON strings

Signed-off-by: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent cf10c50a
import { htmlEscapeAttributeString, htmlEscapeJsonString } from './htmlescape'
describe('htmlEscapeJsonString', () => {
it('returns strings without HTML-sensitive characters unchanged', () => {
expect(htmlEscapeJsonString('plain JSON data / 😀')).toBe(
'plain JSON data / 😀'
)
})
it('escapes every HTML-sensitive character', () => {
expect(htmlEscapeJsonString('&><\u2028\u2029')).toBe(
'\\u0026\\u003e\\u003c\\u2028\\u2029'
)
})
it('escapes sparse matches at the start, middle, and end', () => {
expect(htmlEscapeJsonString(`<${'a'.repeat(64)}&${'b'.repeat(64)}>`)).toBe(
`\\u003c${'a'.repeat(64)}\\u0026${'b'.repeat(64)}\\u003e`
)
})
it('preserves malformed surrogate code units', () => {
expect(htmlEscapeJsonString('\ud800safe\udfff')).toBe('\ud800safe\udfff')
})
it('preserves behavior at and above the fast-path size boundary', () => {
const atBoundary = `${'a'.repeat(1024 * 1024 - 1)}<`
const aboveBoundary = `${'a'.repeat(1024 * 1024)}&`
expect(htmlEscapeJsonString(atBoundary)).toBe(
`${'a'.repeat(1024 * 1024 - 1)}\\u003c`
)
expect(htmlEscapeJsonString(aboveBoundary)).toBe(
`${'a'.repeat(1024 * 1024)}\\u0026`
)
})
})
describe('htmlEscapeAttributeString', () => {
it('continues to escape HTML attributes', () => {
expect(htmlEscapeAttributeString(`&"'<>`)).toBe('&amp;&quot;&#39;&lt;&gt;')
})
})
// This utility is based on https://github.com/zertosh/htmlescape // This utility is based on https://github.com/zertosh/htmlescape
// License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE // License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE
const ESCAPE_LOOKUP: { [match: string]: string } = { const JSON_ESCAPE_FAST_PATH_MAX_LENGTH = 1024 * 1024
'&': '\\u0026',
'>': '\\u003e',
'<': '\\u003c',
'\u2028': '\\u2028',
'\u2029': '\\u2029',
}
export const ESCAPE_REGEX = /[&><\u2028\u2029]/g export const ESCAPE_REGEX = /[&><\u2028\u2029]/g
function getJsonEscapeReplacement(match: string): string {
switch (match.charCodeAt(0)) {
case 38:
return '\\u0026'
case 62:
return '\\u003e'
case 60:
return '\\u003c'
case 0x2028:
return '\\u2028'
case 0x2029:
return '\\u2029'
default:
return `\\u${match.charCodeAt(0).toString(16).padStart(4, '0')}`
}
}
const ATTRIBUTE_ESCAPE_LOOKUP: { [match: string]: string } = { const ATTRIBUTE_ESCAPE_LOOKUP: { [match: string]: string } = {
'&': '&amp;', '&': '&amp;',
'"': '&quot;', '"': '&quot;',
...@@ -22,7 +33,20 @@ const ATTRIBUTE_ESCAPE_LOOKUP: { [match: string]: string } = { ...@@ -22,7 +33,20 @@ const ATTRIBUTE_ESCAPE_LOOKUP: { [match: string]: string } = {
const ATTRIBUTE_ESCAPE_REGEX = /[&"'<>]/g const ATTRIBUTE_ESCAPE_REGEX = /[&"'<>]/g
export function htmlEscapeJsonString(str: string): string { export function htmlEscapeJsonString(str: string): string {
return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]) // Native substring search avoids the RegExp replacement setup for the common
// case where medium-sized serialized data contains no HTML-sensitive bytes.
// The RegExp path becomes faster again for multi-megabyte one-byte strings.
if (
str.length < JSON_ESCAPE_FAST_PATH_MAX_LENGTH &&
str.indexOf('&') === -1 &&
str.indexOf('>') === -1 &&
str.indexOf('<') === -1 &&
str.indexOf('\u2028') === -1 &&
str.indexOf('\u2029') === -1
) {
return str
}
return str.replace(ESCAPE_REGEX, getJsonEscapeReplacement)
} }
export function htmlEscapeAttributeString(str: string): string { export function htmlEscapeAttributeString(str: string): string {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment