Skip to content

Commit b474a0c

Browse files
committed
fix(@angular/build): contain asset symlinks within the workspace root
Asset inputs are validated against the workspace root before they are globbed, but the check compares paths textually while a symbolic link only resolves to its target on disk. A link committed inside an asset directory therefore passes validation and the globber reads through it, copying files from outside the workspace into the build output. The globber also followed links by default. `followSymlinks` is documented as `false` in the builder schema, but schema defaults are not applied to builder options, so the option arrived as `undefined` and tinyglobby treats that as enabled. Resolve the asset input to its real location before the containment check, provide the documented default explicitly, and verify that each resolved file stays within the workspace root when links are traversed. Links that resolve inside the workspace root, such as those produced by a package manager's workspace layout, keep working with `followSymlinks` enabled, and an input that does not exist is still resolved textually so that a workspace reached through a link is unaffected. Apply the same containment and default to the assets of the webpack builders in `@angular-devkit/build-angular`.
1 parent d968259 commit b474a0c

11 files changed

Lines changed: 401 additions & 13 deletions

File tree

‎packages/angular/build/src/builders/application/schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@
633633
"followSymlinks": {
634634
"type": "boolean",
635635
"default": false,
636-
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched."
636+
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched. Symlinks that resolve outside of the workspace root are not allowed."
637637
},
638638
"glob": {
639639
"type": "string",

‎packages/angular/build/src/builders/application/tests/options/assets_spec.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import * as fs from 'node:fs';
10+
import * as os from 'node:os';
11+
import * as path from 'node:path';
912
import { buildApplication } from '../../index';
1013
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
1114

@@ -409,5 +412,109 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
409412
harness.expectFile('dist/browser/test.svg').toNotExist();
410413
});
411414
});
415+
416+
describe('symbolic links', () => {
417+
let outsideDirectory: string;
418+
419+
beforeEach(() => {
420+
outsideDirectory = fs.realpathSync(
421+
fs.mkdtempSync(path.join(os.tmpdir(), 'outside-workspace-')),
422+
);
423+
fs.writeFileSync(path.join(outsideDirectory, 'secret.txt'), 'secret');
424+
});
425+
426+
afterEach(() => {
427+
fs.rmSync(outsideDirectory, { recursive: true, force: true });
428+
});
429+
430+
it('does not copy files behind a directory link by default', async () => {
431+
await harness.writeFile('public/favicon.ico', 'icon');
432+
fs.symlinkSync(outsideDirectory, harness.resolvePath('public/docs'), 'junction');
433+
434+
harness.useTarget('build', {
435+
...BASE_OPTIONS,
436+
assets: [{ glob: '**/*', input: 'public', output: '.' }],
437+
});
438+
439+
const { result } = await harness.executeOnce();
440+
441+
expect(result?.success).toBe(true);
442+
443+
harness.expectFile('dist/browser/favicon.ico').toExist();
444+
harness.expectFile('dist/browser/docs/secret.txt').toNotExist();
445+
});
446+
447+
it('does not copy a file link by default', async () => {
448+
await harness.writeFile('public/favicon.ico', 'icon');
449+
fs.symlinkSync(
450+
path.join(outsideDirectory, 'secret.txt'),
451+
harness.resolvePath('public/notes.txt'),
452+
);
453+
454+
harness.useTarget('build', {
455+
...BASE_OPTIONS,
456+
assets: [{ glob: '**/*', input: 'public', output: '.' }],
457+
});
458+
459+
const { result } = await harness.executeOnce();
460+
461+
expect(result?.success).toBe(true);
462+
463+
harness.expectFile('dist/browser/favicon.ico').toExist();
464+
harness.expectFile('dist/browser/notes.txt').toNotExist();
465+
});
466+
467+
it('copies files behind a directory link within the workspace root when enabled', async () => {
468+
await harness.writeFile('public/favicon.ico', 'icon');
469+
await harness.writeFile('shared/logo.svg', '<svg></svg>');
470+
fs.symlinkSync(
471+
harness.resolvePath('shared'),
472+
harness.resolvePath('public/branding'),
473+
'junction',
474+
);
475+
476+
harness.useTarget('build', {
477+
...BASE_OPTIONS,
478+
assets: [{ glob: '**/*', input: 'public', output: '.', followSymlinks: true }],
479+
});
480+
481+
const { result } = await harness.executeOnce();
482+
483+
expect(result?.success).toBe(true);
484+
485+
harness.expectFile('dist/browser/branding/logo.svg').content.toBe('<svg></svg>');
486+
});
487+
488+
it('fails if asset input option is a link outside workspace root', async () => {
489+
fs.symlinkSync(outsideDirectory, harness.resolvePath('public'), 'junction');
490+
491+
harness.useTarget('build', {
492+
...BASE_OPTIONS,
493+
assets: [{ glob: '**/*', input: 'public', output: '.' }],
494+
});
495+
496+
const { error } = await harness.executeOnce({ outputLogsOnException: false });
497+
498+
expect(error?.message).toContain('asset path must be within the workspace root');
499+
500+
harness.expectFile('dist/browser/secret.txt').toNotExist();
501+
});
502+
503+
it('fails if a followed link resolves outside workspace root', async () => {
504+
await harness.writeFile('public/favicon.ico', 'icon');
505+
fs.symlinkSync(outsideDirectory, harness.resolvePath('public/docs'), 'junction');
506+
507+
harness.useTarget('build', {
508+
...BASE_OPTIONS,
509+
assets: [{ glob: '**/*', input: 'public', output: '.', followSymlinks: true }],
510+
});
511+
512+
const { error } = await harness.executeOnce({ outputLogsOnException: false });
513+
514+
expect(error?.message).toContain('asset path must be within the workspace root');
515+
516+
harness.expectFile('dist/browser/docs/secret.txt').toNotExist();
517+
});
518+
});
412519
});
413520
});

