Browse Source

Fixed 'Parse URI' operation and improved error handling from worker

n1474335 7 years ago
parent
commit
599fefb39b

+ 4 - 3
src/core/Chef.js

@@ -68,9 +68,10 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
     try {
         progress = await recipe.execute(this.dish, progress);
     } catch (err) {
-        // Return the error in the result so that everything else gets correctly updated
-        // rather than throwing it here and losing state info.
-        error = err;
+        console.log(err);
+        error = {
+            displayStr: err.displayStr,
+        };
         progress = err.progress;
     }
 

+ 3 - 3
src/core/config/OperationConfig.js

@@ -760,14 +760,14 @@ const OperationConfig = {
         ]
     },
     "URL Decode": {
-        module: "Default",
+        module: "URL",
         description: "Converts URI/URL percent-encoded characters back to their raw values.<br><br>e.g. <code>%3d</code> becomes <code>=</code>",
         inputType: "string",
         outputType: "string",
         args: []
     },
     "URL Encode": {
-        module: "Default",
+        module: "URL",
         description: "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.<br><br>e.g. <code>=</code> becomes <code>%3d</code>",
         inputType: "string",
         outputType: "string",
@@ -780,7 +780,7 @@ const OperationConfig = {
         ]
     },
     "Parse URI": {
-        module: "Default",
+        module: "URL",
         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.",
         inputType: "string",
         outputType: "string",

+ 0 - 4
src/core/config/modules/Default.js

@@ -26,7 +26,6 @@ import SeqUtils from "../../operations/SeqUtils.js";
 import StrUtils from "../../operations/StrUtils.js";
 import Tidy from "../../operations/Tidy.js";
 import Unicode from "../../operations/Unicode.js";
-import URL_ from "../../operations/URL.js";
 import UUID from "../../operations/UUID.js";
 
 
@@ -77,9 +76,6 @@ OpModules.Default = {
     "From HTML Entity":     HTML.runFromEntity,
     "Strip HTML tags":      HTML.runStripTags,
     "Parse colour code":    HTML.runParseColourCode,
-    "URL Encode":           URL_.runTo,
-    "URL Decode":           URL_.runFrom,
-    "Parse URI":            URL_.runParse,
     "Unescape Unicode Characters": Unicode.runUnescape,
     "To Quoted Printable":  QuotedPrintable.runTo,
     "From Quoted Printable": QuotedPrintable.runFrom,

+ 3 - 1
src/core/config/modules/OpModules.js

@@ -19,6 +19,7 @@ import ImageModule from "./Image.js";
 import JSBNModule from "./JSBN.js";
 import PublicKeyModule from "./PublicKey.js";
 import ShellcodeModule from "./Shellcode.js";
+import URLModule from "./URL.js";
 
 Object.assign(
     OpModules,
@@ -33,7 +34,8 @@ Object.assign(
     ImageModule,
     JSBNModule,
     PublicKeyModule,
-    ShellcodeModule
+    ShellcodeModule,
+    URLModule
 );
 
 export default OpModules;

+ 23 - 0
src/core/config/modules/URL.js

@@ -0,0 +1,23 @@
+import URL_ from "../../operations/URL.js";
+
+
+/**
+ * URL module.
+ *
+ * Libraries:
+ *  - Utils.js
+ *  - url
+ *
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2017
+ * @license Apache-2.0
+ */
+let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
+
+OpModules.URL = {
+    "URL Encode": URL_.runTo,
+    "URL Decode": URL_.runFrom,
+    "Parse URI":  URL_.runParse,
+};
+
+export default OpModules;

+ 27 - 46
src/core/operations/URL.js

@@ -1,5 +1,6 @@
 /* globals unescape */
 import Utils from "../Utils.js";
+import url from "url";
 
 
 /**
@@ -58,56 +59,36 @@ const URL_ = {
      * @returns {string}
      */
     runParse: function(input, args) {
-        if (!document) {
-            throw "This operation only works in a browser.";
-        }
-
-        const a = document.createElement("a");
-
-        // Overwrite base href which will be the current CyberChef URL to reduce confusion.
-        a.href = "http://example.com/";
-        a.href = input;
-
-        if (a.protocol) {
-            let output = "";
-            if (a.hostname !== window.location.hostname) {
-                output = "Protocol:\t" + a.protocol + "\n";
-                if (a.hostname) output += "Hostname:\t" + a.hostname + "\n";
-                if (a.port) output += "Port:\t\t" + a.port + "\n";
-            }
-
-            if (a.pathname && a.pathname !== window.location.pathname) {
-                let pathname = a.pathname;
-                if (pathname.indexOf(window.location.pathname) === 0)
-                    pathname = pathname.replace(window.location.pathname, "");
-                if (pathname)
-                    output += "Path name:\t" + pathname + "\n";
-            }
-
-            if (a.hash && a.hash !== window.location.hash) {
-                output += "Hash:\t\t" + a.hash + "\n";
-            }
-
-            if (a.search && a.search !== window.location.search) {
-                output += "Arguments:\n";
-                const args_ = (a.search.slice(1, a.search.length)).split("&");
-                let splitArgs = [], padding = 0, i;
-                for (i = 0; i < args_.length; i++) {
-                    splitArgs.push(args_[i].split("="));
-                    padding = (splitArgs[i][0].length > padding) ? splitArgs[i][0].length : padding;
-                }
-                for (i = 0; i < splitArgs.length; i++) {
-                    output += "\t" + Utils.padRight(splitArgs[i][0], padding);
-                    if (splitArgs[i].length > 1 && splitArgs[i][1].length)
-                        output += " = " + splitArgs[i][1] + "\n";
-                    else output += "\n";
+        const uri = url.parse(input, true);
+
+        let output = "";
+
+        if (uri.protocol) output += "Protocol:\t" + uri.protocol + "\n";
+        if (uri.auth) output += "Auth:\t\t" + uri.auth + "\n";
+        if (uri.hostname) output += "Hostname:\t" + uri.hostname + "\n";
+        if (uri.port) output += "Port:\t\t" + uri.port + "\n";
+        if (uri.pathname) output += "Path name:\t" + uri.pathname + "\n";
+        if (uri.query) {
+            let keys = Object.keys(uri.query),
+                padding = 0;
+
+            keys.forEach(k => {
+                padding = (k.length > padding) ? k.length : padding;
+            });
+
+            output += "Arguments:\n";
+            for (let key in uri.query) {
+                output += "\t" + Utils.padRight(key, padding);
+                if (uri.query[key].length) {
+                    output += " = " + uri.query[key] + "\n";
+                } else {
+                    output += "\n";
                 }
             }
-
-            return output;
         }
+        if (uri.hash) output += "Hash:\t\t" + uri.hash + "\n";
 
-        return "Invalid URI";
+        return output;
     },
 
 

+ 3 - 2
src/web/App.js

@@ -88,9 +88,10 @@ App.prototype.loaded = function() {
  * An error handler for displaying the error to the user.
  *
  * @param {Error} err
+ * @param {boolean} [logToConsole=false]
  */
-App.prototype.handleError = function(err) {
-    console.error(err);
+App.prototype.handleError = function(err, logToConsole) {
+    if (logToConsole) console.error(err);
     const msg = err.displayStr || err.toString();
     this.alert(msg, "danger", this.options.errorTimeout, !this.options.showErrors);
 };

+ 1 - 1
src/web/html/index.html

@@ -382,7 +382,7 @@
                         <p>Released under the Apache Licence, Version 2.0.</p>
                         <p>
                             <a href="https://gitter.im/gchq/CyberChef">
-                                <img src="https://badges.gitter.im/gchq/CyberChef.svg">
+                                <img src="<%- require('../static/images/gitter-badge.svg') %>">
                             </a>
                         </p>
                         <br>

+ 1 - 0
src/web/static/images/gitter-badge.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="92" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="92" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h34v20H0z"/><path fill="#46BC99" d="M34 0h58v20H34z"/><path fill="url(#b)" d="M0 0h92v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="17" y="15" fill="#010101" fill-opacity=".3">chat</text><text x="17" y="14">chat</text><text x="62" y="15" fill="#010101" fill-opacity=".3">on gitter</text><text x="62" y="14">on gitter</text></g></svg>