-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-integration.js
More file actions
218 lines (198 loc) · 9.81 KB
/
Copy pathtest-integration.js
File metadata and controls
218 lines (198 loc) · 9.81 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
'use strict';
// Tests de INTEGRACIÓN: levantan servidores HTTP locales y ejercitan las rutas de
// red reales (descarga, crawl, modo activo, escaneo autenticado, redirecciones).
// El allowlist de loopback exige NODE_ENV=test + la variable explícita.
process.env.NODE_ENV = 'test';
process.env.VULN_TEST_ALLOW_LOOPBACK = '1';
const test = require('node:test');
const assert = require('node:assert');
const http = require('http');
const { escanear, escanearDetallado } = require('./src/engine');
const { escanearHeadless } = require('./src/headless');
const { rastrear } = require('./src/crawl');
const { validarObjetivo, descargar } = require('./src/net');
let servidor, servidorB, base, baseB;
test.before(async () => {
// Servidor secundario (distinto origen) para probar fuga de credenciales.
servidorB = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`cookie=${req.headers.cookie || ''}; auth=${req.headers.authorization || ''}`);
});
await new Promise((r) => servidorB.listen(0, '127.0.0.1', r));
baseB = `http://127.0.0.1:${servidorB.address().port}`;
servidor = http.createServer((req, res) => {
const url = new URL(req.url, 'http://localhost');
const q = url.searchParams.get('q') || '';
if (url.pathname === '/echo') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`x-prueba=${req.headers['x-prueba'] || ''}; cookie=${req.headers.cookie || ''}; ` +
`ae=${req.headers['accept-encoding'] || ''}`);
return;
}
if (url.pathname === '/page2') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html><body><iframe src="x"></iframe></body></html>');
return;
}
if (url.pathname === '/notfound') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<html><body>No encontrado</body></html>');
return;
}
if (url.pathname === '/goto') {
res.writeHead(302, { Location: '/page2' });
res.end();
return;
}
if (url.pathname === '/redir') {
res.writeHead(302, { Location: url.searchParams.get('next') || '/' });
res.end();
return;
}
// Redirección a OTRO origen (servidor B), para verificar que no se filtran credenciales.
if (url.pathname === '/redir-ext') {
res.writeHead(302, { Location: `${baseB}/echo` });
res.end();
return;
}
if (url.pathname === '/buscar') {
let salida = q;
if (q.includes("'")) salida += ' — You have an error in your SQL syntax near MySQL';
if (q.includes('{{73*137}}')) salida = salida.replace('{{73*137}}', '10001');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`<html><body>res: ${salida}</body></html>`);
return;
}
res.writeHead(200, { 'Content-Type': 'text/html', 'Set-Cookie': 'sid=abc; Path=/' });
res.end(`<html><body>
<img src=p onerror="alert(1)">
<script src="/js/jquery-1.12.4.min.js"></script>
<a href="/page2">2</a>
<a href="http://externo.example/x">ext</a>
<div>buscado: ${q}</div>
</body></html>`);
});
await new Promise((r) => servidor.listen(0, '127.0.0.1', r));
base = `http://127.0.0.1:${servidor.address().port}`;
});
test.after(() => {
if (servidor) servidor.close();
if (servidorB) servidorB.close();
});
test('integración: escaneo detecta hallazgos reales', async () => {
const reporte = await escanear(`${base}/`);
const ids = reporte.hallazgos.map((f) => f.id);
assert.ok(ids.includes('csp-ausente'));
assert.ok(ids.includes('cookie-insegura'));
assert.ok(ids.includes('evento-inline'));
assert.ok(ids.includes('libreria-vulnerable'));
});
test('integración: crawl sigue solo el mismo origen', async () => {
const reportes = await rastrear(`${base}/`, { crawl: 1, maxPaginas: 10, concurrencia: 3 });
const urls = reportes.map((r) => r.url);
assert.ok(urls.some((u) => u.endsWith('/page2')));
assert.ok(!urls.some((u) => u.includes('externo.example')));
});
test('integración: modo activo detecta XSS reflejado', async () => {
const { reporte } = await escanearDetallado(`${base}/?q=hola`, { active: true, authorized: true });
assert.ok(reporte.hallazgos.some((f) => f.id === 'xss-reflejado'));
});
test('integración: modo activo detecta SSTI y SQLi', async () => {
const { reporte } = await escanearDetallado(`${base}/buscar?q=hola`, { active: true, authorized: true });
const ids = reporte.hallazgos.map((f) => f.id);
assert.ok(ids.includes('ssti'), 'debería detectar SSTI');
assert.ok(ids.includes('sqli-error'), 'debería detectar SQLi');
});
test('integración: modo activo detecta open redirect', async () => {
const { reporte } = await escanearDetallado(`${base}/redir?next=/`, { active: true, authorized: true });
assert.ok(reporte.hallazgos.some((f) => f.id === 'open-redirect-activo'));
});
test('integración: la URL reportada es la final tras una redirección', async () => {
const reporte = await escanear(`${base}/goto`);
assert.ok(reporte.url.endsWith('/page2'), `url=${reporte.url}`);
});
test('integración: NO se reenvían credenciales a otro origen tras redirección', async () => {
const target = await validarObjetivo(`${base}/redir-ext`);
const { body } = await descargar(target, { headers: { Cookie: 'sesion=secreta', Authorization: 'Bearer T' } });
assert.ok(body.includes('cookie=;'), `no debería llegar la cookie: ${body}`);
assert.ok(body.includes('auth='), body);
assert.ok(!body.includes('secreta'), 'la cookie no debe filtrarse al otro origen');
assert.ok(!body.includes('Bearer T'), 'el Authorization no debe filtrarse al otro origen');
});
test('integración: envía Accept-Encoding identity y cabeceras propias', async () => {
const target = await validarObjetivo(`${base}/echo`);
const { body } = await descargar(target, { headers: { 'x-prueba': 'ok', Cookie: 'sesion=1' } });
assert.ok(body.includes('x-prueba=ok'));
assert.ok(body.includes('cookie=sesion=1'));
assert.ok(body.includes('ae=identity'), `Accept-Encoding debería ser identity: ${body}`);
});
test('integración: orquestación headless (Playwright simulado)', async () => {
const handlers = {};
const page = {
on: (ev, cb) => ((handlers[ev] = handlers[ev] || []).push(cb)),
goto: async () => {
(handlers.console || []).forEach((cb) =>
cb({ text: () => 'Refused to execute inline script: Content Security Policy' })
);
return { status: () => 200, headers: () => ({}) };
},
content: async () => '<html><body><img src=x onerror="roba()"></body></html>',
};
const contexto = { newPage: async () => page, close: async () => {} };
const playwrightFalso = {
chromium: { launch: async () => ({ newContext: async () => contexto, close: async () => {} }) },
};
const { reporte } = await escanearHeadless(`${base}/`, {}, { playwright: playwrightFalso });
const ids = reporte.hallazgos.map((f) => f.id);
assert.ok(ids.includes('csp-violacion-runtime'), 'CSP runtime');
assert.ok(ids.includes('evento-inline'), 'analiza el DOM renderizado');
});
test('integración: escaneo por NOMBRE DE HOST (autoSelectFamily / Happy Eyeballs)', async () => {
// Reproduce el fallo real "Invalid IP address: undefined": al escanear por
// hostname (no IP literal), Node llama a nuestro lookup con all:true.
const dns = require('dns').promises;
const { address } = await dns.lookup('localhost');
const srv = require('http').createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html><body><img onerror="x()"></body></html>');
});
await new Promise((r) => srv.listen(0, address, r));
try {
const reporte = await escanear(`http://localhost:${srv.address().port}/`);
assert.ok(reporte.totalHallazgos >= 1, 'debería analizar sin "Invalid IP address"');
} finally {
srv.close();
}
});
test('integración: analiza respuestas 4xx en vez de abortar', async () => {
const reporte = await escanear(`${base}/notfound`);
assert.strictEqual(reporte.statusCode, 404);
assert.ok(reporte.hallazgos.some((f) => f.id === 'csp-ausente'), 'debe analizar cabeceras del 404');
});
test('integración: enruta a través de HTTP_PROXY cuando está definido', async () => {
const recibidas = [];
const proxy = http.createServer((req, res) => {
recibidas.push(req.url); // debería ser la URL absoluta (http://host:port/...)
let u;
try { u = new URL(req.url); } catch { res.writeHead(400); return res.end(); }
const fwd = http.request(
{ host: u.hostname, port: u.port, path: u.pathname + u.search, method: 'GET', headers: req.headers },
(r) => { res.writeHead(r.statusCode, r.headers); r.pipe(res); }
);
fwd.on('error', () => { res.writeHead(502); res.end(); });
fwd.end();
});
await new Promise((r) => proxy.listen(0, '127.0.0.1', r));
const prev = process.env.HTTP_PROXY;
process.env.HTTP_PROXY = `http://127.0.0.1:${proxy.address().port}`;
try {
const reporte = await escanear(`${base}/`);
assert.ok(reporte.totalHallazgos >= 1, 'debería analizar a través del proxy');
assert.ok(recibidas.length >= 1, 'el proxy debería recibir la petición');
assert.ok(recibidas[0].startsWith('http://'), `forma absoluta esperada, recibido: ${recibidas[0]}`);
} finally {
if (prev === undefined) delete process.env.HTTP_PROXY;
else process.env.HTTP_PROXY = prev;
proxy.close();
}
});