|
| 1 | +import * as net from 'net'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 4 | + |
| 5 | +import { createServer } from '../src/server.js'; |
| 6 | + |
| 7 | +describe("TLS-required endpoints over plain HTTP", () => { |
| 8 | + |
| 9 | + let server: DestroyableServer; |
| 10 | + let serverPort: number; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + server = makeDestroyable(await createServer()); |
| 14 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 15 | + serverPort = (server.address() as net.AddressInfo).port; |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async () => { |
| 19 | + await server.destroy(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("redirects plain HTTP to HTTPS for TLS-configuring subdomains", () => { |
| 23 | + |
| 24 | + for (const subdomain of [ |
| 25 | + 'tls-v1-0', |
| 26 | + 'tls-v1-1', |
| 27 | + 'tls-v1-2', |
| 28 | + 'tls-v1-3', |
| 29 | + 'expired', |
| 30 | + 'revoked', |
| 31 | + 'self-signed', |
| 32 | + 'untrusted-root', |
| 33 | + 'wrong-host', |
| 34 | + 'http2', |
| 35 | + 'http1' |
| 36 | + ]) { |
| 37 | + it(`redirects http://${subdomain}.* to HTTPS`, async () => { |
| 38 | + const response = await fetch( |
| 39 | + `http://${subdomain}.localhost:${serverPort}/status/200`, |
| 40 | + { redirect: 'manual' } |
| 41 | + ); |
| 42 | + |
| 43 | + expect(response.status).to.equal(301); |
| 44 | + expect(response.headers.get('location')).to.equal( |
| 45 | + `https://${subdomain}.localhost:${serverPort}/status/200` |
| 46 | + ); |
| 47 | + |
| 48 | + const body = await response.text(); |
| 49 | + expect(body).to.include('requires HTTPS'); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + }); |
| 54 | + |
| 55 | + it("redirects combined TLS subdomains to HTTPS", async () => { |
| 56 | + const response = await fetch( |
| 57 | + `http://tls-v1-2--expired.localhost:${serverPort}/status/200`, |
| 58 | + { redirect: 'manual' } |
| 59 | + ); |
| 60 | + |
| 61 | + expect(response.status).to.equal(301); |
| 62 | + expect(response.headers.get('location')).to.equal( |
| 63 | + `https://tls-v1-2--expired.localhost:${serverPort}/status/200` |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("preserves the full path and query in the redirect", async () => { |
| 68 | + const response = await fetch( |
| 69 | + `http://tls-v1-2.localhost:${serverPort}/anything?foo=bar`, |
| 70 | + { redirect: 'manual' } |
| 71 | + ); |
| 72 | + |
| 73 | + expect(response.status).to.equal(301); |
| 74 | + expect(response.headers.get('location')).to.equal( |
| 75 | + `https://tls-v1-2.localhost:${serverPort}/anything?foo=bar` |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("allows plain HTTP for plainTextAllowed subdomains", () => { |
| 80 | + |
| 81 | + it("allows http://example.* requests", async () => { |
| 82 | + const response = await fetch( |
| 83 | + `http://example.localhost:${serverPort}/` |
| 84 | + ); |
| 85 | + |
| 86 | + expect(response.status).to.equal(200); |
| 87 | + }); |
| 88 | + |
| 89 | + it("allows http://no-tls.* requests", async () => { |
| 90 | + const response = await fetch( |
| 91 | + `http://no-tls.localhost:${serverPort}/status/200` |
| 92 | + ); |
| 93 | + |
| 94 | + expect(response.status).to.equal(200); |
| 95 | + }); |
| 96 | + |
| 97 | + }); |
| 98 | + |
| 99 | + it("allows plain HTTP for requests with no subdomain", async () => { |
| 100 | + const response = await fetch( |
| 101 | + `http://localhost:${serverPort}/status/200` |
| 102 | + ); |
| 103 | + |
| 104 | + expect(response.status).to.equal(200); |
| 105 | + }); |
| 106 | + |
| 107 | +}); |
0 commit comments