AtlatestRepositorysigil-http3
sigil-http3 / tree / testtest-phone-page-host.mjs
1
// Real HTTPS sockets, loopback only, on Linux. No WebTransport fixture or2
// manual browser certificate bypass is involved in these static host checks.3
import assert from 'node:assert/strict';4
import { request } from 'node:https';5
import { connect } from 'node:net';6
import { readFileSync } from 'node:fs';7
import { hostname } from 'node:os';8
import { resolve } from 'node:path';9
import { test } from 'node:test';10
import { startPhonePage } from '../scripts/phone-page-host.mjs';12
const certificateDirectory = resolve('build/lifecycle/certificate');13
const ca = readFileSync(resolve(certificateDirectory, 'cert.pem'));14
const config = { schema: 1, scope: 'test-loopback', expectedHost: hostname(),15
bindAddress: '127.0.0.1', port: 0, lifetimeSeconds: 5,16
page: resolve('web/phone.html'), certificateDirectory };18
function get(host, path = '/phone.html', options = {}) {19
return new Promise((done, reject) => {20
const req = request(new URL(path, host.origin), { ca, agent: false, timeout: 3000, ...options }, response => {21
let body = '';22
response.setEncoding('utf8');23
response.on('data', bytes => { body += bytes; });24
response.once('end', () => done({ status: response.statusCode, headers: response.headers, body }));25
response.once('error', reject);26
});27
req.once('error', reject);28
req.once('timeout', () => req.destroy(new Error('HTTPS page request deadline')));29
req.end();30
});31
}33
test('page host rejects unapproved binds, hosts and lifetimes before listening', async () => {34
await assert.rejects(startPhonePage({ ...config, bindAddress: '0.0.0.0' }), /configured exact address/);35
await assert.rejects(startPhonePage({ ...config, scope: 'public' }), /configured exact address/);36
await assert.rejects(startPhonePage({ ...config, expectedHost: 'wrong-host' }), /host mismatch/);37
await assert.rejects(startPhonePage({ ...config, lifetimeSeconds: 7201 }), /7200/);38
});40
test('verified HTTPS serves the exact page only and validates method, host and path', async t => {41
const host = await startPhonePage(config);42
t.after(() => host.close());43
const page = await get(host);44
assert.equal(page.status, 200);45
assert.equal(page.body, readFileSync(config.page, 'utf8'));46
assert.equal(page.headers['cache-control'], 'no-store');47
const configured = await get(host, '/phone.html?host=%3A%3A1&port=54433&hash=' + 'ab'.repeat(32));48
assert.equal(configured.status, 200);49
assert.equal(configured.body, page.body, 'Query parameters select no other file and enter no server HTML');50
const head = await get(host, '/', { method: 'HEAD' });51
assert.equal(head.status, 200);52
assert.equal(head.body, '');53
assert.equal((await get(host, '/key.pem')).status, 404);54
assert.equal((await get(host, '/phone.html?file=private.raw')).status, 404);55
assert.equal((await get(host, '/phone.html', { method: 'POST' })).status, 405);56
// Authenticate localhost with its actual certificate while independently57
// probing an invalid HTTP authority; Host must not change the TLS identity.58
assert.equal((await get(host, '/', {59
servername: 'localhost', headers: { Host: 'unapproved.test' },60
})).status, 400);61
});63
test('automatic page expiry closes the listener and repeated close is safe', async () => {64
const host = await startPhonePage({ ...config, lifetimeSeconds: 1 });65
try {66
await host.closed;67
await host.close();68
await assert.rejects(get(host), /ECONNREFUSED|ECONNRESET|socket hang up/);69
} finally { await host.close(); }70
});72
test('page teardown also retires an admitted unfinished TLS socket', async t => {73
const host = await startPhonePage(config);74
t.after(() => host.close());75
const socket = connect(host.port, '127.0.0.1');76
t.after(() => socket.destroy());77
await new Promise((ready, reject) => { socket.once('connect', ready); socket.once('error', reject); });78
const closed = new Promise(done => socket.once('close', done));79
await host.close();80
await closed;81
assert.equal(socket.destroyed, true);82
});