Skip to content

Commit 71470f8

Browse files
committed
Merge branch 'master' of github.com:gchq/CyberChef-server
2 parents 090a1e1 + ff4dbfa commit 71470f8

File tree

5 files changed

+96
-28
lines changed

5 files changed

+96
-28
lines changed

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/errorHandler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OperationError } from "cyberchef/src/node/index.mjs";
1+
import { OperationError, DishError } from "cyberchef/src/node/index.mjs";
22

33

44
/**
@@ -17,7 +17,11 @@ export default function errorHandler(err, req, res, next) {
1717
return next(err);
1818
}
1919

20-
if (err instanceof TypeError || err instanceof OperationError) {
20+
if (
21+
err instanceof TypeError ||
22+
err instanceof OperationError ||
23+
err instanceof DishError
24+
) {
2125
res.status(400).send(err.message).end();
2226
} else {
2327
res.status(500).send(err.stack).end();

routes/bake.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Router } from "express";
22
const router = Router();
3-
import { bake } from "cyberchef/src/node/index.mjs";
3+
import { bake, Dish } from "cyberchef/src/node/index.mjs";
44

55
/**
66
* bakePost
@@ -16,7 +16,18 @@ router.post("/", async function bakePost(req, res, next) {
1616
}
1717

1818
const dish = await bake(req.body.input, req.body.recipe);
19-
res.send(dish.value);
19+
20+
// Attempt to translate to another type. Any translation errors
21+
// propagate through to the errorHandler.
22+
if (req.body.outputType) {
23+
dish.get(req.body.outputType);
24+
}
25+
26+
res.send({
27+
value: dish.value,
28+
type: Dish.enumLookup(dish.type),
29+
});
30+
2031
} catch (e) {
2132
next(e);
2233
}

routes/users.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/bake.js

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ describe("POST /bake", function() {
3232
.post("/bake")
3333
.send({input: "testing, one two three", recipe: []})
3434
.expect(200)
35-
.expect("testing, one two three", done);
35+
.expect({
36+
value: "testing, one two three",
37+
type: "string",
38+
}, done);
3639
});
3740

3841
it("should parse the recipe if it is a valid operation name", (done) => {
@@ -41,7 +44,10 @@ describe("POST /bake", function() {
4144
.set("Content-Type", "application/json")
4245
.send({input: "hello", recipe: "To Hexdump"})
4346
.expect(200)
44-
.expect("00000000 68 65 6c 6c 6f |hello|", done);
47+
.expect({
48+
value: "00000000 68 65 6c 6c 6f |hello|",
49+
type: "string",
50+
}, done);
4551
});
4652

4753
it("should parse the recipe if it is a valid operation name string", (done) => {
@@ -50,7 +56,10 @@ describe("POST /bake", function() {
5056
.set("Content-Type", "application/json")
5157
.send({input: "hello", recipe: "toHexdump"})
5258
.expect(200)
53-
.expect("00000000 68 65 6c 6c 6f |hello|", done);
59+
.expect({
60+
value: "00000000 68 65 6c 6c 6f |hello|",
61+
type: "string",
62+
}, done);
5463
});
5564

5665
it("should parse the recipe if it is an array of operation names", (done) => {
@@ -59,7 +68,10 @@ describe("POST /bake", function() {
5968
.set("Content-Type", "application/json")
6069
.send({input: "Testing, 1 2 3", recipe: ["to decimal", "MD5", "to braille"]})
6170
.expect(200)
62-
.expect("⠲⠆⠙⠋⠲⠆⠶⠶⠖⠶⠖⠙⠋⠶⠉⠆⠃⠲⠂⠑⠲⠢⠲⠆⠲⠒⠑⠶⠲⠢⠋⠃", done);
71+
.expect({
72+
value: "⠲⠆⠙⠋⠲⠆⠶⠶⠖⠶⠖⠙⠋⠶⠉⠆⠃⠲⠂⠑⠲⠢⠲⠆⠲⠒⠑⠶⠲⠢⠋⠃",
73+
type: "string",
74+
}, done);
6375
});
6476

6577
it("should parse the recipe if it is an operation with some custom arguments", (done) => {
@@ -68,7 +80,10 @@ describe("POST /bake", function() {
6880
.set("Content-Type", "application/json")
6981
.send({input: "Testing, 1 2 3", recipe: { op: "to hex", args: { delimiter: "Colon" }}})
7082
.expect(200)
71-
.expect("54:65:73:74:69:6e:67:2c:20:31:20:32:20:33", done);
83+
.expect({
84+
value: "54:65:73:74:69:6e:67:2c:20:31:20:32:20:33",
85+
type: "string",
86+
}, done);
7287
});
7388

7489
it("should parse the recipe if it is an operation with no custom arguments", (done) => {
@@ -77,7 +92,10 @@ describe("POST /bake", function() {
7792
.set("Content-Type", "application/json")
7893
.send({input: "Testing, 1 2 3", recipe: {op: "to hex" }})
7994
.expect(200)
80-
.expect("54 65 73 74 69 6e 67 2c 20 31 20 32 20 33", done);
95+
.expect({
96+
value: "54 65 73 74 69 6e 67 2c 20 31 20 32 20 33",
97+
type: "string",
98+
}, done);
8199
});
82100

83101
it("should parse a recipe in the compact JSON format taken from the CyberChef website", (done) => {
@@ -86,7 +104,10 @@ describe("POST /bake", function() {
86104
.set("Content-Type", "application/json")
87105
.send({input: "some input", recipe: [{"op": "To Morse Code", "args": ["Dash/Dot", "Backslash", "Comma"]}, {"op": "Hex to PEM", "args": ["SOMETHING"]}, {"op": "To Snake case", "args": [false]}]})
88106
.expect(200)
89-
.expect("begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something", done);
107+
.expect({
108+
value: "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something",
109+
type: "string",
110+
}, done);
90111
});
91112

92113
it("should parse a recipe ib the clean JSON format taken from the CyberChef website", (done) => {
@@ -102,7 +123,10 @@ describe("POST /bake", function() {
102123
"args": [false] }
103124
]})
104125
.expect(200)
105-
.expect("begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something", done);
126+
.expect({
127+
value: "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something",
128+
type: "string",
129+
}, done);
106130
});
107131

108132
it("should return a useful error if we give an input/recipe combination that results in an OperationError", (done) => {
@@ -122,4 +146,49 @@ describe("POST /bake", function() {
122146
.expect("Invalid key length: 2 bytes\n\nThe following algorithms will be used based on the size of the key:\n 16 bytes = AES-128\n 24 bytes = AES-192\n 32 bytes = AES-256", done);
123147
});
124148

149+
it("should return a string output as a byte array, if outputType is defined", (done) => {
150+
request(app)
151+
.post("/bake")
152+
.set("Content-Type", "application/json")
153+
.send({
154+
input: "irregular alcove",
155+
recipe: "to hex",
156+
outputType: "byte array",
157+
})
158+
.expect(200)
159+
.expect({
160+
value: [54, 57, 32, 55, 50, 32, 55, 50, 32, 54, 53, 32, 54, 55, 32, 55, 53, 32, 54, 99, 32, 54, 49, 32, 55, 50, 32, 50, 48, 32, 54, 49, 32, 54, 99, 32, 54, 51, 32, 54, 102, 32, 55, 54, 32, 54, 53],
161+
type: "byteArray",
162+
}, done);
163+
});
164+
165+
it("should return a json output as a number, if outputType is defined", (done) => {
166+
request(app)
167+
.post("/bake")
168+
.set("Content-Type", "application/json")
169+
.send({
170+
input: "something oddly colourful",
171+
recipe: "entropy",
172+
outputType: "number",
173+
})
174+
.expect(200)
175+
.expect({
176+
value: 3.893660689688185,
177+
type: "number",
178+
}, done);
179+
});
180+
181+
it("should return a useful error if returnType is given but has an invalid value", (done) => {
182+
request(app)
183+
.post("/bake")
184+
.set("Content-Type", "application/json")
185+
.send({
186+
input: "irregular alcove",
187+
recipe: "to hex",
188+
outputType: "some invalid type",
189+
})
190+
.expect(400)
191+
.expect("Invalid data type string. No matching enum.", done);
192+
});
193+
125194
});

0 commit comments

Comments
 (0)