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 — chore(opencode): consolidate escape logic (#32360) · comio/opencode@a774c62

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/opencode
    • src
      • mcp
        • oauth-callback.ts
      • plugin
        • openai
          • codex.ts
        • xai.ts
      • util
        • html.ts
    • test
      • plugin
        • codex.test.ts
        • xai.test.ts
      • util
        • html.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/mcp/oauth-callback.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/oauth-callback.ts
+1-9Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createConnection } from "net"
22
import { createServer } from "http"
3+
import { escapeHtml } from "@/util/html"
34
import { OAUTH_CALLBACK_PORT, OAUTH_CALLBACK_PATH, parseRedirectUri } from "./oauth-provider"
45

56
// Current callback server configuration (may differ from defaults if custom redirectUri is used)
@@ -26,15 +27,6 @@ const HTML_SUCCESS = `<!DOCTYPE html>
2627
</body>
2728
</html>`
2829

29-
function escapeHtml(value: string) {
30-
return value
31-
.replaceAll("&", "&amp;")
32-
.replaceAll("<", "&lt;")
33-
.replaceAll(">", "&gt;")
34-
.replaceAll('"', "&quot;")
35-
.replaceAll("'", "&#39;")
36-
}
37-
3830
const HTML_ERROR = (error: string) => `<!DOCTYPE html>
3931
<html>
4032
<head>
Collapse file

‎packages/opencode/src/plugin/openai/codex.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/openai/codex.ts
+10-9Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os from "os"
55
import { setTimeout as sleep } from "node:timers/promises"
66
import { createServer } from "http"
77
import { OpenAIWebSocketPool } from "./ws-pool"
8+
import { escapeHtml } from "@/util/html"
89

910
const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
1011
const ISSUER = "https://auth.openai.com"
@@ -178,7 +179,7 @@ const HTML_SUCCESS = `<!doctype html>
178179
</body>
179180
</html>`
180181

181-
const HTML_ERROR = (error: string) => `<!doctype html>
182+
export const renderOAuthError = (error: string) => `<!doctype html>
182183
<html>
183184
<head>
184185
<title>OpenCode - Codex Authorization Failed</title>
@@ -221,7 +222,7 @@ const HTML_ERROR = (error: string) => `<!doctype html>
221222
<div class="container">
222223
<h1>Authorization Failed</h1>
223224
<p>An error occurred during authorization.</p>
224-
<div class="error">${error}</div>
225+
<div class="error">${escapeHtml(error)}</div>
225226
</div>
226227
</body>
227228
</html>`
@@ -254,26 +255,26 @@ async function startOAuthServer(): Promise<{ port: number; redirectUri: string }
254255
const errorMsg = errorDescription || error
255256
pendingOAuth?.reject(new Error(errorMsg))
256257
pendingOAuth = undefined
257-
res.writeHead(200, { "Content-Type": "text/html" })
258-
res.end(HTML_ERROR(errorMsg))
258+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
259+
res.end(renderOAuthError(errorMsg))
259260
return
260261
}
261262

262263
if (!code) {
263264
const errorMsg = "Missing authorization code"
264265
pendingOAuth?.reject(new Error(errorMsg))
265266
pendingOAuth = undefined
266-
res.writeHead(400, { "Content-Type": "text/html" })
267-
res.end(HTML_ERROR(errorMsg))
267+
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" })
268+
res.end(renderOAuthError(errorMsg))
268269
return
269270
}
270271

271272
if (!pendingOAuth || state !== pendingOAuth.state) {
272273
const errorMsg = "Invalid state - potential CSRF attack"
273274
pendingOAuth?.reject(new Error(errorMsg))
274275
pendingOAuth = undefined
275-
res.writeHead(400, { "Content-Type": "text/html" })
276-
res.end(HTML_ERROR(errorMsg))
276+
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" })
277+
res.end(renderOAuthError(errorMsg))
277278
return
278279
}
279280

@@ -284,7 +285,7 @@ async function startOAuthServer(): Promise<{ port: number; redirectUri: string }
284285
.then((tokens) => current.resolve(tokens))
285286
.catch((err) => current.reject(err))
286287

287-
res.writeHead(200, { "Content-Type": "text/html" })
288+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
288289
res.end(HTML_SUCCESS)
289290
return
290291
}
Collapse file

‎packages/opencode/src/plugin/xai.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/xai.ts
+1-19Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Hooks, PluginInput } from "@opencode-ai/plugin"
22
import { OAUTH_DUMMY_KEY } from "../auth"
33
import { createServer } from "http"
44
import { InstallationVersion } from "@opencode-ai/core/installation/version"
5+
import { escapeHtml } from "@/util/html"
56

67
// Public Grok-CLI OAuth client. xAI's auth server rejects loopback OAuth from
78
// non-allowlisted clients, so we reuse the Grok-CLI client_id that xAI ships
@@ -74,25 +75,6 @@ function generateState(): string {
7475
return base64UrlEncode(crypto.getRandomValues(new Uint8Array(32)).buffer)
7576
}
7677

77-
export function escapeHtml(value: string): string {
78-
return value.replace(/[&<>"']/g, (char) => {
79-
switch (char) {
80-
case "&":
81-
return "&amp;"
82-
case "<":
83-
return "&lt;"
84-
case ">":
85-
return "&gt;"
86-
case '"':
87-
return "&quot;"
88-
case "'":
89-
return "&#39;"
90-
default:
91-
return char
92-
}
93-
})
94-
}
95-
9678
interface TokenResponse {
9779
access_token: string
9880
refresh_token: string
Collapse file

‎packages/opencode/src/util/html.ts‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function escapeHtml(value: string) {
2+
return value
3+
.replaceAll("&", "&amp;")
4+
.replaceAll("<", "&lt;")
5+
.replaceAll(">", "&gt;")
6+
.replaceAll('"', "&quot;")
7+
.replaceAll("'", "&#39;")
8+
}
Collapse file

‎packages/opencode/test/plugin/codex.test.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/test/plugin/codex.test.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
parseJwtClaims,
55
extractAccountIdFromClaims,
66
extractAccountId,
7+
renderOAuthError,
78
type IdTokenClaims,
89
} from "../../src/plugin/openai/codex"
910

@@ -14,6 +15,14 @@ function createTestJwt(payload: object): string {
1415
}
1516

1617
describe("plugin.codex", () => {
18+
test("escapes provider errors in callback HTML", () => {
19+
const error = `</div><script>alert("xss" & 'more')</script>`
20+
const html = renderOAuthError(error)
21+
22+
expect(html).toContain("&lt;/div&gt;&lt;script&gt;alert(&quot;xss&quot; &amp; &#39;more&#39;)&lt;/script&gt;")
23+
expect(html).not.toContain(error)
24+
})
25+
1726
describe("parseJwtClaims", () => {
1827
test("parses valid JWT with claims", () => {
1928
const payload = { email: "[email protected]", chatgpt_account_id: "acc-123" }
Collapse file

‎packages/opencode/test/plugin/xai.test.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/test/plugin/xai.test.ts
-14Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, expect, test } from "bun:test"
22
import {
33
accessTokenIsExpiring,
44
buildAuthorizeUrl,
5-
escapeHtml,
65
pollDeviceCodeToken,
76
requestDeviceCode,
87
XaiAuthPlugin,
@@ -103,19 +102,6 @@ describe("plugin.xai", () => {
103102
})
104103
})
105104

106-
describe("escapeHtml", () => {
107-
test("escapes HTML metacharacters", () => {
108-
expect(escapeHtml(`</div><script>alert(1)</script><div class="x">`)).toBe(
109-
"&lt;/div&gt;&lt;script&gt;alert(1)&lt;/script&gt;&lt;div class=&quot;x&quot;&gt;",
110-
)
111-
expect(escapeHtml("a & b")).toBe("a &amp; b")
112-
expect(escapeHtml("it's fine")).toBe("it&#39;s fine")
113-
expect(escapeHtml("invalid_grant")).toBe("invalid_grant")
114-
expect(escapeHtml("")).toBe("")
115-
expect(escapeHtml("&<")).toBe("&amp;&lt;")
116-
})
117-
})
118-
119105
describe("loader", () => {
120106
test("returns no options unless stored auth is OAuth and exposes methods in order", async () => {
121107
const hooks = await XaiAuthPlugin({} as any)
Collapse file

‎packages/opencode/test/util/html.test.ts‎

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { escapeHtml } from "../../src/util/html"
3+
4+
describe("escapeHtml", () => {
5+
test("escapes HTML metacharacters", () => {
6+
expect(escapeHtml(`</div><script>alert(1)</script><div class="x">`)).toBe(
7+
"&lt;/div&gt;&lt;script&gt;alert(1)&lt;/script&gt;&lt;div class=&quot;x&quot;&gt;",
8+
)
9+
expect(escapeHtml("a & b")).toBe("a &amp; b")
10+
expect(escapeHtml("it's fine")).toBe("it&#39;s fine")
11+
expect(escapeHtml("invalid_grant")).toBe("invalid_grant")
12+
expect(escapeHtml("")).toBe("")
13+
expect(escapeHtml("&<")).toBe("&amp;&lt;")
14+
})
15+
})

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.