mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 07:16:17 -04:00
Data pasted into the input is treat as a file if it's over the IO threshold
This commit is contained in:
parent
2d9f87abef
commit
4588cd151c
5 changed files with 54 additions and 12 deletions
|
@ -80,7 +80,7 @@ Chef.prototype.bake = async function(input, recipeConfig, options, progress, ste
|
||||||
// Depending on the size of the output, we may send it back as a string or an ArrayBuffer.
|
// Depending on the size of the output, we may send it back as a string or an ArrayBuffer.
|
||||||
// This can prevent unnecessary casting as an ArrayBuffer can be easily downloaded as a file.
|
// This can prevent unnecessary casting as an ArrayBuffer can be easily downloaded as a file.
|
||||||
// The threshold is specified in KiB.
|
// The threshold is specified in KiB.
|
||||||
const threshold = (options.outputFileThreshold || 1024) * 1024;
|
const threshold = (options.ioDisplayThreshold || 1024) * 1024;
|
||||||
const returnType = this.dish.size() > threshold ? Dish.ARRAY_BUFFER : Dish.STRING;
|
const returnType = this.dish.size() > threshold ? Dish.ARRAY_BUFFER : Dish.STRING;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -61,9 +61,13 @@ InputWaiter.prototype.set = function(input) {
|
||||||
if (input instanceof File) {
|
if (input instanceof File) {
|
||||||
this.setFile(input);
|
this.setFile(input);
|
||||||
inputText.value = "";
|
inputText.value = "";
|
||||||
|
this.setInputInfo(input.size, null);
|
||||||
} else {
|
} else {
|
||||||
inputText.value = input;
|
inputText.value = input;
|
||||||
window.dispatchEvent(this.manager.statechange);
|
window.dispatchEvent(this.manager.statechange);
|
||||||
|
const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
||||||
|
input.count("\n") + 1 : null;
|
||||||
|
this.setInputInfo(input.length, lines);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -81,6 +85,7 @@ InputWaiter.prototype.setFile = function(file) {
|
||||||
fileType = document.getElementById("input-file-type"),
|
fileType = document.getElementById("input-file-type"),
|
||||||
fileLoaded = document.getElementById("input-file-loaded");
|
fileLoaded = document.getElementById("input-file-loaded");
|
||||||
|
|
||||||
|
this.fileBuffer = new ArrayBuffer();
|
||||||
fileOverlay.style.display = "block";
|
fileOverlay.style.display = "block";
|
||||||
fileName.textContent = file.name;
|
fileName.textContent = file.name;
|
||||||
fileSize.textContent = file.size.toLocaleString() + " bytes";
|
fileSize.textContent = file.size.toLocaleString() + " bytes";
|
||||||
|
@ -100,21 +105,28 @@ InputWaiter.prototype.setInputInfo = function(length, lines) {
|
||||||
width = width < 2 ? 2 : width;
|
width = width < 2 ? 2 : width;
|
||||||
|
|
||||||
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, " ");
|
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, " ");
|
||||||
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, " ");
|
let msg = "length: " + lengthStr;
|
||||||
|
|
||||||
document.getElementById("input-info").innerHTML = "length: " + lengthStr + "<br>lines: " + linesStr;
|
if (typeof lines === "number") {
|
||||||
|
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, " ");
|
||||||
|
msg += "<br>lines: " + linesStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById("input-info").innerHTML = msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for input scroll events.
|
* Handler for input change events.
|
||||||
* Scrolls the highlighter pane to match the input textarea position and updates history state.
|
|
||||||
*
|
*
|
||||||
* @param {event} e
|
* @param {event} e
|
||||||
*
|
*
|
||||||
* @fires Manager#statechange
|
* @fires Manager#statechange
|
||||||
*/
|
*/
|
||||||
InputWaiter.prototype.inputChange = function(e) {
|
InputWaiter.prototype.inputChange = function(e) {
|
||||||
|
// Ignore this function if the input is a File
|
||||||
|
if (this.fileBuffer) return;
|
||||||
|
|
||||||
// Remove highlighting from input and output panes as the offsets might be different now
|
// Remove highlighting from input and output panes as the offsets might be different now
|
||||||
this.manager.highlighter.removeHighlights();
|
this.manager.highlighter.removeHighlights();
|
||||||
|
|
||||||
|
@ -123,18 +135,47 @@ InputWaiter.prototype.inputChange = function(e) {
|
||||||
|
|
||||||
// Update the input metadata info
|
// Update the input metadata info
|
||||||
const inputText = this.get();
|
const inputText = this.get();
|
||||||
const lines = inputText.count("\n") + 1;
|
const lines = inputText.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
||||||
|
inputText.count("\n") + 1 : null;
|
||||||
|
|
||||||
this.setInputInfo(inputText.length, lines);
|
this.setInputInfo(inputText.length, lines);
|
||||||
|
|
||||||
|
if (e && this.badKeys.indexOf(e.keyCode) < 0) {
|
||||||
if (this.badKeys.indexOf(e.keyCode) < 0) {
|
|
||||||
// Fire the statechange event as the input has been modified
|
// Fire the statechange event as the input has been modified
|
||||||
window.dispatchEvent(this.manager.statechange);
|
window.dispatchEvent(this.manager.statechange);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for input paste events.
|
||||||
|
* Checks that the size of the input is below the display limit, otherwise treats it as a file/blob.
|
||||||
|
*
|
||||||
|
* @param {event} e
|
||||||
|
*/
|
||||||
|
InputWaiter.prototype.inputPaste = function(e) {
|
||||||
|
const pastedData = e.clipboardData.getData("Text");
|
||||||
|
|
||||||
|
if (pastedData.length < (this.app.options.ioDisplayThreshold * 1024)) {
|
||||||
|
this.inputChange(e);
|
||||||
|
} else {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
const file = new File([pastedData], "PastedData", {
|
||||||
|
type: "text/plain",
|
||||||
|
lastModified: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
|
this.loaderWorker = new LoaderWorker();
|
||||||
|
this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
|
||||||
|
this.loaderWorker.postMessage({"file": file});
|
||||||
|
this.set(file);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for input dragover events.
|
* Handler for input dragover events.
|
||||||
* Gives the user a visual cue to show that items can be dropped here.
|
* Gives the user a visual cue to show that items can be dropped here.
|
||||||
|
|
|
@ -132,7 +132,8 @@ Manager.prototype.initialiseEventListeners = function() {
|
||||||
this.addDynamicListener("#rec-list", "operationremove", this.recipe.opRemove.bind(this.recipe));
|
this.addDynamicListener("#rec-list", "operationremove", this.recipe.opRemove.bind(this.recipe));
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
this.addMultiEventListener("#input-text", "keyup paste", this.input.inputChange, this.input);
|
this.addMultiEventListener("#input-text", "keyup", this.input.inputChange, this.input);
|
||||||
|
this.addMultiEventListener("#input-text", "paste", this.input.inputPaste, this.input);
|
||||||
document.getElementById("reset-layout").addEventListener("click", this.app.resetLayout.bind(this.app));
|
document.getElementById("reset-layout").addEventListener("click", this.app.resetLayout.bind(this.app));
|
||||||
document.getElementById("clr-io").addEventListener("click", this.input.clearIoClick.bind(this.input));
|
document.getElementById("clr-io").addEventListener("click", this.input.clearIoClick.bind(this.input));
|
||||||
this.addListeners("#input-text,#input-file", "dragover", this.input.inputDragover, this.input);
|
this.addListeners("#input-text,#input-file", "dragover", this.input.inputDragover, this.input);
|
||||||
|
|
|
@ -372,8 +372,8 @@
|
||||||
<label for="errorTimeout"> Operation error timeout in ms (0 for never)</label>
|
<label for="errorTimeout"> Operation error timeout in ms (0 for never)</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="option-item">
|
<div class="option-item">
|
||||||
<input type="number" option="outputFileThreshold" id="outputFileThreshold" />
|
<input type="number" option="ioDisplayThreshold" id="ioDisplayThreshold" />
|
||||||
<label for="outputFileThreshold"> Size threshold for treating the output as a file (KiB)</label>
|
<label for="ioDisplayThreshold"> Size threshold for treating the input or output as a file (KiB)</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="option-item">
|
<div class="option-item">
|
||||||
<select option="logLevel" id="logLevel">
|
<select option="logLevel" id="logLevel">
|
||||||
|
|
|
@ -47,7 +47,7 @@ function main() {
|
||||||
attemptHighlight: true,
|
attemptHighlight: true,
|
||||||
theme: "classic",
|
theme: "classic",
|
||||||
useMetaKey: false,
|
useMetaKey: false,
|
||||||
outputFileThreshold: 1024,
|
ioDisplayThreshold: 512,
|
||||||
logLevel: "info"
|
logLevel: "info"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue