From 5a7e67d0b8167818136ff1f9021efa68c2b54c73 Mon Sep 17 00:00:00 2001 From: Hoelee Date: Tue, 10 Nov 2026 13:03:46 +0800 Subject: [PATCH] fix(http): count redirects, not requests, against the chain limit A chain of five redirects is six requests. The check failed it while net-guard had just followed it to the end, so the tool blamed the visitor for something it had already done. The budget now counts redirects, and an abandoned chain that ends on a 3xx counts the redirect it was in the middle of. 99 tests. --- src/checks/http.mjs | 14 ++++++++--- test/http-boundary.test.mjs | 50 +++++++++++++++++++++++++++++++++++++ test/http.test.mjs | 21 +++++++++++----- 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 test/http-boundary.test.mjs diff --git a/src/checks/http.mjs b/src/checks/http.mjs index 11f45ec..aa4e475 100644 --- a/src/checks/http.mjs +++ b/src/checks/http.mjs @@ -17,7 +17,10 @@ import { fetchFollowing } from '../lib/net-guard.mjs'; const HSTS_MIN_AGE = 15_552_000; // 180 days, per the HSTS guidance browsers follow -const MAX_HOPS = 5; +// A redirect budget is counted in REDIRECTS, not requests. Counting requests made a site with +// exactly five redirects (six requests) a failure while net-guard happily fetched it — the tool +// blamed the visitor for a chain it had just followed to the end. +const MAX_REDIRECTS = 5; const HEAD_SCAN_LIMIT = 200_000; const LEVEL_RANK = { ok: 0, info: 1, warn: 2, fail: 3 }; @@ -357,7 +360,10 @@ export function analyseHttp(result, { host, now = Date.now() } = {}) { const finalStatus = final && typeof final.status === 'number' ? final.status : (lastHop ? lastHop.status : 0); - const redirectCount = Math.max(0, hops.length - 1); + // A chain that was abandoned mid-redirect (net-guard's too_many_redirects hands back the hops it + // collected) ends on a 3xx with no final response: that last redirect did happen, so it counts. + const endsOnRedirect = hops.length > 0 && hops[hops.length - 1].status >= 300 && hops[hops.length - 1].status < 400; + const redirectCount = Math.max(0, hops.length - 1 + (endsOnRedirect && !final ? 1 : 0)); const findings = []; const evidence = []; @@ -400,11 +406,11 @@ export function analyseHttp(result, { host, now = Date.now() } = {}) { } // --- redirect chain ----------------------------------------------------------------------- - if (hops.length > MAX_HOPS) { + if (redirectCount > MAX_REDIRECTS) { findings.push({ level: 'fail', code: 'http_too_many_hops', - message: `The page took ${hops.length} requests to load (${redirectCount} redirects). Anything past ${MAX_HOPS} makes the site feel slow and wastes crawl budget.`, + message: `The page took ${redirectCount} redirects before it loaded (${hops.length} requests). Anything past ${MAX_REDIRECTS} makes the site feel slow and wastes crawl budget.`, fix: 'Point the advertised address straight at the final page.' }); } diff --git a/test/http-boundary.test.mjs b/test/http-boundary.test.mjs new file mode 100644 index 0000000..74317f2 --- /dev/null +++ b/test/http-boundary.test.mjs @@ -0,0 +1,50 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { analyseHttp } from '../src/checks/http.mjs'; + +/** A chain with `redirects` 301s followed by one 200. */ +function chain(redirects) { + const hops = []; + for (let i = 0; i < redirects; i += 1) { + hops.push({ url: `https://example.com/${i}`, status: 301, location: `https://example.com/${i + 1}`, totalMs: 40 }); + } + const finalUrl = `https://example.com/${redirects}`; + hops.push({ url: finalUrl, status: 200, location: null, totalMs: 40 }); + return { + hops, + final: { status: 200, url: finalUrl, headers: {} }, + headers: { 'strict-transport-security': 'max-age=31536000', 'x-content-type-options': 'nosniff' }, + body: 'Exampleok', + truncated: false + }; +} + +test('five redirects is the last acceptable chain, not a failure', () => { + const out = analyseHttp(chain(5), { host: 'example.com' }); + const hopsFinding = out.findings.find((f) => f.code === 'http_too_many_hops'); + assert.equal(hopsFinding, undefined, 'six requests / five redirects must not be failed'); + assert.equal(out.state === 'fail', false); +}); + +test('six redirects is a failure and names the redirect count', () => { + const out = analyseHttp(chain(6), { host: 'example.com' }); + const hopsFinding = out.findings.find((f) => f.code === 'http_too_many_hops'); + assert.ok(hopsFinding, 'six redirects must produce http_too_many_hops'); + assert.equal(hopsFinding.level, 'fail'); + assert.match(hopsFinding.message, /6 redirects/); +}); + +test('a single http to https redirect is a win, not a finding', () => { + const out = analyseHttp({ + hops: [ + { url: 'http://example.com/', status: 301, location: 'https://example.com/', totalMs: 30 }, + { url: 'https://example.com/', status: 200, location: null, totalMs: 30 } + ], + final: { status: 200, url: 'https://example.com/', headers: {} }, + headers: { 'strict-transport-security': 'max-age=31536000' }, + body: 'Example' + }, { host: 'example.com' }); + assert.equal(out.findings.some((f) => f.level === 'fail'), false); + assert.ok(out.preview, 'the preview object must reach the report'); + assert.equal(out.preview.image, 'https://example.com/a.png'); +}); diff --git a/test/http.test.mjs b/test/http.test.mjs index 7f083d9..15c3722 100644 --- a/test/http.test.mjs +++ b/test/http.test.mjs @@ -94,8 +94,8 @@ test('a page served only over http is a failure', () => { // --- redirect chain ------------------------------------------------------------------------- -test('six requests is more hops than the limit and fails', () => { - const hops = [ +test('five redirects is the last chain that passes; a sixth fails', () => { + const sixRequests = [ hop(301, 'https://example.com/a', 'https://example.com/b'), hop(301, 'https://example.com/b', 'https://example.com/c'), hop(301, 'https://example.com/c', 'https://example.com/d'), @@ -103,10 +103,19 @@ test('six requests is more hops than the limit and fails', () => { hop(301, 'https://example.com/e', 'https://example.com/f'), hop(200, 'https://example.com/f') ]; - const analysis = analyseHttp(page(hops, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); - assert.equal(analysis.state, 'fail'); - assert.ok(codes(analysis).includes('http_too_many_hops')); - assert.equal(analysis.redirects.count, 5); + const allowed = analyseHttp(page(sixRequests, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(allowed.redirects.count, 5); + assert.equal(codes(allowed).includes('http_too_many_hops'), false, 'six requests is five redirects, not too many'); + + const sevenRequests = [ + ...sixRequests.slice(0, 5), + hop(301, 'https://example.com/f', 'https://example.com/g'), + hop(200, 'https://example.com/g') + ]; + const failed = analyseHttp(page(sevenRequests, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(failed.state, 'fail'); + assert.ok(codes(failed).includes('http_too_many_hops')); + assert.equal(failed.redirects.count, 6); }); test('a redirect loop fails and names the address it loops on', () => {