Skip to content

Allow OPTIONS requests through domain proxy #7284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface UserProvidedArgs extends UserProvidedCodeArgs {
"trusted-origins"?: string[]
version?: boolean
"proxy-domain"?: string[]
"skip-auth-preflight"?: boolean
"reuse-window"?: boolean
"new-window"?: boolean
"ignore-last-opened"?: boolean
Expand Down Expand Up @@ -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).",
},
"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": {
type: "boolean",
short: "e",
Expand Down
3 changes: 3 additions & 0 deletions src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ export const runCodeServer = async (
logger.info(` - ${plural(args["proxy-domain"].length, "Proxying the following 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) {
logger.info(`Using proxy URI in PORTS tab: ${process.env.VSCODE_PROXY_URI}`)
}
Expand Down
5 changes: 5 additions & 0 deletions src/node/routes/domainProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ router.all(/.*/, async (req, res, next) => {

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.
const isAuthenticated = await authenticated(req)
if (!isAuthenticated) {
Expand Down
4 changes: 3 additions & 1 deletion src/node/routes/pathProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export async function proxy(
): Promise<void> {
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 (!req.params.path || req.params.path === "/") {
const to = self(req)
Expand Down
3 changes: 3 additions & 0 deletions test/unit/node/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ describe("parser", () => {

["--abs-proxy-base-path", "/codeserver/app1"],

"--skip-auth-preflight",

["--session-socket", "/tmp/override-code-server-ipc-socket"],

["--host", "0.0.0.0"],
Expand Down Expand Up @@ -146,6 +148,7 @@ describe("parser", () => {
"bind-addr": "192.169.0.1:8080",
"session-socket": "/tmp/override-code-server-ipc-socket",
"abs-proxy-base-path": "/codeserver/app1",
"skip-auth-preflight": true,
})
})

Expand Down
15 changes: 15 additions & 0 deletions test/unit/node/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ describe("proxy", () => {
const text = await resp.text()
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
Expand Down
Loading