Zero-dependency Node service mounted at www.hoelee.com/check/. net-guard pins the resolved IP, re-validates every redirect hop, and refuses private ranges; the certificate is read off the same socket as the request. Pages: hub, certificate, email auth, redirects/preview, about, report. Argon review applied: optional-hardening wording, DKIM/SHA-1 root rules, neutral not-evaluated, cache-before-scan, single-connection TLS.
292 lines
12 KiB
JavaScript
292 lines
12 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
summariseTls,
|
|
checkTls,
|
|
parseCertDate,
|
|
parseSanList,
|
|
sanNames,
|
|
matchesName,
|
|
isWeakSignature,
|
|
worstLevel,
|
|
describeHandshakeError
|
|
} from '../src/checks/tls.mjs';
|
|
import { NetGuardError } from '../src/lib/net-guard.mjs';
|
|
|
|
// Every fixture is built from plain objects shaped exactly like net-guard's readCertificate()
|
|
// output. Nothing here resolves, connects, or reads the clock.
|
|
const NOW = Date.UTC(2026, 0, 15, 12, 0, 0); // 15 Jan 2026 12:00 UTC
|
|
|
|
const LEAF_SUBJECT = { CN: 'example.com' };
|
|
const INTERMEDIATE_SUBJECT = { CN: 'R3', O: "Let's Encrypt" };
|
|
const ROOT_SUBJECT = { CN: 'ISRG Root X1', O: 'Internet Security Research Group' };
|
|
|
|
function certEntry({
|
|
subject,
|
|
issuer,
|
|
sigalg = 'sha256WithRSAEncryption',
|
|
fingerprint256,
|
|
validFrom = 'Dec 1 00:00:00 2025 GMT',
|
|
validTo = 'Jun 1 00:00:00 2026 GMT'
|
|
}) {
|
|
return {
|
|
subject,
|
|
issuer,
|
|
validFrom,
|
|
validTo,
|
|
signatureAlgorithm: sigalg,
|
|
fingerprint256,
|
|
selfSigned: JSON.stringify(subject) === JSON.stringify(issuer)
|
|
};
|
|
}
|
|
|
|
function buildChain({
|
|
leafSigalg = 'sha256WithRSAEncryption',
|
|
intermediateSigalg = 'sha256WithRSAEncryption',
|
|
rootSigalg = 'sha256WithRSAEncryption',
|
|
includeRoot = true,
|
|
includeIntermediate = true,
|
|
validTo = 'Jun 1 00:00:00 2026 GMT'
|
|
} = {}) {
|
|
const leaf = certEntry({ subject: LEAF_SUBJECT, issuer: INTERMEDIATE_SUBJECT, sigalg: leafSigalg, fingerprint256: 'FP-LEAF', validTo });
|
|
const intermediate = certEntry({ subject: INTERMEDIATE_SUBJECT, issuer: ROOT_SUBJECT, sigalg: intermediateSigalg, fingerprint256: 'FP-INTERMEDIATE' });
|
|
const root = certEntry({ subject: ROOT_SUBJECT, issuer: ROOT_SUBJECT, sigalg: rootSigalg, fingerprint256: 'FP-ROOT' });
|
|
if (!includeIntermediate) return [leaf];
|
|
return includeRoot ? [leaf, intermediate, root] : [leaf, intermediate];
|
|
}
|
|
|
|
function tlsFixture(options = {}) {
|
|
const {
|
|
san = 'DNS:example.com',
|
|
authorized = true,
|
|
authorizationError = null,
|
|
chain = buildChain(options)
|
|
} = options;
|
|
const leaf = chain[0];
|
|
const cert = {
|
|
subject: leaf.subject,
|
|
issuer: leaf.issuer,
|
|
validFrom: leaf.validFrom,
|
|
validTo: leaf.validTo,
|
|
subjectAltName: san,
|
|
serialNumber: '00AB',
|
|
fingerprint256: leaf.fingerprint256,
|
|
signatureAlgorithm: leaf.signatureAlgorithm,
|
|
authorized,
|
|
authorizationError,
|
|
protocol: 'TLSv1.3',
|
|
chain
|
|
};
|
|
return {
|
|
host: 'example.com',
|
|
port: 443,
|
|
address: '93.184.216.34',
|
|
alpn: 'http/1.1',
|
|
cipher: { name: 'TLS_AES_256_GCM_SHA384' },
|
|
cert,
|
|
chain
|
|
};
|
|
}
|
|
|
|
function codes(analysis) {
|
|
return analysis.findings.map((finding) => finding.code);
|
|
}
|
|
|
|
function levels(analysis) {
|
|
return analysis.findings.map((finding) => finding.level);
|
|
}
|
|
|
|
// --- pure helpers ---------------------------------------------------------------------------
|
|
|
|
test('parseCertDate reads OpenSSL dates with the double space before single-digit days', () => {
|
|
assert.equal(parseCertDate('Mar 1 12:00:00 2027 GMT'), Date.UTC(2027, 2, 1, 12, 0, 0));
|
|
assert.equal(parseCertDate('Jan 8 00:00:00 2026 GMT'), Date.UTC(2026, 0, 8, 0, 0, 0));
|
|
assert.equal(parseCertDate('Dec 31 23:59:59 2026 GMT'), Date.UTC(2026, 11, 31, 23, 59, 59));
|
|
assert.equal(parseCertDate('2026-01-08T00:00:00.000Z'), Date.UTC(2026, 0, 8));
|
|
assert.equal(parseCertDate('not a date'), null);
|
|
assert.equal(parseCertDate(''), null);
|
|
assert.equal(parseCertDate(null), null);
|
|
});
|
|
|
|
test('SAN matching accepts an exact name and a single-label wildcard only', () => {
|
|
assert.equal(matchesName('example.com', 'example.com'), true);
|
|
assert.equal(matchesName('WWW.Example.com', '*.example.com'), true);
|
|
assert.equal(matchesName('www.example.com', 'example.com'), false);
|
|
assert.equal(matchesName('example.com', '*.example.com'), false); // wildcard covers one label, not the apex
|
|
assert.equal(matchesName('a.b.example.com', '*.example.com'), false); // not two labels
|
|
assert.equal(matchesName('www.example.com', 'www.example.com'), true);
|
|
assert.equal(matchesName('www.example.com.', 'www.example.com'), true); // trailing dot
|
|
assert.equal(matchesName('evil-example.com', '*.example.com'), false);
|
|
});
|
|
|
|
test('parseSanList splits DNS and IP entries', () => {
|
|
const parsed = parseSanList('DNS:example.com, DNS:*.example.com, IP:203.0.113.9');
|
|
assert.deepEqual(parsed.map((entry) => entry.type), ['DNS', 'DNS', 'IP']);
|
|
assert.deepEqual(sanNames('DNS:example.com, IP:203.0.113.9'), ['example.com']);
|
|
assert.deepEqual(parseSanList(''), []);
|
|
});
|
|
|
|
test('isWeakSignature flags SHA-1 and MD5 but not SHA-256', () => {
|
|
assert.equal(isWeakSignature('sha1WithRSAEncryption'), true);
|
|
assert.equal(isWeakSignature('ecdsa-with-SHA1'), true);
|
|
assert.equal(isWeakSignature('md5WithRSAEncryption'), true);
|
|
assert.equal(isWeakSignature('sha256WithRSAEncryption'), false);
|
|
assert.equal(isWeakSignature('ecdsa-with-SHA384'), false);
|
|
assert.equal(isWeakSignature(null), false);
|
|
});
|
|
|
|
test('worstLevel never returns info as a state', () => {
|
|
assert.equal(worstLevel([{ level: 'info' }, { level: 'info' }]), 'ok');
|
|
assert.equal(worstLevel([{ level: 'info' }, { level: 'warn' }]), 'warn');
|
|
assert.equal(worstLevel([{ level: 'warn' }, { level: 'fail' }]), 'fail');
|
|
assert.equal(worstLevel([]), 'ok');
|
|
});
|
|
|
|
// --- expiry ---------------------------------------------------------------------------------
|
|
|
|
test('an expired certificate is a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ validTo: 'Jan 1 12:00:00 2026 GMT' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_expired'));
|
|
assert.equal(analysis.daysToExpiry, -14);
|
|
assert.match(analysis.summary, /expired/i);
|
|
});
|
|
|
|
test('seven days left is a warning, and the day count is exact', () => {
|
|
const analysis = summariseTls(tlsFixture({ validTo: 'Jan 22 12:00:00 2026 GMT' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.daysToExpiry, 7);
|
|
assert.equal(analysis.state, 'warn');
|
|
assert.ok(codes(analysis).includes('tls_expiring_soon'));
|
|
assert.match(analysis.summary, /7 days/);
|
|
});
|
|
|
|
test('under 30 days is the softer warning', () => {
|
|
const analysis = summariseTls(tlsFixture({ validTo: 'Feb 9 12:00:00 2026 GMT' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.daysToExpiry, 25);
|
|
assert.equal(analysis.state, 'warn');
|
|
assert.ok(codes(analysis).includes('tls_expiring'));
|
|
assert.ok(!codes(analysis).includes('tls_expiring_soon'));
|
|
});
|
|
|
|
test('a healthy certificate more than 30 days out is ok', () => {
|
|
const analysis = summariseTls(tlsFixture(), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'ok');
|
|
assert.equal(analysis.daysToExpiry, 136);
|
|
assert.deepEqual(levels(analysis).filter((level) => level !== 'ok'), []);
|
|
assert.match(analysis.summary, /valid/i);
|
|
});
|
|
|
|
// --- name coverage --------------------------------------------------------------------------
|
|
|
|
test('a wildcard SAN covers one subdomain', () => {
|
|
const analysis = summariseTls(tlsFixture({ san: 'DNS:example.com, DNS:*.example.com' }), { host: 'www.example.com', now: NOW });
|
|
assert.equal(analysis.state, 'ok');
|
|
assert.ok(codes(analysis).includes('tls_name_match'));
|
|
});
|
|
|
|
test('a name that is not on the certificate is a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ san: 'DNS:example.com, DNS:*.example.com' }), { host: 'shop.example.org', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_name_mismatch'));
|
|
assert.match(analysis.summary, /shop\.example\.org/);
|
|
});
|
|
|
|
test('a certificate with no DNS names at all is a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ san: '' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_no_san'));
|
|
});
|
|
|
|
// --- signature algorithms -------------------------------------------------------------------
|
|
|
|
test('a SHA-1 leaf is a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ leafSigalg: 'sha1WithRSAEncryption' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_weak_signature_leaf'));
|
|
});
|
|
|
|
test('a SHA-1 ROOT is a trust anchor and produces no finding at all', () => {
|
|
const analysis = summariseTls(tlsFixture({ rootSigalg: 'sha1WithRSAEncryption' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'ok');
|
|
assert.ok(!codes(analysis).some((code) => code.startsWith('tls_weak_signature')));
|
|
assert.ok(!analysis.findings.some((finding) => /root/i.test(finding.message)));
|
|
// The root is still described in evidence — it is just not judged.
|
|
assert.equal(analysis.chainLength, 3);
|
|
});
|
|
|
|
test('a SHA-1 intermediate is a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ intermediateSigalg: 'sha1WithRSAEncryption' }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_weak_signature_intermediate'));
|
|
assert.ok(!codes(analysis).includes('tls_weak_signature_leaf'));
|
|
});
|
|
|
|
// --- chain shape ----------------------------------------------------------------------------
|
|
|
|
test('a self-signed leaf is a failure', () => {
|
|
const selfSigned = certEntry({ subject: { CN: 'shop.internal' }, issuer: { CN: 'shop.internal' }, fingerprint256: 'FP-SELF' });
|
|
const analysis = summariseTls(tlsFixture({ san: 'DNS:shop.internal', chain: [selfSigned] }), { host: 'shop.internal', now: NOW });
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_self_signed_leaf'));
|
|
assert.match(analysis.summary, /self-signed/i);
|
|
});
|
|
|
|
test('a chain of only the leaf is a warning, not a failure', () => {
|
|
const analysis = summariseTls(tlsFixture({ includeIntermediate: false }), { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'warn');
|
|
assert.ok(codes(analysis).includes('tls_chain_short'));
|
|
assert.equal(analysis.chainLength, 1);
|
|
});
|
|
|
|
test('a chain sent out of order is reported', () => {
|
|
const fixture = tlsFixture();
|
|
const [leafEntry, intermediate, root] = fixture.chain;
|
|
// The certificate stays the real leaf; only the chain array is reordered.
|
|
const analysis = summariseTls({ ...fixture, chain: [intermediate, leafEntry, root] }, { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'warn');
|
|
assert.ok(codes(analysis).includes('tls_chain_order'));
|
|
});
|
|
|
|
test('an unverifiable chain is a failure described in plain English', () => {
|
|
const analysis = summariseTls(
|
|
tlsFixture({ authorized: false, authorizationError: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }),
|
|
{ host: 'example.com', now: NOW }
|
|
);
|
|
assert.equal(analysis.state, 'fail');
|
|
assert.ok(codes(analysis).includes('tls_untrusted_chain'));
|
|
const finding = analysis.findings.find((entry) => entry.code === 'tls_untrusted_chain');
|
|
assert.match(finding.message, /intermediate certificate/i);
|
|
assert.doesNotMatch(finding.message, /UNABLE_TO_VERIFY/);
|
|
});
|
|
|
|
// --- input shapes and the network boundary ---------------------------------------------------
|
|
|
|
test('summariseTls accepts the bare certificate object from requestOnce().tls', () => {
|
|
const fixture = tlsFixture();
|
|
const analysis = summariseTls(fixture.cert, { host: 'example.com', now: NOW });
|
|
assert.equal(analysis.state, 'ok');
|
|
assert.equal(analysis.chainLength, 3);
|
|
assert.ok(codes(analysis).includes('tls_name_match'));
|
|
});
|
|
|
|
test('checkTls returns a standard envelope when the handshake never completes', async () => {
|
|
const lookup = async () => {
|
|
throw new NetGuardError('timeout', 'That took too long.');
|
|
};
|
|
const envelope = await checkTls('example.com', { lookup });
|
|
assert.equal(envelope.id, 'tls');
|
|
assert.equal(envelope.state, 'not-evaluated'); // never 'fail' — nothing was judged
|
|
assert.deepEqual(envelope.findings, []);
|
|
assert.equal(envelope.fromCache, false);
|
|
assert.match(envelope.summary, /handshake|time/i);
|
|
assert.ok(!Number.isNaN(Date.parse(envelope.checkedAt)));
|
|
});
|
|
|
|
test('describeHandshakeError keeps a refusal out of the findings', () => {
|
|
assert.match(describeHandshakeError(new NetGuardError('refused', 'x')), /refused/i);
|
|
assert.match(describeHandshakeError(new NetGuardError('dns_nxdomain', 'x')), /did not resolve/i);
|
|
assert.match(describeHandshakeError(new NetGuardError('private_address', 'x')), /private/i);
|
|
assert.match(describeHandshakeError(new Error('boom')), /could not be read/i);
|
|
});
|