mirror of
https://github.com/cdr/code-server.git
synced 2025-12-06 08:27:17 +01:00
Add skip-auth-preflight flag to allow OPTIONS requests through proxy (#7284)
This commit is contained in:
parent
9045919d2b
commit
bbf2e24648
6 changed files with 34 additions and 1 deletions
|
|
@ -84,6 +84,7 @@ export interface UserProvidedArgs extends UserProvidedCodeArgs {
|
||||||
"trusted-origins"?: string[]
|
"trusted-origins"?: string[]
|
||||||
version?: boolean
|
version?: boolean
|
||||||
"proxy-domain"?: string[]
|
"proxy-domain"?: string[]
|
||||||
|
"skip-auth-preflight"?: boolean
|
||||||
"reuse-window"?: boolean
|
"reuse-window"?: boolean
|
||||||
"new-window"?: boolean
|
"new-window"?: boolean
|
||||||
"ignore-last-opened"?: boolean
|
"ignore-last-opened"?: boolean
|
||||||
|
|
@ -252,6 +253,10 @@ export const options: Options<Required<UserProvidedArgs>> = {
|
||||||
description: "GitHub authentication token (can only be passed in via $GITHUB_TOKEN or the config file).",
|
description: "GitHub authentication token (can only be passed in via $GITHUB_TOKEN or the config file).",
|
||||||
},
|
},
|
||||||
"proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
|
"proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
|
||||||
|
"skip-auth-preflight": {
|
||||||
|
type: "boolean",
|
||||||
|
description: "Allows preflight requests through proxy without authentication.",
|
||||||
|
},
|
||||||
"ignore-last-opened": {
|
"ignore-last-opened": {
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
short: "e",
|
short: "e",
|
||||||
|
|
|
||||||
|
|
@ -163,6 +163,9 @@ export const runCodeServer = async (
|
||||||
logger.info(` - ${plural(args["proxy-domain"].length, "Proxying the following domain")}:`)
|
logger.info(` - ${plural(args["proxy-domain"].length, "Proxying the following domain")}:`)
|
||||||
args["proxy-domain"].forEach((domain) => logger.info(` - ${domain}`))
|
args["proxy-domain"].forEach((domain) => logger.info(` - ${domain}`))
|
||||||
}
|
}
|
||||||
|
if (args["skip-auth-preflight"]) {
|
||||||
|
logger.info(" - Skipping authentication for preflight requests")
|
||||||
|
}
|
||||||
if (process.env.VSCODE_PROXY_URI) {
|
if (process.env.VSCODE_PROXY_URI) {
|
||||||
logger.info(`Using proxy URI in PORTS tab: ${process.env.VSCODE_PROXY_URI}`)
|
logger.info(`Using proxy URI in PORTS tab: ${process.env.VSCODE_PROXY_URI}`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,11 @@ router.all(/.*/, async (req, res, next) => {
|
||||||
|
|
||||||
ensureProxyEnabled(req)
|
ensureProxyEnabled(req)
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS" && req.args["skip-auth-preflight"]) {
|
||||||
|
// Allow preflight requests with `skip-auth-preflight` flag
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
// Must be authenticated to use the proxy.
|
// Must be authenticated to use the proxy.
|
||||||
const isAuthenticated = await authenticated(req)
|
const isAuthenticated = await authenticated(req)
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,9 @@ export async function proxy(
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
ensureProxyEnabled(req)
|
ensureProxyEnabled(req)
|
||||||
|
|
||||||
if (!(await authenticated(req))) {
|
if (req.method === "OPTIONS" && req.args["skip-auth-preflight"]) {
|
||||||
|
// Allow preflight requests with `skip-auth-preflight` flag
|
||||||
|
} else if (!(await authenticated(req))) {
|
||||||
// If visiting the root (/:port only) redirect to the login page.
|
// If visiting the root (/:port only) redirect to the login page.
|
||||||
if (!req.params.path || req.params.path === "/") {
|
if (!req.params.path || req.params.path === "/") {
|
||||||
const to = self(req)
|
const to = self(req)
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,8 @@ describe("parser", () => {
|
||||||
|
|
||||||
["--abs-proxy-base-path", "/codeserver/app1"],
|
["--abs-proxy-base-path", "/codeserver/app1"],
|
||||||
|
|
||||||
|
"--skip-auth-preflight",
|
||||||
|
|
||||||
["--session-socket", "/tmp/override-code-server-ipc-socket"],
|
["--session-socket", "/tmp/override-code-server-ipc-socket"],
|
||||||
|
|
||||||
["--host", "0.0.0.0"],
|
["--host", "0.0.0.0"],
|
||||||
|
|
@ -146,6 +148,7 @@ describe("parser", () => {
|
||||||
"bind-addr": "192.169.0.1:8080",
|
"bind-addr": "192.169.0.1:8080",
|
||||||
"session-socket": "/tmp/override-code-server-ipc-socket",
|
"session-socket": "/tmp/override-code-server-ipc-socket",
|
||||||
"abs-proxy-base-path": "/codeserver/app1",
|
"abs-proxy-base-path": "/codeserver/app1",
|
||||||
|
"skip-auth-preflight": true,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -268,6 +268,21 @@ describe("proxy", () => {
|
||||||
const text = await resp.text()
|
const text = await resp.text()
|
||||||
expect(text).toBe("app being served behind a prefixed path")
|
expect(text).toBe("app being served behind a prefixed path")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should not allow OPTIONS without authentication by default", async () => {
|
||||||
|
process.env.PASSWORD = "test"
|
||||||
|
codeServer = await integration.setup(["--auth=password"])
|
||||||
|
const resp = await codeServer.fetch(proxyPath, { method: "OPTIONS" })
|
||||||
|
expect(resp.status).toBe(401)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should allow OPTIONS with `skip-auth-preflight` flag", async () => {
|
||||||
|
process.env.PASSWORD = "test"
|
||||||
|
codeServer = await integration.setup(["--auth=password", "--skip-auth-preflight"])
|
||||||
|
e.post("/wsup", (req, res) => {})
|
||||||
|
const resp = await codeServer.fetch(proxyPath, { method: "OPTIONS" })
|
||||||
|
expect(resp.status).toBe(200)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// NOTE@jsjoeio
|
// NOTE@jsjoeio
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue