GeezerVison is showing github.com — chore(opencode): consolidate escape logic (#32360) · rodboev/opencode@a774c62
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/oauth-callback.ts +1-9Lines changed: 1 addition & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createConnection } from "net" | 2 | 2 | import { createServer } from "http" | | 3 | +import { escapeHtml } from "@/util/html" | 3 | 4 | import { OAUTH_CALLBACK_PORT, OAUTH_CALLBACK_PATH, parseRedirectUri } from "./oauth-provider" | 4 | 5 |
| 5 | 6 | // Current callback server configuration (may differ from defaults if custom redirectUri is used) | @@ -26,15 +27,6 @@ const HTML_SUCCESS = `<!DOCTYPE html> | 26 | 27 | </body> | 27 | 28 | </html>` | 28 | 29 |
| 29 | | -function escapeHtml(value: string) { | 30 | | - return value | 31 | | - .replaceAll("&", "&") | 32 | | - .replaceAll("<", "<") | 33 | | - .replaceAll(">", ">") | 34 | | - .replaceAll('"', """) | 35 | | - .replaceAll("'", "'") | 36 | | -} | 37 | | - | 38 | 30 | const HTML_ERROR = (error: string) => `<!DOCTYPE html> | 39 | 31 | <html> | 40 | 32 | <head> | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/openai/codex.ts +10-9Lines changed: 10 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import os from "os" | 5 | 5 | import { setTimeout as sleep } from "node:timers/promises" | 6 | 6 | import { createServer } from "http" | 7 | 7 | import { OpenAIWebSocketPool } from "./ws-pool" | | 8 | +import { escapeHtml } from "@/util/html" | 8 | 9 |
| 9 | 10 | const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann" | 10 | 11 | const ISSUER = "https://auth.openai.com" | @@ -178,7 +179,7 @@ const HTML_SUCCESS = `<!doctype html> | 178 | 179 | </body> | 179 | 180 | </html>` | 180 | 181 |
| 181 | | -const HTML_ERROR = (error: string) => `<!doctype html> | | 182 | +export const renderOAuthError = (error: string) => `<!doctype html> | 182 | 183 | <html> | 183 | 184 | <head> | 184 | 185 | <title>OpenCode - Codex Authorization Failed</title> | @@ -221,7 +222,7 @@ const HTML_ERROR = (error: string) => `<!doctype html> | 221 | 222 | <div class="container"> | 222 | 223 | <h1>Authorization Failed</h1> | 223 | 224 | <p>An error occurred during authorization.</p> | 224 | | - <div class="error">${error}</div> | | 225 | + <div class="error">${escapeHtml(error)}</div> | 225 | 226 | </div> | 226 | 227 | </body> | 227 | 228 | </html>` | @@ -254,26 +255,26 @@ async function startOAuthServer(): Promise<{ port: number; redirectUri: string } | 254 | 255 | const errorMsg = errorDescription || error | 255 | 256 | pendingOAuth?.reject(new Error(errorMsg)) | 256 | 257 | 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)) | 259 | 260 | return | 260 | 261 | } | 261 | 262 |
| 262 | 263 | if (!code) { | 263 | 264 | const errorMsg = "Missing authorization code" | 264 | 265 | pendingOAuth?.reject(new Error(errorMsg)) | 265 | 266 | 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)) | 268 | 269 | return | 269 | 270 | } | 270 | 271 |
| 271 | 272 | if (!pendingOAuth || state !== pendingOAuth.state) { | 272 | 273 | const errorMsg = "Invalid state - potential CSRF attack" | 273 | 274 | pendingOAuth?.reject(new Error(errorMsg)) | 274 | 275 | 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)) | 277 | 278 | return | 278 | 279 | } | 279 | 280 |
| @@ -284,7 +285,7 @@ async function startOAuthServer(): Promise<{ port: number; redirectUri: string } | 284 | 285 | .then((tokens) => current.resolve(tokens)) | 285 | 286 | .catch((err) => current.reject(err)) | 286 | 287 |
| 287 | | - res.writeHead(200, { "Content-Type": "text/html" }) | | 288 | + res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }) | 288 | 289 | res.end(HTML_SUCCESS) | 289 | 290 | return | 290 | 291 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/xai.ts +1-19Lines changed: 1 addition & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import type { Hooks, PluginInput } from "@opencode-ai/plugin" | 2 | 2 | import { OAUTH_DUMMY_KEY } from "../auth" | 3 | 3 | import { createServer } from "http" | 4 | 4 | import { InstallationVersion } from "@opencode-ai/core/installation/version" | | 5 | +import { escapeHtml } from "@/util/html" | 5 | 6 |
| 6 | 7 | // Public Grok-CLI OAuth client. xAI's auth server rejects loopback OAuth from | 7 | 8 | // non-allowlisted clients, so we reuse the Grok-CLI client_id that xAI ships | @@ -74,25 +75,6 @@ function generateState(): string { | 74 | 75 | return base64UrlEncode(crypto.getRandomValues(new Uint8Array(32)).buffer) | 75 | 76 | } | 76 | 77 |
| 77 | | -export function escapeHtml(value: string): string { | 78 | | - return value.replace(/[&<>"']/g, (char) => { | 79 | | - switch (char) { | 80 | | - case "&": | 81 | | - return "&" | 82 | | - case "<": | 83 | | - return "<" | 84 | | - case ">": | 85 | | - return ">" | 86 | | - case '"': | 87 | | - return """ | 88 | | - case "'": | 89 | | - return "'" | 90 | | - default: | 91 | | - return char | 92 | | - } | 93 | | - }) | 94 | | -} | 95 | | - | 96 | 78 | interface TokenResponse { | 97 | 79 | access_token: string | 98 | 80 | refresh_token: string | |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export function escapeHtml(value: string) { | | 2 | + return value | | 3 | + .replaceAll("&", "&") | | 4 | + .replaceAll("<", "<") | | 5 | + .replaceAll(">", ">") | | 6 | + .replaceAll('"', """) | | 7 | + .replaceAll("'", "'") | | 8 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/plugin/codex.test.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | parseJwtClaims, | 5 | 5 | extractAccountIdFromClaims, | 6 | 6 | extractAccountId, | | 7 | + renderOAuthError, | 7 | 8 | type IdTokenClaims, | 8 | 9 | } from "../../src/plugin/openai/codex" | 9 | 10 |
| @@ -14,6 +15,14 @@ function createTestJwt(payload: object): string { | 14 | 15 | } | 15 | 16 |
| 16 | 17 | 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("</div><script>alert("xss" & 'more')</script>") | | 23 | + expect(html).not.toContain(error) | | 24 | + }) | | 25 | + | 17 | 26 | describe("parseJwtClaims", () => { | 18 | 27 | test("parses valid JWT with claims", () => { | 19 | 28 | | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/plugin/xai.test.ts -14Lines changed: 0 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,6 @@ import { describe, expect, test } from "bun:test" | 2 | 2 | import { | 3 | 3 | accessTokenIsExpiring, | 4 | 4 | buildAuthorizeUrl, | 5 | | - escapeHtml, | 6 | 5 | pollDeviceCodeToken, | 7 | 6 | requestDeviceCode, | 8 | 7 | XaiAuthPlugin, | @@ -103,19 +102,6 @@ describe("plugin.xai", () => { | 103 | 102 | }) | 104 | 103 | }) | 105 | 104 |
| 106 | | - describe("escapeHtml", () => { | 107 | | - test("escapes HTML metacharacters", () => { | 108 | | - expect(escapeHtml(`</div><script>alert(1)</script><div class="x">`)).toBe( | 109 | | - "</div><script>alert(1)</script><div class="x">", | 110 | | - ) | 111 | | - expect(escapeHtml("a & b")).toBe("a & b") | 112 | | - expect(escapeHtml("it's fine")).toBe("it's fine") | 113 | | - expect(escapeHtml("invalid_grant")).toBe("invalid_grant") | 114 | | - expect(escapeHtml("")).toBe("") | 115 | | - expect(escapeHtml("&<")).toBe("&<") | 116 | | - }) | 117 | | - }) | 118 | | - | 119 | 105 | describe("loader", () => { | 120 | 106 | test("returns no options unless stored auth is OAuth and exposes methods in order", async () => { | 121 | 107 | const hooks = await XaiAuthPlugin({} as any) | |
Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + "</div><script>alert(1)</script><div class="x">", | | 8 | + ) | | 9 | + expect(escapeHtml("a & b")).toBe("a & b") | | 10 | + expect(escapeHtml("it's fine")).toBe("it's fine") | | 11 | + expect(escapeHtml("invalid_grant")).toBe("invalid_grant") | | 12 | + expect(escapeHtml("")).toBe("") | | 13 | + expect(escapeHtml("&<")).toBe("&<") | | 14 | + }) | | 15 | +}) |
|