angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

fix(@angular/build): prevent deleting parent directories of project root

Open pradhankukiran opened this issue 8 months ago • 8 comments

PR Checklist

Please check to confirm your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/angular/angular-cli/blob/main/CONTRIBUTING.md#-commit-message-guidelines
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [x] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Documentation content changes
  • [ ] Other... Please describe:

What is the current behavior?

The deleteOutputDir utility only checks if the output path is exactly equal to the project root. Setting the output path to a parent directory (e.g. ../ or ../../) bypasses this check, and ng build silently deletes all contents of that directory potentially wiping out source files or entire directory trees.

Closes: #6485

What is the new behavior?

Uses path.relative() to detect when the resolved output path is the project root OR any ancestor of it, and throws a descriptive error in both cases.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

The original issue reported a user losing 10 days of work after setting outDir: "../../" which caused ng build --prod to delete their entire project directory. This fix closes the gap that has been open since 2017.

pradhankukiran avatar Mar 27 '26 12:03 pradhankukiran

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Mar 27 '26 12:03 google-cla[bot]

Thanks for the review! I tested absolute output paths and they seem to work correctly /tmp/build and /home/other/build are allowed, only ancestors like /home/user (parent of /home/user/project) are blocked. Could you share a specific example of the absolute path case that would break? Happy to adjust.

pradhankukiran avatar Mar 27 '26 13:03 pradhankukiran

@pradhankukiran, based on the logic there is no special handling of absolute paths and thus it will fail. All absolute paths should be allowed.

Example:

deleteOutputDir('/my-project/app', '/tmp')

alan-agius4 avatar Mar 27 '26 13:03 alan-agius4

I tested your exact example deleteOutputDir('/my-project/app', '/tmp') is allowed by the check. relative('/tmp', '/my-project/app') returns ../my-project/app which starts with .., so it passes. The check only blocks when the project root is inside the output path (the output path is an ancestor). Happy to add a test case for this if it helps.

pradhankukiran avatar Mar 27 '26 14:03 pradhankukiran

On Windows, drive letters and UNC paths are also absolute and should be handled here as well.

clydin avatar Mar 27 '26 14:03 clydin

@pradhankukiran, as @clydin mentioned absolute paths needs to be explicitly handled.

alan-agius4 avatar Mar 27 '26 14:03 alan-agius4

Thanks for the feedback! I've added an isAbsolute() guard so that Windows drive letters and UNC paths are correctly allowed. Also added a test case for absolute paths outside the project. The nit is applied too

pradhankukiran avatar Mar 27 '26 14:03 pradhankukiran

@alan-agius4 updated the approach per your suggestion, exact root match, and relative paths escaping the workspace. Absolute paths are now always allowed.
Also added root + sep instead of bare startsWith(root) to avoid false matches on paths like project-backup vs project.

pradhankukiran avatar Mar 27 '26 16:03 pradhankukiran