Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL: Fix URLSearchParams decoding to not throw URIError #1173

Merged
merged 1 commit into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions polyfills/URL/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,29 @@
return output.replace(/%20/g, '+');
}

// NOTE: URL API accepts inputs like `?x=%`, `?x=%a`, and `?x=%2sf`
// as literals, whereas legacy decodeURIComponent would throw
// URIError (as specified by ECMAScript).
//
// https://url.spec.whatwg.org/#percent-decode
function percent_decode(bytes) {
// NOTE:
// * Only decode pairs of exactly two bytes.
// * Only decode bytes in range 0-9, A-F, a-f.
// * Decode as many pairs at the same time as possible.
// This is because we're not actually operating on internal bytes,
// but on a valid UTF string, and the string must remain valid at
// all times, and decodeURIComponent will throw when attempting to
// decode a byte that represents only part of a codepoint, for example
// "%7F" separately from "%7F%C3%BF".
return bytes.replace(/((%[0-9A-Fa-f]{2})*)/g, function (_, m) {
return decodeURIComponent(m);
});
}

// NOTE: Doesn't do the encoding/decoding dance
//
// https://url.spec.whatwg.org/#concept-urlencoded-parser
function urlencoded_parse(input, isindex) {
var sequences = input.split('&');
if (isindex && sequences[0].indexOf('=') === -1)
Expand All @@ -83,8 +105,8 @@
var output = [];
pairs.forEach(function (pair) {
output.push({
name: decodeURIComponent(pair.name),
value: decodeURIComponent(pair.value)
name: percent_decode(pair.name),
value: percent_decode(pair.value)
});
});
return output;
Expand Down
21 changes: 21 additions & 0 deletions polyfills/URL/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ describe('WPT tests', function () {
proclaim["throws"](function() { new URLSearchParams([[1,2,3]]); });
});

// Examples from wpt/url/urlencoded-parser.any.js
[
{
input: "z=b&a=b&z=a&a=a",
Expand All @@ -603,6 +604,26 @@ describe('WPT tests', function () {
input: "\uFFFD=x&\uFFFC&\uFFFD=a",
output: [["\uFFFC", ""], ["\uFFFD", "x"], ["\uFFFD", "a"]]
},
{
input: '%a=a',
output: [['%a', 'a']]
},
{
input: "id=0&value=%",
output: [['id', '0'], ['value', '%']]
},
{
input: "b=%2sf%2a",
output: [['b', '%2sf*']]
},
{
input: "b=%2%2af%2a",
output: [['b', '%2*f*']]
},
{
input: "b=%%2a",
output: [['b', '%*']]
},
{
input: "ffi&🌈", // 🌈 > code point, but < code unit because two code units
output: [["🌈", ""], ["ffi", ""]]
Expand Down