|
| 1 | +import fs from "node:fs"; |
| 2 | +import http from "node:http"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, beforeEach, describe, it } from "node:test"; |
| 6 | +import { setTimeout } from "node:timers/promises"; |
| 7 | +import { expect } from "expect"; |
| 8 | +import request from "supertest"; |
| 9 | +import webpack from "webpack"; |
| 10 | +import { WebSocketServer } from "ws"; |
| 11 | +import Server from "../../lib/Server.js"; |
| 12 | +import portsMap from "../ports-map.js"; |
| 13 | + |
| 14 | +const [devServerPort, backendPort] = portsMap["proxy-web-socket-origin"]; |
| 15 | + |
| 16 | +// @see https://github.com/webpack/webpack-dev-server/security/advisories/GHSA-cfqc-fj3v-j925 |
| 17 | +describe("proxied WebSocket upgrade origin validation", () => { |
| 18 | + let server; |
| 19 | + let backend; |
| 20 | + let webSocketServer; |
| 21 | + let backendConnections; |
| 22 | + let tmp; |
| 23 | + |
| 24 | + async function startServer(proxy) { |
| 25 | + const compiler = webpack({ |
| 26 | + mode: "development", |
| 27 | + context: tmp, |
| 28 | + entry: "./entry.js", |
| 29 | + output: { path: path.join(tmp, "dist") }, |
| 30 | + }); |
| 31 | + |
| 32 | + server = new Server( |
| 33 | + { |
| 34 | + port: devServerPort, |
| 35 | + host: "localhost", |
| 36 | + allowedHosts: "auto", |
| 37 | + client: false, |
| 38 | + hot: false, |
| 39 | + liveReload: false, |
| 40 | + proxy: [proxy], |
| 41 | + }, |
| 42 | + compiler, |
| 43 | + ); |
| 44 | + |
| 45 | + await server.start(); |
| 46 | + } |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + backendConnections = 0; |
| 50 | + |
| 51 | + // A stand-in for a service the developer proxies over WebSocket. |
| 52 | + backend = http.createServer((req, res) => { |
| 53 | + res.end("backend response"); |
| 54 | + }); |
| 55 | + webSocketServer = new WebSocketServer({ server: backend }); |
| 56 | + webSocketServer.on("connection", (client) => { |
| 57 | + backendConnections += 1; |
| 58 | + client.send("INTERNAL-SERVICE-SECRET"); |
| 59 | + }); |
| 60 | + await new Promise((resolve) => { |
| 61 | + backend.listen(backendPort, "127.0.0.1", resolve); |
| 62 | + }); |
| 63 | + |
| 64 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), "wds-proxy-origin-")); |
| 65 | + fs.writeFileSync(path.join(tmp, "entry.js"), "console.log('e2e');\n"); |
| 66 | + |
| 67 | + await startServer({ |
| 68 | + context: ["/api"], |
| 69 | + target: `http://127.0.0.1:${backendPort}`, |
| 70 | + ws: true, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(async () => { |
| 75 | + await server.stop(); |
| 76 | + for (const client of webSocketServer.clients) { |
| 77 | + client.terminate(); |
| 78 | + } |
| 79 | + webSocketServer.close(); |
| 80 | + backend.closeAllConnections?.(); |
| 81 | + await new Promise((resolve) => { |
| 82 | + backend.close(resolve); |
| 83 | + }); |
| 84 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 85 | + }); |
| 86 | + |
| 87 | + function upgrade(origin) { |
| 88 | + return new Promise((resolve) => { |
| 89 | + const req = http.request({ |
| 90 | + host: "localhost", |
| 91 | + port: devServerPort, |
| 92 | + path: "/api/socket", |
| 93 | + timeout: 8000, |
| 94 | + headers: { |
| 95 | + Connection: "Upgrade", |
| 96 | + Upgrade: "websocket", |
| 97 | + "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==", |
| 98 | + "Sec-WebSocket-Version": "13", |
| 99 | + Origin: origin, |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + req.on("upgrade", (res, socket) => { |
| 104 | + socket.destroy(); |
| 105 | + resolve({ upgraded: true, status: res.statusCode }); |
| 106 | + }); |
| 107 | + req.on("response", (res) => { |
| 108 | + res.resume(); |
| 109 | + resolve({ upgraded: false, status: res.statusCode }); |
| 110 | + }); |
| 111 | + req.on("timeout", () => { |
| 112 | + req.destroy(); |
| 113 | + resolve({ upgraded: false }); |
| 114 | + }); |
| 115 | + req.on("error", () => { |
| 116 | + resolve({ upgraded: false }); |
| 117 | + }); |
| 118 | + req.end(); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + it("should reject a cross-origin proxied WebSocket upgrade", async () => { |
| 123 | + const result = await upgrade("http://evil.example.com"); |
| 124 | + |
| 125 | + expect(result.upgraded).toBe(false); |
| 126 | + expect(result.status).toBe(403); |
| 127 | + expect(backendConnections).toBe(0); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should allow a same-origin proxied WebSocket upgrade", async () => { |
| 131 | + const result = await upgrade(`http://localhost:${devServerPort}`); |
| 132 | + |
| 133 | + expect(result.upgraded).toBe(true); |
| 134 | + expect(result.status).toBe(101); |
| 135 | + expect(backendConnections).toBe(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should not forward a rejected upgrade after an HTTP proxy request", async () => { |
| 139 | + await request(server.server).get("/api/hello").expect(200); |
| 140 | + |
| 141 | + const result = await upgrade("http://evil.example.com"); |
| 142 | + |
| 143 | + expect(result.upgraded).toBe(false); |
| 144 | + expect(result.status).toBe(403); |
| 145 | + |
| 146 | + // A separate upgrade listener can still contact the backend after the |
| 147 | + // client receives the rejection, so allow that forwarding to finish. |
| 148 | + await setTimeout(100); |
| 149 | + |
| 150 | + expect(backendConnections).toBe(0); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should allow a same-origin upgrade after an HTTP proxy request", async () => { |
| 154 | + await request(server.server).get("/api/hello").expect(200); |
| 155 | + |
| 156 | + const result = await upgrade(`http://localhost:${devServerPort}`); |
| 157 | + |
| 158 | + expect(result.upgraded).toBe(true); |
| 159 | + expect(result.status).toBe(101); |
| 160 | + expect(backendConnections).toBe(1); |
| 161 | + }); |
| 162 | + |
| 163 | + it("should validate upgrades using the current proxy callback configuration", async () => { |
| 164 | + await server.stop(); |
| 165 | + |
| 166 | + let proxyConfig = { |
| 167 | + context: ["/api"], |
| 168 | + target: `http://127.0.0.1:${backendPort}`, |
| 169 | + ws: false, |
| 170 | + pathRewrite: { "^/api": "/initial" }, |
| 171 | + }; |
| 172 | + |
| 173 | + await startServer(() => ({ ...proxyConfig })); |
| 174 | + await request(server.server).get("/api/hello").expect(200); |
| 175 | + |
| 176 | + proxyConfig = { |
| 177 | + ...proxyConfig, |
| 178 | + ws: true, |
| 179 | + pathRewrite: { "^/api": "/updated" }, |
| 180 | + }; |
| 181 | + |
| 182 | + await request(server.server).get("/api/hello").expect(200); |
| 183 | + |
| 184 | + const rejected = await upgrade("http://evil.example.com"); |
| 185 | + |
| 186 | + expect(rejected.status).toBe(403); |
| 187 | + |
| 188 | + await setTimeout(100); |
| 189 | + |
| 190 | + expect(backendConnections).toBe(0); |
| 191 | + |
| 192 | + let backendPath; |
| 193 | + |
| 194 | + backend.once("upgrade", (req) => { |
| 195 | + backendPath = req.url; |
| 196 | + }); |
| 197 | + |
| 198 | + const allowed = await upgrade(`http://localhost:${devServerPort}`); |
| 199 | + |
| 200 | + expect(allowed.status).toBe(101); |
| 201 | + expect(backendConnections).toBe(1); |
| 202 | + expect(backendPath).toBe("/updated/socket"); |
| 203 | + }); |
| 204 | +}); |
0 commit comments