fix: sanitize snapshot tags to prevent invalid chars and path traversal - #24
Open
vivekyadav-3 wants to merge 11 commits into
Open
fix: sanitize snapshot tags to prevent invalid chars and path traversal#24vivekyadav-3 wants to merge 11 commits into
vivekyadav-3 wants to merge 11 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens snapshot filename handling by sanitizing user-supplied snapshot tags to avoid invalid characters and path traversal, and also makes HTTP request timeouts configurable via an environment variable.
Changes:
- Added
sanitizeTag()and applied it to snapshot save/load paths; sorted snapshot listing output. - Introduced
APIDRIFT_TIMEOUT_MSto configure axios request timeouts. - Expanded CLI init messaging and documented environment variables + troubleshooting in the README.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/storage/snapshotStore.js | Adds sanitizeTag() and uses it for snapshot file paths; sorts listed snapshots. |
| src/core/fetcher.js | Adds configurable request timeout via APIDRIFT_TIMEOUT_MS. |
| src/commands/init.js | Improves messaging when keeping an existing config. |
| README.md | Documents environment variables and adds troubleshooting guidance for timeouts/auth. |
Comments suppressed due to low confidence (1)
src/storage/snapshotStore.js:54
- This change will make previously-created snapshots with unsafe-but-valid POSIX filenames (e.g. tags with spaces/colons saved by older versions) no longer loadable because loadSnapshot now sanitizes the tag before reading. Consider a migration/fallback: if the sanitized path doesn’t exist, try the legacy
${tag}.jsononly when it resolves under SNAP_DIR (no traversal), or proactively rename legacy files on startup/listing.
export function loadSnapshot(tag) {
const safe = sanitizeTag(tag);
const file = path.join(SNAP_DIR, `${safe}.json`);
if (!fs.existsSync(file)) {
console.error(`Snapshot "${tag}" not found. Run: apidrift list`);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| if (!sanitized) { | ||
| throw new Error( | ||
| `Invalid snapshot tag "${tag}": tag must contain at least one alphanumeric character.` |
Comment on lines
+11
to
+15
| * Sanitize a snapshot tag so it is safe to use as a filename on all platforms. | ||
| * | ||
| * Rules: | ||
| * - Only A-Z, a-z, 0-9, `.`, `_`, and `-` are allowed. | ||
| * - Every other character (including `:`, `/`, `\`, and space) is replaced with `_`. |
Comment on lines
43
to
46
| export function saveSnapshot(tag, data) { | ||
| const file = path.join(SNAP_DIR, `${tag}.json`); | ||
| const safe = sanitizeTag(tag); | ||
| const file = path.join(SNAP_DIR, `${safe}.json`); | ||
| fs.writeFileSync(file, JSON.stringify(data, null, 2)); |
Comment on lines
+26
to
+33
| export function sanitizeTag(tag) { | ||
| // Replace every character that is NOT in the safe set with '_' | ||
| const sanitized = String(tag) | ||
| .replace(/[^A-Za-z0-9._-]/g, "_") | ||
| // Collapse consecutive underscores for readability (optional, keeps names clean) | ||
| .replace(/_+/g, "_") | ||
| // Remove leading/trailing underscores and dots | ||
| .replace(/^[._]+|[._]+$/g, ""); |
…ent-exit-on-no-overwrite
…rable-http-timeout-env-var
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.
Summary
Closes #5
Snapshot files were saved as
${tag}.jsonwithout any validation.A tag like
../evilorv1:prodwould break on Windows or allowpath traversal outside
~/.apidrift/snapshots/.Changes
src/storage/snapshotStore.js: Added exportedsanitizeTag()functionA-Z a-z 0-9 . _ -(safe on all OS):,/,\, spaces, etc.) with_Before / After
v1.0.13v1.0.13.json✅v1.0.13.json✅prod:v2prod:v2.json❌ (Windows)prod_v2.json✅../evil../evil.json🔴 traversal_evil.jsonwait... →evil.json✅v1 finalv1 final.json❌v1_final.json✅Notes
v1.0.13,prod-userspass through unchanged ✅sanitizeTagis exported so it can be tested and reused ✅