Skip to content

fix(storage): sanitize snapshot tags to safe filenames (prevent path traversal)#13

Open
MD-Mushfiqur123 wants to merge 1 commit into
tanmayjoddar:mainfrom
MD-Mushfiqur123:fix/sanitize-snapshot-tags
Open

fix(storage): sanitize snapshot tags to safe filenames (prevent path traversal)#13
MD-Mushfiqur123 wants to merge 1 commit into
tanmayjoddar:mainfrom
MD-Mushfiqur123:fix/sanitize-snapshot-tags

Conversation

@MD-Mushfiqur123

Copy link
Copy Markdown

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:

  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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant