Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): require issuer and base without query, fragment or userinfo (RFC 8414 §3.3)

Diego Zacariasecdcba04e216
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index a9d59615e..bbd5e34b6 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.787",
+ "version": "0.1.788",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.787",
+ "version": "0.1.788",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 1c8f9ca9d..e6a3f89d3 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.787",
+ "version": "0.1.788",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/oauth/discovery.ts b/control-api/src/oauth/discovery.ts
index f94667d51..605a74e20 100644
--- a/control-api/src/oauth/discovery.ts
+++ b/control-api/src/oauth/discovery.ts
@@ -521,20 +521,34 @@ function isResourceConsistentWithMcpUrl(resource: string, mcpUrl: URL): boolean
return mcpPath === resPath || mcpPath.startsWith(`${resPath}/`)
}
+/**
+ * An RFC 8414 §2 issuer identifier: no query, no fragment and no userinfo. Checked
+ * on the raw string because `URL` drops an empty `?` or `#`.
+ */
+function isBareIdentifier(raw: string, parsed: URL): boolean {
+ return (
+ !raw.includes('?') && !raw.includes('#') && parsed.username === '' && parsed.password === ''
+ )
+}
+
/**
* RFC 8414 §3.3: the AS metadata `issuer` must be identical to the authorization
- * server it was retrieved from. Otherwise a hostile AS response could hand us an
- * `issuer` we then trust for the RFC 9207 callback `iss` check. Compared as
- * origin+path with trailing slashes normalized.
+ * server it was retrieved from; neither carries a query, fragment or userinfo
+ * (RFC 8414 §2). Otherwise a hostile AS response could hand us an `issuer` we then
+ * trust for the RFC 9207 callback `iss` check. Compared as origin+path with
+ * trailing slashes normalized.
*/
-function isIssuerConsistentWithBase(issuer: string, base: URL): boolean {
+function isIssuerConsistentWithBase(issuer: string, base: string): boolean {
let issuerUrl: URL
+ let baseUrl: URL
try {
issuerUrl = new URL(issuer)
+ baseUrl = new URL(base)
} catch {
return false
}
- return issuerUrl.origin === base.origin && trimmedPath(issuerUrl) === trimmedPath(base)
+ if (!isBareIdentifier(issuer, issuerUrl) || !isBareIdentifier(base, baseUrl)) return false
+ return issuerUrl.origin === baseUrl.origin && trimmedPath(issuerUrl) === trimmedPath(baseUrl)
}
/**
@@ -707,7 +721,7 @@ export async function discoverRemoteOAuth(
// RFC 8414 §3.3: `issuer` must match the AS we fetched the metadata from. The
// `issuer` feeds the RFC 9207 callback `iss` check, so a hostile mismatch is
// rejected here rather than trusted downstream.
- if (!isIssuerConsistentWithBase(candidate.issuer, asBaseUrl)) {
+ if (!isIssuerConsistentWithBase(candidate.issuer, asBase)) {
lastAsError = {
kind: 'issuer_mismatch',
detail: `AS issuer "${candidate.issuer}" does not match authorization server "${asBase}"`,
@@ -864,7 +878,7 @@ async function resolveAsMetadataFromBase(
}
continue
}
- if (!isIssuerConsistentWithBase(candidate.issuer, asBaseUrl)) {
+ if (!isIssuerConsistentWithBase(candidate.issuer, asBaseUrl.toString())) {
lastError = {
kind: 'issuer_mismatch',
detail: `AS issuer "${candidate.issuer}" does not match authorization server "${asBaseUrl.toString()}"`,
diff --git a/control-api/test/oauth.discovery.properties.test.ts b/control-api/test/oauth.discovery.properties.test.ts
index dede253ce..1cd802c0b 100644
--- a/control-api/test/oauth.discovery.properties.test.ts
+++ b/control-api/test/oauth.discovery.properties.test.ts
@@ -5,10 +5,12 @@ import {
type ProtectedResourceMetadata,
type RegistrationMode,
advertisesIssBinding,
+ asMetadataUrls,
deriveQuirks,
+ discoverRemoteOAuth,
selectRegistrationMode,
} from '../src/oauth/discovery.js'
-import { PILOTS } from './fixtures/remoteOAuthDiscovery.js'
+import { PILOTS, makeDiscoveryTransport } from './fixtures/remoteOAuthDiscovery.js'
/**
* T2 property-based coverage for the two pure decision functions of discovery.
@@ -198,3 +200,53 @@ describe('deriveQuirks (D-8, T2)', () => {
}
})
})
+
+describe('issuer identity (RFC 8414 §3.3 / §2): no query, fragment or userinfo', () => {
+ // The real Sentry pilot with its PRM pointed at a pathful authorization server and
+ // the AS issuer replaced; Sentry returns `iss` (RFC 9207), so the same-site rule does
+ // not interfere with the issuer comparison under test.
+ const base = 'https://as.example.com/p'
+ const [oauthUrl, oidcUrl] = asMetadataUrls(new URL(base))
+ const prmJson = JSON.stringify({
+ ...JSON.parse(PILOTS.sentry.prm.json),
+ authorization_servers: [base],
+ })
+ const discoverWithIssuer = (issuer: string) => {
+ const asJson = JSON.stringify({ ...JSON.parse(PILOTS.sentry.as.json), issuer })
+ const pilot = {
+ ...PILOTS.sentry,
+ prm: { ...PILOTS.sentry.prm, json: prmJson },
+ as: { url: oauthUrl, json: asJson },
+ // Both AS-metadata candidates carry the issuer, so the outcome is the comparison's.
+ asExtra: [{ url: oidcUrl, status: 200, json: asJson }],
+ }
+ return discoverRemoteOAuth(pilot.mcpUrl, {
+ transport: makeDiscoveryTransport(pilot),
+ resolveDns: async () => ['93.184.216.34'],
+ })
+ }
+
+ it('control: the issuer identical to the authorization server is accepted', async () => {
+ const outcome = await discoverWithIssuer(base)
+ expect(outcome.ok).toBe(true)
+ if (outcome.ok) expect(outcome.result.issuer).toBe(base)
+ })
+
+ it('an issuer decorated with a query or fragment, even an empty one, is issuer_mismatch', async () => {
+ await fc.assert(
+ fc.asyncProperty(fc.constantFrom('?', '#', '?q=1', '#f'), fc.string(), async (mark, tail) => {
+ const outcome = await discoverWithIssuer(`${base}${mark}${tail}`)
+ expect(outcome.ok).toBe(false)
+ if (!outcome.ok) expect(outcome.error.kind).toBe('issuer_mismatch')
+ })
+ )
+ })
+
+ it('an issuer with userinfo is issuer_mismatch', async () => {
+ for (const issuer of ['https://u:[email protected]/p', 'https://[email protected]/p']) {
+ const outcome = await discoverWithIssuer(issuer)
+ expect(outcome.ok).toBe(false)
+ if (!outcome.ok) expect(outcome.error.kind).toBe('issuer_mismatch')
+ }
+ })
+})
diff --git a/control-api/test/oauth.discovery.test.ts b/control-api/test/oauth.discovery.test.ts
index bc980da77..45285ce85 100644
--- a/control-api/test/oauth.discovery.test.ts
+++ b/control-api/test/oauth.discovery.test.ts
@@ -426,6 +426,31 @@ describe('discoverRemoteOAuth — third-party metadata self-consistency (C2/C3)'
if (!outcome.ok) expect(outcome.error.kind).toBe('issuer_mismatch')
})
+ // Table row 11: RFC 8414 §2 forbids query and fragment in an issuer, and userinfo is
+ // no part of an identifier. URL origin+pathname ignore all three, so an issuer that
+ // carries them must be compared as written, even when the `?` or `#` is empty.
+ for (const issuer of [
+ 'https://mcp.notion.com?',
+ 'https://mcp.notion.com#',
+ 'https://mcp.notion.com/?q=1',
+ 'https://mcp.notion.com#f',
+ 'https://u:[email protected]',
+ ]) {
+ it(`C3: rejects an issuer with query, fragment or userinfo (${issuer})`, async () => {
+ const asJson = JSON.stringify({ ...JSON.parse(PILOTS.notion.as.json), issuer })
+ const [oauthUrl, oidcUrl] = asMetadataUrls(new URL('https://mcp.notion.com'))
+ const pilot = {
+ ...PILOTS.notion,
+ as: { url: oauthUrl, json: asJson },
+ // Served on both AS-metadata candidates so neither clears the check.
+ asExtra: [{ url: oidcUrl, status: 200, json: asJson }],
+ }
+ const outcome = await discoverRemoteOAuth(pilot.mcpUrl, deps(makeDiscoveryTransport(pilot)))
+ expect(outcome.ok).toBe(false)
+ if (!outcome.ok) expect(outcome.error.kind).toBe('issuer_mismatch')
+ })
+ }
+
it('C3: accepts the real pilots (issuer == authorization server base)', async () => {
for (const key of ['notion', 'sentry'] as const) {
const outcome = await discoverRemoteOAuth(