‎packages/angular/build/src/utils/path.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { realpath } from 'node:fs/promises';
910
import { isAbsolute, posix, relative, resolve } from 'node:path';
1011
import { platform } from 'node:process';
1112

@@ -51,6 +52,21 @@ export function isSubDirectory(parent: string, child: string): boolean {
5152
return relativePath !== '..' && !relativePath.startsWith('../') && !isAbsolute(relativePath);
5253
}
5354

55+
/**
56+
* Resolves a path to its real location on disk, following any symbolic links.
57+
*
58+
* @param pathString - The file path to resolve.
59+
* @returns The canonicalized real path, or `undefined` when the path cannot be resolved,
60+
* for instance because it does not exist.
61+
*/
62+
export async function resolveRealPath(pathString: string): Promise<string | undefined> {
63+
try {
64+
return canonicalizePath(await realpath(pathString));
65+
} catch {
66+
return undefined;
67+
}
68+
}
69+
5470
/**
5571
* Canonicalizes a file path by normalising Windows drive-letter casing to uppercase.
5672
*

‎packages/angular/build/src/utils/resolve-assets.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import path from 'node:path';
1010
import { glob } from 'tinyglobby';
11-
import { isSubDirectory } from './path';
11+
import { isSubDirectory, resolveRealPath } from './path';
1212

1313
export async function resolveAssets(
1414
entries: {
@@ -24,6 +24,8 @@ export async function resolveAssets(
2424
const defaultIgnore = ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'];
2525

2626
const outputFiles: { source: string; destination: string }[] = [];
27+
const resolvedRoot = path.resolve(root);
28+
const realRoot = (await resolveRealPath(resolvedRoot)) ?? resolvedRoot;
2729

2830
for (const entry of entries) {
2931
if (!isSubDirectory(root, entry.input)) {
@@ -32,15 +34,45 @@ export async function resolveAssets(
3234

3335
const cwd = path.resolve(root, entry.input);
3436

37+
// The check above compares paths textually, while a symbolic link only resolves to its
38+
// target on disk. An input that appears to be inside the workspace root can therefore
39+
// still point outside of it. A path that cannot be resolved does not exist and has
40+
// nothing to read.
41+
const realCwd = await resolveRealPath(cwd);
42+
if (realCwd !== undefined && !isSubDirectory(realRoot, realCwd)) {
43+
throw new Error(
44+
`The ${entry.input} asset path must be within the workspace root. ` +
45+
`It resolves to '${realCwd}' through a symbolic link.`,
46+
);
47+
}
48+
49+
// Schema defaults are not applied to builder options, so the documented default of
50+
// 'false' has to be provided here. Without it the globber follows symbolic links.
51+
const followSymbolicLinks = entry.followSymlinks ?? false;
52+
3553
const files = await glob(entry.glob, {
3654
cwd,
3755
dot: true,
3856
ignore: entry.ignore ? defaultIgnore.concat(entry.ignore) : defaultIgnore,
39-
followSymbolicLinks: entry.followSymlinks,
57+
followSymbolicLinks,
4058
});
4159

4260
for (const file of files) {
4361
const src = path.join(cwd, file);
62+
63+
// Links are only traversed when explicitly enabled, and a traversed link can lead
64+
// outside of the workspace root. When they are not traversed the globber omits them
65+
// entirely and no additional check is needed.
66+
if (followSymbolicLinks) {
67+
const realSrc = await resolveRealPath(src);
68+
if (realSrc !== undefined && !isSubDirectory(realRoot, realSrc)) {
69+
throw new Error(
70+
`The ${entry.input} asset path must be within the workspace root. ` +
71+
`'${file}' resolves to '${realSrc}' through a symbolic link.`,
72+
);
73+
}
74+
}
75+
4476
const filePath = entry.flatten ? path.basename(file) : file;
4577

4678
outputFiles.push({ source: src, destination: path.join(entry.output, filePath) });
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import * as fs from 'node:fs';
10+
import * as os from 'node:os';
11+
import * as path from 'node:path';
12+
import { resolveAssets } from './resolve-assets';
13+
14+
describe('resolveAssets', () => {
15+
let tempDir: string;
16+
let workspaceRoot: string;
17+
let outsideDir: string;
18+
19+
beforeEach(() => {
20+
tempDir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-assets-')));
21+
22+
workspaceRoot = path.join(tempDir, 'workspace');
23+
fs.mkdirSync(path.join(workspaceRoot, 'public'), { recursive: true });
24+
fs.writeFileSync(path.join(workspaceRoot, 'public', 'favicon.ico'), 'icon');
25+
26+
outsideDir = path.join(tempDir, 'outside');
27+
fs.mkdirSync(outsideDir, { recursive: true });
28+
fs.writeFileSync(path.join(outsideDir, 'secret.txt'), 'secret');
29+
});
30+
31+
afterEach(() => {
32+
fs.rmSync(tempDir, { recursive: true, force: true });
33+
});
34+
35+
const publicAssets = (followSymlinks?: boolean) => [
36+
{ glob: '**/*', input: 'public', output: '.', followSymlinks },
37+
];
38+
39+
it('resolves the files within the input directory', async () => {
40+
const assets = await resolveAssets(publicAssets(), workspaceRoot);
41+
42+
expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']);
43+
});
44+
45+
it('does not follow a directory symlink by default', async () => {
46+
fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public', 'docs'), 'junction');
47+
48+
const assets = await resolveAssets(publicAssets(), workspaceRoot);
49+
50+
expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']);
51+
});
52+
53+
it('does not follow a file symlink by default', async () => {
54+
fs.symlinkSync(
55+
path.join(outsideDir, 'secret.txt'),
56+
path.join(workspaceRoot, 'public', 'notes.txt'),
57+
);
58+
59+
const assets = await resolveAssets(publicAssets(), workspaceRoot);
60+
61+
expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']);
62+
});
63+
64+
it('follows a directory symlink that stays within the workspace root when enabled', async () => {
65+
fs.mkdirSync(path.join(workspaceRoot, 'shared'));
66+
fs.writeFileSync(path.join(workspaceRoot, 'shared', 'logo.svg'), '<svg></svg>');
67+
fs.symlinkSync(
68+
path.join(workspaceRoot, 'shared'),
69+
path.join(workspaceRoot, 'public', 'branding'),
70+
'junction',
71+
);
72+
73+
const assets = await resolveAssets(publicAssets(true), workspaceRoot);
74+
75+
expect(assets.map(({ destination }) => destination).sort()).toEqual([
76+
path.join('branding', 'logo.svg'),
77+
'favicon.ico',
78+
]);
79+
});
80+
81+
it('fails when the input directory is a symlink outside the workspace root', async () => {
82+
fs.rmSync(path.join(workspaceRoot, 'public'), { recursive: true });
83+
fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public'), 'junction');
84+
85+
await expectAsync(resolveAssets(publicAssets(), workspaceRoot)).toBeRejectedWithError(
86+
/asset path must be within the workspace root/,
87+
);
88+
});
89+
90+
it('fails when a followed symlink resolves outside the workspace root', async () => {
91+
fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public', 'docs'), 'junction');
92+
93+
await expectAsync(resolveAssets(publicAssets(true), workspaceRoot)).toBeRejectedWithError(
94+
/asset path must be within the workspace root/,
95+
);
96+
});
97+
98+
it('resolves an input directory that does not exist', async () => {
99+
const assets = await resolveAssets(
100+
[{ glob: '**/*', input: 'not-created-yet', output: '.' }],
101+
workspaceRoot,
102+
);
103+
104+
expect(assets).toEqual([]);
105+
});
106+
107+
it('resolves a missing input directory when the workspace root is reached through a symlink', async () => {
108+
const linkedRoot = path.join(tempDir, 'linked-workspace');
109+
fs.symlinkSync(workspaceRoot, linkedRoot, 'junction');
110+
111+
const assets = await resolveAssets(
112+
[{ glob: '**/*', input: 'not-created-yet', output: '.' }],
113+
linkedRoot,
114+
);
115+
116+
expect(assets).toEqual([]);
117+
});
118+
119+
it('resolves assets when the workspace root itself is reached through a symlink', async () => {
120+
const linkedRoot = path.join(tempDir, 'linked-workspace');
121+
fs.symlinkSync(workspaceRoot, linkedRoot, 'junction');
122+
123+
const assets = await resolveAssets(publicAssets(), linkedRoot);
124+
125+
expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']);
126+
});
127+
});

‎packages/angular_devkit/build_angular/src/builders/browser-esbuild/schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
"followSymlinks": {
448448
"type": "boolean",
449449
"default": false,
450-
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched."
450+
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched. Symlinks that resolve outside of the workspace root are not allowed."
451451
},
452452
"glob": {
453453
"type": "string",

‎packages/angular_devkit/build_angular/src/builders/browser/schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@
435435
"followSymlinks": {
436436
"type": "boolean",
437437
"default": false,
438-
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched."
438+
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched. Symlinks that resolve outside of the workspace root are not allowed."
439439
},
440440
"glob": {
441441
"type": "string",

‎packages/angular_devkit/build_angular/src/builders/server/schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
"followSymlinks": {
233233
"type": "boolean",
234234
"default": false,
235-
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched."
235+
"description": "Allow glob patterns to follow symlink directories. This allows subdirectories of the symlink to be searched. Symlinks that resolve outside of the workspace root are not allowed."
236236
},
237237
"glob": {
238238
"type": "string",

‎packages/angular_devkit/build_angular/src/utils/copy-assets.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export async function copyAssets(
3333
cwd,
3434
dot: true,
3535
ignore: entry.ignore ? defaultIgnore.concat(entry.ignore) : defaultIgnore,
36-
followSymbolicLinks: entry.followSymlinks,
36+
// Schema defaults are not applied to builder options, so the documented default of
37+
// 'false' has to be provided here. Without it the globber follows symbolic links.
38+
followSymbolicLinks: entry.followSymlinks ?? false,
3739
});
3840

3941
const directoryExists = new Set<string>();

0 commit comments

Comments
 (0)