fix(aep-15): la récupération de page refuse les adresses internes
lib/recupererPage.js : http(s) seulement, port par défaut, ni identifiants ni nom d'hôte local, toutes les IP résolues publiques ; redirections suivies à la main et contrôlées à chaque saut (au plus 5). enrich.js s'en sert à la place de fetchCapped et garde l'adresse finale et la chaîne de redirections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Lot 15 — la récupération de page refuse les adresses internes, avant l'appel et après
|
||||
* chaque redirection. Aucun réseau : fetch et la résolution DNS sont simulés.
|
||||
*/
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { recupererPage, controlerAdresse, verifierAdresse, ipInterne, tableRedirections, AdresseRefusee, MAX_SAUTS } from '../lib/recupererPage.js';
|
||||
|
||||
/** Résolution simulée : nom → liste d'IP ; tout nom absent lève ENOTFOUND. */
|
||||
const dns = table => async (nom) => {
|
||||
if (!(nom in table)) throw Object.assign(new Error('introuvable'), { code: 'ENOTFOUND' });
|
||||
return table[nom].map(address => ({ address, family: address.includes(':') ? 6 : 4 }));
|
||||
};
|
||||
|
||||
/** fetch simulé : adresse → { status, location?, body? } ; garde la trace des appels. */
|
||||
function faux(routes) {
|
||||
const appels = [];
|
||||
const fn = async (url, opts) => {
|
||||
appels.push({ url, redirect: opts?.redirect });
|
||||
const r = routes[url];
|
||||
if (!r) throw new Error(`appel inattendu à ${url}`);
|
||||
return new Response(r.body ?? '', { status: r.status ?? 200, headers: r.location ? { location: r.location } : {} });
|
||||
};
|
||||
fn.appels = appels;
|
||||
return fn;
|
||||
}
|
||||
|
||||
const PUBLIC = { 'exemple.org': ['93.184.215.14'], 'www.exemple.org': ['93.184.215.14'], 'ailleurs.net': ['2606:4700::6810:84e5'] };
|
||||
|
||||
describe('lot 15 · filtre d’hôte (adresses internes refusées)', () => {
|
||||
it('IP internes refusées : boucle, privées, lien local, CGNAT, multicast, IPv4 dans IPv6', () => {
|
||||
for (const ip of ['127.0.0.1', '127.8.8.8', '10.1.2.3', '172.16.0.1', '172.31.255.255', '192.168.1.1', '169.254.169.254',
|
||||
'100.64.0.1', '0.0.0.0', '224.0.0.1', '255.255.255.255', '::1', '::', 'fe80::1', 'fc00::1', 'fd12:3456::1',
|
||||
'::ffff:127.0.0.1', '::ffff:7f00:1', '::ffff:a00:1', '64:ff9b::a9fe:a9fe', '2002:c0a8:101::1', 'ff02::1', 'pas-une-ip']) {
|
||||
assert.equal(ipInterne(ip), true, ip);
|
||||
}
|
||||
for (const ip of ['93.184.215.14', '1.1.1.1', '172.32.0.1', '2606:4700::6810:84e5', '::ffff:5db8:d70e']) {
|
||||
assert.equal(ipInterne(ip), false, ip);
|
||||
}
|
||||
});
|
||||
|
||||
it('adresses refusées sans réseau : schéma, port, identifiants, nom local, IP littérale', () => {
|
||||
for (const a of ['file:///etc/passwd', 'ftp://exemple.org', 'gopher://exemple.org', 'javascript:alert(1)', 'data:text/html,x',
|
||||
'http://localhost/', 'http://LOCALHOST./', 'http://api.localhost/', 'http://nas.local/', 'http://db.internal/', 'http://intranet/',
|
||||
'http://127.0.0.1/', 'http://2130706433/', 'http://0x7f.1/', 'http://[::1]/', 'http://[::ffff:127.0.0.1]/', 'http://169.254.169.254/latest/meta-data',
|
||||
'http://exemple.org:8070/', 'https://exemple.org:8443/', 'http://user:mdp@exemple.org/', 'pas une adresse']) {
|
||||
assert.equal(controlerAdresse(a).ok, false, a);
|
||||
}
|
||||
assert.equal(controlerAdresse('https://exemple.org/page?x=1').ok, true);
|
||||
assert.equal(controlerAdresse('http://exemple.org:80/').ok, true, 'le port par défaut est retiré par URL');
|
||||
assert.equal(controlerAdresse('https://93.184.215.14/').ok, true);
|
||||
});
|
||||
|
||||
it('un nom public qui résout vers une IP interne est refusé (une seule IP interne suffit)', async () => {
|
||||
const lookup = dns({ 'piege.exemple.org': ['93.184.215.14', '127.0.0.1'], 'nocodb.exemple.org': ['10.0.0.5'], ...PUBLIC });
|
||||
assert.equal((await verifierAdresse('https://piege.exemple.org/', { lookup })).ok, false);
|
||||
assert.equal((await verifierAdresse('https://nocodb.exemple.org/', { lookup })).ok, false);
|
||||
assert.equal((await verifierAdresse('https://inconnu.exemple.org/', { lookup })).ok, false, 'non résolu : refusé');
|
||||
assert.equal((await verifierAdresse('https://ailleurs.net/', { lookup })).ok, true);
|
||||
});
|
||||
|
||||
it('aucune requête n’est faite vers une adresse refusée', async () => {
|
||||
const fetch = faux({});
|
||||
await assert.rejects(recupererPage('http://localhost:8070/api/v1/db', { fetch, lookup: dns(PUBLIC) }), AdresseRefusee);
|
||||
await assert.rejects(recupererPage('http://127.0.0.1/', { fetch, lookup: dns(PUBLIC) }), AdresseRefusee);
|
||||
assert.equal(fetch.appels.length, 0);
|
||||
});
|
||||
|
||||
it('redirection vers une adresse interne : refusée après le saut, la cible n’est jamais appelée', async () => {
|
||||
const fetch = faux({
|
||||
'https://exemple.org/': { status: 302, location: 'http://127.0.0.1:8070/api/v1/db/meta' },
|
||||
'https://www.exemple.org/': { status: 301, location: '/suite' },
|
||||
'https://www.exemple.org/suite': { status: 302, location: 'http://169.254.169.254/latest/meta-data' },
|
||||
});
|
||||
await assert.rejects(recupererPage('https://exemple.org/', { fetch, lookup: dns(PUBLIC) }), e => e instanceof AdresseRefusee && /port|interne/.test(e.motif));
|
||||
await assert.rejects(recupererPage('https://www.exemple.org/', { fetch, lookup: dns(PUBLIC) }), AdresseRefusee);
|
||||
assert.deepEqual(fetch.appels.map(a => a.url), ['https://exemple.org/', 'https://www.exemple.org/', 'https://www.exemple.org/suite']);
|
||||
assert.ok(fetch.appels.every(a => a.redirect === 'manual'), 'jamais redirect: follow');
|
||||
});
|
||||
|
||||
it('redirection vers un nom qui résout en interne : refusée', async () => {
|
||||
const fetch = faux({ 'https://exemple.org/': { status: 307, location: 'https://rebond.exemple.org/' } });
|
||||
const lookup = dns({ ...PUBLIC, 'rebond.exemple.org': ['192.168.0.10'] });
|
||||
await assert.rejects(recupererPage('https://exemple.org/', { fetch, lookup }), AdresseRefusee);
|
||||
assert.equal(fetch.appels.length, 1);
|
||||
});
|
||||
|
||||
it('redirections publiques suivies : adresse finale et chaîne rendues, table de redirections tirée', async () => {
|
||||
const fetch = faux({
|
||||
'http://exemple.org/': { status: 301, location: 'https://www.exemple.org/' },
|
||||
'https://www.exemple.org/': { status: 302, location: '/accueil' },
|
||||
'https://www.exemple.org/accueil': { status: 200, body: '<title>Accueil</title>' },
|
||||
});
|
||||
const r = await recupererPage('http://exemple.org', { fetch, lookup: dns(PUBLIC) });
|
||||
assert.equal(r.urlFinale, 'https://www.exemple.org/accueil');
|
||||
assert.deepEqual(r.chaine, ['http://exemple.org/', 'https://www.exemple.org/', 'https://www.exemple.org/accueil']);
|
||||
assert.match(r.html, /Accueil/);
|
||||
assert.deepEqual(tableRedirections(r.chaine), {
|
||||
'http://exemple.org/': 'https://www.exemple.org/',
|
||||
'https://www.exemple.org/': 'https://www.exemple.org/accueil',
|
||||
});
|
||||
});
|
||||
|
||||
it(`au plus ${MAX_SAUTS} redirections, puis erreur`, async () => {
|
||||
const routes = {};
|
||||
for (let i = 0; i <= MAX_SAUTS + 1; i++) routes[`https://exemple.org/${i}`] = { status: 302, location: `/${i + 1}` };
|
||||
await assert.rejects(recupererPage('https://exemple.org/0', { fetch: faux(routes), lookup: dns(PUBLIC) }), /redirections/);
|
||||
});
|
||||
|
||||
it('le corps est plafonné à 500 Ko', async () => {
|
||||
const fetch = faux({ 'https://exemple.org/': { status: 200, body: 'a'.repeat(600_000) } });
|
||||
const r = await recupererPage('https://exemple.org/', { fetch, lookup: dns(PUBLIC) });
|
||||
assert.equal(r.html.length, 500_000);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user