Skip to content

Add decode and encode operations for URL query strings #1317

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 52 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"process": "^0.11.10",
"protobufjs": "^6.11.3",
"qr-image": "^3.2.0",
"qs": "^6.10.3",
"reflect-metadata": "^0.1.13",
"scryptsy": "^2.1.0",
"snackbarjs": "^1.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"From HTML Entity",
"URL Encode",
"URL Decode",
"Query String Encode",
"Query String Decode",
"Escape Unicode Characters",
"Unescape Unicode Characters",
"Normalise Unicode",
Expand Down
74 changes: 74 additions & 0 deletions src/core/operations/QueryStringDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @author Benjamin Altpeter [[email protected]]
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/

import qs from "qs";
import Operation from "../Operation.mjs";

/**
* Query String Decode operation
*/
class QueryStringDecode extends Operation {
/**
* QueryStringDecode constructor
*/
constructor() {
super();

this.name = "Query String Decode";
this.module = "URL";
this.description =
"Converts URL query strings into a JSON representation.<br><br>e.g. <code>a=b&c=1</code> becomes <code>{&quot;a&quot;: &quot;b&quot;, &quot;c&quot;: &quot;1&quot;}</code>";
this.infoURL = "https://wikipedia.org/wiki/Query_string";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
{
name: "Depth",
type: "number",
value: 5,
},
{
name: "Parameter limit",
type: "number",
value: 1000,
},
{
name: "Delimiter",
type: "string",
value: "&",
},
{
name: "Allow dot notation (<code>a.b=c</code>)?",
type: "boolean",
value: false,
},
{
name: "Allow comma arrays (<code>a=b,c</code>)?",
type: "boolean",
value: false,
},
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
const [depth, parameterLimit, delimiter, allowDots, comma] = args;
return qs.parse(input, {
depth,
delimiter,
parameterLimit,
allowDots,
comma,
ignoreQueryPrefix: true,
});
}
}

export default QueryStringDecode;
64 changes: 64 additions & 0 deletions src/core/operations/QueryStringEncode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @author Benjamin Altpeter [[email protected]]
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/

import qs from "qs";
import Operation from "../Operation.mjs";

/**
* Query String Encode operation
*/
class QueryStringEncode extends Operation {
/**
* QueryStringEncode constructor
*/
constructor() {
super();

this.name = "Query String Encode";
this.module = "URL";
this.description =
"Converts JSON objects into a URL query string representation.<br><br>e.g. <code>{&quot;a&quot;: &quot;b&quot;, &quot;c&quot;: 1}</code> becomes <code>a=b&c=1</code>";
this.infoURL = "https://wikipedia.org/wiki/Query_string";
this.inputType = "JSON";
this.outputType = "string";
this.args = [
{
name: "Array format",
type: "option",
value: ["brackets", "indices", "repeat", "comma"],
defaultIndex: 0,
},
{
name: "Object format",
type: "option",
value: ["brackets", "dots"],
defaultIndex: 0,
},
{
name: "Delimiter",
type: "string",
value: "&",
},
];
}

/**
* @param {JSON} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [arrayFormat, objectFormat, delimiter] = args;
return qs.stringify(input, {
arrayFormat,
delimiter,
allowDots: objectFormat === "dots",
encode: false,
});
}
}

export default QueryStringEncode;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import "./tests/FletcherChecksum.mjs";
import "./tests/CMAC.mjs";
import "./tests/AESKeyWrap.mjs";
import "./tests/Rabbit.mjs";
import "./tests/QueryString.mjs";

// Cannot test operations that use the File type yet
// import "./tests/SplitColourChannels.mjs";
Expand Down
120 changes: 120 additions & 0 deletions tests/operations/tests/QueryString.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Query String tests.
*
* @author Benjamin Altpeter [[email protected]]
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/

import TestRegister from "../../lib/TestRegister.mjs";

/**
* Small helper for JSON.stringify() with the correct settings.
* @param {any} obj An object to stringify
* @returns A stringified version of the object, indented by four spaces.
*/
const json = (obj) => JSON.stringify(obj, null, 4);

TestRegister.addTests([
{
name: "Query String Decode simple example (defaults)",
input: "?a=b&c=1&d=e;f=g",
expectedOutput: json({
a: "b",
c: "1",
d: "e;f=g",
}),
recipeConfig: [
{ op: "Query String Decode", args: [5, 1000, "&", false, false] },
],
},
{
name: "Query String Decode arrays and objects (defaults)",
input: "a[]=b&a[2]=b&c[d]=e&f=g,h&i.j=k",
expectedOutput: json({
a: ["b", "b"],
c: {
d: "e",
},
f: "g,h",
"i.j": "k",
}),
recipeConfig: [
{ op: "Query String Decode", args: [5, 1000, "&", false, false] },
],
},
{
name: "Query String Decode arrays and objects (extended)",
input: "a[]=b&a[2]=b&c[d]=e&f=g,h&i.j=k",
expectedOutput: json({
a: ["b", "b"],
c: {
d: "e",
},
f: ["g", "h"],
i: {
j: "k",
},
}),
recipeConfig: [
{ op: "Query String Decode", args: [5, 1000, "&", true, true] },
],
},
{
name: "Query String Decode delimiter",
input: "a=b;c=d",
expectedOutput: json({
a: "b",
c: "d",
}),
recipeConfig: [
{ op: "Query String Decode", args: [5, 1000, ";", false, false] },
],
},
{
name: "Query String Decode depth (default)",
input: "a[b][c][d][e][f][g][h]=5",
expectedOutput: json({
a: {
b: {
c: {
d: {
e: {
f: {
"[g][h]": "5",
},
},
},
},
},
},
}),
recipeConfig: [
{ op: "Query String Decode", args: [5, 1000, "&", false, false] },
],
},
{
name: "Query String Decode depth (higher)",
input: "a[b][c][d][e][f][g][h]=5",
expectedOutput: json({
a: {
b: {
c: {
d: {
e: {
f: {
g: {
h: "5",
},
},
},
},
},
},
},
}),
recipeConfig: [
{ op: "Query String Decode", args: [7, 1000, "&", false, false] },
],
},
]);