mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-19 20:55:03 -04:00
Add Template operation for basic JSON rendering
This commit is contained in:
parent
7c8be12d52
commit
361a3b2929
6 changed files with 138 additions and 3 deletions
|
@ -373,7 +373,8 @@
|
|||
"Extract EXIF",
|
||||
"Extract ID3",
|
||||
"Extract Files",
|
||||
"RAKE"
|
||||
"RAKE",
|
||||
"Template"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
53
src/core/operations/Template.mjs
Normal file
53
src/core/operations/Template.mjs
Normal file
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* @author kendallgoto [k@kgo.to]
|
||||
* @copyright Crown Copyright 2025
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Handlebars from "handlebars";
|
||||
|
||||
/**
|
||||
* Template operation
|
||||
*/
|
||||
class Template extends Operation {
|
||||
|
||||
/**
|
||||
* Template constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Template";
|
||||
this.module = "Handlebars";
|
||||
this.description = "Render a template with Handlebars/Mustache substituting variables using JSON input. Templates will be rendered to plain-text only, to prevent XSS.";
|
||||
this.infoURL = "https://handlebarsjs.com/";
|
||||
this.inputType = "JSON";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
name: "Template definition (.handlebars)",
|
||||
type: "text",
|
||||
value: ""
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {JSON} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [templateStr] = args;
|
||||
try {
|
||||
const template = Handlebars.compile(templateStr);
|
||||
return template(input);
|
||||
} catch (e) {
|
||||
throw new OperationError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Template;
|
Loading…
Add table
Add a link
Reference in a new issue