From d6eca8c850562e40cf883a14d7d9b606ea3f73a2 Mon Sep 17 00:00:00 2001 From: Tony Averty Date: Wed, 21 Oct 2026 16:11:12 +0200 Subject: [PATCH] fix: drop non-http urls when saving scraped prospects Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SfihHazrSHXgndJtSNLubJ --- src/handlers.js | 11 +++++++++-- test/handlers.test.js | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/handlers.js b/src/handlers.js index 8938008..935a666 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -14,6 +14,13 @@ const sleep = (ms, signal) => new Promise((resolve, reject) => { signal?.addEventListener('abort', () => { clearTimeout(t); reject(new Error('Annulé')); }, { once: true }); }); +// Une page web peut tenter de faire enregistrer "javascript:…" : seules les URLs http(s) sont gardées +export function httpUrl(v) { + const s = String(v ?? '').trim(); + if (!/^https?:\/\/[^\s<>"]+$/i.test(s)) return null; + try { return new URL(s).protocol.startsWith('http') ? s.slice(0, 300) : null; } catch { return null; } +} + export function parseProspectLine(line) { const m = /^\s*PROSPECT\s+(\{.*\})\s*$/.exec(line); if (!m) return null; @@ -24,8 +31,8 @@ export function parseProspectLine(line) { const str = (v, max = 300) => (v == null ? null : String(v).slice(0, max)); const score = Math.max(1, Math.min(10, Math.round(Number(p.score) || 5))); return { - company: str(p.company, 200), email, website: str(p.website), city: str(p.city, 100), - sector: str(p.sector, 100), hook: str(p.hook, 400), source: str(p.source), score, + company: str(p.company, 200), email, website: httpUrl(p.website), city: str(p.city, 100), + sector: str(p.sector, 100), hook: str(p.hook, 400), source: httpUrl(p.source), score, }; } diff --git a/test/handlers.test.js b/test/handlers.test.js index 7dd2391..4928354 100644 --- a/test/handlers.test.js +++ b/test/handlers.test.js @@ -97,6 +97,14 @@ test('flux complet : analyse → cible → prospection → modèle → envoi → } finally { rmSync(s.tmp, { recursive: true, force: true }); } }); +test('parseProspectLine ne garde que des URLs http(s)', () => { + const p = parseProspectLine('PROSPECT {"company":"X","email":"a@b.fr","website":"javascript:alert(1)","source":"https://ok.fr/contact"}'); + assert.equal(p.website, null); + assert.equal(p.source, 'https://ok.fr/contact'); + assert.equal(parseProspectLine('PROSPECT {"company":"X","email":"a@b.fr","website":"https://a.fr\\" onclick=x"}').website, null); + assert.equal(parseProspectLine('PROSPECT {"company":"X","email":"a@b.fr","website":"data:text/html,x"}').website, null); +}); + test('parseProspectLine filtre les lignes invalides', () => { assert.equal(parseProspectLine('blabla'), null); assert.equal(parseProspectLine('PROSPECT {cassé'), null);