2018-07-06 13:54:19 +02:00
|
|
|
/**
|
|
|
|
* Wrap operations for consumption in Node.
|
|
|
|
*
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2018-09-04 10:33:49 +02:00
|
|
|
/*eslint no-console: ["off"] */
|
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
import SyncDish from "./SyncDish";
|
2018-08-31 15:52:14 +02:00
|
|
|
import NodeRecipe from "./NodeRecipe";
|
2019-01-23 10:54:52 +01:00
|
|
|
import OperationConfig from "../core/config/OperationConfig.json";
|
2018-12-07 16:46:05 +01:00
|
|
|
import { sanitise, removeSubheadingsFromArray, sentenceToCamelCase } from "./apiUtils";
|
2018-08-31 15:43:14 +02:00
|
|
|
import ExludedOperationError from "../core/errors/ExcludedOperationError";
|
2018-07-06 13:54:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract default arg value from operation argument
|
|
|
|
* @param {Object} arg - an arg from an operation
|
|
|
|
*/
|
|
|
|
function extractArg(arg) {
|
|
|
|
if (arg.type === "option") {
|
|
|
|
// pick default option if not already chosen
|
|
|
|
return typeof arg.value === "string" ? arg.value : arg.value[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg.type === "editableOption") {
|
|
|
|
return typeof arg.value === "string" ? arg.value : arg.value[0].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg.type === "toggleString") {
|
|
|
|
// ensure string and option exist when user hasn't defined
|
|
|
|
arg.string = arg.string || "";
|
|
|
|
arg.option = arg.option || arg.toggleValues[0];
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* transformArgs
|
|
|
|
*
|
|
|
|
* Take the default args array and update with any user-defined
|
|
|
|
* operation arguments. Allows user to define arguments in object style,
|
|
|
|
* with accommodating name matching. Using named args in the API is more
|
|
|
|
* clear to the user.
|
|
|
|
*
|
|
|
|
* Argument name matching is case and space insensitive
|
|
|
|
* @private
|
2018-08-17 18:16:24 +02:00
|
|
|
* @param {Object[]} originalArgs - the operation-s args list
|
|
|
|
* @param {Object} newArgs - any inputted args
|
2018-07-06 13:54:19 +02:00
|
|
|
*/
|
|
|
|
function transformArgs(originalArgs, newArgs) {
|
2018-08-17 18:16:24 +02:00
|
|
|
|
|
|
|
// Filter out arg values that are list subheadings - they are surrounded in [].
|
|
|
|
// See Strings op for example.
|
|
|
|
const allArgs = Object.assign([], originalArgs).map((a) => {
|
|
|
|
if (Array.isArray(a.value)) {
|
2018-12-07 16:46:05 +01:00
|
|
|
a.value = removeSubheadingsFromArray(a.value);
|
2018-08-17 18:16:24 +02:00
|
|
|
}
|
|
|
|
return a;
|
|
|
|
});
|
2018-07-06 13:54:19 +02:00
|
|
|
|
|
|
|
if (newArgs) {
|
|
|
|
Object.keys(newArgs).map((key) => {
|
|
|
|
const index = allArgs.findIndex((arg) => {
|
|
|
|
return arg.name.toLowerCase().replace(/ /g, "") ===
|
|
|
|
key.toLowerCase().replace(/ /g, "");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
const argument = allArgs[index];
|
2018-08-31 14:45:12 +02:00
|
|
|
if (argument.type === "toggleString") {
|
|
|
|
if (typeof newArgs[key] === "string") {
|
|
|
|
argument.string = newArgs[key];
|
|
|
|
} else {
|
|
|
|
argument.string = newArgs[key].string;
|
|
|
|
argument.option = newArgs[key].option;
|
|
|
|
}
|
2018-07-06 13:54:19 +02:00
|
|
|
} else if (argument.type === "editableOption") {
|
|
|
|
// takes key: "option", key: {name, val: "string"}, key: {name, val: [...]}
|
|
|
|
argument.value = typeof newArgs[key] === "string" ? newArgs[key]: newArgs[key].value;
|
|
|
|
} else {
|
|
|
|
argument.value = newArgs[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return allArgs.map(extractArg);
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
/**
|
|
|
|
* Ensure an input is a SyncDish object.
|
|
|
|
* @param input
|
|
|
|
*/
|
2018-08-19 23:34:50 +02:00
|
|
|
function ensureIsDish(input) {
|
2018-08-14 13:03:10 +02:00
|
|
|
if (!input) {
|
|
|
|
return new SyncDish();
|
|
|
|
}
|
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
if (input instanceof SyncDish) {
|
2018-10-05 17:32:12 +02:00
|
|
|
return input;
|
2018-07-06 13:54:19 +02:00
|
|
|
} else {
|
2018-10-05 17:32:12 +02:00
|
|
|
return new SyncDish(input);
|
2018-07-06 13:54:19 +02:00
|
|
|
}
|
2018-08-19 23:55:40 +02:00
|
|
|
}
|
2018-07-06 13:54:19 +02:00
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
2018-08-16 18:14:28 +02:00
|
|
|
/**
|
|
|
|
* prepareOp: transform args, make input the right type.
|
2018-08-19 23:55:40 +02:00
|
|
|
* Also convert any Buffers to ArrayBuffers.
|
2018-08-16 18:14:28 +02:00
|
|
|
* @param opInstance - instance of the operation
|
|
|
|
* @param input - operation input
|
|
|
|
* @param args - operation args
|
|
|
|
*/
|
2018-08-19 23:34:50 +02:00
|
|
|
function prepareOp(opInstance, input, args) {
|
2018-08-16 18:14:28 +02:00
|
|
|
const dish = ensureIsDish(input);
|
|
|
|
let transformedArgs;
|
|
|
|
// Transform object-style args to original args array
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
transformedArgs = transformArgs(opInstance.args, args);
|
|
|
|
} else {
|
|
|
|
transformedArgs = args;
|
|
|
|
}
|
|
|
|
const transformedInput = dish.get(opInstance.inputType);
|
|
|
|
return {transformedInput, transformedArgs};
|
2018-08-19 23:55:40 +02:00
|
|
|
}
|
2018-08-16 18:14:28 +02:00
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* createArgOptions
|
|
|
|
*
|
|
|
|
* Create an object of options for each option or togglestring argument
|
|
|
|
* in the given operation.
|
|
|
|
*
|
|
|
|
* Argument names are converted to camel case for consistency.
|
|
|
|
*
|
|
|
|
* @param {Operation} op - the operation to extract args from
|
|
|
|
* @returns {{}} - arrays of options for option and toggleString args.
|
|
|
|
*/
|
|
|
|
function createArgOptions(op) {
|
|
|
|
const result = {};
|
|
|
|
op.args.forEach((a) => {
|
|
|
|
if (a.type === "option") {
|
|
|
|
result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.value);
|
|
|
|
} else if (a.type === "toggleString") {
|
|
|
|
result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.toggleValues);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
/**
|
|
|
|
* Wrap an operation to be consumed by node API.
|
2018-08-16 18:14:28 +02:00
|
|
|
* Checks to see if run function is async or not.
|
2018-07-06 13:54:19 +02:00
|
|
|
* new Operation().run() becomes operation()
|
|
|
|
* Perform type conversion on input
|
|
|
|
* @private
|
|
|
|
* @param {Operation} Operation
|
|
|
|
* @returns {Function} The operation's run function, wrapped in
|
|
|
|
* some type conversion logic
|
|
|
|
*/
|
|
|
|
export function wrap(OpClass) {
|
2018-08-16 18:14:28 +02:00
|
|
|
|
|
|
|
// Check to see if class's run function is async.
|
|
|
|
const opInstance = new OpClass();
|
|
|
|
const isAsync = opInstance.run.constructor.name === "AsyncFunction";
|
|
|
|
|
|
|
|
let wrapped;
|
|
|
|
|
|
|
|
// If async, wrap must be async.
|
|
|
|
if (isAsync) {
|
|
|
|
/**
|
|
|
|
* Async wrapped operation run function
|
|
|
|
* @param {*} input
|
|
|
|
* @param {Object | String[]} args - either in Object or normal args array
|
|
|
|
* @returns {Promise<SyncDish>} operation's output, on a Dish.
|
|
|
|
* @throws {OperationError} if the operation throws one.
|
|
|
|
*/
|
|
|
|
wrapped = async (input, args=null) => {
|
|
|
|
const {transformedInput, transformedArgs} = prepareOp(opInstance, input, args);
|
|
|
|
const result = await opInstance.run(transformedInput, transformedArgs);
|
|
|
|
return new SyncDish({
|
|
|
|
value: result,
|
|
|
|
type: opInstance.outputType
|
|
|
|
});
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* wrapped operation run function
|
|
|
|
* @param {*} input
|
|
|
|
* @param {Object | String[]} args - either in Object or normal args array
|
|
|
|
* @returns {SyncDish} operation's output, on a Dish.
|
|
|
|
* @throws {OperationError} if the operation throws one.
|
|
|
|
*/
|
|
|
|
wrapped = (input, args=null) => {
|
|
|
|
const {transformedInput, transformedArgs} = prepareOp(opInstance, input, args);
|
|
|
|
const result = opInstance.run(transformedInput, transformedArgs);
|
|
|
|
return new SyncDish({
|
|
|
|
value: result,
|
|
|
|
type: opInstance.outputType
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2018-07-06 13:54:19 +02:00
|
|
|
|
|
|
|
// used in chef.help
|
|
|
|
wrapped.opName = OpClass.name;
|
2018-12-21 10:46:30 +01:00
|
|
|
wrapped.argOptions = createArgOptions(opInstance);
|
2018-12-07 16:46:05 +01:00
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
return wrapped;
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
/**
|
2018-09-04 10:33:49 +02:00
|
|
|
* help: Give information about operations matching the given search term,
|
|
|
|
* or inputted operation.
|
2018-12-21 10:46:30 +01:00
|
|
|
*
|
2018-09-04 10:33:49 +02:00
|
|
|
* @param {String || wrapped operation} input - the name of the operation to get help for.
|
2018-07-06 13:54:19 +02:00
|
|
|
* Case and whitespace are ignored in search.
|
2018-09-04 10:33:49 +02:00
|
|
|
* @returns {Object[]} Config of matching operations.
|
2018-07-06 13:54:19 +02:00
|
|
|
*/
|
2018-09-04 10:33:49 +02:00
|
|
|
export function help(input) {
|
|
|
|
let searchTerm = false;
|
|
|
|
if (typeof input === "string") {
|
|
|
|
searchTerm = input;
|
|
|
|
} else if (typeof input === "function") {
|
|
|
|
searchTerm = input.opName;
|
2018-07-06 13:54:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 10:33:49 +02:00
|
|
|
if (!searchTerm) {
|
2018-07-06 13:54:19 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:33:49 +02:00
|
|
|
// Look for matches in operation name and description, listing name
|
|
|
|
// matches first.
|
|
|
|
const matches = Object.keys(OperationConfig)
|
|
|
|
// hydrate operation: swap op name for op config object (with name)
|
|
|
|
.map((m) => {
|
|
|
|
const hydrated = OperationConfig[m];
|
|
|
|
hydrated.name = m;
|
|
|
|
|
|
|
|
// Return hydrated along with what type of match it was
|
|
|
|
return {
|
|
|
|
hydrated,
|
|
|
|
nameMatch: sanitise(hydrated.name).includes(sanitise(searchTerm)),
|
|
|
|
descMatch: sanitise(hydrated.description).includes(sanitise(searchTerm))
|
|
|
|
};
|
|
|
|
})
|
|
|
|
// Filter out non-matches
|
|
|
|
.filter((result) => {
|
|
|
|
return result.nameMatch || result.descMatch;
|
|
|
|
})
|
|
|
|
// sort results with name match first
|
|
|
|
.sort((a, b) => {
|
|
|
|
const aInt = a.nameMatch ? 1 : 0;
|
|
|
|
const bInt = b.nameMatch ? 1 : 0;
|
|
|
|
return bInt - aInt;
|
|
|
|
})
|
|
|
|
// extract just the hydrated config
|
|
|
|
.map(result => result.hydrated);
|
|
|
|
|
|
|
|
// Concatenate matches but remove duplicates
|
|
|
|
if (matches && matches.length) {
|
|
|
|
console.log(`${matches.length} results found.`);
|
|
|
|
return matches;
|
2018-07-06 13:54:19 +02:00
|
|
|
}
|
2018-09-04 10:33:49 +02:00
|
|
|
|
|
|
|
console.log("No results found.");
|
2018-07-06 13:54:19 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
2018-07-06 13:54:19 +02:00
|
|
|
/**
|
|
|
|
* bake [Wrapped] - Perform an array of operations on some input.
|
|
|
|
* @param operations array of chef's operations (used in wrapping stage)
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function bake(operations){
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bake
|
|
|
|
*
|
|
|
|
* @param {*} input - some input for a recipe.
|
|
|
|
* @param {String | Function | String[] | Function[] | [String | Function]} recipeConfig -
|
|
|
|
* An operation, operation name, or an array of either.
|
|
|
|
* @returns {SyncDish} of the result
|
|
|
|
* @throws {TypeError} if invalid recipe given.
|
|
|
|
*/
|
|
|
|
return function(input, recipeConfig) {
|
2018-08-31 15:52:14 +02:00
|
|
|
const recipe = new NodeRecipe(recipeConfig);
|
2018-07-06 13:54:19 +02:00
|
|
|
const dish = ensureIsDish(input);
|
|
|
|
return recipe.execute(dish);
|
|
|
|
};
|
|
|
|
}
|
2018-08-31 15:43:14 +02:00
|
|
|
|
2018-12-21 10:46:30 +01:00
|
|
|
|
2018-08-31 15:43:14 +02:00
|
|
|
/**
|
2018-12-21 10:46:30 +01:00
|
|
|
* explainExcludedFunction
|
|
|
|
*
|
2018-08-31 15:43:14 +02:00
|
|
|
* Explain that the given operation is not included in the Node.js version.
|
|
|
|
* @param {String} name - name of operation
|
|
|
|
*/
|
|
|
|
export function explainExludedFunction(name) {
|
|
|
|
/**
|
|
|
|
* Throw new error type with useful message.
|
|
|
|
*/
|
|
|
|
const func = () => {
|
|
|
|
throw new ExludedOperationError(`Sorry, the ${name} operation is not available in the Node.js version of CyberChef.`);
|
|
|
|
};
|
2018-08-31 15:52:14 +02:00
|
|
|
// Add opName prop so NodeRecipe can handle it, just like wrap does.
|
2018-08-31 15:43:14 +02:00
|
|
|
func.opName = name;
|
|
|
|
return func;
|
|
|
|
}
|