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