-
-
Notifications
You must be signed in to change notification settings - Fork 281
fix: path traversal for tus #1039
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
Open
ferhatelmas
wants to merge
1
commit into
master
Choose a base branch
from
ferhat/tus-path-traversal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import path from 'node:path' | ||
| import { resolveSecureFilesystemPath } from './secure-path' | ||
|
|
||
| describe('resolveSecureFilesystemPath', () => { | ||
| const storageRoot = path.resolve('tmp', 'storage-root') | ||
|
|
||
| it('resolves safe paths under the storage root', () => { | ||
| expect(resolveSecureFilesystemPath(storageRoot, 'bucket/folder/file.txt')).toBe( | ||
| path.join(storageRoot, 'bucket', 'folder', 'file.txt') | ||
| ) | ||
| }) | ||
|
|
||
| it('normalizes the configured root path before resolving children', () => { | ||
| const rootWithParentSegment = path.join(storageRoot, 'tenant', '..') | ||
|
|
||
| expect(resolveSecureFilesystemPath(rootWithParentSegment, 'bucket/file.txt')).toBe( | ||
| path.join(storageRoot, 'bucket', 'file.txt') | ||
| ) | ||
| }) | ||
|
|
||
| it('allows child paths when the configured root is the filesystem root', () => { | ||
| const filesystemRoot = path.parse(storageRoot).root | ||
|
|
||
| expect(resolveSecureFilesystemPath(filesystemRoot, 'bucket/file.txt')).toBe( | ||
| path.join(filesystemRoot, 'bucket', 'file.txt') | ||
| ) | ||
| }) | ||
|
|
||
| it('allows double-dot in file names when not used as a path segment', () => { | ||
| expect(resolveSecureFilesystemPath(storageRoot, 'bucket/file..name.txt')).toBe( | ||
| path.join(storageRoot, 'bucket', 'file..name.txt') | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| 'bucket/dir/../file.txt', | ||
| '.', | ||
| '../escape.txt', | ||
| ])('rejects dot path segments in %s', (relativePath) => { | ||
| expect(() => resolveSecureFilesystemPath(storageRoot, relativePath)).toThrow( | ||
| expect.objectContaining({ | ||
| code: 'InvalidKey', | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('rejects absolute posix paths', () => { | ||
| expect(() => resolveSecureFilesystemPath(storageRoot, '/tmp/escape.txt')).toThrow( | ||
| expect.objectContaining({ | ||
| code: 'InvalidKey', | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| 'C:\\temp\\escape.txt', | ||
| '\\\\server\\share\\escape.txt', | ||
| ])('rejects absolute windows paths in %s format', (relativePath) => { | ||
| expect(() => resolveSecureFilesystemPath(storageRoot, relativePath)).toThrow( | ||
| expect.objectContaining({ | ||
| code: 'InvalidKey', | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('rejects null bytes', () => { | ||
| expect(() => resolveSecureFilesystemPath(storageRoot, 'bucket/\0escape.txt')).toThrow( | ||
| expect.objectContaining({ | ||
| code: 'InvalidKey', | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { | ||
| relativePath: 'bucket/\0escape.txt', | ||
| expectedMessage: 'Invalid key: bucket/\0escape.txt contains null byte', | ||
| }, | ||
| { | ||
| relativePath: '/tmp/escape.txt', | ||
| expectedMessage: 'Invalid key: /tmp/escape.txt must be a relative path', | ||
| }, | ||
| { | ||
| relativePath: '../escape.txt', | ||
| expectedMessage: 'Invalid key: ../escape.txt contains dot path segment', | ||
| }, | ||
| ])('keeps InvalidKey resource and message aligned for %s', ({ | ||
| relativePath, | ||
| expectedMessage, | ||
| }) => { | ||
| try { | ||
| resolveSecureFilesystemPath(storageRoot, relativePath) | ||
| throw new Error('expected resolveSecureFilesystemPath to throw') | ||
| } catch (error) { | ||
| expect(error).toMatchObject({ | ||
| code: 'InvalidKey', | ||
| message: expectedMessage, | ||
| resource: relativePath, | ||
| }) | ||
| } | ||
| }) | ||
| }) |
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,53 @@ | ||
| import { ErrorCode, StorageBackendError } from '@internal/errors' | ||
| import path from 'path' | ||
|
|
||
| /** | ||
| * Resolve a user-controlled relative path under a fixed filesystem root. | ||
| * Rejects null bytes, absolute paths, and `.` / `..` path segments before resolution. | ||
| */ | ||
| export function resolveSecureFilesystemPath(rootPath: string, relativePath: string): string { | ||
| if (relativePath.includes('\0')) { | ||
| throwInvalidKey(relativePath, 'contains null byte') | ||
| } | ||
|
|
||
| if (path.isAbsolute(relativePath)) { | ||
| throwInvalidKey(relativePath, 'must be a relative path') | ||
| } | ||
|
|
||
| const isWindowsDriveAbsolutePath = /^[a-zA-Z]:[\\/]/.test(relativePath) | ||
| const isWindowsUncPath = /^\\\\[^\\/]+[\\/][^\\/]+/.test(relativePath) | ||
| if (isWindowsDriveAbsolutePath || isWindowsUncPath) { | ||
| throwInvalidKey(relativePath, 'must not be an absolute Windows path') | ||
| } | ||
|
|
||
| const hasDotTraversalSegment = relativePath | ||
| .split(/[\\/]+/) | ||
| .filter(Boolean) | ||
| .some((segment) => segment === '.' || segment === '..') | ||
|
|
||
| if (hasDotTraversalSegment) { | ||
| throwInvalidKey(relativePath, 'contains dot path segment') | ||
| } | ||
|
|
||
| const normalizedRootPath = path.normalize(path.resolve(rootPath)) | ||
| const resolvedPath = path.resolve(normalizedRootPath, relativePath) | ||
| const normalizedPath = path.normalize(resolvedPath) | ||
| const normalizedRootPrefix = normalizedRootPath.endsWith(path.sep) | ||
| ? normalizedRootPath | ||
| : normalizedRootPath + path.sep | ||
|
|
||
| if (!normalizedPath.startsWith(normalizedRootPrefix) && normalizedPath !== normalizedRootPath) { | ||
| throwInvalidKey(relativePath, 'resolves outside storage directory') | ||
| } | ||
|
|
||
| return normalizedPath | ||
| } | ||
|
|
||
| function throwInvalidKey(relativePath: string, reason: string): never { | ||
| throw new StorageBackendError({ | ||
| code: ErrorCode.InvalidKey, | ||
| resource: relativePath, | ||
| httpStatusCode: 400, | ||
| message: `Invalid key: ${relativePath} ${reason}`, | ||
| }) | ||
| } | ||
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.