Repository navigation
Expand file tree
/
Copy pathsanitization.test.js
More file actions
149 lines (114 loc) · 4.69 KB
/
Copy pathsanitization.test.js
File metadata and controls
149 lines (114 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* __tests__/sanitization.test.js
* Unit tests for the sanitization middleware (issue #535).
*
* Verifies that script/HTML injection payloads are neutralised while valid
* Stellar addresses (and unrelated body fields such as memos/amounts) pass
* through unchanged.
*/
"use strict";
const { sanitizePublicKey, sanitizeUsername } = require("../src/middleware/sanitization");
const VALID_PUBLIC_KEY = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
function createReq({ params = {}, body = {} } = {}) {
return { params, body };
}
function createRes() {
const res = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
return res;
}
describe("sanitizePublicKey", () => {
it("calls next() and leaves the request untouched when no publicKey param is present", () => {
const req = createReq({ params: {}, body: { memo: "coffee", amount: "5" } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(res.status).not.toHaveBeenCalled();
expect(req.body).toEqual({ memo: "coffee", amount: "5" });
});
it("passes a valid Stellar public key through unchanged", () => {
const req = createReq({ params: { publicKey: VALID_PUBLIC_KEY } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(req.params.publicKey).toBe(VALID_PUBLIC_KEY);
expect(next).toHaveBeenCalledTimes(1);
expect(res.status).not.toHaveBeenCalled();
});
it("leaves unrelated body fields such as memo and amount unchanged", () => {
const req = createReq({
params: { publicKey: VALID_PUBLIC_KEY },
body: { memo: "Great work! <3", amount: "12.5000000" },
});
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(req.body).toEqual({ memo: "Great work! <3", amount: "12.5000000" });
expect(next).toHaveBeenCalledTimes(1);
});
it("strips non-alphanumeric injection characters wrapped around a valid key", () => {
const req = createReq({ params: { publicKey: `"'><${VALID_PUBLIC_KEY}<script>` } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
// The <script> tag's letters survive stripping (only non-alphanumeric chars
// are removed), so the sanitized value is no longer a valid 56-char key and
// must be rejected rather than silently passed through.
expect(res.status).toHaveBeenCalledWith(400);
expect(next).not.toHaveBeenCalled();
});
it("rejects a pure script/HTML injection payload with a 400 and does not call next()", () => {
const req = createReq({ params: { publicKey: "<script>alert('xss')</script>" } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: "Invalid Stellar public key format" });
expect(next).not.toHaveBeenCalled();
});
it("rejects a key of the wrong length after sanitization", () => {
const req = createReq({ params: { publicKey: "GTOO_SHORT" } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(next).not.toHaveBeenCalled();
});
it("rejects a 56-character key that does not start with G", () => {
const invalidPrefix = `A${VALID_PUBLIC_KEY.slice(1)}`;
const req = createReq({ params: { publicKey: invalidPrefix } });
const res = createRes();
const next = jest.fn();
sanitizePublicKey(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(next).not.toHaveBeenCalled();
});
});
describe("sanitizeUsername", () => {
it("calls next() when no username param is present", () => {
const req = createReq({ params: {} });
const res = createRes();
const next = jest.fn();
sanitizeUsername(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(req.params.username).toBeUndefined();
});
it("trims and lowercases a valid username", () => {
const req = createReq({ params: { username: " Alice " } });
const res = createRes();
const next = jest.fn();
sanitizeUsername(req, res, next);
expect(req.params.username).toBe("alice");
expect(next).toHaveBeenCalledTimes(1);
});
it("passes an already-normalised username through unchanged", () => {
const req = createReq({ params: { username: "bob" } });
const res = createRes();
const next = jest.fn();
sanitizeUsername(req, res, next);
expect(req.params.username).toBe("bob");
expect(next).toHaveBeenCalledTimes(1);
});
});