Elffuss BraidHub

utopia-ia/elffuss-code public

web.fetch says only what it can know when it cannot read a page

With /proxy answering 404, every failure was explained as "the site does not
let the browser read it (CORS) ... it's not that the page doesn't exist". That
was said even when the direct fetch had reached the site and got the site's
own 404, and when the host does not resolve; the agent was then told that a
missing page exists.

- When the direct fetch gets an answer from the site (with CORS the browser
  shows its status), that answer is what counts. A 404 or 410 is reported as
  "the site answered HTTP 404, that page does not exist", and the proxy is
  not asked. Another status (403, 5xx…) is reported with the proxy as a
  fallback, as before.
- When the direct fetch fails without an answer, the browser cannot tell a
  site without CORS from one it could not connect to: the error says both and
  that this says nothing about whether the page exists.
- "No pude leer" and "no tiene el proxy de respaldo" stay, which is what
  alltools.mjs looks for.

tests/web-honest.mjs: check 1 now asks for the honest wording, and three new
scenarios stand in with page.route: a CORS site answering its own 404 (no CORS
talk and the proxy untouched), a host that does not resolve, and a site
answering 403 without a proxy. Against the previous web.js 5 checks fail.
Changeset152be91d1268abb8dbd845f8108b0cc89dd7ba4e096c7ff4ebd50780d69dc6c6
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-11 17:34 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-11 17:34 +02:00
Parentsa6879f75021c

Browse the files at this changeset

2 files changed

modified tests/web-honest.mjs +47 −6
@@ -9,7 +9,10 @@
99 // 3. DuckDuckGo pide su verificación anti-bot: es un bloqueo, no una lista vacía;
1010 // 4. con resultados, los da (y «Sin resultados» solo cuando el buscador
1111 // contestó y no encontró nada);
12-// 5. un sitio con CORS se lee directo, sin pasar por el proxy.
12+// 5. un sitio con CORS se lee directo, sin pasar por el proxy;
13+// 6-8. y sin decir más de lo que se sabe: el 404 del propio sitio es que la
14+// página no existe; un fetch fallido sin respuesta no distingue «sin CORS»
15+// de «no se pudo conectar», y no se afirma que la página exista.
1316 // Todo con page.route: ninguna petición sale a internet.
1417 //
1518 // BASE=http://localhost:8799 node tests/web-honest.mjs
@@ -25,14 +28,17 @@
2528 const ANTIBOT = '<html><body><div class="anomaly-modal__title">Unfortunately, bots use DuckDuckGo too.</div></body></html>';
2629
2730 const b = await chromium.launch();
28-async function escenario(proxy, { directoCORS = false } = {}) {
31+async function escenario(proxy, { directoCORS = false, sitio = null } = {}) {
2932 const ctx = await b.newContext();
3033 const p = await ctx.newPage();
3134 const alProxy = [];
3235 // Los sitios de fuera: sin CORS (el fetch directo falla), salvo que se diga.
33- await p.route(/^https:\/\/(example\.com|html\.duckduckgo\.com|lite\.duckduckgo\.com)\//, r => directoCORS
34- ? r.fulfill({ status: 200, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: '<html><body><p>Example Domain directo</p></body></html>' })
35- : r.abort('failed'));
36+ // `sitio` contesta en su lugar (un 404 propio, un dominio que no resuelve…).
37+ await p.route(/^https:\/\/(example\.com|html\.duckduckgo\.com|lite\.duckduckgo\.com|cors-site\.example|no-such-host\.invalid)\//, r => sitio
38+ ? sitio(r)
39+ : directoCORS
40+ ? r.fulfill({ status: 200, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: '<html><body><p>Example Domain directo</p></body></html>' })
41+ : r.abort('failed'));
3642 await p.route('**/proxy?url=*', r => { alProxy.push(decodeURIComponent(r.request().url().split('url=')[1] || '')); return proxy(r); });
3743 await p.goto(BASE + '/manifest.json');
3844 const llama = (que, args) => p.evaluate(async ({ que, args }) => {
@@ -48,7 +54,10 @@
4854 const { ctx, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }));
4955 const f = await llama('fetch', { url: 'https://example.com' });
5056 ok('1 · web.fetch sin proxy: falla diciendo que el servidor no tiene el proxy', !f.ok && /no tiene el proxy de respaldo/.test(f.out) && /404/.test(f.out), f.out);
51- ok(' …y que no es que la página no exista', /No es que la página no exista/.test(f.out), f.out);
57+ // Desde el navegador un fetch fallido no distingue «sin CORS» de «no existe»:
58+ // se dicen las dos posibilidades y no se afirma que la página exista.
59+ ok(' …sin afirmar que la página exista: CORS o sin conexión, no se distingue',
60+ /CORS/.test(f.out) && /no se pudiera conectar/.test(f.out) && /no dice nada de si la página existe/.test(f.out) && !/No es que la página no exista/.test(f.out), f.out);
5261 const s = await llama('search', { query: 'mdn array map' });
5362 ok('1 · web.search sin proxy: falla con la causa, NO «Sin resultados»', !s.ok && /no tiene el proxy de respaldo/.test(s.out) && !/Sin resultados/.test(s.out), s.out);
5463 await ctx.close();
@@ -85,6 +94,38 @@
8594 ok('5 · con CORS se lee directo y el proxy ni se toca', f.ok && /Example Domain directo/.test(f.out) && alProxy.length === 0, `${f.out} · proxy ${alProxy.length}`);
8695 await ctx.close();
8796 }
97+// 6 · el sitio contesta él mismo un 404 (con CORS se ve): la página NO existe, y
98+// eso es lo que se dice. Antes, con /proxy en 404, decía «CORS» y «no es que la
99+// página no exista».
100+{
101+ const sinProxy = r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' });
102+ const { ctx, alProxy, llama } = await escenario(sinProxy, {
103+ sitio: r => r.fulfill({ status: 404, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: 'Not Found' }),
104+ });
105+ const f = await llama('fetch', { url: 'https://cors-site.example/no-such-page' });
106+ ok('6 · el 404 del propio sitio: dice que esa página no existe, sin hablar de CORS', !f.ok && /respondió HTTP 404/.test(f.out) && /no existe/.test(f.out) && !/CORS/.test(f.out) && !/No es que/.test(f.out), f.out);
107+ ok(' …y ni pregunta al proxy (la respuesta del sitio ya es la que es)', alProxy.length === 0, `proxy ${alProxy.length}`);
108+ await ctx.close();
109+}
110+// 7 · un dominio que no resuelve, sin proxy: no se le llama «CORS» a secas ni se
111+// dice que la página exista.
112+{
113+ const { ctx, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }), {
114+ sitio: r => r.abort('namenotresolved'),
115+ });
116+ const f = await llama('fetch', { url: 'https://no-such-host.invalid/' });
117+ ok('7 · un dominio que no resuelve: CORS o sin conexión, sin afirmar que la página exista', !f.ok && /no se pudiera conectar/.test(f.out) && !/No es que la página no exista/.test(f.out), f.out);
118+ await ctx.close();
119+}
120+// 8 · el sitio contesta otro error (403) y no hay proxy: se dice su estado.
121+{
122+ const { ctx, alProxy, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }), {
123+ sitio: r => r.fulfill({ status: 403, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: 'Forbidden' }),
124+ });
125+ const f = await llama('fetch', { url: 'https://cors-site.example/privada' });
126+ ok('8 · otro error del sitio (403): se dice su estado y que no hay proxy para intentarlo de otra forma', !f.ok && /respondió HTTP 403/.test(f.out) && /no tiene el proxy de respaldo/.test(f.out) && !/CORS/.test(f.out), `${f.out} · proxy ${alProxy.length}`);
127+ await ctx.close();
128+}
88129
89130 console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ WEB: si no puede leer o buscar, dice por qué');
90131 await b.close();
modified web/js/tools/web.js +15 −3
@@ -15,17 +15,29 @@
1515 // proxy no existe en este servidor (404): antes salía «HTTP 404» a secas, que
1616 // se lee como «esa página no existe», y la búsqueda lo convertía en «Sin
1717 // resultados», como si internet no tuviera nada que decir.
18+//
19+// Y sin decir más de lo que se sabe. Si el sitio contestó al fetch directo (con
20+// CORS el navegador deja ver su estado), lo que importa es lo que dijo: su 404
21+// es que la página no existe, haya proxy o no. Si el fetch directo falló sin
22+// respuesta, desde el navegador no se distingue un sitio sin CORS de uno que no
23+// existe o no resuelve: se dicen las dos cosas y no se afirma que la página
24+// exista. Antes, con /proxy en 404, siempre era «CORS» y «no es que la página no
25+// exista», también ante el 404 del propio sitio o un dominio que no existe.
1826 const hostDe = url => { try { return new URL(url).host; } catch { return url; } };
1927 async function proxyGet(url) {
20- let directo;
21- try { const r = await fetch(url); if (r.ok) return await r.text(); directo = new Error('HTTP ' + r.status); }
28+ let directo, estado = 0;
29+ try { const r = await fetch(url); if (r.ok) return await r.text(); estado = r.status; directo = new Error('HTTP ' + r.status); }
2230 catch (e) { directo = e; }
31+ if (estado === 404 || estado === 410)
32+ throw new Error(`No pude leer ${url}: el sitio respondió HTTP ${estado}, esa página no existe (o ya no está).`);
2333 let r;
2434 try { r = await fetch('/proxy?url=' + encodeURIComponent(url)); }
2535 catch (e) { throw new Error(`No pude leer ${hostDe(url)}: ni directamente (${directo?.message || 'sin respuesta'}) ni por el proxy del servidor (${e?.message || 'sin respuesta'}).`); }
2636 if (r.ok) return await r.text();
2737 if (r.status === 404 || r.status === 405 || r.status === 501) {
28- const e = new Error(`No pude leer ${hostDe(url)}: el sitio no deja leerlo directamente desde el navegador (CORS) y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}). No es que la página no exista: desde aquí no se puede leer.`);
38+ const e = new Error(estado
39+ ? `No pude leer ${hostDe(url)}: el sitio respondió HTTP ${estado}, y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}) para intentarlo de otra forma.`
40+ : `No pude leer ${hostDe(url)}: el navegador no pudo leerlo directamente (puede que el sitio no lo permita, por CORS, o que no se pudiera conectar con él; desde aquí no se distingue) y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}). Esto no dice nada de si la página existe.`);
2941 e.sinProxy = true;
3042 throw e;
3143 }