Repository navigation
fix: prevent path traversal by sanitizing URLs #1011
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
7 commits
Select commit
Hold shift + click to select a range
eec9890
fix: prevent path traversal by sanitizing URLs
barjin a80ceb7
Merge branch 'master' into fix/prevent-path-traversal
barjin 1f29cf8
chore: regenerate API report
barjin af35290
Merge branch 'v3' into fix/prevent-path-traversal
barjin c1c0232
chore: use zod for validation on v3
barjin 35dce7f
chore: revert public API report
barjin d8c4067
chore: regenerate API report
barjin 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import http from 'node:http'; | ||
| import type { AddressInfo } from 'node:net'; | ||
|
|
||
| import { ApifyClient } from 'apify-client'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| describe('URL path encoding', () => { | ||
| let server: http.Server; | ||
| let client: ApifyClient; | ||
| let lastPath: string | undefined; | ||
|
|
||
| beforeAll(async () => { | ||
| server = http.createServer((req, res) => { | ||
| lastPath = req.url; | ||
| res.setHeader('content-type', 'application/json'); | ||
| res.end(JSON.stringify({ data: {} })); | ||
| }); | ||
| await new Promise<void>((resolve) => server.listen(0, resolve)); | ||
| client = new ApifyClient({ | ||
| baseUrl: `http://localhost:${(server.address() as AddressInfo).port}`, | ||
| token: 'token', | ||
| maxRetries: 0, | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await new Promise((resolve) => server.close(resolve)); | ||
| }); | ||
|
|
||
| it('encodes record keys instead of letting them restructure the path', async () => { | ||
| await client.keyValueStore('STORE').getRecord('../../actor-runs/VICTIM/abort'); | ||
| expect(lastPath).toBe( | ||
| '/v2/key-value-stores/STORE/records/..%2F..%2Factor-runs%2FVICTIM%2Fabort?attachment=true', | ||
| ); | ||
|
|
||
| await client.keyValueStore('STORE').getRecord('foo?injected=1'); | ||
| expect(lastPath).toBe('/v2/key-value-stores/STORE/records/foo%3Finjected%3D1?attachment=true'); | ||
|
|
||
| await client.keyValueStore('STORE').getRecord('foo#frag'); | ||
| expect(lastPath).toBe('/v2/key-value-stores/STORE/records/foo%23frag?attachment=true'); | ||
| }); | ||
|
|
||
| it('encodes request ids', async () => { | ||
| await client.requestQueue('QUEUE').getRequest('../../../datasets/V/items'); | ||
| expect(lastPath).toBe('/v2/request-queues/QUEUE/requests/..%2F..%2F..%2Fdatasets%2FV%2Fitems'); | ||
| }); | ||
|
|
||
| it('keeps literal path separators', async () => { | ||
| await client.requestQueue('QUEUE').prolongRequestLock('abc', { lockSecs: 1 }); | ||
| expect(lastPath).toBe('/v2/request-queues/QUEUE/requests/abc/lock?lockSecs=1'); | ||
| }); | ||
|
|
||
| it('replaces every slash in a resource id', async () => { | ||
| await client.actor('user/name/extra').get(); | ||
| expect(lastPath).toBe('/v2/actors/user~name~extra'); | ||
| }); | ||
|
|
||
| it('rejects dot segments and empty values', async () => { | ||
| await expect(client.keyValueStore('STORE').getRecord('..')).rejects.toThrow(/must not contain dot segments/); | ||
| await expect(client.requestQueue('QUEUE').getRequest('.')).rejects.toThrow(/must not contain dot segments/); | ||
| expect(() => client.dataset('..')).toThrow(/must not contain dot segments/); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are from the previous commits to
v3, the checks were just failing without them.