Checkpoint 22/11, correction 2. Quatre formes passaient : [::127.0.0.1] (réécrit ::7f00:1), ::ffff:0:a.b.c.d (traduite), 64:ff9b:1::/48 (NAT64 local), Teredo 2001::/32. L'IPv6 est maintenant lue en huit groupes, quelle que soit son écriture ; NAT64 local et Teredo sont refusés en entier ; une forme illisible est refusée. Test : les quatre formes et leurs variantes d'écriture refusées, les publiques passent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
8.1 KiB
JavaScript
135 lines
8.1 KiB
JavaScript
/**
|
||
* 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('les quatre formes IPv6 du checkpoint (22/11) sont refusées, quelle que soit l’écriture', () => {
|
||
// [::127.0.0.1] que le parseur réécrit ::7f00:1 ; ::ffff:0:a.b.c.d (traduite) ; NAT64 local ; Teredo.
|
||
for (const a of ['http://[::127.0.0.1]/', 'http://[::ffff:0:127.0.0.1]/', 'http://[::ffff:0:a00:1]/',
|
||
'http://[64:ff9b:1::7f00:1]/', 'http://[64:ff9b:1::5db8:d70e]/', 'http://[2001:0:4136:e378:8000:63bf:3fff:fdd2]/',
|
||
'http://[::a9fe:a9fe]/', 'http://[0:0:0:0:0:ffff:7f00:1]/']) {
|
||
assert.equal(controlerAdresse(a).ok, false, a);
|
||
}
|
||
// Ce que la résolution DNS peut rendre, écrit autrement que par le parseur d'URL.
|
||
for (const ip of ['::7f00:1', '::127.0.0.1', '0:0:0:0:0:0:7f00:1', '0::ffff:7f00:1', '0:0:0:0:0:ffff:127.0.0.1',
|
||
'::ffff:0:7f00:1', '::FFFF:0:C0A8:101', '64:ff9b:1::a00:1', '2001::1', '2001:0:0:0:0:0:0:1', 'fe80::1%eth0',
|
||
'::ffff:0:300.0.0.1', '1::2::3', '1:2:3:4:5:6:7:8:9']) {
|
||
assert.equal(ipInterne(ip), true, ip);
|
||
}
|
||
// Les publiques passent toujours, IPv4 publique logée comprise.
|
||
for (const ip of ['::5db8:d70e', '::ffff:0:5db8:d70e', '64:ff9b::5db8:d70e', '2002:5db8:d70e::1', '2606:4700::6810:84e5', '2001:4860:4860::8888']) {
|
||
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);
|
||
});
|
||
});
|