-
Notifications
You must be signed in to change notification settings - Fork 290
feat(server): add bearer token auth for HTTP transport #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a73b65d
feat(server): add bearer token auth for HTTP transport (#66)
tianzhou 37c4414
refactor: simplify auth token resolution and healthz exemption
tianzhou 670eeb9
docs: drop vendor name-dropping from auth rationale
tianzhou d447f30
fix: address Copilot review findings on auth-token
tianzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { validateAuthToken } from '../auth-token.js'; | ||
|
|
||
| describe('validateAuthToken', () => { | ||
| it('allows any request when no tokens are configured', () => { | ||
| expect(validateAuthToken(undefined, [])).toEqual({ ok: true }); | ||
| expect(validateAuthToken('Bearer whatever', [])).toEqual({ ok: true }); | ||
| }); | ||
|
|
||
| it('accepts a matching bearer token', () => { | ||
| const result = validateAuthToken('Bearer secret123', ['secret123']); | ||
| expect(result).toEqual({ ok: true }); | ||
| }); | ||
|
|
||
| it('accepts a token that matches any entry in a multi-token list', () => { | ||
| const tokens = ['first-token', 'second-token', 'third-token']; | ||
| expect(validateAuthToken('Bearer second-token', tokens)).toEqual({ ok: true }); | ||
| }); | ||
|
|
||
| it('rejects a missing Authorization header', () => { | ||
| const result = validateAuthToken(undefined, ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| expect(result).toMatchObject({ status: 401 }); | ||
| }); | ||
|
|
||
| it('rejects a header without the Bearer scheme', () => { | ||
| const result = validateAuthToken('secret123', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| expect(result).toMatchObject({ status: 401 }); | ||
| }); | ||
|
|
||
| it('rejects a wrong scheme such as Basic auth', () => { | ||
| const result = validateAuthToken('Basic dXNlcjpwYXNz', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| expect(result).toMatchObject({ status: 401 }); | ||
| }); | ||
|
|
||
| it('rejects an incorrect token', () => { | ||
| const result = validateAuthToken('Bearer wrong-token', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| expect(result).toMatchObject({ status: 401 }); | ||
| }); | ||
|
|
||
| it('rejects an empty bearer token', () => { | ||
| const result = validateAuthToken('Bearer ', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| }); | ||
|
|
||
| it('rejects a token differing only in length from a configured one', () => { | ||
| const result = validateAuthToken('Bearer secret1234extra', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| }); | ||
|
|
||
| it('is case sensitive on token comparison', () => { | ||
| const result = validateAuthToken('Bearer SECRET123', ['secret123']); | ||
| expect(result.ok).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { timingSafeEqual } from "node:crypto"; | ||
|
|
||
| /** | ||
| * Result of validating an HTTP request's `Authorization` header against the | ||
| * configured bearer token allow-list. | ||
| */ | ||
| export type AuthTokenValidation = | ||
| | { ok: true } | ||
| | { ok: false; status: 401; message: string }; | ||
|
|
||
| const BEARER_PREFIX = "Bearer "; | ||
|
|
||
| /** | ||
| * Constant-time string equality. `timingSafeEqual` throws on unequal-length | ||
| * buffers, so unequal lengths are rejected up front — this leaks only the | ||
| * length of the configured token, not which bytes matched, which is the same | ||
| * trade-off `timingSafeEqual` itself makes. | ||
| */ | ||
| function constantTimeEqual(a: string, b: string): boolean { | ||
| const bufA = Buffer.from(a); | ||
| const bufB = Buffer.from(b); | ||
| if (bufA.length !== bufB.length) return false; | ||
| return timingSafeEqual(bufA, bufB); | ||
| } | ||
|
|
||
| /** | ||
| * Bearer token auth for the HTTP transport (issue #66): "anyone with the | ||
| * server URL can access the database." An empty `tokens` list means auth is | ||
| * disabled — configuring a token is itself the opt-in (see | ||
| * `resolveAuthTokens()` in `src/config/env.ts`), so there is no separate | ||
| * "--require-auth" flag to forget to set. | ||
| * | ||
| * This intentionally stops at a shared-secret allow-list rather than | ||
| * implementing the MCP spec's full OAuth 2.1 resource-server model (RFC 9728 | ||
| * protected-resource metadata, authorization-server discovery, dynamic client | ||
| * registration, PKCE). That machinery solves multi-tenant identity | ||
| * federation; DBHub's actual gap is coarser — "is this request from someone | ||
| * who has the secret," not "who is this user and what scopes do they have." | ||
| */ | ||
| export function validateAuthToken( | ||
| authorizationHeader: string | undefined, | ||
| tokens: string[] | ||
| ): AuthTokenValidation { | ||
| if (tokens.length === 0) return { ok: true }; | ||
|
|
||
| if (!authorizationHeader || !authorizationHeader.startsWith(BEARER_PREFIX)) { | ||
| return { | ||
| ok: false, | ||
| status: 401, | ||
| message: "Missing or malformed Authorization header. Expected: Bearer <token>", | ||
| }; | ||
| } | ||
|
|
||
| const presented = authorizationHeader.slice(BEARER_PREFIX.length); | ||
| const matches = tokens.some((token) => constantTimeEqual(presented, token)); | ||
| if (!matches) { | ||
| return { ok: false, status: 401, message: "Invalid bearer token" }; | ||
| } | ||
|
|
||
| return { ok: true }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.