-
Notifications
You must be signed in to change notification settings - Fork 84
feat: add authorizationToken config option #136
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fe66c3
feat: add authorizationToken config option
andre-j3sus ce4c00b
fix: resolve absolute API URLs without requiring a DOM
andre-j3sus 08c8616
refactor: derive tokenized API URLs from a single list
andre-j3sus 8ed1e7e
fix: redact the authorization token from error paths
andre-j3sus 5853f9f
refactor: rename the authorization query param to jwt
andre-j3sus 27bc390
docs: correct the authorization param name to jwt
andre-j3sus 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
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
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,103 @@ | ||
| /** | ||
| * Query-string parameter carrying the authorization token. | ||
| * | ||
| * Named `jwt`, not `token`: the measurement log POST body already has a | ||
| * `token` field holding a server-issued per-measurement value, and both are | ||
| * sent to `logMeasurementApiUrl`. | ||
| */ | ||
| export const AUTHORIZATION_TOKEN_PARAM = 'jwt'; | ||
|
|
||
| /** Placeholder substituted for the token in error messages. */ | ||
| const REDACTED = 'REDACTED'; | ||
|
|
||
| /** | ||
| * Appends the authorization token to `apiUrl`, preserving existing params. | ||
| * | ||
| * Query string rather than a header, which would trigger a CORS preflight and | ||
| * suppress BandwidthEngine's server-time calibration. Never over plain HTTP. | ||
| */ | ||
| export const withAuthorizationToken = ( | ||
| apiUrl: string, | ||
| token: string | null | ||
| ): string => { | ||
| if (!token) return apiUrl; | ||
|
|
||
| // Only relative URLs need the page origin, so absolute ones work without a DOM. | ||
| let urlObj: URL; | ||
| try { | ||
| urlObj = new URL(apiUrl); | ||
| } catch { | ||
| urlObj = new URL(apiUrl, window.location.origin); | ||
| } | ||
|
|
||
| if (urlObj.protocol !== 'https:') return apiUrl; | ||
|
|
||
| urlObj.searchParams.set(AUTHORIZATION_TOKEN_PARAM, token); | ||
| return urlObj.href; | ||
| }; | ||
|
|
||
| /** | ||
| * Masks the authorization token in a URL bound for a log or an error callback. | ||
| * | ||
| * Consumers routinely forward `onError` payloads to third-party log sinks, so | ||
| * the credential must not travel with them. Returns `apiUrl` untouched when | ||
| * there is no token to mask. | ||
| */ | ||
| export const redactAuthorizationToken = (apiUrl: string): string => { | ||
| let urlObj: URL; | ||
| try { | ||
| urlObj = new URL(apiUrl); | ||
| } catch { | ||
| try { | ||
| urlObj = new URL(apiUrl, window.location.origin); | ||
| } catch { | ||
| return apiUrl; | ||
| } | ||
| } | ||
|
|
||
| if (!urlObj.searchParams.has(AUTHORIZATION_TOKEN_PARAM)) return apiUrl; | ||
|
|
||
| urlObj.searchParams.set(AUTHORIZATION_TOKEN_PARAM, REDACTED); | ||
| return urlObj.href; | ||
| }; | ||
|
|
||
| /** | ||
| * Config URLs that carry the authorization token. Single source of truth: a new | ||
| * measurement endpoint must be added here to be attributed. | ||
| * | ||
| * `turnServerUri` is excluded (not HTTP), as are the reachability, RPKI and | ||
| * NXDOMAIN probe hosts, which are unrelated to the measurement endpoints. | ||
| */ | ||
| export const AUTHORIZABLE_URLS = [ | ||
| 'downloadApiUrl', | ||
| 'uploadApiUrl', | ||
| 'turnServerCredsApiUrl', | ||
| 'logAimApiUrl', | ||
| 'logMeasurementApiUrl' | ||
| ] as const; | ||
|
|
||
| /** The config fields {@link applyAuthorizationToken} rewrites. */ | ||
| type AuthorizableUrls = { authorizationToken: string | null } & { | ||
| [K in (typeof AUTHORIZABLE_URLS)[number]]: string | null; | ||
| }; | ||
|
|
||
| /** | ||
| * Attaches the token to every URL in {@link AUTHORIZABLE_URLS}, so the engines | ||
| * inherit it through the URLs they already receive. Mutates `config`, which is | ||
| * always a freshly merged object. | ||
| */ | ||
| export const applyAuthorizationToken = <T extends AuthorizableUrls>( | ||
| config: T | ||
| ): T => { | ||
| const token = config.authorizationToken; | ||
| if (!token) return config; | ||
|
|
||
| // Widened to write through the union of keys; T only narrows the field types. | ||
| const urls = config as AuthorizableUrls; | ||
| for (const key of AUTHORIZABLE_URLS) { | ||
| const url = urls[key]; | ||
| if (url) urls[key] = withAuthorizationToken(url, token); | ||
| } | ||
|
|
||
| return config; | ||
| }; |
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,89 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import defaultConfig from '../../../src/config/defaultConfig.ts'; | ||
| import { | ||
| applyAuthorizationToken, | ||
| AUTHORIZABLE_URLS | ||
| } from '../../../src/utils/authorization.ts'; | ||
|
|
||
| const TOKEN = 'test-token-123'; | ||
|
|
||
| /** Merges user config over the defaults the way the engine constructors do. */ | ||
| const resolveConfig = (userConfig: Partial<typeof defaultConfig>) => | ||
| applyAuthorizationToken(Object.assign({}, defaultConfig, userConfig)); | ||
|
|
||
| describe('applyAuthorizationToken', () => { | ||
| beforeEach(() => { | ||
| vi.stubGlobal('window', { | ||
| location: { origin: 'https://speed.cloudflare.com' } | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('attaches the token to every URL in AUTHORIZABLE_URLS', () => { | ||
| const config = resolveConfig({ | ||
| authorizationToken: TOKEN, | ||
| // Null by default, so set it to cover every key in the list. | ||
| logMeasurementApiUrl: 'https://speed.cloudflare.com/__log' | ||
| }); | ||
|
|
||
| for (const key of AUTHORIZABLE_URLS) { | ||
| expect(new URL(config[key]!).searchParams.get('jwt'), key).toBe(TOKEN); | ||
| } | ||
| }); | ||
|
|
||
| it('leaves disabled logging endpoints null', () => { | ||
| const config = resolveConfig({ | ||
| authorizationToken: TOKEN, | ||
| logAimApiUrl: null, | ||
| logMeasurementApiUrl: null | ||
| }); | ||
|
|
||
| expect(config.logAimApiUrl).toBeNull(); | ||
| expect(config.logMeasurementApiUrl).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not attach the token to excluded hosts', () => { | ||
| const config = resolveConfig({ authorizationToken: TOKEN }); | ||
|
|
||
| expect(config.rpkiInvalidHost).toBe('invalid.rpki.cloudflare.com'); | ||
| expect(config.turnServerUri).toBe('turn.speed.cloudflare.com:50000'); | ||
| }); | ||
|
|
||
| it('leaves every URL untouched when no token is configured', () => { | ||
| const config = resolveConfig({}); | ||
|
|
||
| for (const key of AUTHORIZABLE_URLS) { | ||
| expect(config[key], key).toBe(defaultConfig[key]); | ||
| } | ||
| }); | ||
|
|
||
| it('does not mutate the shared defaultConfig object', () => { | ||
| // applyAuthorizationToken mutates in place, so it must only ever be handed | ||
| // a freshly merged object — otherwise the token leaks across instances. | ||
| resolveConfig({ authorizationToken: TOKEN }); | ||
|
|
||
| expect(defaultConfig.downloadApiUrl).toBe( | ||
| 'https://speed.cloudflare.com/__down' | ||
| ); | ||
| expect(defaultConfig.logAimApiUrl).toBe( | ||
| 'https://speed.cloudflare.com/__results' | ||
| ); | ||
| expect(defaultConfig.authorizationToken).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not attach the token over plain HTTP', () => { | ||
| const config = resolveConfig({ | ||
| authorizationToken: TOKEN, | ||
| downloadApiUrl: 'http://speed.cloudflare.com/__down', | ||
| uploadApiUrl: 'http://speed.cloudflare.com/__up' | ||
| }); | ||
|
|
||
| expect(config.downloadApiUrl).toBe('http://speed.cloudflare.com/__down'); | ||
| expect(config.uploadApiUrl).toBe('http://speed.cloudflare.com/__up'); | ||
| // HTTPS endpoints in the same config are still attributed. | ||
| expect(new URL(config.logAimApiUrl!).searchParams.get('jwt')).toBe(TOKEN); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.