fix(storage): sanitize snapshot tags to safe filenames (prevent path traversal)#13
Open
MD-Mushfiqur123 wants to merge 1 commit into
Open
Conversation
…traversal) This commit fixes issue tanmayjoddar#5 by introducing a sanitizeTag function that ensures snapshot tags are converted to safe filenames before being used in file paths. This prevents several security and compatibility issues: 1. Path traversal attacks: Tags like '../etc/passwd' or '..\windows\system32' are sanitized to prevent escaping the ~/.apidrift/snapshots/ directory. The function strips leading/trailing dots and replaces '..' sequences to eliminate traversal vectors. 2. Windows filename incompatibility: Characters invalid on Windows filesystems (colon, asterisk, question mark, quotes, angle brackets, pipe) are replaced with underscores, ensuring snapshots work cross-platform. 3. Accidental subdirectory creation: Forward slashes and backslashes in tags are replaced with underscores, preventing unintended directory structures within the snapshot storage. The sanitization algorithm: - Allows only A-Z, a-z, 0-9, dot, underscore, and hyphen characters - Replaces all other characters with underscores - Collapses consecutive underscores into single underscores - Strips leading and trailing underscores and dots - Replaces '..' sequences (path traversal) with underscores - Returns 'untitled' as a safe default for empty/invalid input The function is applied in both saveSnapshot() and loadSnapshot() to ensure consistent behavior. Existing safe tags like 'v1.0.13' and 'prod-users' continue to work unchanged. Comprehensive test coverage with 10 test cases validates path traversal prevention, Windows character handling, empty input, consecutive unsafe characters, and preservation of safe characters. All 16 tests pass.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix(storage): sanitize snapshot tags to safe filenames (prevent path traversal)
This commit fixes issue #5 by introducing a sanitizeTag function that
ensures snapshot tags are converted to safe filenames before being used
in file paths. This prevents several security and compatibility issues:
Path traversal attacks: Tags like '../etc/passwd' or '..\windows\system32'
are sanitized to prevent escaping the ~/.apidrift/snapshots/ directory.
The function strips leading/trailing dots and replaces '..' sequences
to eliminate traversal vectors.
Windows filename incompatibility: Characters invalid on Windows
filesystems (colon, asterisk, question mark, quotes, angle brackets, pipe)
are replaced with underscores, ensuring snapshots work cross-platform.
Accidental subdirectory creation: Forward slashes and backslashes
in tags are replaced with underscores, preventing unintended directory
structures within the snapshot storage.
The sanitization algorithm:
The function is applied in both saveSnapshot() and loadSnapshot() to
ensure consistent behavior. Existing safe tags like 'v1.0.13' and
'prod-users' continue to work unchanged.
Comprehensive test coverage with 10 test cases validates path traversal
prevention, Windows character handling, empty input, consecutive unsafe
characters, and preservation of safe characters. All 16 tests pass.