Skip to content

feat(openapi): describe principal providers' credentials truthfully (#1047) - #1054

Merged
jimhoyd merged 2 commits into
mainfrom
claude/openapi-security-1047
Sep 30, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/openapi-security-1047

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1047

What changed

packages/core/src/openapi.ts gave every providesPrincipal gate {type: 'apiKey', in: 'cookie', name: 'session'}, a cookie no provider reads. A generated client would send it.

  • Contract. A principal provider may declare openapiSecurity, an OpenAPI 3.1 Security Scheme Object limited to http (scheme, bearerFormat), apiKey (in, name) and mutualTLS, each with an optional description. It is closed-key JSON with token-checked and length-bounded strings (extensionOpenApiSecurityLimits, openApiSecurityProblem in the new packages/core/src/openapi-security.ts). It says where a credential goes and has no field that could hold a credential. oauth2 and openIdConnect are left out because their URLs are operator configuration.
    • It is declared on the defineExtension definition. The build writes it into urlcode.json and the release catalog, so the export reads it passively and never imports the provider.
    • The registration that host() returns must carry the same value, which composeHost checks. defineExtension, activation and descriptor/catalog parsing refuse it on a non-provider or outside the contract. With --host-file, the registration's value is used and checked before use.
  • Export.
    • A complete declaration becomes components.securitySchemes["urlcodePrincipal.<name>"] and the gated operations' security.
    • A cookie whose name the operator configures (apiKey/cookie with no name) gets no scheme and no requirement. The same goes for a provider that declares nothing. The operation states these under x-urlcode.authentication, as {extension, credential: "cookie", cookieName: "operator-defined"} or {extension, credential: "unknown"}.
    • The 401/403/503 are unchanged except for wording: "session" becomes "credential".
  • Why not a scheme for the unknown cases. OpenAPI 3.1 requires name on apiKey, and every scheme type asserts a transport. Any scheme we emitted would be an invented credential that clients like hey-api then send (they read security to add Cookie/headers). Leaving out security is valid 3.1. Clients generate normally, and a browser sends its own cookies. The x-urlcode extension carries the fact without affecting clients that ignore it.
  • auth. It declares Better Auth's session cookie without a name. The name is operator configuration: the cookie prefix, per-cookie names, and the __Secure- prefix an https origin adds. It never appears in the export, which the test checks with an operator cookiePrefix.
  • Contract version. Not bumped. The field is optional and additive, and an older core ignores it.
  • Docs. EXTENSIONS.md has a new "Declaring the credential's OpenAPI security scheme" section, and the request principal and definition sample are updated. TOOLING.md's Authentication section is updated, and the RIM-OPENAPI-001 card, FRAMEWORK.md, the authjs proof README and the auth CHANGELOG are updated too.

Evidence

  • npm run verify and npm run test:package pass locally (macOS).
  • test/openapi.test.ts covers four cases, each document validated against the shipped official 3.1 schema:
    • Descriptor-only providers: a provider-declared cookie name, a bearer scheme, an unknown transport, and a mixed gate. The provider package throws if imported, so the export provably reads data only.
    • Host-file override, and five refusals of out-of-contract declarations.
    • A contract run with a declared session cookie.
    • A new hey-api generated client that gets 401 without its auth option and 200 with it. The client put the token in the cookie the scheme names.
  • packages/auth/test/auth.test.ts: the definition, registration and urlcode.json declare the same scheme. With an operator cookiePrefix, the export with and without a registration contains no prefix, no better-auth and no session_token, while the served sign-in sets the prefixed cookie.
  • test/authjs-provider.integration.ts: the independent Auth.js provider (undeclared) exports credential: unknown and no scheme.
  • Tests for the addon catalog, operator host and store OpenAPI (the badge stand-in now declares its x-badge header).

Limitations

  • No bearer-token provider exists in this repository. Bearer is covered by synthetic declarations only.
  • First-party auth still gets no standard scheme. That is deliberate: its cookie name is operator configuration, and OpenAPI cannot state an apiKey without a name.

🤖 Generated with Claude Code

…1047)

The OpenAPI export gave every providesPrincipal gate an apiKey cookie
scheme named `session`, a credential no provider reads and a generated
client would send.

- A principal provider may declare `openapiSecurity`, a bounded, JSON-only
  OpenAPI Security Scheme Object (http, apiKey or mutualTLS) that names
  where a credential goes, never one. It lives on the defineExtension
  definition (written into urlcode.json and the release catalog, so the
  passive export reads it without running the extension) and on the
  registration host() returns; composeHost refuses a difference, and
  core refuses it on a non-provider or outside the contract.
- A complete declaration becomes `urlcodePrincipal.<name>` in
  components.securitySchemes and the gated operations' security.
- A cookie whose name the operator configures, and an undeclared
  transport, get no scheme and no requirement: the operation states them
  under x-urlcode.authentication (credential: cookie with cookieName:
  operator-defined, or credential: unknown). Nothing is invented.
- The auth extension declares Better Auth's session cookie without a
  name, since the name is the operator's Better Auth configuration.
- The optional field adds to the extension contract without breaking
  it, so extensionContract stays 2.

Tests cover a provider-declared cookie name, bearer, unknown and mixed
gates from descriptors (the export never imports the provider), host-file
overrides and refusals, a hey-api client authenticating through the
declared cookie, the auth package with an operator cookie prefix, and the
Auth.js proof's unknown transport.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 30, 2026 14:46
Takes main's side for the host lease and multiprocess removal (#1050) and
keeps the OpenAPI security scheme changes (#1047).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit 7fc421b into main Sep 30, 2026
27 checks passed
@jimhoyd
jimhoyd deleted the claude/openapi-security-1047 branch September 30, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Request]: Describe principal providers truthfully in OpenAPI security schemes

1 participant