Skip to content

Conversation

odaysec
Copy link

@odaysec odaysec commented Sep 18, 2025

Accessing files using paths constructed from user-controlled data can allow an attacker to access unexpected resources. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files. Paths that are naively constructed from data controlled by a user may be absolute paths, or may contain unexpected special characters such as "..". Such a path could point anywhere on the file system.

Description
This PR fixes multiple path traversal vulnerabilities and strengthens input validation to ensure safe filesystem access.

  • web/handler.go (parseImportMap)

    • Normalize and resolve both AppDir and user-supplied paths before file access.
    • Reject requests if the resolved path escapes AppDir.
    • Return "Invalid file name" for invalid input.
  • server/npmrc.go (NewNpmRcFromJSON)

    • Validate zoneId to allow only alphanumeric characters, dashes, and underscores (^[\w\-]+$).
    • Reject unsafe values containing separators, dots, or disallowed characters.
    • Enforce optional length limits.
  • server/build_resolver.go (validateJSFile)

    • Ensure constructed filenames resolve strictly within the safe root (ctx.wd/node_modules/pkgName).
    • Apply absolute path resolution and strict prefix validation.
    • Reject attempts to access files outside the target package directory.
  • internal/storage/storage_fs.go

    • Added a helper function to validate all user-supplied keys/paths.
    • Confirm resolved paths remain within the configured storage root.
    • Updated Stat, Get, Put, Delete, DeleteAll, and List to use the validated paths.

Notes

  • All fixes use the Go standard library (path/filepath, strings, regexp).
  • These changes mitigate directory traversal attacks and improve overall security of filesystem operations.

@ije
Copy link
Member

ije commented Sep 19, 2025

@odaysec thanks, can you please fix the testing?

fix the problem, implement robust validation for user-controlled segments of the path (namely `zoneId` in `NpmRC`).

- In the `StoreDir()` method, after constructing the proposed directory path, normalize it (using `filepath.Clean`) and ensure it’s a subpath of the configured working directory (`config.WorkDir`).
- If validation fails (i.e., the resulting path falls outside of `config.WorkDir`), either return an error or default to a safe directory.
- This change should occur in `server/npmrc.go`, specifically within or around the `StoreDir()` method, since this is the central place where user-controlled data enters the filesystem path.

This fix does not require new methods or complex flow tracking—just robust application of path normalization and containment checks in the StoreDir logic, possibly switching from `path` to `path/filepath` for correct OS path handling.
This PR updates zoneId validation in `NewNpmRcFromJSON`:
- Removed regexp.MustCompile usage
- Replaced with `valid.IsDomain(rc.zoneId)` for better performance
- Removed unused regexp import

Thanks @ije for the suggestion!
@ije
Copy link
Member

ije commented Sep 19, 2025

Screenshot 2025-09-19 at 20 51 32

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.

4 participants