Skip to content

Commit 19c8a8a

Browse files
fix: support constructor and __proto__ parameters in Parse URI (gchq#2578) (gchq#2581)
1 parent 8674629 commit 19c8a8a

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/core/operations/ParseURI.mjs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ParseURI extends Operation {
3333
* @returns {string}
3434
*/
3535
run(input, args) {
36-
const uri = url.parse(input, true);
36+
const uri = url.parse(input, false);
3737

3838
let output = "";
3939

@@ -43,18 +43,31 @@ class ParseURI extends Operation {
4343
if (uri.port) output += "Port:\t\t" + uri.port + "\n";
4444
if (uri.pathname) output += "Path name:\t" + uri.pathname + "\n";
4545
if (uri.query) {
46-
const keys = Object.keys(uri.query);
46+
const queryObj = Object.create(null);
47+
for (const [key, value] of new URLSearchParams(uri.query)) {
48+
if (Object.prototype.hasOwnProperty.call(queryObj, key)) {
49+
if (Array.isArray(queryObj[key])) {
50+
queryObj[key].push(value);
51+
} else {
52+
queryObj[key] = [queryObj[key], value];
53+
}
54+
} else {
55+
queryObj[key] = value;
56+
}
57+
}
58+
59+
const keys = Object.keys(queryObj);
4760
let padding = 0;
4861

4962
keys.forEach(k => {
5063
padding = (k.length > padding) ? k.length : padding;
5164
});
5265

5366
output += "Arguments:\n";
54-
for (const key in uri.query) {
67+
for (const key in queryObj) {
5568
output += "\t" + key.padEnd(padding, " ");
56-
if (uri.query[key].length) {
57-
output += " = " + uri.query[key] + "\n";
69+
if (queryObj[key].length) {
70+
output += " = " + queryObj[key] + "\n";
5871
} else {
5972
output += "\n";
6073
}

tests/browser/02_ops.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ module.exports = {
278278
// testOp(browser, "Parse TLV", "test input", "test_output");
279279
testOpHtml(browser, "Parse UDP", "04 89 00 35 00 2c 01 01", "tr:last-child td:last-child", "0x0101");
280280
// testOp(browser, "Parse UNIX file permissions", "test input", "test_output");
281-
// testOp(browser, "Parse URI", "test input", "test_output");
281+
testOp(browser, "Parse URI", "https://example.com/?constructor=ok&__proto__=hello", /Arguments:\s+constructor = ok\s+__proto__\s+= hello/);
282282
testOp(browser, "Parse User Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 ", /Architecture: amd64/);
283283
// testOp(browser, "Parse X.509 certificate", "test input", "test_output");
284284
testOpFile(browser, "Play Media", "files/mp3example.mp3", "audio", "");

tests/node/tests/operations.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,18 @@ Arguments:
729729
assert.strictEqual(result.toString(), expected);
730730
}),
731731

732+
it("Parse URI with constructor and __proto__ arguments", () => {
733+
const result = chef.parseURI("https://example.com/?constructor=ok&__proto__=hello");
734+
const expected = `Protocol: https:
735+
Hostname: example.com
736+
Path name: /
737+
Arguments:
738+
\tconstructor = ok
739+
\t__proto__ = hello
740+
`;
741+
assert.strictEqual(result.toString(), expected);
742+
}),
743+
732744
it("Parse user agent", () => {
733745
const result = chef.parseUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 ");
734746
const expected = `Browser

0 commit comments

Comments
 (0)