-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular-devkit/schematics): prevent schematic writes from escaping the workspace via symlinks #33325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(@angular-devkit/schematics): prevent schematic writes from escaping the workspace via symlinks #33325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,9 @@ | |||||
|
|
||||||
| import { Path, getSystemPath, normalize, schema, virtualFs } from '@angular-devkit/core'; | ||||||
| import { NodeJsSyncHost } from '@angular-devkit/core/node'; | ||||||
| import { realpathSync } from 'node:fs'; | ||||||
| import { dirname, isAbsolute, relative, resolve as resolveSystemPath } from 'node:path'; | ||||||
| import { Observable } from 'rxjs'; | ||||||
| import { workflow } from '../../src'; | ||||||
| import { BuiltinTaskExecutor } from '../../tasks/node'; | ||||||
| import { FileSystemEngine } from '../description'; | ||||||
|
|
@@ -28,6 +31,72 @@ export interface NodeWorkflowOptions { | |||||
| engineHostCreator?: (options: NodeWorkflowOptions) => NodeModulesEngineHost; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * A {@link virtualFs.ScopedHost} that additionally rejects any write/delete/rename whose real | ||||||
| * (symlink-resolved) location escapes the workspace root. | ||||||
| * | ||||||
| * The lexical containment of `ScopedHost` (and the schematics `Tree`, which rejects `..`) does not | ||||||
| * resolve symlinks, so a workspace that contains a symlinked directory could otherwise route a | ||||||
| * schematic/migration write to a file outside the workspace. This mirrors the realpath-based root | ||||||
| * restriction already used by the MCP host (`createRootRestrictedHost`). | ||||||
| */ | ||||||
| class WorkspaceRootHost<T extends object> extends virtualFs.ScopedHost<T> { | ||||||
| private readonly _systemRoot: string; | ||||||
|
|
||||||
| constructor(delegate: virtualFs.Host<T>, root: Path) { | ||||||
| super(delegate, root); | ||||||
| this._systemRoot = realpathSync(getSystemPath(root)); | ||||||
| } | ||||||
|
|
||||||
| private _assertWithinRoot(path: Path): void { | ||||||
| // Resolve the real path, walking up to the first existing ancestor for not-yet-created files. | ||||||
| let current = resolveSystemPath(getSystemPath(this._resolve(path))); | ||||||
| let real: string; | ||||||
| for (;;) { | ||||||
| try { | ||||||
| real = realpathSync(current); | ||||||
| break; | ||||||
| } catch (e) { | ||||||
| if ((e as NodeJS.ErrnoException).code !== 'ENOENT') { | ||||||
| throw e; | ||||||
| } | ||||||
| const parent = dirname(current); | ||||||
| if (parent === current) { | ||||||
| throw e; | ||||||
| } | ||||||
| current = parent; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const rel = relative(this._systemRoot, real); | ||||||
| if (rel.startsWith('..') || isAbsolute(rel)) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using To prevent this, check if the relative path is exactly
Suggested change
|
||||||
| throw new Error( | ||||||
| `Schematic attempted to access a path outside of the workspace root: ` + | ||||||
| getSystemPath(this._resolve(path)), | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| override write(path: Path, content: virtualFs.FileBuffer): Observable<void> { | ||||||
| this._assertWithinRoot(path); | ||||||
|
|
||||||
| return super.write(path, content); | ||||||
| } | ||||||
|
|
||||||
| override delete(path: Path): Observable<void> { | ||||||
| this._assertWithinRoot(path); | ||||||
|
|
||||||
| return super.delete(path); | ||||||
| } | ||||||
|
|
||||||
| override rename(from: Path, to: Path): Observable<void> { | ||||||
| this._assertWithinRoot(from); | ||||||
| this._assertWithinRoot(to); | ||||||
|
|
||||||
| return super.rename(from, to); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * A workflow specifically for Node tools. | ||||||
| */ | ||||||
|
|
@@ -41,7 +110,7 @@ export class NodeWorkflow extends workflow.BaseWorkflow { | |||||
| let root; | ||||||
| if (typeof hostOrRoot === 'string') { | ||||||
| root = normalize(hostOrRoot); | ||||||
| host = new virtualFs.ScopedHost(new NodeJsSyncHost(), root); | ||||||
| host = new WorkspaceRootHost(new NodeJsSyncHost(), root); | ||||||
| } else { | ||||||
| host = hostOrRoot; | ||||||
| root = options.root; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
sepfromnode:pathto safely check for path boundaries when validating if a path escapes the workspace root.