Skip to content

Commit d49b3bf

Browse files
committed
Fix query string being double-encoding over path proxy
Instead of trying to piece together the original URL and re-encode what needs to be re-encoded, strip out the base from the original URL. Fixes #6307.
1 parent e87499c commit d49b3bf

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

src/node/routes/pathProxy.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { Request, Response } from "express"
22
import * as path from "path"
3-
import * as qs from "qs"
43
import * as pluginapi from "../../../typings/pluginapi"
54
import { HttpCode, HttpError } from "../../common/http"
65
import { ensureProxyEnabled, authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http"
76
import { proxy as _proxy } from "../proxy"
87

9-
const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
10-
if (passthroughPath) {
11-
return `http://0.0.0.0:${req.params.port}/${req.originalUrl}`
12-
}
13-
const query = qs.stringify(req.query)
14-
return encodeURI(`http://0.0.0.0:${req.params.port}${req.params[0] || ""}${query ? `?${query}` : ""}`)
8+
const getProxyTarget = (req: Request): string => {
9+
// If there is a base path, strip it out.
10+
const base = (req as any).base || ""
11+
return `http://0.0.0.0:${req.params.port}/${req.originalUrl.slice(base.length)}`
1512
}
1613

1714
export async function proxy(
@@ -34,15 +31,14 @@ export async function proxy(
3431
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
3532
}
3633

34+
// The base is used for rewriting (redirects, target).
3735
if (!opts?.passthroughPath) {
38-
// Absolute redirects need to be based on the subpath when rewriting.
39-
// See proxy.ts.
4036
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
4137
}
4238

4339
_proxy.web(req, res, {
4440
ignorePath: true,
45-
target: getProxyTarget(req, opts?.passthroughPath),
41+
target: getProxyTarget(req),
4642
})
4743
}
4844

@@ -55,8 +51,14 @@ export async function wsProxy(
5551
ensureProxyEnabled(req)
5652
ensureOrigin(req)
5753
await ensureAuthenticated(req)
54+
55+
// The base is used for rewriting (redirects, target).
56+
if (!opts?.passthroughPath) {
57+
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
58+
}
59+
5860
_proxy.ws(req, req.ws, req.head, {
5961
ignorePath: true,
60-
target: getProxyTarget(req, opts?.passthroughPath),
62+
target: getProxyTarget(req),
6163
})
6264
}

test/unit/node/proxy.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,54 @@ describe("proxy", () => {
208208
const json = await resp.json()
209209
expect(json).toBe("ほげ")
210210
})
211+
212+
it("should not double-encode query variables", async () => {
213+
const spy = jest.fn()
214+
e.get("*", (req, res) => {
215+
spy([req.originalUrl, req.query])
216+
res.end()
217+
})
218+
codeServer = await integration.setup(["--auth=none"], "")
219+
for (const test of [
220+
{
221+
endpoint: proxyPath,
222+
query: { foo: "bar with spaces" },
223+
expected: "/wsup?foo=bar+with+spaces",
224+
},
225+
{
226+
endpoint: absProxyPath,
227+
query: { foo: "bar with spaces" },
228+
expected: absProxyPath + "?foo=bar+with+spaces",
229+
},
230+
{
231+
endpoint: proxyPath,
232+
query: { foo: "with-&-ampersand" },
233+
expected: "/wsup?foo=with-%26-ampersand",
234+
},
235+
{
236+
endpoint: absProxyPath,
237+
query: { foo: "with-&-ampersand" },
238+
expected: absProxyPath + "?foo=with-%26-ampersand",
239+
},
240+
{
241+
endpoint: absProxyPath,
242+
query: { foo: "ほげ ほげ" },
243+
expected: absProxyPath + "?foo=%E3%81%BB%E3%81%92+%E3%81%BB%E3%81%92",
244+
},
245+
{
246+
endpoint: proxyPath,
247+
query: { foo: "ほげ ほげ" },
248+
expected: "/wsup?foo=%E3%81%BB%E3%81%92+%E3%81%BB%E3%81%92",
249+
},
250+
]) {
251+
spy.mockClear()
252+
const resp = await codeServer.fetch(test.endpoint, undefined, test.query)
253+
expect(resp.status).toBe(200)
254+
await resp.text()
255+
expect(spy).toHaveBeenCalledTimes(1)
256+
expect(spy).toHaveBeenCalledWith([test.expected, test.query])
257+
}
258+
})
211259
})
212260

213261
// NOTE@jsjoeio

0 commit comments

Comments
 (0)