GeezerVison is showing github.com — url: skip plain prefixes in percent-escape checks · nodejs/node@1540dfd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common.js'); | | 4 | +const assert = require('node:assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e5], | | 8 | + length: [32, 1024], | | 9 | + position: ['start', 'end'], | | 10 | + encoding: ['valid', 'invalid', 'unicode', 'plain'], | | 11 | +}); | | 12 | + | | 13 | +function main({ n, length, position, encoding }) { | | 14 | + const prefix = 'a'.repeat(length); | | 15 | + const [encoded, decoded] = { | | 16 | + valid: ['%20', ' '], | | 17 | + invalid: ['%zz', '%zz'], | | 18 | + unicode: ['%E2%82%AC', '\u20ac'], | | 19 | + plain: ['text', 'text'], | | 20 | + }[encoding]; | | 21 | + const input = position === 'start' ? encoded + prefix : prefix + encoded; | | 22 | + const expected = position === 'start' ? decoded + prefix : prefix + decoded; | | 23 | + const query = `key=${input}`; | | 24 | + for (let index = 0; index < 1000; index++) { | | 25 | + new URLSearchParams(query); | | 26 | + } | | 27 | + | | 28 | + let result; | | 29 | + bench.start(); | | 30 | + for (let index = 0; index < n; index++) { | | 31 | + result = new URLSearchParams(query); | | 32 | + } | | 33 | + bench.end(n); | | 34 | + assert.deepStrictEqual([...result], [['key', expected]]); | | 35 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1321,11 +1321,11 @@ function unescapeFormComponent(s) { | 1321 | 1321 | } | 1322 | 1322 | } | 1323 | 1323 |
| 1324 | | -function hasPercentHex(s) { | | 1324 | +function hasPercentHex(s, start) { | 1325 | 1325 | const end = s.length - 2; | 1326 | | - for (let i = 0; i < end; i++) { | 1327 | | - if (StringPrototypeCharCodeAt(s, i) === 37 && // '%' | 1328 | | - isHexTable[StringPrototypeCharCodeAt(s, i + 1)] === 1 && | | 1326 | + for (let i = start; i !== -1 && i < end; | | 1327 | + i = StringPrototypeIndexOf(s, '%', i + 1)) { | | 1328 | + if (isHexTable[StringPrototypeCharCodeAt(s, i + 1)] === 1 && | 1329 | 1329 | isHexTable[StringPrototypeCharCodeAt(s, i + 2)] === 1) { | 1330 | 1330 | return true; | 1331 | 1331 | } | @@ -1347,7 +1347,7 @@ function decodeFormComponent(qs, start, end) { | 1347 | 1347 | // Only percent-decode when a complete %HH sequence exists. A lone '%' or | 1348 | 1348 | // a '%' followed by a non-hex character must be left intact so later | 1349 | 1349 | // serialization can encode the raw bytes. | 1350 | | - if (pct === -1 || !hasPercentHex(replaced)) { | | 1350 | + if (pct === -1 || !hasPercentHex(replaced, pct)) { | 1351 | 1351 | return replaced; | 1352 | 1352 | } | 1353 | 1353 | return unescapeFormComponent(replaced); | |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +require('../common'); | | 4 | +const assert = require('node:assert'); | | 5 | + | | 6 | +const cases = [ | | 7 | + ['%41', 'A'], | | 8 | + ['%zz', '%zz'], | | 9 | + ['%zz%41', '%zzA'], | | 10 | + ['%', '%'], | | 11 | + ['%A', '%A'], | | 12 | + ['%C3%A9', '\u00e9'], | | 13 | + ['%F0%9F%98%80', '\ud83d\ude00'], | | 14 | + ['%FF', '\ufffd'], | | 15 | + ['%E2%82', '\ufffd'], | | 16 | +]; | | 17 | + | | 18 | +for (const prefix of ['', 'k'.repeat(32), 'k'.repeat(1024), 'k+'.repeat(64)]) { | | 19 | + const decodedPrefix = prefix.replaceAll('+', ' '); | | 20 | + for (const [encoded, decoded] of cases) { | | 21 | + for (const position of ['start', 'end']) { | | 22 | + const input = position === 'start' ? encoded + prefix : prefix + encoded; | | 23 | + const expected = position === 'start' ? | | 24 | + decoded + decodedPrefix : decodedPrefix + decoded; | | 25 | + const query = `${input}=${input}`; | | 26 | + assert.deepStrictEqual([...new URLSearchParams(query)], [[expected, expected]]); | | 27 | + const url = new URL(`https://example.org/?${query}`); | | 28 | + assert.deepStrictEqual([...url.searchParams], [[expected, expected]]); | | 29 | + } | | 30 | + } | | 31 | +} |
|