Skip to content

Commit 599fefb

Browse files
committed
Fixed 'Parse URI' operation and improved error handling from worker
1 parent ec7294d commit 599fefb

File tree

9 files changed

+65
-60
lines changed

9 files changed

+65
-60
lines changed

src/core/Chef.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
6868
try {
6969
progress = await recipe.execute(this.dish, progress);
7070
} catch (err) {
71-
// Return the error in the result so that everything else gets correctly updated
72-
// rather than throwing it here and losing state info.
73-
error = err;
71+
console.log(err);
72+
error = {
73+
displayStr: err.displayStr,
74+
};
7475
progress = err.progress;
7576
}
7677

src/core/config/OperationConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,14 @@ const OperationConfig = {
760760
]
761761
},
762762
"URL Decode": {
763-
module: "Default",
763+
module: "URL",
764764
description: "Converts URI/URL percent-encoded characters back to their raw values.<br><br>e.g. <code>%3d</code> becomes <code>=</code>",
765765
inputType: "string",
766766
outputType: "string",
767767
args: []
768768
},
769769
"URL Encode": {
770-
module: "Default",
770+
module: "URL",
771771
description: "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.<br><br>e.g. <code>=</code> becomes <code>%3d</code>",
772772
inputType: "string",
773773
outputType: "string",
@@ -780,7 +780,7 @@ const OperationConfig = {
780780
]
781781
},
782782
"Parse URI": {
783-
module: "Default",
783+
module: "URL",
784784
description: "Pretty prints complicated Uniform Resource Identifier (URI) strings for ease of reading. Particularly useful for Uniform Resource Locators (URLs) with a lot of arguments.",
785785
inputType: "string",
786786
outputType: "string",

src/core/config/modules/Default.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import SeqUtils from "../../operations/SeqUtils.js";
2626
import StrUtils from "../../operations/StrUtils.js";
2727
import Tidy from "../../operations/Tidy.js";
2828
import Unicode from "../../operations/Unicode.js";
29-
import URL_ from "../../operations/URL.js";
3029
import UUID from "../../operations/UUID.js";
3130

3231

@@ -77,9 +76,6 @@ OpModules.Default = {
7776
"From HTML Entity": HTML.runFromEntity,
7877
"Strip HTML tags": HTML.runStripTags,
7978
"Parse colour code": HTML.runParseColourCode,
80-
"URL Encode": URL_.runTo,
81-
"URL Decode": URL_.runFrom,
82-
"Parse URI": URL_.runParse,
8379
"Unescape Unicode Characters": Unicode.runUnescape,
8480
"To Quoted Printable": QuotedPrintable.runTo,
8581
"From Quoted Printable": QuotedPrintable.runFrom,

src/core/config/modules/OpModules.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import ImageModule from "./Image.js";
1919
import JSBNModule from "./JSBN.js";
2020
import PublicKeyModule from "./PublicKey.js";
2121
import ShellcodeModule from "./Shellcode.js";
22+
import URLModule from "./URL.js";
2223

2324
Object.assign(
2425
OpModules,
@@ -33,7 +34,8 @@ Object.assign(
3334
ImageModule,
3435
JSBNModule,
3536
PublicKeyModule,
36-
ShellcodeModule
37+
ShellcodeModule,
38+
URLModule
3739
);
3840

3941
export default OpModules;

src/core/config/modules/URL.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import URL_ from "../../operations/URL.js";
2+
3+
4+
/**
5+
* URL module.
6+
*
7+
* Libraries:
8+
* - Utils.js
9+
* - url
10+
*
11+
* @author n1474335 [n1474335@gmail.com]
12+
* @copyright Crown Copyright 2017
13+
* @license Apache-2.0
14+
*/
15+
let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
16+
17+
OpModules.URL = {
18+
"URL Encode": URL_.runTo,
19+
"URL Decode": URL_.runFrom,
20+
"Parse URI": URL_.runParse,
21+
};
22+
23+
export default OpModules;

src/core/operations/URL.js

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* globals unescape */
22
import Utils from "../Utils.js";
3+
import url from "url";
34

45

56
/**
@@ -58,56 +59,36 @@ const URL_ = {
5859
* @returns {string}
5960
*/
6061
runParse: function(input, args) {
61-
if (!document) {
62-
throw "This operation only works in a browser.";
63-
}
64-
65-
const a = document.createElement("a");
66-
67-
// Overwrite base href which will be the current CyberChef URL to reduce confusion.
68-
a.href = "http://example.com/";
69-
a.href = input;
70-
71-
if (a.protocol) {
72-
let output = "";
73-
if (a.hostname !== window.location.hostname) {
74-
output = "Protocol:\t" + a.protocol + "\n";
75-
if (a.hostname) output += "Hostname:\t" + a.hostname + "\n";
76-
if (a.port) output += "Port:\t\t" + a.port + "\n";
77-
}
78-
79-
if (a.pathname && a.pathname !== window.location.pathname) {
80-
let pathname = a.pathname;
81-
if (pathname.indexOf(window.location.pathname) === 0)
82-
pathname = pathname.replace(window.location.pathname, "");
83-
if (pathname)
84-
output += "Path name:\t" + pathname + "\n";
85-
}
86-
87-
if (a.hash && a.hash !== window.location.hash) {
88-
output += "Hash:\t\t" + a.hash + "\n";
89-
}
90-
91-
if (a.search && a.search !== window.location.search) {
92-
output += "Arguments:\n";
93-
const args_ = (a.search.slice(1, a.search.length)).split("&");
94-
let splitArgs = [], padding = 0, i;
95-
for (i = 0; i < args_.length; i++) {
96-
splitArgs.push(args_[i].split("="));
97-
padding = (splitArgs[i][0].length > padding) ? splitArgs[i][0].length : padding;
98-
}
99-
for (i = 0; i < splitArgs.length; i++) {
100-
output += "\t" + Utils.padRight(splitArgs[i][0], padding);
101-
if (splitArgs[i].length > 1 && splitArgs[i][1].length)
102-
output += " = " + splitArgs[i][1] + "\n";
103-
else output += "\n";
62+
const uri = url.parse(input, true);
63+
64+
let output = "";
65+
66+
if (uri.protocol) output += "Protocol:\t" + uri.protocol + "\n";
67+
if (uri.auth) output += "Auth:\t\t" + uri.auth + "\n";
68+
if (uri.hostname) output += "Hostname:\t" + uri.hostname + "\n";
69+
if (uri.port) output += "Port:\t\t" + uri.port + "\n";
70+
if (uri.pathname) output += "Path name:\t" + uri.pathname + "\n";
71+
if (uri.query) {
72+
let keys = Object.keys(uri.query),
73+
padding = 0;
74+
75+
keys.forEach(k => {
76+
padding = (k.length > padding) ? k.length : padding;
77+
});
78+
79+
output += "Arguments:\n";
80+
for (let key in uri.query) {
81+
output += "\t" + Utils.padRight(key, padding);
82+
if (uri.query[key].length) {
83+
output += " = " + uri.query[key] + "\n";
84+
} else {
85+
output += "\n";
10486
}
10587
}
106-
107-
return output;
10888
}
89+
if (uri.hash) output += "Hash:\t\t" + uri.hash + "\n";
10990

110-
return "Invalid URI";
91+
return output;
11192
},
11293

11394

src/web/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ App.prototype.loaded = function() {
8888
* An error handler for displaying the error to the user.
8989
*
9090
* @param {Error} err
91+
* @param {boolean} [logToConsole=false]
9192
*/
92-
App.prototype.handleError = function(err) {
93-
console.error(err);
93+
App.prototype.handleError = function(err, logToConsole) {
94+
if (logToConsole) console.error(err);
9495
const msg = err.displayStr || err.toString();
9596
this.alert(msg, "danger", this.options.errorTimeout, !this.options.showErrors);
9697
};

src/web/html/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ <h4 class="modal-title">CyberChef - The Cyber Swiss Army Knife</h4>
382382
<p>Released under the Apache Licence, Version 2.0.</p>
383383
<p>
384384
<a href="https://gitter.im/gchq/CyberChef">
385-
<img src="https://badges.gitter.im/gchq/CyberChef.svg">
385+
<img src="<%- require('../static/images/gitter-badge.svg') %>">
386386
</a>
387387
</p>
388388
<br>
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)