GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(console): validate auth redirects (#45027) · litui/opencode@ba4d0ea

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bun.lock
  • packages/console/function
    • package.json
    • src
      • auth-redirect.test.ts
      • auth-redirect.ts
      • auth.ts
    • tsconfig.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bun.lock‎

Copy file name to clipboardExpand all lines: bun.lock
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎packages/console/function/package.json‎

Copy file name to clipboardExpand all lines: packages/console/function/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"devDependencies": {
1212
"@cloudflare/workers-types": "catalog:",
1313
"@tsconfig/node22": "22.0.2",
14+
"@types/bun": "catalog:",
1415
"@types/node": "catalog:",
1516
"openai": "5.11.0",
1617
"typescript": "catalog:",
Collapse file

‎packages/console/function/src/auth-redirect.test.ts‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

‎packages/console/function/src/auth-redirect.ts‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

‎packages/console/function/src/auth.ts‎

Copy file name to clipboardExpand all lines: packages/console/function/src/auth.ts
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.j
1717
import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
1818
import { AuthTable } from "@opencode-ai/console-core/schema/auth.sql.js"
1919
import { Identifier } from "@opencode-ai/console-core/identifier.js"
20+
import { isAllowedAuthorizationRedirect } from "./auth-redirect.js"
2021

2122
type Env = {
2223
AuthStorage: KVNamespace
@@ -41,6 +42,17 @@ const MY_THEME: Theme = {
4142

4243
export default {
4344
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+
4456
const result = await issuer({
4557
theme: MY_THEME,
4658
providers: {
@@ -102,6 +114,7 @@ export default {
102114
namespace: env.AuthStorage,
103115
}),
104116
subjects,
117+
allow: ({ clientID, redirectURI }) => Promise.resolve(isAllowedAuthorizationRedirect(clientID, redirectURI)),
105118
async success(ctx, response) {
106119
console.log(response)
107120

Collapse file

‎packages/console/function/tsconfig.json‎

Copy file name to clipboardExpand all lines: packages/console/function/tsconfig.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"moduleResolution": "bundler",
77
"jsx": "preserve",
88
"jsxImportSource": "react",
9-
"types": ["@cloudflare/workers-types", "node"]
9+
"types": ["@cloudflare/workers-types", "bun", "node"]
1010
}
1111
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.