|
| 1 | +import * as net from 'net'; |
| 2 | +import * as http from 'http'; |
| 3 | +import * as https from 'https'; |
| 4 | +import * as streamConsumers from 'stream/consumers'; |
| 5 | + |
| 6 | +import { expect } from 'chai'; |
| 7 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 8 | + |
| 9 | +import { createServer } from '../src/server.js'; |
| 10 | + |
| 11 | +describe("TLS client hello endpoint", () => { |
| 12 | + |
| 13 | + let server: DestroyableServer; |
| 14 | + let serverPort: number; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + server = makeDestroyable(await createServer()); |
| 18 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 19 | + serverPort = (server.address() as net.AddressInfo).port; |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + await server.destroy(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns the full annotated client hello", async () => { |
| 27 | + const response = await new Promise<any>((resolve, reject) => { |
| 28 | + https.get(`https://localhost:${serverPort}/tls/client-hello`, { |
| 29 | + rejectUnauthorized: false |
| 30 | + }, async (res) => { |
| 31 | + try { |
| 32 | + const body = await streamConsumers.text(res); |
| 33 | + resolve(JSON.parse(body)); |
| 34 | + } catch (e) { |
| 35 | + reject(e); |
| 36 | + } |
| 37 | + }).on('error', reject); |
| 38 | + }); |
| 39 | + |
| 40 | + // Top-level structure |
| 41 | + expect(response).to.have.keys([ |
| 42 | + 'version', 'random', 'sessionId', |
| 43 | + 'cipherSuites', 'compressionMethods', 'extensions' |
| 44 | + ]); |
| 45 | + |
| 46 | + // Version is annotated with id and name |
| 47 | + expect(response.version).to.deep.equal({ id: 0x0303, name: 'TLS 1.2' }); |
| 48 | + |
| 49 | + // Random is a 32-byte hex string |
| 50 | + expect(response.random).to.be.a('string').with.lengthOf(64); |
| 51 | + |
| 52 | + // SessionId is a hex string or null |
| 53 | + expect(response.sessionId).to.satisfy( |
| 54 | + (v: any) => typeof v === 'string' || v === null |
| 55 | + ); |
| 56 | + |
| 57 | + // Cipher suites are annotated |
| 58 | + expect(response.cipherSuites).to.be.an('array').that.is.not.empty; |
| 59 | + for (const cipher of response.cipherSuites) { |
| 60 | + expect(cipher).to.have.property('id').that.is.a('number'); |
| 61 | + expect(cipher).to.have.property('name'); |
| 62 | + } |
| 63 | + |
| 64 | + // Compression methods — modern TLS only sends null (0) |
| 65 | + expect(response.compressionMethods).to.deep.include({ id: 0, name: 'null' }); |
| 66 | + |
| 67 | + // Extensions are annotated |
| 68 | + expect(response.extensions).to.be.an('array').that.is.not.empty; |
| 69 | + for (const ext of response.extensions) { |
| 70 | + expect(ext).to.have.property('id').that.is.a('number'); |
| 71 | + expect(ext).to.have.property('name'); |
| 72 | + expect(ext).to.have.property('data'); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it("includes server_name extension with correct hostname", async () => { |
| 77 | + const response = await new Promise<any>((resolve, reject) => { |
| 78 | + https.get(`https://localhost:${serverPort}/tls/client-hello`, { |
| 79 | + rejectUnauthorized: false |
| 80 | + }, async (res) => { |
| 81 | + try { |
| 82 | + const body = await streamConsumers.text(res); |
| 83 | + resolve(JSON.parse(body)); |
| 84 | + } catch (e) { |
| 85 | + reject(e); |
| 86 | + } |
| 87 | + }).on('error', reject); |
| 88 | + }); |
| 89 | + |
| 90 | + const sniExt = response.extensions.find((e: any) => e.id === 0); |
| 91 | + expect(sniExt).to.exist; |
| 92 | + expect(sniExt.name).to.equal('server_name'); |
| 93 | + expect(sniExt.data).to.deep.equal({ serverName: 'localhost' }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("annotates IDs within extension data", async () => { |
| 97 | + const response = await new Promise<any>((resolve, reject) => { |
| 98 | + https.get(`https://localhost:${serverPort}/tls/client-hello`, { |
| 99 | + rejectUnauthorized: false |
| 100 | + }, async (res) => { |
| 101 | + try { |
| 102 | + const body = await streamConsumers.text(res); |
| 103 | + resolve(JSON.parse(body)); |
| 104 | + } catch (e) { |
| 105 | + reject(e); |
| 106 | + } |
| 107 | + }).on('error', reject); |
| 108 | + }); |
| 109 | + |
| 110 | + // supported_versions should have annotated version objects |
| 111 | + const versionsExt = response.extensions.find((e: any) => e.id === 43); |
| 112 | + expect(versionsExt).to.exist; |
| 113 | + expect(versionsExt.name).to.equal('supported_versions'); |
| 114 | + expect(versionsExt.data.versions).to.be.an('array').that.is.not.empty; |
| 115 | + for (const v of versionsExt.data.versions) { |
| 116 | + expect(v).to.have.property('id').that.is.a('number'); |
| 117 | + expect(v).to.have.property('name'); |
| 118 | + } |
| 119 | + |
| 120 | + // supported_groups should have annotated group objects |
| 121 | + const groupsExt = response.extensions.find((e: any) => e.id === 10); |
| 122 | + expect(groupsExt).to.exist; |
| 123 | + expect(groupsExt.data.groups).to.be.an('array').that.is.not.empty; |
| 124 | + for (const g of groupsExt.data.groups) { |
| 125 | + expect(g).to.have.property('id').that.is.a('number'); |
| 126 | + expect(g).to.have.property('name'); |
| 127 | + } |
| 128 | + |
| 129 | + // signature_algorithms should have annotated algorithm objects |
| 130 | + const sigAlgsExt = response.extensions.find((e: any) => e.id === 13); |
| 131 | + expect(sigAlgsExt).to.exist; |
| 132 | + expect(sigAlgsExt.data.algorithms).to.be.an('array').that.is.not.empty; |
| 133 | + for (const a of sigAlgsExt.data.algorithms) { |
| 134 | + expect(a).to.have.property('id').that.is.a('number'); |
| 135 | + expect(a).to.have.property('name'); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it("returns 400 for non-TLS connections", async () => { |
| 140 | + const response = await new Promise<{ status: number; body: any }>((resolve, reject) => { |
| 141 | + http.get(`http://localhost:${serverPort}/tls/client-hello`, async (res) => { |
| 142 | + try { |
| 143 | + const body = await streamConsumers.text(res); |
| 144 | + resolve({ status: res.statusCode!, body: JSON.parse(body) }); |
| 145 | + } catch (e) { |
| 146 | + reject(e); |
| 147 | + } |
| 148 | + }).on('error', reject); |
| 149 | + }); |
| 150 | + |
| 151 | + expect(response.status).to.equal(400); |
| 152 | + expect(response.body.error).to.equal('Not a TLS connection'); |
| 153 | + }); |
| 154 | + |
| 155 | +}); |
0 commit comments