From 0bc8024d3fa7838e216aa3758be2bf02f50388b3 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 20 Oct 2026 09:10:54 +0000 Subject: [PATCH 1/5] fix(@angular/build): use isSubDirectory in normalizeAssetPatterns and ignore nested .gitkeep - Replace `.startsWith(...)` prefix checks in `normalizeAssetPatterns` with `isSubDirectory(...)` so sibling directories sharing a prefix with `sourceRoot`, `projectRoot`, or `workspaceRoot` (such as `src-assets` when `sourceRoot` is `src`) are resolved against the proper parent directory instead of producing a `..` output path error. - Update `DEFAULT_ASSET_IGNORE` in `resolve-assets.ts` from `'.gitkeep'` to `'**/.gitkeep'` so nested `.gitkeep` files are also ignored, matching `'**/.DS_Store'` and `'**/Thumbs.db'`. - Validate that resolved asset `destination` paths in `resolveAssets` do not escape the output root via `..`. --- .../src/utils/normalize-asset-patterns.ts | 6 +- .../utils/normalize-asset-patterns_spec.ts | 169 ++++++++++++++++++ .../angular/build/src/utils/resolve-assets.ts | 41 +++-- 3 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 packages/angular/build/src/utils/normalize-asset-patterns_spec.ts diff --git a/packages/angular/build/src/utils/normalize-asset-patterns.ts b/packages/angular/build/src/utils/normalize-asset-patterns.ts index defa6511172f..a95d91829766 100644 --- a/packages/angular/build/src/utils/normalize-asset-patterns.ts +++ b/packages/angular/build/src/utils/normalize-asset-patterns.ts @@ -55,11 +55,11 @@ export function normalizeAssetPatterns( let root: string; // Check if the string asset is within sourceRoot. - if (resolvedAssetPath.startsWith(resolvedSourceRoot)) { + if (isSubDirectory(resolvedSourceRoot, resolvedAssetPath)) { root = resolvedSourceRoot; - } else if (resolvedAssetPath.startsWith(resolvedProjectRoot)) { + } else if (isSubDirectory(resolvedProjectRoot, resolvedAssetPath)) { root = resolvedProjectRoot; - } else if (resolvedAssetPath.startsWith(resolvedWorkspaceRoot)) { + } else if (isSubDirectory(resolvedWorkspaceRoot, resolvedAssetPath)) { root = resolvedWorkspaceRoot; } else { throw new Error(`The ${assetPattern} asset path must be within the workspace root.`); diff --git a/packages/angular/build/src/utils/normalize-asset-patterns_spec.ts b/packages/angular/build/src/utils/normalize-asset-patterns_spec.ts new file mode 100644 index 000000000000..711a6716afb1 --- /dev/null +++ b/packages/angular/build/src/utils/normalize-asset-patterns_spec.ts @@ -0,0 +1,169 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import assert from 'node:assert'; +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import path, { join } from 'node:path'; +import { normalizeAssetPatterns } from './normalize-asset-patterns'; +import { resolveAssets } from './resolve-assets'; + +describe('normalizeAssetPatterns', () => { + let tempRoot: string; + + beforeEach(async () => { + const baseTmpDir = process.env['TEST_TMPDIR']; + assert(baseTmpDir, 'TEST_TMPDIR is not set'); + tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-assets-test-')); + }); + + afterEach(async () => { + if (tempRoot) { + await rm(tempRoot, { recursive: true, force: true }); + } + }); + + it('should return an empty array when given no asset patterns', () => { + expect(normalizeAssetPatterns([], tempRoot, '', 'src')).toEqual([]); + }); + + it('should normalize a string asset within sourceRoot relative to sourceRoot', async () => { + await mkdir(join(tempRoot, 'src/assets'), { recursive: true }); + await writeFile(join(tempRoot, 'src/assets/logo.png'), 'PNG'); + + const result = normalizeAssetPatterns( + ['src/assets', 'src/assets/logo.png'], + tempRoot, + '', + 'src', + ); + + expect(result).toEqual([ + { glob: '**/*', input: 'src/assets', output: 'assets' }, + { glob: 'logo.png', input: 'src/assets', output: 'assets' }, + ]); + }); + + it('should normalize a string asset in a sibling directory sharing a prefix with sourceRoot (e.g. src-assets)', async () => { + await mkdir(join(tempRoot, 'src-assets'), { recursive: true }); + await writeFile(join(tempRoot, 'src-assets/logo.png'), 'PNG'); + + const result = normalizeAssetPatterns( + ['src-assets', 'src-assets/logo.png'], + tempRoot, + '', + 'src', + ); + + expect(result).toEqual([ + { glob: '**/*', input: 'src-assets', output: 'src-assets' }, + { glob: 'logo.png', input: 'src-assets', output: 'src-assets' }, + ]); + }); + + it('should normalize a library string asset in a sibling directory sharing a prefix with projectSourceRoot', async () => { + await mkdir(join(tempRoot, 'projects/lib/src-assets'), { recursive: true }); + await writeFile(join(tempRoot, 'projects/lib/src-assets/logo.png'), 'PNG'); + + const result = normalizeAssetPatterns( + ['projects/lib/src-assets', 'projects/lib/src-assets/logo.png'], + tempRoot, + 'projects/lib', + 'projects/lib/src', + ); + + expect(result).toEqual([ + { + glob: '**/*', + input: path.normalize('projects/lib/src-assets'), + output: 'src-assets', + }, + { + glob: 'logo.png', + input: path.normalize('projects/lib/src-assets'), + output: 'src-assets', + }, + ]); + }); + + it('should normalize a string asset in a sibling directory sharing a prefix with projectRoot relative to workspaceRoot', async () => { + await mkdir(join(tempRoot, 'projects/lib-shared'), { recursive: true }); + await writeFile(join(tempRoot, 'projects/lib-shared/logo.png'), 'PNG'); + + const result = normalizeAssetPatterns( + ['projects/lib-shared/logo.png'], + tempRoot, + 'projects/lib', + 'projects/lib/src', + ); + + expect(result).toEqual([ + { + glob: 'logo.png', + input: path.normalize('projects/lib-shared'), + output: path.normalize('projects/lib-shared'), + }, + ]); + }); + + it('should throw when a string asset is in a sibling directory sharing a prefix with workspaceRoot', () => { + const siblingDir = `../${path.basename(tempRoot)}-outside/logo.png`; + + expect(() => normalizeAssetPatterns([siblingDir], tempRoot, '', 'src')).toThrowError( + /asset path must be within the workspace root/, + ); + }); +}); + +describe('resolveAssets', () => { + let tempRoot: string; + + beforeEach(async () => { + const baseTmpDir = process.env['TEST_TMPDIR']; + assert(baseTmpDir, 'TEST_TMPDIR is not set'); + tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-resolve-assets-test-')); + }); + + afterEach(async () => { + if (tempRoot) { + await rm(tempRoot, { recursive: true, force: true }); + } + }); + + it('should ignore both root and nested .gitkeep files by default', async () => { + await mkdir(join(tempRoot, 'src/assets/nested/deep'), { recursive: true }); + await writeFile(join(tempRoot, 'src/assets/.gitkeep'), ''); + await writeFile(join(tempRoot, 'src/assets/nested/.gitkeep'), ''); + await writeFile(join(tempRoot, 'src/assets/nested/deep/.gitkeep'), ''); + await writeFile(join(tempRoot, 'src/assets/nested/deep/logo.png'), 'PNG'); + + const resolved = await resolveAssets( + [{ glob: '**/*', input: 'src/assets', output: 'assets' }], + tempRoot, + ); + + expect(resolved).toEqual([ + { + source: join(tempRoot, 'src/assets/nested/deep/logo.png'), + destination: join('assets', 'nested/deep/logo.png'), + }, + ]); + }); + + it('should throw when a resolved destination escapes the output root via ".." in glob', async () => { + await mkdir(join(tempRoot, 'src'), { recursive: true }); + await writeFile(join(tempRoot, 'outside.txt'), 'secret'); + + await expectAsync( + resolveAssets([{ glob: '../outside.txt', input: 'src', output: '.' }], tempRoot), + ).toBeRejectedWithError('An asset cannot be written to a location outside of the output path.'); + + await expectAsync( + resolveAssets([{ glob: '../*.txt', input: 'src', output: '.' }], tempRoot), + ).toBeRejectedWithError('An asset cannot be written to a location outside of the output path.'); + }); +}); diff --git a/packages/angular/build/src/utils/resolve-assets.ts b/packages/angular/build/src/utils/resolve-assets.ts index 06a6c6c157e9..02e8a5f7c817 100644 --- a/packages/angular/build/src/utils/resolve-assets.ts +++ b/packages/angular/build/src/utils/resolve-assets.ts @@ -15,7 +15,7 @@ import { isSubDirectory } from './path'; /** * Default glob ignore patterns for assets. */ -export const DEFAULT_ASSET_IGNORE = ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'] as const; +export const DEFAULT_ASSET_IGNORE = ['**/.gitkeep', '**/.DS_Store', '**/Thumbs.db'] as const; export async function resolveAssets( entries: { @@ -38,19 +38,25 @@ export async function resolveAssets( if (!entry.ignore?.length && !isDynamicPattern(entry.glob)) { const src = path.join(cwd, entry.glob); if (isSubDirectory(cwd, src)) { + let stats; try { - const stats = await (entry.followSymlinks ? stat(src) : lstat(src)); - if (stats.isFile()) { - const filePath = entry.flatten ? path.basename(entry.glob) : entry.glob; + stats = await (entry.followSymlinks ? stat(src) : lstat(src)); + } catch { + // File does not exist or cannot be accessed. + return []; + } - return [{ source: src, destination: path.join(entry.output, filePath) }]; + if (stats.isFile()) { + const filePath = entry.flatten ? path.basename(entry.glob) : entry.glob; + const destination = path.join(entry.output, filePath); + if (destination === '..' || destination.startsWith(`..${path.sep}`)) { + throw new Error('An asset cannot be written to a location outside of the output path.'); } - if (!stats.isSymbolicLink()) { - return []; - } - } catch { - // File does not exist or cannot be accessed. + return [{ source: src, destination }]; + } + + if (!stats.isSymbolicLink()) { return []; } } @@ -63,10 +69,17 @@ export async function resolveAssets( followSymbolicLinks: entry.followSymlinks ?? false, }); - return files.map((file) => ({ - source: path.join(cwd, file), - destination: path.join(entry.output, entry.flatten ? path.basename(file) : file), - })); + return files.map((file) => { + const destination = path.join(entry.output, entry.flatten ? path.basename(file) : file); + if (destination === '..' || destination.startsWith(`..${path.sep}`)) { + throw new Error('An asset cannot be written to a location outside of the output path.'); + } + + return { + source: path.join(cwd, file), + destination, + }; + }); }); return resolvedEntries.flat(); From 5f77439f24b43070eb59a3c361d37ea4f0301c4c Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 20 Oct 2026 09:27:36 +0000 Subject: [PATCH 2/5] fixup! fix(@angular/build): use isSubDirectory in normalizeAssetPatterns and ignore nested .gitkeep --- .../angular/build/src/utils/resolve-assets.ts | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/angular/build/src/utils/resolve-assets.ts b/packages/angular/build/src/utils/resolve-assets.ts index 02e8a5f7c817..8a599f9f100c 100644 --- a/packages/angular/build/src/utils/resolve-assets.ts +++ b/packages/angular/build/src/utils/resolve-assets.ts @@ -17,6 +17,15 @@ import { isSubDirectory } from './path'; */ export const DEFAULT_ASSET_IGNORE = ['**/.gitkeep', '**/.DS_Store', '**/Thumbs.db'] as const; +function resolveDestination(output: string, file: string, flatten?: boolean): string { + const destination = path.join(output, flatten ? path.basename(file) : file); + if (destination === '..' || destination.startsWith(`..${path.sep}`)) { + throw new Error('An asset cannot be written to a location outside of the output path.'); + } + + return destination; +} + export async function resolveAssets( entries: { glob: string; @@ -47,13 +56,12 @@ export async function resolveAssets( } if (stats.isFile()) { - const filePath = entry.flatten ? path.basename(entry.glob) : entry.glob; - const destination = path.join(entry.output, filePath); - if (destination === '..' || destination.startsWith(`..${path.sep}`)) { - throw new Error('An asset cannot be written to a location outside of the output path.'); - } - - return [{ source: src, destination }]; + return [ + { + source: src, + destination: resolveDestination(entry.output, entry.glob, entry.flatten), + }, + ]; } if (!stats.isSymbolicLink()) { @@ -69,17 +77,10 @@ export async function resolveAssets( followSymbolicLinks: entry.followSymlinks ?? false, }); - return files.map((file) => { - const destination = path.join(entry.output, entry.flatten ? path.basename(file) : file); - if (destination === '..' || destination.startsWith(`..${path.sep}`)) { - throw new Error('An asset cannot be written to a location outside of the output path.'); - } - - return { - source: path.join(cwd, file), - destination, - }; - }); + return files.map((file) => ({ + source: path.join(cwd, file), + destination: resolveDestination(entry.output, file, entry.flatten), + })); }); return resolvedEntries.flat(); From b0d0c6fc0eb7d025bae97e191e051592a0033908 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 20 Oct 2026 09:50:52 +0000 Subject: [PATCH 3/5] fixup! fix(@angular/build): use isSubDirectory in normalizeAssetPatterns and ignore nested .gitkeep --- packages/angular/build/src/utils/resolve-assets.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/src/utils/resolve-assets.ts b/packages/angular/build/src/utils/resolve-assets.ts index 8a599f9f100c..0801add072dd 100644 --- a/packages/angular/build/src/utils/resolve-assets.ts +++ b/packages/angular/build/src/utils/resolve-assets.ts @@ -19,7 +19,11 @@ export const DEFAULT_ASSET_IGNORE = ['**/.gitkeep', '**/.DS_Store', '**/Thumbs.d function resolveDestination(output: string, file: string, flatten?: boolean): string { const destination = path.join(output, flatten ? path.basename(file) : file); - if (destination === '..' || destination.startsWith(`..${path.sep}`)) { + if ( + path.isAbsolute(destination) || + destination === '..' || + destination.startsWith(`..${path.sep}`) + ) { throw new Error('An asset cannot be written to a location outside of the output path.'); } From 08c1c79d4f9b8e7f421842d6ebf7750a45d89d36 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 20 Oct 2026 10:23:35 +0000 Subject: [PATCH 4/5] fixup! fix(@angular/build): use isSubDirectory in normalizeAssetPatterns and ignore nested .gitkeep --- packages/angular/build/src/utils/resolve-assets.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/angular/build/src/utils/resolve-assets.ts b/packages/angular/build/src/utils/resolve-assets.ts index 0801add072dd..22c51d633034 100644 --- a/packages/angular/build/src/utils/resolve-assets.ts +++ b/packages/angular/build/src/utils/resolve-assets.ts @@ -17,13 +17,9 @@ import { isSubDirectory } from './path'; */ export const DEFAULT_ASSET_IGNORE = ['**/.gitkeep', '**/.DS_Store', '**/Thumbs.db'] as const; -function resolveDestination(output: string, file: string, flatten?: boolean): string { - const destination = path.join(output, flatten ? path.basename(file) : file); - if ( - path.isAbsolute(destination) || - destination === '..' || - destination.startsWith(`..${path.sep}`) - ) { +function resolveDestination(output: string, filePath: string, flatten?: boolean): string { + const destination = path.join(output, flatten ? path.basename(filePath) : filePath); + if (destination === '..' || destination.startsWith(`..${path.sep}`)) { throw new Error('An asset cannot be written to a location outside of the output path.'); } From 72fcaf0b6e1131793a0e2fbf9ffbcb87c900ab70 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 20 Oct 2026 13:06:07 +0000 Subject: [PATCH 5/5] fixup! fix(@angular/build): use isSubDirectory in normalizeAssetPatterns and ignore nested .gitkeep --- .../angular/build/src/utils/resolve-assets.ts | 49 ++++++++----------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/packages/angular/build/src/utils/resolve-assets.ts b/packages/angular/build/src/utils/resolve-assets.ts index 22c51d633034..04a25e63c9db 100644 --- a/packages/angular/build/src/utils/resolve-assets.ts +++ b/packages/angular/build/src/utils/resolve-assets.ts @@ -17,15 +17,6 @@ import { isSubDirectory } from './path'; */ export const DEFAULT_ASSET_IGNORE = ['**/.gitkeep', '**/.DS_Store', '**/Thumbs.db'] as const; -function resolveDestination(output: string, filePath: string, flatten?: boolean): string { - const destination = path.join(output, flatten ? path.basename(filePath) : filePath); - if (destination === '..' || destination.startsWith(`..${path.sep}`)) { - throw new Error('An asset cannot be written to a location outside of the output path.'); - } - - return destination; -} - export async function resolveAssets( entries: { glob: string; @@ -47,24 +38,19 @@ export async function resolveAssets( if (!entry.ignore?.length && !isDynamicPattern(entry.glob)) { const src = path.join(cwd, entry.glob); if (isSubDirectory(cwd, src)) { - let stats; try { - stats = await (entry.followSymlinks ? stat(src) : lstat(src)); - } catch { - // File does not exist or cannot be accessed. - return []; - } + const stats = await (entry.followSymlinks ? stat(src) : lstat(src)); + if (stats.isFile()) { + const filePath = entry.flatten ? path.basename(entry.glob) : entry.glob; - if (stats.isFile()) { - return [ - { - source: src, - destination: resolveDestination(entry.output, entry.glob, entry.flatten), - }, - ]; - } + return [{ source: src, destination: path.join(entry.output, filePath) }]; + } - if (!stats.isSymbolicLink()) { + if (!stats.isSymbolicLink()) { + return []; + } + } catch { + // File does not exist or cannot be accessed. return []; } } @@ -77,10 +63,17 @@ export async function resolveAssets( followSymbolicLinks: entry.followSymlinks ?? false, }); - return files.map((file) => ({ - source: path.join(cwd, file), - destination: resolveDestination(entry.output, file, entry.flatten), - })); + return files.map((file) => { + const destination = path.join(entry.output, entry.flatten ? path.basename(file) : file); + if (destination === '..' || destination.startsWith(`..${path.sep}`)) { + throw new Error('An asset cannot be written to a location outside of the output path.'); + } + + return { + source: path.join(cwd, file), + destination, + }; + }); }); return resolvedEntries.flat();