Files
hoelee-check/test/host-shape.test.mjs
T
hoelee e407c0daba feat(check): who-holds and host-shape, wired and honest about what they cannot see
Two new checks: the registry/mail/hosting card, and whether the address works with and
without www. Wiring forced three corrections that are the point of this commit:

- who-holds emitted a numeric checkedAt while the envelope contract is an ISO string;
  a second shape in the same results array breaks any staleness comparison.
- Linode's pattern matched 'akamai', so maybank2u.com.my - a bank behind Akamai's edge -
  was reported as sitting on Linode. Two brands, one owner, different platforms.
- A refused DNS query was printed as 'this domain does not resolve', which is a claim
  about the customer's domain rather than about our resolver. Transient codes now get
  one retry, and only NXDOMAIN is allowed to say a name does not exist.

about page now lists the real request budget (page + five HEAD hops + image + three
www-form requests + two registry), and the hub says five checks instead of three.
2026-11-10 18:45:36 +08:00

344 lines
15 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { checkHostShape, analyseHostShape, planForms, normaliseAddress, startUrlFor, REQUEST_BUDGET } from '../src/checks/host-shape.mjs';
import { NetGuardError } from '../src/lib/net-guard.mjs';
// Every fixture here is a plain object shaped like net-guard's requestOnce() result. The resolver
// is never called and no socket is opened: `lookup` is undefined throughout, and the only
// transport is the fake below.
/** Routes by "scheme://hostname"; a handler may be a function (for path-based chains) or an Error. */
function fakeRequest(routes) {
const calls = [];
const fn = async (url, options = {}) => {
const method = options.method || 'GET';
calls.push({ url, method });
const parsed = new URL(url);
const key = `${parsed.protocol}//${parsed.hostname}`;
const handler = routes[key];
if (handler === undefined) throw new NetGuardError('dns_nxdomain', 'That domain did not resolve.');
const res = typeof handler === 'function' ? handler(options, url) : handler;
if (res instanceof Error) throw res;
return { url, status: 200, headers: {}, body: '', timing: { totalMs: 1 }, tls: null, ...res };
};
fn.calls = calls;
return fn;
}
function codes(result) {
return result.findings.map((finding) => finding.code);
}
function finding(result, code) {
return result.findings.find((entry) => entry.code === code);
}
function evidenceValue(result, label) {
const row = result.evidence.find((entry) => entry.label === label);
return row ? row.value : undefined;
}
// --- both forms, one page ---------------------------------------------------------------------
test('a bare domain that redirects to www is reported as one page, in two requests', async () => {
const request = fakeRequest({
'https://example.com': { status: 301, headers: { location: 'https://www.example.com/' } },
'https://www.example.com': { status: 200 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.id, 'host-shape');
assert.equal(result.state, 'ok');
assert.equal(result.fromCache, false);
assert.match(result.summary, /same page/);
assert.equal(finding(result, 'host_forms_converge').level, 'ok');
assert.match(finding(result, 'host_forms_converge').message, /https:\/\/www\.example\.com\//);
assert.equal(evidenceValue(result, 'https://example.com/'), 'https://www.example.com/ (301, after a redirect)');
assert.equal(evidenceValue(result, 'https://www.example.com/'), 'https://www.example.com/ (200)');
assert.equal(request.calls.length, 2, 'the apex redirect target is the other form, so it is not fetched twice');
assert.equal(typeof result.checkedAt, 'string');
assert.ok(Number.isFinite(Date.parse(result.checkedAt)));
});
test('typing www.example.com checks www first and the bare domain as its counter-form', async () => {
const request = fakeRequest({
'https://www.example.com': { status: 200 },
'https://example.com': { status: 301, headers: { location: 'https://www.example.com/' } }
});
const result = await checkHostShape('www.example.com', { request });
assert.equal(result.state, 'ok');
assert.deepEqual(planForms('www.example.com').forms, ['www.example.com', 'example.com']);
assert.equal(request.calls[0].url, 'https://www.example.com/');
});
test('two addresses that land on different pages are a warning naming both', async () => {
const request = fakeRequest({
'https://example.com': { status: 200 },
'https://www.example.com': { status: 200 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'warn');
const split = finding(result, 'host_forms_split');
assert.equal(split.level, 'warn');
assert.match(split.message, /example\.com ends on https:\/\/example\.com\//);
assert.match(split.message, /www\.example\.com ends on https:\/\/www\.example\.com\//);
assert.match(result.summary, /two addresses/);
assert.equal(finding(result, 'host_forms_converge'), undefined);
});
test('the same page reached by a different path is still a split', async () => {
const request = fakeRequest({
'https://example.com': { status: 301, headers: { location: 'https://www.example.com/home' } },
'https://www.example.com': (options, url) => ({ status: 200, url })
});
const result = await checkHostShape('example.com', { request });
// The apex chain lands on /home, and the www probe itself is asked for "/", so the two forms do
// not end on the same URL.
assert.equal(result.state, 'warn');
assert.ok(codes(result).includes('host_forms_split'));
});
// --- a form that is not there ----------------------------------------------------------------
test('a www name that does not resolve is a failure that names www', async () => {
const request = fakeRequest({
'https://example.com': { status: 301, headers: { location: 'https://www.example.com/' } }
// https://www.example.com is absent -> the fake throws dns_nxdomain, as net-guard would
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'fail');
const dead = finding(result, 'host_form_dead');
assert.equal(dead.level, 'fail');
assert.match(dead.message, /^www\.example\.com does not answer at all/);
assert.match(dead.message, /does not exist in DNS/);
assert.match(result.summary, /www\.example\.com does not answer at all/);
assert.equal(evidenceValue(result, 'https://www.example.com/'), 'no answer -- the name does not exist in DNS');
assert.match(
evidenceValue(result, 'https://example.com/'),
/redirects to an address that does not answer/,
'the apex must not claim it lands on a name that does not exist'
);
});
test('a form returning a 500 is a failure, not a warning', async () => {
const request = fakeRequest({
'https://example.com': { status: 200 },
'https://www.example.com': { status: 500 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'fail');
assert.match(finding(result, 'host_form_dead').message, /returns a 500 error/);
});
test('a 404 is an answer, so it is reported as a split rather than a dead form', async () => {
const request = fakeRequest({
'https://example.com': { status: 200 },
'https://www.example.com': { status: 404 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'warn');
assert.equal(finding(result, 'host_form_dead'), undefined);
assert.ok(codes(result).includes('host_forms_split'));
});
// --- encryption ------------------------------------------------------------------------------
test('a site that only answers on http is a warning about the unencrypted page', async () => {
const request = fakeRequest({
'https://example.com': new NetGuardError('refused', 'The server refused the connection.'),
'http://example.com': { status: 200 },
'https://www.example.com': { status: 301, headers: { location: 'http://example.com/' } }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'warn');
const http = result.findings.filter((entry) => entry.code === 'host_form_http');
assert.ok(http.length >= 1);
assert.match(http[0].message, /not encrypted|plain http/);
assert.match(result.summary, /http/);
assert.ok(request.calls.length <= REQUEST_BUDGET);
});
test('a redirect that drops to http is a warning that names the http address', async () => {
const request = fakeRequest({
'https://example.com': { status: 301, headers: { location: 'http://example.com/' } },
'http://example.com': { status: 200 },
'https://www.example.com': { status: 200 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'warn');
assert.match(finding(result, 'host_form_http').message, /http:\/\/example\.com\//);
});
// --- refusals are never findings against the site --------------------------------------------
test('a CDN challenge page is not evaluated, and nothing is called broken', async () => {
const request = fakeRequest({
'https://example.com': { status: 403, headers: { server: 'cloudflare', 'cf-ray': '8a1b2c3d' } },
'https://www.example.com': { status: 200 }
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'not-evaluated');
assert.equal(finding(result, 'host_forms_blocked').level, 'info');
assert.match(result.summary, /refused our automated request/);
assert.equal(result.findings.filter((entry) => entry.level === 'fail').length, 0);
assert.equal(result.findings.filter((entry) => entry.level === 'warn').length, 0);
});
test('a counter-form that points into a private network is not evaluated, with the reason', async () => {
const request = fakeRequest({
'https://example.com': { status: 200 },
'https://www.example.com': new NetGuardError('private_host', 'That address is private.')
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'not-evaluated');
assert.match(result.summary, /private network address/);
assert.equal(finding(result, 'host_form_unjudged').level, 'info');
assert.equal(finding(result, 'host_form_dead'), undefined);
});
test('a timeout is not judged either', async () => {
const request = fakeRequest({
'https://example.com': { status: 200 },
'https://www.example.com': new NetGuardError('timeout', 'That took too long.')
});
const result = await checkHostShape('example.com', { request });
assert.equal(result.state, 'not-evaluated');
assert.match(result.summary, /did not answer in time/);
});
// --- one form only ---------------------------------------------------------------------------
test('a hosted subdomain has no second form, so only one address is probed', async () => {
const request = fakeRequest({ 'https://myshop.github.io': { status: 200 } });
const result = await checkHostShape('myshop.github.io', { request });
assert.equal(result.state, 'ok');
assert.equal(finding(result, 'host_form_single').level, 'info');
assert.match(result.summary, /hosted subdomain/);
assert.equal(request.calls.length, 1);
assert.equal(request.calls[0].url, 'https://myshop.github.io/');
});
test('a deeper subdomain is compared with the registrable apex', async () => {
const request = fakeRequest({
'https://shop.example.com.my': { status: 200 },
'https://example.com.my': { status: 200 }
});
const result = await checkHostShape('shop.example.com.my', { request });
assert.deepEqual(planForms('shop.example.com.my').forms, ['shop.example.com.my', 'example.com.my']);
assert.equal(result.state, 'warn', 'a subdomain that lands somewhere else is still two addresses');
assert.ok(codes(result).includes('host_forms_split'));
});
// --- budget ----------------------------------------------------------------------------------
test('a long redirect chain cannot spend more than three requests, and still checks www', async () => {
const request = fakeRequest({
'https://example.com': (options, url) => {
const path = new URL(url).pathname;
if (path === '/') return { status: 301, headers: { location: '/one' } };
if (path === '/one') return { status: 301, headers: { location: '/two' } };
if (path === '/two') return { status: 301, headers: { location: '/three' } };
return { status: 200 };
},
'https://www.example.com': { status: 200 }
});
const result = await checkHostShape('example.com', { request });
assert.ok(request.calls.length <= REQUEST_BUDGET, `made ${request.calls.length} requests`);
assert.ok(
request.calls.some((call) => call.url.includes('www.example.com')),
'one request is held back for the counter-form'
);
assert.equal(result.state, 'not-evaluated', 'the first form was not followed to a page');
});
test('a server that refuses HEAD gets one GET of the first kilobyte, inside the same budget', async () => {
const request = fakeRequest({
'https://example.com': (options) => (options.method === 'HEAD' ? { status: 405 } : { status: 200 }),
'https://www.example.com': { status: 301, headers: { location: 'https://example.com/' } }
});
const result = await checkHostShape('example.com', { request });
assert.deepEqual(request.calls.map((call) => call.method), ['HEAD', 'GET', 'HEAD']);
assert.equal(result.state, 'ok');
assert.ok(codes(result).includes('host_forms_converge'));
});
test('two forms and a redirect fit inside three requests', async () => {
const request = fakeRequest({
'https://example.com': { status: 301, headers: { location: 'https://www.example.com/' } },
'https://www.example.com': { status: 200 }
});
await checkHostShape('example.com', { request });
assert.equal(request.calls.length, 2);
assert.ok(request.calls.every((call) => call.method === 'HEAD'), 'no page bodies are downloaded');
});
// --- the analyser is pure --------------------------------------------------------------------
test('the analyser reads probes without touching the network', () => {
const plan = planForms('example.com');
const now = Date.UTC(2026, 0, 15, 12, 0, 0);
const probes = [
{ form: 'example.com', startUrl: 'https://example.com/', finalUrl: 'https://www.example.com/', status: 301, outcome: 'answered', reason: null, httpOnly: false },
{ form: 'www.example.com', startUrl: 'https://www.example.com/', finalUrl: 'https://www.example.com/', status: 200, outcome: 'answered', reason: null, httpOnly: false }
];
const result = analyseHostShape(plan, probes, { now });
assert.equal(result.state, 'ok');
assert.equal(result.checkedAt, new Date(now).toISOString());
assert.deepEqual(result.evidence.map((row) => row.label), ['https://example.com/', 'https://www.example.com/']);
assert.ok(codes(result).includes('host_forms_converge'));
});
// --- planning and comparison ----------------------------------------------------------------
test('planForms picks the two addresses a customer could type', () => {
assert.deepEqual(planForms('example.com').forms, ['example.com', 'www.example.com']);
assert.deepEqual(planForms('www.example.com').forms, ['www.example.com', 'example.com']);
assert.deepEqual(planForms('https://Example.COM./').forms, ['example.com', 'www.example.com']);
assert.deepEqual(planForms('myshop.github.io').forms, ['myshop.github.io']);
assert.equal(planForms('myshop.github.io').single, 'platform');
assert.deepEqual(planForms('').forms, []);
});
test('normaliseAddress ignores what a browser ignores and nothing else', () => {
assert.equal(normaliseAddress('https://WWW.Example.com'), normaliseAddress('https://www.example.com/'));
assert.equal(normaliseAddress('https://example.com:443/'), normaliseAddress('https://example.com/'));
assert.notEqual(normaliseAddress('https://example.com/'), normaliseAddress('https://example.com/Home'));
assert.notEqual(normaliseAddress('http://example.com/'), normaliseAddress('https://example.com/'));
assert.equal(startUrlFor('example.com'), 'https://example.com/');
});