-
Notifications
You must be signed in to change notification settings - Fork 11
INTER-2499 fix: url encode path parameters #282
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
9 commits
Select commit
Hold shift + click to select a range
9bace3e
fix: url encode path parameters
JuroUhlar 83e57e1
docs: describe the dot segment rejection in the changeset
JuroUhlar b341d9e
fix: coerce path parameters to primitive strings
JuroUhlar 612035a
fix: reject path parameters that cannot be encoded
JuroUhlar 1db3928
refactor: simplify path parameter handling
JuroUhlar 2cf857e
fix: catch coercion errors in path parameter validation
JuroUhlar 8aa1f66
fix: improve tests
JuroUhlar 991f7b5
fix: improve tests
JuroUhlar 66c2cb4
fix: review improvments
JuroUhlar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@fingerprint/node-sdk': patch | ||
| --- | ||
|
|
||
| URL-encode path parameters; reject "." and ".." path parameters with a TypeError. |
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
72 changes: 72 additions & 0 deletions
72
tests/mocked-responses-tests/pathParamEncodingTests.spec.ts
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,72 @@ | ||
| import { FingerprintServerApiClient, Region } from '../../src' | ||
| import { getIntegrationInfo } from '../../src/urlUtils' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { mockFetch } from './mockFetch' | ||
|
|
||
| /** | ||
| * A path parameter must never be able to change which endpoint or which host the SDK talks | ||
| * to, no matter what the caller passes in. | ||
| */ | ||
| describe('[Mocked response] Path parameter encoding', () => { | ||
| const apiKey = 'dummy_api_key' | ||
| const ii = `ii=${encodeURIComponent(getIntegrationInfo())}` | ||
|
|
||
| const client = new FingerprintServerApiClient({ region: Region.EU, apiKey }) | ||
|
|
||
| const emptyResponse = () => new Response(undefined, { headers: { 'content-length': '0' } }) | ||
|
|
||
| const operations = [ | ||
| { | ||
| name: 'getEvent', | ||
| prefix: 'v4/events', | ||
| placeholder: 'event_id', | ||
| paramName: 'eventId', | ||
| call: (param: string) => client.getEvent(param), | ||
| }, | ||
| { | ||
| name: 'updateEvent', | ||
| prefix: 'v4/events', | ||
| placeholder: 'event_id', | ||
| paramName: 'eventId', | ||
| call: (param: string) => client.updateEvent(param, { suspect: true }), | ||
| }, | ||
| { | ||
| name: 'deleteVisitorData', | ||
| prefix: 'v4/visitors', | ||
| placeholder: 'visitor_id', | ||
| paramName: 'visitorId', | ||
| call: (param: string) => client.deleteVisitorData(param), | ||
| }, | ||
| ] as const | ||
|
|
||
| describe.each(operations)('$name', ({ prefix, placeholder, paramName, call }) => { | ||
| // The full encoding table lives in the unit tests; these are the cases INTER-2499 asks to | ||
| // be pinned at the wire level for every method. | ||
| it.each([ | ||
| ['../events', '..%2Fevents'], | ||
| ['evil.com', 'evil.com'], | ||
| ['//evil.com', '%2F%2Fevil.com'], | ||
| ])('requests a single path segment for %j', async (param, encoded) => { | ||
| mockFetch.mockReturnValue(Promise.resolve(emptyResponse())) | ||
|
|
||
| // `getEvent` rejects on the empty body; only the requested URL matters here. | ||
| await call(param).catch(() => undefined) | ||
|
|
||
| const requestedUrl = mockFetch.mock.calls[0]?.[0] as string | ||
| expect(requestedUrl).toEqual(`https://eu.api.fpjs.io/${prefix}/${encoded}?${ii}`) | ||
| expect(new URL(requestedUrl).host).toEqual('eu.api.fpjs.io') | ||
| }) | ||
|
|
||
| it.each(['.', '..'])('does not send a request for %j', async (param) => { | ||
| await expect(call(param)).rejects.toThrow(new TypeError(`Invalid path parameter for ${placeholder}: ${param}`)) | ||
|
|
||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not send a request for an empty parameter', async () => { | ||
| await expect(call('')).rejects.toThrow(new TypeError(`${paramName} is not set`)) | ||
|
|
||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
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.
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.