-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecurity-tests.js
More file actions
366 lines (327 loc) · 13.5 KB
/
Copy pathsecurity-tests.js
File metadata and controls
366 lines (327 loc) · 13.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// security-tests.js — Security test suite for InterPoll backend
// Run: node security-tests.js [relay-url] [gun-url]
//
// Tests: invalid payloads, injection attempts, oversized inputs,
// malformed JSON, auth gates, CORS, WS message limits
const http = require('http');
const WebSocket = require('ws');
const RELAY_URL = process.argv[2] || 'http://localhost:8080';
const GUN_URL = process.argv[3] || 'http://localhost:8765';
let passed = 0;
let failed = 0;
let skipped = 0;
function log(status, name, detail) {
const sym = status === 'PASS' ? '✅' : status === 'FAIL' ? '❌' : '⏭️';
console.log(`${sym} [${status}] ${name}${detail ? ': ' + detail : ''}`);
if (status === 'PASS') passed++;
else if (status === 'FAIL') failed++;
else skipped++;
}
function fetchJSON(url, options = {}) {
return new Promise((resolve, reject) => {
const u = new URL(url);
const opts = {
hostname: u.hostname,
port: u.port,
path: u.pathname + u.search,
method: options.method || 'GET',
headers: { 'Content-Type': 'application/json', ...(options.headers || {}) },
};
const req = http.request(opts, (res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => {
try { resolve({ status: res.statusCode, headers: res.headers, body: JSON.parse(data) }); }
catch { resolve({ status: res.statusCode, headers: res.headers, body: data }); }
});
});
req.on('error', reject);
if (options.body) req.write(typeof options.body === 'string' ? options.body : JSON.stringify(options.body));
req.end();
});
}
function fetchRaw(url, options = {}) {
return new Promise((resolve, reject) => {
const u = new URL(url);
const opts = {
hostname: u.hostname,
port: u.port,
path: u.pathname + u.search,
method: options.method || 'POST',
headers: options.headers || {},
};
const req = http.request(opts, (res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, body: data }));
});
req.on('error', reject);
if (options.body) req.write(options.body);
req.end();
});
}
// ─── Test Groups ──────────────────────────────────────────────────────────────
async function testCORS() {
console.log('\n── CORS Tests ──');
try {
const res = await fetchJSON(`${RELAY_URL}/health`, {
headers: { Origin: 'https://evil.example.com' },
});
const acaoHeader = res.headers['access-control-allow-origin'];
if (!acaoHeader || acaoHeader !== 'https://evil.example.com') {
log('PASS', 'CORS rejects unknown origin', `ACAO: ${acaoHeader || 'none'}`);
} else {
log('FAIL', 'CORS rejects unknown origin', `ACAO reflected evil origin: ${acaoHeader}`);
}
} catch (e) { log('FAIL', 'CORS rejects unknown origin', e.message); }
try {
const res = await fetchJSON(`${RELAY_URL}/health`, {
headers: { Origin: 'http://localhost:5173' },
});
const acaoHeader = res.headers['access-control-allow-origin'];
if (acaoHeader === 'http://localhost:5173') {
log('PASS', 'CORS allows localhost:5173', `ACAO: ${acaoHeader}`);
} else {
log('FAIL', 'CORS allows localhost:5173', `ACAO: ${acaoHeader || 'none'}`);
}
} catch (e) { log('FAIL', 'CORS allows localhost:5173', e.message); }
}
async function testSecurityHeaders() {
console.log('\n── Security Headers Tests ──');
try {
const res = await fetchJSON(`${RELAY_URL}/health`);
const headers = res.headers;
const checks = [
['x-content-type-options', 'nosniff'],
['x-frame-options', 'DENY'],
['referrer-policy', 'strict-origin-when-cross-origin'],
];
for (const [header, expected] of checks) {
if (headers[header] === expected) {
log('PASS', `Header ${header}`, headers[header]);
} else {
log('FAIL', `Header ${header}`, `got: ${headers[header] || 'missing'}, expected: ${expected}`);
}
}
} catch (e) { log('FAIL', 'Security headers check', e.message); }
}
async function testBodySizeLimits() {
console.log('\n── Body Size Limits Tests ──');
// Oversized vote-authorize body (>4KB)
try {
const bigPayload = JSON.stringify({ pollId: 'x'.repeat(5000), deviceId: 'test' });
const res = await fetchRaw(`${RELAY_URL}/api/vote-authorize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: bigPayload,
});
if (res.status === 413 || res.status === 400) {
log('PASS', 'Oversized vote-authorize rejected', `status: ${res.status}`);
} else {
log('FAIL', 'Oversized vote-authorize rejected', `status: ${res.status}`);
}
} catch (e) { log('FAIL', 'Oversized vote-authorize body', e.message); }
// Oversized /db/write body (>100KB) on gun-relay
try {
const bigData = JSON.stringify({ soul: 'test', data: { x: 'y'.repeat(200000) } });
const res = await fetchRaw(`${GUN_URL}/db/write`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: bigData,
});
if (res.status === 413 || res.status === 400 || res.status === 401 || res.status === 403) {
log('PASS', 'Oversized /db/write rejected', `status: ${res.status}`);
} else {
log('FAIL', 'Oversized /db/write rejected', `status: ${res.status}`);
}
} catch (e) { log('FAIL', 'Oversized /db/write body', e.message); }
}
async function testAuthGates() {
console.log('\n── Authentication Gate Tests ──');
// /db/write without auth
try {
const res = await fetchJSON(`${GUN_URL}/db/write`, {
method: 'POST',
body: { soul: 'test-soul', data: { test: true } },
});
if (res.status === 401 || res.status === 403) {
log('PASS', '/db/write rejects unauthenticated', `status: ${res.status}`);
} else {
log('FAIL', '/db/write rejects unauthenticated', `status: ${res.status}, body: ${JSON.stringify(res.body)}`);
}
} catch (e) { log('FAIL', '/db/write auth gate', e.message); }
// /admin/reindex without auth
try {
const res = await fetchJSON(`${GUN_URL}/admin/reindex`);
if (res.status === 401 || res.status === 403) {
log('PASS', '/admin/reindex rejects unauthenticated', `status: ${res.status}`);
} else {
log('FAIL', '/admin/reindex rejects unauthenticated', `status: ${res.status}`);
}
} catch (e) { log('FAIL', '/admin/reindex auth gate', e.message); }
}
async function testInputValidation() {
console.log('\n── Input Validation Tests ──');
// SQL injection in pollId
const injectionPayloads = [
{ name: 'SQL injection in pollId', pollId: "'; DROP TABLE gun_nodes; --", deviceId: 'dev1' },
{ name: 'Script injection in pollId', pollId: '<script>alert(1)</script>', deviceId: 'dev1' },
{ name: 'Null byte in deviceId', pollId: 'poll1', deviceId: 'dev\x001' },
{ name: 'Command injection in deviceId', pollId: 'poll1', deviceId: '$(rm -rf /)' },
{ name: 'Oversized pollId', pollId: 'a'.repeat(300), deviceId: 'dev1' },
];
for (const payload of injectionPayloads) {
try {
const res = await fetchJSON(`${RELAY_URL}/api/vote-authorize`, {
method: 'POST',
body: { pollId: payload.pollId, deviceId: payload.deviceId },
});
// Should either reject (400) or sanitize and proceed without crash
if (res.status < 500) {
log('PASS', payload.name, `status: ${res.status}`);
} else {
log('FAIL', payload.name, `status: ${res.status} — server error`);
}
} catch (e) { log('FAIL', payload.name, e.message); }
}
// Soul validation on gun-relay
const soulInjections = [
{ name: 'SQL injection in soul', soul: "'; DROP TABLE gun_nodes; --" },
{ name: 'Path traversal in soul', soul: '../../../etc/passwd' },
{ name: 'Null byte in soul', soul: 'test\x00soul' },
];
for (const payload of soulInjections) {
try {
const res = await fetchJSON(`${GUN_URL}/db/soul?soul=${encodeURIComponent(payload.soul)}`);
if (res.status === 400) {
log('PASS', payload.name, 'rejected with 400');
} else if (res.status === 404) {
log('PASS', payload.name, 'not found (safe)');
} else if (res.status < 500) {
log('PASS', payload.name, `status: ${res.status} (no server error)`);
} else {
log('FAIL', payload.name, `status: ${res.status} — server error`);
}
} catch (e) { log('FAIL', payload.name, e.message); }
}
}
async function testMalformedJSON() {
console.log('\n── Malformed JSON Tests ──');
const malformed = [
{ name: 'Truncated JSON', body: '{"pollId": "test"' },
{ name: 'Empty body', body: '' },
{ name: 'Array instead of object', body: '[1,2,3]' },
{ name: 'Non-JSON text', body: 'hello world' },
{ name: 'Nested deeply', body: '{"a":'.repeat(100) + '"x"' + '}'.repeat(100) },
];
for (const payload of malformed) {
try {
const res = await fetchRaw(`${RELAY_URL}/api/vote-authorize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: payload.body,
});
if (res.status < 500) {
log('PASS', payload.name, `status: ${res.status}`);
} else {
log('FAIL', payload.name, `status: ${res.status} — server error`);
}
} catch (e) { log('FAIL', payload.name, e.message); }
}
}
async function testErrorLeakage() {
console.log('\n── Error Leakage Tests ──');
// Check that error responses don't contain stack traces
try {
const res = await fetchJSON(`${GUN_URL}/db/soul?soul=nonexistent-soul-xyz`);
const body = JSON.stringify(res.body);
if (body.includes('at ') || body.includes('Error:') || body.includes('stack')) {
log('FAIL', 'Error leakage in /db/soul', 'response contains stack trace');
} else {
log('PASS', 'No error leakage in /db/soul', `body: ${body.substring(0, 100)}`);
}
} catch (e) { log('FAIL', 'Error leakage check', e.message); }
}
async function testWSMessageValidation() {
console.log('\n── WebSocket Message Validation Tests ──');
const wsUrl = RELAY_URL.replace('http', 'ws');
const wsTests = [
{ name: 'Missing type field', msg: { data: 'test' } },
{ name: 'Unknown type', msg: { type: 'hack-the-planet' } },
{ name: 'Register with invalid peerId (too short)', msg: { type: 'register', peerId: 'ab' } },
{ name: 'Register with injection in peerId', msg: { type: 'register', peerId: '<script>alert(1)</script>' } },
{ name: 'Broadcast with non-object data', msg: { type: 'broadcast', data: 'string-not-object' } },
{ name: 'Join-room with oversized roomId', msg: { type: 'join-room', roomId: 'x'.repeat(200) } },
];
for (const test of wsTests) {
try {
await new Promise((resolve, reject) => {
const ws = new WebSocket(wsUrl);
const timer = setTimeout(() => { ws.close(); resolve('timeout'); }, 3000);
ws.on('open', () => {
ws.send(JSON.stringify(test.msg));
});
ws.on('message', (data) => {
clearTimeout(timer);
const msg = JSON.parse(data.toString());
ws.close();
if (msg.type === 'error') {
resolve('rejected');
} else {
resolve('accepted');
}
});
ws.on('close', () => { clearTimeout(timer); resolve('closed'); });
ws.on('error', (e) => { clearTimeout(timer); resolve('error'); });
}).then((result) => {
if (result === 'rejected' || result === 'closed') {
log('PASS', test.name, `result: ${result}`);
} else if (result === 'timeout') {
log('PASS', test.name, 'no response (silently dropped)');
} else {
log('FAIL', test.name, `message was accepted: ${result}`);
}
});
} catch (e) { log('FAIL', test.name, e.message); }
}
}
async function testVoteAuthorizeErrorBehavior() {
console.log('\n── Vote Authorize Error Behavior ──');
// The old bug: error handler returned allowed:true
// With missing fields, should return allowed:false (not crash with allowed:true)
try {
const res = await fetchJSON(`${RELAY_URL}/api/vote-authorize`, {
method: 'POST',
body: {},
});
if (res.body && res.body.allowed === true) {
log('FAIL', 'Empty vote-authorize returns allowed:false', 'got allowed:true (CRITICAL BUG)');
} else {
log('PASS', 'Empty vote-authorize returns allowed:false', `body: ${JSON.stringify(res.body)}`);
}
} catch (e) { log('FAIL', 'Vote-authorize error behavior', e.message); }
}
// ─── Run All Tests ────────────────────────────────────────────────────────────
async function main() {
console.log('🔒 InterPoll Security Test Suite');
console.log(` Relay: ${RELAY_URL}`);
console.log(` Gun: ${GUN_URL}`);
console.log('');
await testCORS();
await testSecurityHeaders();
await testBodySizeLimits();
await testAuthGates();
await testInputValidation();
await testMalformedJSON();
await testErrorLeakage();
await testWSMessageValidation();
await testVoteAuthorizeErrorBehavior();
console.log(`\n${'═'.repeat(50)}`);
console.log(`Results: ${passed} passed, ${failed} failed, ${skipped} skipped`);
console.log(`${'═'.repeat(50)}`);
if (failed > 0) process.exit(1);
}
main().catch((err) => {
console.error('Test runner error:', err.message);
process.exit(1);
});