-
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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** Query-string parameter carrying the authorization token. */ | ||
| export const AUTHORIZATION_TOKEN_PARAM = 'token'; | ||
|
|
||
| /** | ||
| * Returns `apiUrl` with the authorization token appended as a query-string | ||
| * parameter, preserving any params already present. | ||
| * | ||
| * The token is sent in the query string rather than a header to avoid a CORS | ||
| * preflight on the measurement endpoints: an extra round trip there would cost | ||
| * test time and, by reusing the TCP connection, suppress the server-time | ||
| * calibration in `BandwidthEngine` that relies on seeing a fresh handshake. | ||
| * | ||
| * Returns `apiUrl` untouched when there is no token, or when the resolved URL | ||
| * is not HTTPS — a token observed in cleartext must be treated as compromised, | ||
| * so it must never leave the client over plain HTTP. | ||
| */ | ||
| export const withAuthorizationToken = ( | ||
| apiUrl: string, | ||
| token: string | null | ||
| ): string => { | ||
| if (!token) return apiUrl; | ||
|
|
||
| const urlObj = new URL(apiUrl, window.location.origin); | ||
|
andre-j3sus marked this conversation as resolved.
Outdated
|
||
| if (urlObj.protocol !== 'https:') return apiUrl; | ||
|
|
||
| urlObj.searchParams.set(AUTHORIZATION_TOKEN_PARAM, token); | ||
| return urlObj.href; | ||
| }; | ||
|
|
||
| /** The config fields {@link applyAuthorizationToken} rewrites. */ | ||
| interface AuthorizableUrls { | ||
| authorizationToken: string | null; | ||
| downloadApiUrl: string; | ||
| uploadApiUrl: string; | ||
| turnServerCredsApiUrl: string; | ||
| logAimApiUrl: string | null; | ||
| logMeasurementApiUrl: string | null; | ||
| } | ||
|
andre-j3sus marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Bakes the authorization token into every API URL billed to a customer, so | ||
| * that the engines inherit it through the URLs they already receive. | ||
| * | ||
| * The reachability, RPKI and NXDOMAIN probes are deliberately excluded: they | ||
| * target unrelated hosts rather than the measurement endpoints. Mutates and | ||
| * returns `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; | ||
|
|
||
| config.downloadApiUrl = withAuthorizationToken(config.downloadApiUrl, token); | ||
| config.uploadApiUrl = withAuthorizationToken(config.uploadApiUrl, token); | ||
| config.turnServerCredsApiUrl = withAuthorizationToken( | ||
| config.turnServerCredsApiUrl, | ||
| token | ||
| ); | ||
| if (config.logAimApiUrl) | ||
| config.logAimApiUrl = withAuthorizationToken(config.logAimApiUrl, token); | ||
| if (config.logMeasurementApiUrl) | ||
| config.logMeasurementApiUrl = withAuthorizationToken( | ||
| config.logMeasurementApiUrl, | ||
| 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,101 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import defaultConfig from '../../../src/config/defaultConfig.ts'; | ||
| import { applyAuthorizationToken } 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)); | ||
|
|
||
| /** Endpoints that bill against a customer and must carry the token. */ | ||
| const BILLED_URL_KEYS = [ | ||
| 'downloadApiUrl', | ||
| 'uploadApiUrl', | ||
| 'turnServerCredsApiUrl', | ||
| 'logAimApiUrl' | ||
| ] as const; | ||
|
|
||
| describe('applyAuthorizationToken', () => { | ||
| beforeEach(() => { | ||
| vi.stubGlobal('window', { | ||
| location: { origin: 'https://speed.cloudflare.com' } | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('attaches the token to every billed endpoint', () => { | ||
| const config = resolveConfig({ authorizationToken: TOKEN }); | ||
|
|
||
| for (const key of BILLED_URL_KEYS) { | ||
| expect(new URL(config[key]!).searchParams.get('token'), key).toBe(TOKEN); | ||
| } | ||
| }); | ||
|
|
||
| it('attaches the token to per-measurement logging when configured', () => { | ||
| const config = resolveConfig({ | ||
| authorizationToken: TOKEN, | ||
| logMeasurementApiUrl: 'https://speed.cloudflare.com/__log' | ||
| }); | ||
|
|
||
| expect( | ||
| new URL(config.logMeasurementApiUrl!).searchParams.get('token') | ||
| ).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 the RPKI probe host', () => { | ||
| // Reachability/RPKI probes target unrelated hosts, not billed endpoints. | ||
| const config = resolveConfig({ authorizationToken: TOKEN }); | ||
|
|
||
| expect(config.rpkiInvalidHost).toBe('invalid.rpki.cloudflare.com'); | ||
| }); | ||
|
|
||
| it('leaves every URL untouched when no token is configured', () => { | ||
| const config = resolveConfig({}); | ||
|
|
||
| for (const key of BILLED_URL_KEYS) { | ||
| 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('token')).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
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,107 @@ | ||
| import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
| import { | ||
| AUTHORIZATION_TOKEN_PARAM, | ||
| withAuthorizationToken | ||
| } from '../../../src/utils/authorization.ts'; | ||
|
|
||
| /** The helper resolves relative URLs against the page origin, like the engines do. */ | ||
| const stubOrigin = (origin: string): void => { | ||
| vi.stubGlobal('window', { location: { origin } }); | ||
| }; | ||
|
|
||
| const TOKEN = 'eyJhbGciOiJFUzI1NiJ9.payload.signature'; | ||
|
|
||
| describe('withAuthorizationToken', () => { | ||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('appends the token as a query-string param', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| const url = withAuthorizationToken( | ||
| 'https://speed.example.com/__down', | ||
| TOKEN | ||
| ); | ||
|
|
||
| expect(new URL(url).searchParams.get(AUTHORIZATION_TOKEN_PARAM)).toBe( | ||
| TOKEN | ||
| ); | ||
| }); | ||
|
|
||
| it('uses `token` as the param name', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| expect( | ||
| withAuthorizationToken('https://speed.example.com/__up', 'abc') | ||
| ).toBe('https://speed.example.com/__up?token=abc'); | ||
| }); | ||
|
|
||
| it('preserves query params already present on the URL', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| const url = new URL( | ||
| withAuthorizationToken( | ||
| 'https://speed.example.com/__down?foo=bar&baz=1', | ||
| TOKEN | ||
| ) | ||
| ); | ||
|
|
||
| expect(url.searchParams.get('foo')).toBe('bar'); | ||
| expect(url.searchParams.get('baz')).toBe('1'); | ||
| expect(url.searchParams.get(AUTHORIZATION_TOKEN_PARAM)).toBe(TOKEN); | ||
| }); | ||
|
|
||
| it('url-encodes tokens containing reserved characters', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| const url = withAuthorizationToken( | ||
| 'https://speed.example.com/__down', | ||
| 'a+b/c=d&e' | ||
| ); | ||
|
|
||
| expect(url).not.toContain('a+b/c=d&e'); | ||
| expect(new URL(url).searchParams.get(AUTHORIZATION_TOKEN_PARAM)).toBe( | ||
| 'a+b/c=d&e' | ||
| ); | ||
| }); | ||
|
|
||
| it('resolves relative URLs against the page origin', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| expect(withAuthorizationToken('/__down', TOKEN)).toBe( | ||
| `https://speed.example.com/__down?token=${encodeURIComponent(TOKEN)}` | ||
| ); | ||
| }); | ||
|
|
||
| it('returns the URL untouched when there is no token', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| expect( | ||
| withAuthorizationToken('https://speed.example.com/__down', null) | ||
| ).toBe('https://speed.example.com/__down'); | ||
| expect(withAuthorizationToken('https://speed.example.com/__down', '')).toBe( | ||
| 'https://speed.example.com/__down' | ||
| ); | ||
| }); | ||
|
|
||
| it('never attaches the token over plain HTTP', () => { | ||
| stubOrigin('https://speed.example.com'); | ||
|
|
||
| expect( | ||
| withAuthorizationToken('http://speed.example.com/__down', TOKEN) | ||
| ).toBe('http://speed.example.com/__down'); | ||
| }); | ||
|
|
||
| it('never attaches the token to a relative URL on an HTTP page', () => { | ||
| stubOrigin('http://speed.example.com'); | ||
|
|
||
| expect(withAuthorizationToken('/__down', TOKEN)).toBe('/__down'); | ||
| }); | ||
|
|
||
| it('does not touch `window` when there is no token', () => { | ||
| // Guards SSR/`autoStart: false` construction: the default config must not | ||
| // require a DOM just to build the engine. | ||
| expect(() => withAuthorizationToken('/__down', null)).not.toThrow(); | ||
| }); | ||
| }); |
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.