Files
accounted/extensions/general/mcp-server/origin-guard.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

60 lines
2.0 KiB
TypeScript

/**
* Origin-header validation for the MCP Streamable HTTP endpoint.
*
* MCP spec (2025-06-18, Streamable HTTP transport): "Servers MUST validate
* the Origin header on all incoming connections to prevent DNS rebinding
* attacks." Also an explicit Claude Connectors Directory submission
* requirement.
*
* Non-browser clients send no Origin header and are allowed: claude.ai's
* backend connector, Claude Desktop, the npx gnubok-mcp bridge, Claude Code,
* and MCP Inspector (whose Node proxy makes the actual call). A browser page
* sends its own origin: allowed only when it matches the deployment's own
* host: compared against the request Host (covers Vercel previews and
* self-hosted domains without hardcoding) and NEXT_PUBLIC_APP_URL (covers
* proxies that rewrite Host). Anything else is a cross-site browser request
* the endpoint never serves (it sets no CORS headers), so reject explicitly.
*/
export function isForbiddenOrigin(request: Request): boolean {
const origin = request.headers.get('origin')
if (!origin) return false
let originHost: string
try {
originHost = new URL(origin).host
} catch {
// Malformed Origin (including the literal "null" some browsers send for
// sandboxed/opaque contexts): treat as foreign.
return true
}
const allowedHosts = new Set<string>()
const hostHeader = request.headers.get('host')
if (hostHeader) allowedHosts.add(hostHeader)
try {
allowedHosts.add(new URL(request.url).host)
} catch {
// request.url should always parse; ignore if not.
}
if (process.env.NEXT_PUBLIC_APP_URL) {
try {
allowedHosts.add(new URL(process.env.NEXT_PUBLIC_APP_URL).host)
} catch {
// Misconfigured env var: fall through to the request-derived hosts.
}
}
return !allowedHosts.has(originHost)
}
export function forbiddenOriginResponse(): Response {
return Response.json(
{
jsonrpc: '2.0',
id: null,
error: { code: -32600, message: 'Origin not allowed' },
},
{ status: 403 },
)
}