-
Notifications
You must be signed in to change notification settings - Fork 892
feat: validate copy src paths are relative and within context directory #1106
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
20 commits
Select commit
Hold shift + click to select a range
4ecf763
feat: validate copy src paths are relative and within context directory
mishushakov 27dc3d9
accept
mishushakov 81ad8f0
lint & fmt
mishushakov 438914e
commit
mishushakov 5bba5a5
chore: remove template.copy integration tests
mishushakov 73c7ed3
format
mishushakov cb72b68
added stacktrace test
mishushakov 411bad9
test: add test case for ./.. path escape
mishushakov 9c49e71
fmt
mishushakov b28a43a
fix: detect Windows absolute paths on Unix platforms
mishushakov 1846d99
fix: allow filenames starting with double dots
mishushakov 348b6bb
chore: remove unused failure map entries
mishushakov 02b4333
fix: validate copyItems paths with correct stack trace
mishushakov 4fcf515
format
mishushakov ab27127
re-raise with stack trace
mishushakov 15c21c4
removed redundant code
mishushakov 90bb89b
added changeset
mishushakov 74166b9
simplify path validation to use native cross-platform APIs
mishushakov b72ab40
refactor: extract make_traceback utility function
mishushakov c1a8eb9
Merge branch 'main' into mishushakov/relative-path-validation
mishushakov 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,6 @@ | ||
| --- | ||
| '@e2b/python-sdk': patch | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| fix: validate copy src paths are relative and within context directory |
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
168 changes: 168 additions & 0 deletions
168
packages/js-sdk/tests/template/utils/validateRelativePath.test.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,168 @@ | ||
| import { describe, expect, test } from 'vitest' | ||
| import { validateRelativePath } from '../../../src/template/utils' | ||
| import { TemplateError } from '../../../src/errors' | ||
|
|
||
| const isWindows = process.platform === 'win32' | ||
|
|
||
| describe('validateRelativePath', () => { | ||
| describe('valid paths', () => { | ||
| test('accepts simple relative path', () => { | ||
| expect(() => validateRelativePath('foo', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts nested relative path', () => { | ||
| expect(() => validateRelativePath('foo/bar', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts path with ./ prefix', () => { | ||
| expect(() => validateRelativePath('./foo', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts nested path with ./ prefix', () => { | ||
| expect(() => validateRelativePath('./foo/bar', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts path with internal parent ref that stays within context', () => { | ||
| expect(() => validateRelativePath('foo/../bar', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts current directory', () => { | ||
| expect(() => validateRelativePath('.', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts glob patterns', () => { | ||
| expect(() => validateRelativePath('*.txt', undefined)).not.toThrow() | ||
| expect(() => validateRelativePath('**/*.ts', undefined)).not.toThrow() | ||
| expect(() => validateRelativePath('src/**/*', undefined)).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts hidden files and directories', () => { | ||
| expect(() => validateRelativePath('.hidden', undefined)).not.toThrow() | ||
| expect(() => | ||
| validateRelativePath('.config/settings', undefined) | ||
| ).not.toThrow() | ||
| }) | ||
|
|
||
| test('accepts filenames starting with double dots', () => { | ||
| expect(() => validateRelativePath('..myconfig', undefined)).not.toThrow() | ||
| expect(() => validateRelativePath('..cache', undefined)).not.toThrow() | ||
| expect(() => | ||
| validateRelativePath('...something', undefined) | ||
| ).not.toThrow() | ||
| expect(() => | ||
| validateRelativePath('foo/..myconfig', undefined) | ||
| ).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('invalid paths - absolute', () => { | ||
| test('rejects Unix absolute path', () => { | ||
| expect(() => validateRelativePath('/absolute/path', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| expect(() => validateRelativePath('/absolute/path', undefined)).toThrow( | ||
| 'absolute paths are not allowed' | ||
| ) | ||
| }) | ||
|
|
||
| test('rejects root path', () => { | ||
| expect(() => validateRelativePath('/', undefined)).toThrow(TemplateError) | ||
| }) | ||
|
|
||
| // Windows path tests - only run on Windows where path.isAbsolute detects them | ||
| test.skipIf(!isWindows)('rejects Windows drive letter path', () => { | ||
| expect(() => | ||
| validateRelativePath('C:\\Windows\\System32', undefined) | ||
| ).toThrow(TemplateError) | ||
| expect(() => | ||
| validateRelativePath('C:\\Windows\\System32', undefined) | ||
| ).toThrow('absolute paths are not allowed') | ||
| }) | ||
|
|
||
| test.skipIf(!isWindows)('rejects Windows UNC path', () => { | ||
| expect(() => | ||
| validateRelativePath('\\\\server\\share', undefined) | ||
| ).toThrow(TemplateError) | ||
| }) | ||
| }) | ||
|
|
||
| describe('invalid paths - parent directory escape', () => { | ||
| test('rejects simple parent directory escape', () => { | ||
| expect(() => validateRelativePath('../foo', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| expect(() => validateRelativePath('../foo', undefined)).toThrow( | ||
| 'path escapes the context directory' | ||
| ) | ||
| }) | ||
|
|
||
| test('rejects parent directory escape with forward slash', () => { | ||
| expect(() => validateRelativePath('../file.txt', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| }) | ||
|
|
||
| test.skipIf(!isWindows)( | ||
| 'rejects parent directory escape with backslash', | ||
| () => { | ||
| expect(() => validateRelativePath('..\\file.txt', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| test('rejects double parent directory escape', () => { | ||
| expect(() => validateRelativePath('../../foo', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| }) | ||
|
|
||
| test('rejects path that escapes via nested parent refs', () => { | ||
| expect(() => validateRelativePath('foo/../../bar', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| }) | ||
|
|
||
| test('rejects path with ./ prefix that escapes', () => { | ||
| expect(() => | ||
| validateRelativePath('./foo/../../../bar', undefined) | ||
| ).toThrow(TemplateError) | ||
| }) | ||
|
|
||
| test('rejects just parent directory', () => { | ||
| expect(() => validateRelativePath('..', undefined)).toThrow(TemplateError) | ||
| }) | ||
|
|
||
| test('rejects current directory followed by parent', () => { | ||
| expect(() => validateRelativePath('./..', undefined)).toThrow( | ||
| TemplateError | ||
| ) | ||
| }) | ||
|
|
||
| test('rejects deeply nested escape', () => { | ||
| expect(() => | ||
| validateRelativePath('a/b/c/../../../../escape', undefined) | ||
| ).toThrow(TemplateError) | ||
| }) | ||
| }) | ||
|
|
||
| describe('error messages include path', () => { | ||
| test('absolute path error includes the path', () => { | ||
| try { | ||
| validateRelativePath('/etc/passwd', undefined) | ||
| expect.fail('Should have thrown') | ||
| } catch (e) { | ||
| expect(e.message).toContain('/etc/passwd') | ||
| } | ||
| }) | ||
|
|
||
| test('escape path error includes the path', () => { | ||
| try { | ||
| validateRelativePath('../secret', undefined) | ||
| expect.fail('Should have thrown') | ||
| } catch (e) { | ||
| expect(e.message).toContain('../secret') | ||
| } | ||
| }) | ||
| }) | ||
| }) |
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.