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

Add an Operation to Decode a URI to a JSON Object #1935

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
"Parse UDP",
"Parse SSH Host Key",
"Parse URI",
"Extract URI",
"URL Encode",
"URL Decode",
"Protobuf Decode",
Expand Down
65 changes: 65 additions & 0 deletions src/core/operations/ExtractURI.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @author David Tomaschik [[email protected]]
* @copyright Google LLC 2024
* @license Apache-2.0
*/

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

/**
* Extract a URI to JSON Operation
*/
class ExtractURI extends Operation {
/**
* ExtractURI Constructor
*/
constructor() {
super();

this.name = "Extract URI";
this.module = "URL";
this.description = "Extract components of URI to JSON for further processing.";
this.infoURL = "https://wikipedia.org/wiki/Uniform_Resource_Identifier";
this.inputType = "string";
this.outputType = "JSON";
this.args = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
const uri = new URL(input);
const pieces = {};
// Straight copy some attributes
[
"hash",
"hostname",
"password",
"pathname",
"port",
"protocol",
"username"
].forEach((name) => {
if (uri[name]) pieces[name] = uri[name];
});
// Now handle query params
const params = uri.searchParams;
if (params.size) {
pieces.query = {};
for (const name of params.keys()) {
const values = params.getAll(name);
if (values.length > 1) {
pieces.query[name] = values;
} else {
pieces.query[name] = values[0];
}
}
}
return pieces;
}
};

export default ExtractURI;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import "./tests/ELFInfo.mjs";
import "./tests/Enigma.mjs";
import "./tests/ExtractEmailAddresses.mjs";
import "./tests/ExtractHashes.mjs";
import "./tests/ExtractURI.mjs";
import "./tests/Float.mjs";
import "./tests/FileTree.mjs";
import "./tests/FletcherChecksum.mjs";
Expand Down
36 changes: 36 additions & 0 deletions tests/operations/tests/ExtractURI.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Extract URI Tests
*
* @author David Tomaschik [[email protected]]
* @copyright Google LLC 2024
* @license Apache-2.0
*/

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

TestRegister.addTests([
{
name: "Extract URI: Test",
input: "http://www.example.org:9999/path?foo=bar&baz=1&baz=2#frob",
expectedOutput: JSON.stringify({
"hash": "#frob",
"hostname": "www.example.org",
"pathname": "/path",
"port": "9999",
"protocol": "http:",
"query": {
"foo": "bar",
"baz": [
"1",
"2"
]
}
}, null, 4),
recipeConfig: [
{
"op": "Extract URI",
"args": [],
}
]
}
]);
Loading