GeezerVison is showing github.com — fix(console): validate auth redirects (#45027) · litui/opencode@ba4d0ea
File treeExpand file treeCollapse file tree Open diff view settings packages/console/function
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +1Lines changed: 1 addition & 0 deletions Collapse file Copy file name to clipboardExpand all lines: packages/console/function/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | "devDependencies": { | 12 | 12 | "@cloudflare/workers-types": "catalog:", | 13 | 13 | "@tsconfig/node22": "22.0.2", | | 14 | + "@types/bun": "catalog:", | 14 | 15 | "@types/node": "catalog:", | 15 | 16 | "openai": "5.11.0", | 16 | 17 | "typescript": "catalog:", | |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import { isAllowedAuthorizationRedirect } from "./auth-redirect" | | 3 | + | | 4 | +describe("authorization redirect validation", () => { | | 5 | + test("allows registered OpenCode callbacks", () => { | | 6 | + expect(isAllowedAuthorizationRedirect("app", "https://opencode.ai/auth/callback")).toBe(true) | | 7 | + expect(isAllowedAuthorizationRedirect("app", "https://dev.opencode.ai/auth/callback")).toBe(true) | | 8 | + expect(isAllowedAuthorizationRedirect("app", "http://localhost:3000/auth/callback")).toBe(true) | | 9 | + expect(isAllowedAuthorizationRedirect("app", "http://127.0.0.1:3000/auth/callback")).toBe(true) | | 10 | + }) | | 11 | + | | 12 | + test("rejects unregistered clients and external redirects", () => { | | 13 | + expect(isAllowedAuthorizationRedirect("other", "https://opencode.ai/auth/callback")).toBe(false) | | 14 | + expect(isAllowedAuthorizationRedirect("app", "https://evil.example/callback")).toBe(false) | | 15 | + expect(isAllowedAuthorizationRedirect("app", "https://opencode.ai.evil.example/callback")).toBe(false) | | 16 | + expect(isAllowedAuthorizationRedirect("app", "javascript:alert(1)")).toBe(false) | | 17 | + }) | | 18 | +}) |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export const isAllowedAuthorizationRedirect = (clientID: string, redirectURI: string) => { | | 2 | + if (clientID !== "app") return false | | 3 | + const redirect = (() => { | | 4 | + try { | | 5 | + return new URL(redirectURI) | | 6 | + } catch { | | 7 | + return undefined | | 8 | + } | | 9 | + })() | | 10 | + if (redirect === undefined) return false | | 11 | + if (redirect.hostname === "localhost" || redirect.hostname === "127.0.0.1") { | | 12 | + return redirect.protocol === "http:" || redirect.protocol === "https:" | | 13 | + } | | 14 | + return ( | | 15 | + redirect.protocol === "https:" && | | 16 | + (redirect.hostname === "opencode.ai" || redirect.hostname.endsWith(".opencode.ai")) | | 17 | + ) | | 18 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/console/function/src/auth.ts +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.j | 17 | 17 | import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js" | 18 | 18 | import { AuthTable } from "@opencode-ai/console-core/schema/auth.sql.js" | 19 | 19 | import { Identifier } from "@opencode-ai/console-core/identifier.js" | | 20 | +import { isAllowedAuthorizationRedirect } from "./auth-redirect.js" | 20 | 21 |
| 21 | 22 | type Env = { | 22 | 23 | AuthStorage: KVNamespace | @@ -41,6 +42,17 @@ const MY_THEME: Theme = { | 41 | 42 |
| 42 | 43 | export default { | 43 | 44 | async fetch(request: Request, env: Env, ctx: ExecutionContext) { | | 45 | + const requestURL = new URL(request.url) | | 46 | + if (requestURL.pathname === "/authorize") { | | 47 | + const redirectURI = requestURL.searchParams.get("redirect_uri") | | 48 | + if ( | | 49 | + redirectURI !== null && | | 50 | + !isAllowedAuthorizationRedirect(requestURL.searchParams.get("client_id") ?? "", redirectURI) | | 51 | + ) { | | 52 | + return new Response("Unauthorized client", { status: 400 }) | | 53 | + } | | 54 | + } | | 55 | + | 44 | 56 | const result = await issuer({ | 45 | 57 | theme: MY_THEME, | 46 | 58 | providers: { | @@ -102,6 +114,7 @@ export default { | 102 | 114 | namespace: env.AuthStorage, | 103 | 115 | }), | 104 | 116 | subjects, | | 117 | + allow: ({ clientID, redirectURI }) => Promise.resolve(isAllowedAuthorizationRedirect(clientID, redirectURI)), | 105 | 118 | async success(ctx, response) { | 106 | 119 | console.log(response) | 107 | 120 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/console/function/tsconfig.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | "moduleResolution": "bundler", | 7 | 7 | "jsx": "preserve", | 8 | 8 | "jsxImportSource": "react", | 9 | | - "types": ["@cloudflare/workers-types", "node"] | | 9 | + "types": ["@cloudflare/workers-types", "bun", "node"] | 10 | 10 | } | 11 | 11 | } |
|