7d9d478baa
在 arcrun-mcp worker 實作 MCP Authorization 規範(OAuth 2.1 + PKCE S256), 讓 claude.ai 遠端 connector 安全登入;並修掉「Bearer 明碼 namespace 直接放行」漏洞。 安全模型 - /authorize 同意頁以 owner secret(CF Secrets MCP_OWNER_SECRET)把關,只有 owner 知道 → 只知 URL 的人走不完 OAuth、拿不到 token。 - access_token 是 /mcp 唯一接受的 bearer(預設);明碼 namespace 舊路徑移除(步驟 5 直接 401)。 實作 endpoint(掛 worker 根路徑) - RFC 9728 /.well-known/oauth-protected-resource(+/mcp 變體)+ 401 帶 WWW-Authenticate: Bearer resource_metadata=... - RFC 8414 /.well-known/oauth-authorization-server(response_types=code, S256, none) - RFC 7591 /register(public client,無 secret,無狀態不落地) - GET/POST /authorize(PKCE S256 + owner-secret 閘 + redirect_uri 白名單) - POST /token(authorization_code + PKCE 驗證 → access_token 綁定 owner namespace) 儲存鐵律 - authorization code / access token → 短效 KV OAUTH_KV(key 用 SHA-256 hash、帶 TTL、code 一次性) - owner secret / static token → CF Secrets(非 KV、非明碼 var) - DCR client / refresh token → 不落地(無狀態 / 不實作,避免長效機密進 KV) 相容決策 - 本機 CLI/GUI/Claude Code → 用真祕密 MCP_STATIC_TOKEN(CF Secret)取代舊明碼 namespace - 官方 SaaS partner-key 路徑行為不變 - ALLOW_PLAINTEXT_NAMESPACE 逃生門預設關(僅遷移期) 驗證:tsc exit 0;vitest 42/42(oauth 22 + partner-auth 10 改測真實 middleware + 既有 10); wrangler deploy --dry-run 打包過、OAUTH_KV binding 正確識別。設計文件 mcp/OAUTH.md。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d5jDbuqT5Htwv3Q88XXKk
87 lines
3.2 KiB
TypeScript
87 lines
3.2 KiB
TypeScript
// /authorize 同意頁:極簡單檔 HTML,要求輸入 owner 祕密才發碼。零外部資源。
|
||
// 所有反射進 HTML 的 OAuth 參數都 escape,防 XSS(redirect_uri / state / client_id 由外部帶入)。
|
||
|
||
/** HTML attribute / text 跳脫。 */
|
||
export function esc(s: string): string {
|
||
return s
|
||
.replace(/&/g, "&")
|
||
.replace(/</g, "<")
|
||
.replace(/>/g, ">")
|
||
.replace(/"/g, """)
|
||
.replace(/'/g, "'");
|
||
}
|
||
|
||
/** 同意頁需要 round-trip 回 POST /authorize 的隱藏欄位。 */
|
||
export interface ConsentParams {
|
||
client_id: string;
|
||
redirect_uri: string;
|
||
state: string;
|
||
code_challenge: string;
|
||
code_challenge_method: string;
|
||
scope: string;
|
||
resource: string;
|
||
}
|
||
|
||
function hidden(name: string, value: string): string {
|
||
return `<input type="hidden" name="${esc(name)}" value="${esc(value)}">`;
|
||
}
|
||
|
||
/**
|
||
* 同意頁 HTML。`error` 有值時(如祕密錯誤)顯示紅字,但仍保留隱藏欄位讓 owner 重試。
|
||
*/
|
||
export function consentPage(p: ConsentParams, error?: string): string {
|
||
const fields = [
|
||
hidden("client_id", p.client_id),
|
||
hidden("redirect_uri", p.redirect_uri),
|
||
hidden("state", p.state),
|
||
hidden("code_challenge", p.code_challenge),
|
||
hidden("code_challenge_method", p.code_challenge_method),
|
||
hidden("scope", p.scope),
|
||
hidden("resource", p.resource),
|
||
].join("\n ");
|
||
|
||
const errBlock = error
|
||
? `<p class="err" role="alert">${esc(error)}</p>`
|
||
: "";
|
||
|
||
return `<!doctype html>
|
||
<html lang="zh-Hant">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Arcrun MCP 授權</title>
|
||
<style>
|
||
:root { color-scheme: light dark; }
|
||
body { font-family: -apple-system, "Segoe UI", system-ui, sans-serif; max-width: 26rem;
|
||
margin: 4rem auto; padding: 0 1.25rem; line-height: 1.6; }
|
||
h1 { font-size: 1.25rem; }
|
||
p.desc { color: #666; font-size: .95rem; }
|
||
code { background: rgba(127,127,127,.15); padding: .1rem .35rem; border-radius: .25rem;
|
||
font-size: .85rem; word-break: break-all; }
|
||
label { display: block; margin: 1.25rem 0 .35rem; font-weight: 600; }
|
||
input[type=password] { width: 100%; padding: .6rem .7rem; font-size: 1rem;
|
||
border: 1px solid #8888; border-radius: .5rem; box-sizing: border-box; }
|
||
button { margin-top: 1.25rem; width: 100%; padding: .7rem; font-size: 1rem; font-weight: 600;
|
||
border: 0; border-radius: .5rem; background: #8a5f1e; color: #fff; cursor: pointer; }
|
||
button:hover { background: #6f4c18; }
|
||
p.err { color: #c0392b; font-weight: 600; }
|
||
p.foot { color: #999; font-size: .8rem; margin-top: 2rem; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<h1>Arcrun MCP 授權</h1>
|
||
<p class="desc">應用程式 <code>${esc(p.client_id)}</code> 想連上你的 Arcrun MCP,
|
||
這會讓它能<strong>讀寫你的 KBDB 全部資料</strong>。</p>
|
||
${errBlock}
|
||
<form method="POST" action="/authorize">
|
||
${fields}
|
||
<label for="owner_secret">Owner 祕密</label>
|
||
<input id="owner_secret" name="owner_secret" type="password" autocomplete="off"
|
||
autofocus required placeholder="只有你知道的祕密">
|
||
<button type="submit">授權連線</button>
|
||
</form>
|
||
<p class="foot">祕密不正確不會發出授權碼。此頁不儲存你的輸入。</p>
|
||
</body>
|
||
</html>`;
|
||
}
|