Skip to content

Commit e4ccb50

Browse files
authored
fix(mcp): escape OAuth callback errors (anomalyco#32242)
1 parent 85e278b commit e4ccb50

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ const HTML_SUCCESS = `<!DOCTYPE html>
2626
</body>
2727
</html>`
2828

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+
2938
const HTML_ERROR = (error: string) => `<!DOCTYPE html>
3039
<html>
3140
<head>
@@ -42,7 +51,7 @@ const HTML_ERROR = (error: string) => `<!DOCTYPE html>
4251
<div class="container">
4352
<h1>Authorization Failed</h1>
4453
<p>An error occurred during authorization.</p>
45-
<div class="error">${error}</div>
54+
<div class="error">${escapeHtml(error)}</div>
4655
</div>
4756
</body>
4857
</html>`
@@ -87,7 +96,7 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
8796
// Enforce state parameter presence
8897
if (!state) {
8998
const errorMsg = "Missing required state parameter - potential CSRF attack"
90-
res.writeHead(400, { "Content-Type": "text/html" })
99+
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" })
91100
res.end(HTML_ERROR(errorMsg))
92101
return
93102
}
@@ -101,21 +110,21 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
101110
cleanupStateIndex(state)
102111
pending.reject(new Error(errorMsg))
103112
}
104-
res.writeHead(200, { "Content-Type": "text/html" })
113+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
105114
res.end(HTML_ERROR(errorMsg))
106115
return
107116
}
108117

109118
if (!code) {
110-
res.writeHead(400, { "Content-Type": "text/html" })
119+
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" })
111120
res.end(HTML_ERROR("No authorization code provided"))
112121
return
113122
}
114123

115124
// Validate state parameter
116125
if (!pendingAuths.has(state)) {
117126
const errorMsg = "Invalid or expired state parameter - potential CSRF attack"
118-
res.writeHead(400, { "Content-Type": "text/html" })
127+
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" })
119128
res.end(HTML_ERROR(errorMsg))
120129
return
121130
}
@@ -127,7 +136,7 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
127136
cleanupStateIndex(state)
128137
pending.resolve(code)
129138

130-
res.writeHead(200, { "Content-Type": "text/html" })
139+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
131140
res.end(HTML_SUCCESS)
132141
}
133142

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,30 @@ describe("McpOAuthCallback.ensureRunning", () => {
3131
await McpOAuthCallback.ensureRunning("http://127.0.0.1:18000/custom/callback")
3232
expect(McpOAuthCallback.isRunning()).toBe(true)
3333
})
34+
35+
test("escapes provider error markup in callback HTML", async () => {
36+
const redirectUri = "http://127.0.0.1:18001/custom/callback"
37+
await McpOAuthCallback.ensureRunning(redirectUri)
38+
39+
const error = `<script>alert("xss" & 'more')</script>`
40+
const response = await fetch(
41+
`${redirectUri}?state=test&error=access_denied&error_description=${encodeURIComponent(error)}`,
42+
)
43+
const body = await response.text()
44+
45+
expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8")
46+
expect(body).toContain("&lt;script&gt;alert(&quot;xss&quot; &amp; &#39;more&#39;)&lt;/script&gt;")
47+
expect(body).not.toContain(error)
48+
})
49+
50+
test("keeps normal provider errors readable", async () => {
51+
const redirectUri = "http://127.0.0.1:18002/custom/callback"
52+
await McpOAuthCallback.ensureRunning(redirectUri)
53+
54+
const response = await fetch(
55+
`${redirectUri}?state=test&error=access_denied&error_description=${encodeURIComponent("The user denied access")}`,
56+
)
57+
58+
expect(await response.text()).toContain('<div class="error">The user denied access</div>')
59+
})
3460
})

0 commit comments

Comments
 (0)