notes
All repositories: gitoria
4.4 KB
// #375: abrupt TLS EOF/errors must deliver close once and allow reconnect.import assert from 'node:assert/strict';import net from 'node:net';import tls from 'node:tls';import { createHash } from 'node:crypto';import { spawn, spawnSync } from 'node:child_process';import { once } from 'node:events';import { mkdtempSync, readFileSync, readlinkSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { dirname, resolve, join } from 'node:path';import { fileURLToPath } from 'node:url';const HERE = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(HERE, '../../..');const BIN = process.env.HL_BIN || resolve(ROOT, 'native/zig-out/bin/hybriel');const PORT = Number(process.env.HL_TEST_PORT || 30430);const work = mkdtempSync(join(tmpdir(), 'ws-close-'));function command(cmd, args) {const r = spawnSync(cmd, args, { cwd: ROOT, encoding: 'utf8' });assert.equal(r.status, 0, r.stdout + r.stderr);}let checks = 0;try {command('openssl', ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', join(work, 'key.pem'), '-out', join(work, 'cert.pem'), '-days', '1', '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost']);command(BIN, ['--native', resolve(HERE, 'ws-client-close.hl'), '-o', join(work, 'client'), '--quiet']);for (const secure of [false, true]) for (const mode of ['abrupt', 'clean', 'half']) {let accepted = 0;const sockets = new Set();const timers = new Set();const handler = socket => {sockets.add(socket);socket.on('error', () => {});socket.on('close', () => sockets.delete(socket));let head = '';socket.on('data', function handshake(data) {head += data.toString();if (!head.includes('\r\n\r\n')) return;socket.removeListener('data', handshake);const key = head.match(/Sec-WebSocket-Key: (.*)/i)?.[1].trim();assert.ok(key);accepted++;socket.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ' + createHash('sha1').update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64') + '\r\n\r\n');// Longer than one read tick: a retryable TLS timeout must stay open.const timer = setTimeout(() => {timers.delete(timer);if (mode === 'abrupt') socket.destroy();if (mode === 'half') socket.end(); // TLS close_notify or TCP FIN, no WS closeif (mode === 'clean') socket.end(Buffer.from([0x88, 2, 3, 0xe8]));}, 700);timers.add(timer);});};const server = secure ? tls.createServer({ cert: readFileSync(join(work, 'cert.pem')), key: readFileSync(join(work, 'key.pem')), allowHalfOpen: true }, handler) : net.createServer({ allowHalfOpen: true }, handler);let child, timeout;try {server.listen(PORT, '127.0.0.1');await once(server, 'listening');child = spawn(join(work, 'client'), [`${secure ? 'wss' : 'ws'}://localhost:${PORT}/`, join(work, 'cert.pem')], { cwd: ROOT, env: process.env });let log = '';child.stdout.on('data', d => { log += d; });child.stderr.on('data', d => { log += d; });const exited = once(child, 'exit');timeout = setTimeout(() => {assert.equal(readlinkSync(`/proc/${child.pid}/cwd`), ROOT);spawnSync('ss', ['-ltnp', `sport = ${PORT}`]);child.kill('SIGKILL');}, 6000);const [code] = await exited;assert.equal(code, 0, log);assert.equal(accepted, 2, log);assert.deepEqual(log.split('\n').filter(s => /^(opened|closed|retry)/.test(s)), ['opened', `closed ${mode === 'clean' ? 1000 : 1006}`, 'retry true', 'opened', `closed ${mode === 'clean' ? 1000 : 1006}`]);checks += 3;console.log(`ok ${secure ? 'wss' : 'ws'} ${mode}: two opens, two closes, reconnect`);} finally {clearTimeout(timeout);for (const timer of timers) clearTimeout(timer);if (child && child.exitCode === null && child.signalCode === null) {assert.equal(readlinkSync(`/proc/${child.pid}/cwd`), ROOT);spawnSync('ss', ['-ltnp', `sport = ${PORT}`]);const exited = once(child, 'exit'); child.kill('SIGKILL'); await exited;}for (const socket of sockets) socket.destroy();await new Promise(r => server.close(r));}}console.log(`ok ws-client-close: ${checks} checks across 6 cases`);} finally {rmSync(work, { recursive: true, force: true });}
Branches
- hl-standard
- hl-standard-2
- mainmain branch
- notes-pr7-kunibert
- notes13-day61
- notes17-ghost-note
- notes9-no-poll
- vendor-day56
- vendor-day59
- w7
Latest commits
- 9cc92bdfKeep conflict and history fixes while awaiting queued save supportAtlas
- a5c27cb6Allow real edits back to earlier text and honor configured identAtlas
- 15c46ced|||PR Preserve note edits, conflict bases and visible historyAtlas
- 4c6e0bf6Merge branch 'notes13-day61' into mainAtlas
- eb51864f|||PR Back/Forward via navigate, server drafts, Hybriel day61Atlas
- 3537cf7eMerge branch 'vendor-day59' into mainAtlas
- c13d188b|||PR Vendor Hybriel day59Atlas
- 24c95433Merge branch 'notes17-ghost-note' into mainAtlas
- ae1075b2Preserve rejected drafts during note ID adoptionAtlas
- 383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas
- 99ca1cf6Merge branch 'vendor-day56' into mainAtlas
- 649cef37|||PR Vendor Hybriel day56Atlas
- aadd0abcMerge branch 'hl-standard-2' into mainAtlas
- 52ffac9f|||PR Keyboard and reload checks for the notes gate (HL-STANDARD F21, F5)Atlas
- 617ee8c0Merge branch 'hl-standard' into mainAtlas
- 452458ee|||PR Type the function parameters of notes' own .hl files (HL-STANDARD L10)Atlas
- 23758cd6Re-vendor complete Hybriel plugins and runtime to day44mre
- 5d7cc700Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
- 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
- d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre