77 */
88
99import assert from 'node:assert' ;
10- import { mkdtempSync , rmSync , writeFileSync } from 'node:fs' ;
10+ import { mkdirSync , mkdtempSync , rmSync , symlinkSync , writeFileSync } from 'node:fs' ;
1111import { join } from 'node:path' ;
12- import { LocalWorkspaceHost , createRootRestrictedHost } from './host' ;
12+ import { type Host , LocalWorkspaceHost , createRootRestrictedHost } from './host' ;
13+ import { createMockHost } from './testing/test-utils' ;
1314
1415describe ( 'createRootRestrictedHost' , ( ) => {
1516 let root1 : string ;
@@ -41,6 +42,14 @@ describe('createRootRestrictedHost', () => {
4142 expect ( host . existsSync ( join ( root2 , 'file2.txt' ) ) ) . toBeTrue ( ) ;
4243 } ) ;
4344
45+ it ( 'should allow entries whose names start with two dots inside a root' , ( ) => {
46+ const dotDotFile = join ( root1 , '..hidden.txt' ) ;
47+ writeFileSync ( dotDotFile , 'content' ) ;
48+ const host = createRootRestrictedHost ( LocalWorkspaceHost , [ root1 ] ) ;
49+
50+ expect ( host . existsSync ( dotDotFile ) ) . toBeTrue ( ) ;
51+ } ) ;
52+
4453 it ( 'should reject file access outside of the configured roots' , ( ) => {
4554 const host = createRootRestrictedHost ( LocalWorkspaceHost , [ root1 , root2 ] ) ;
4655
@@ -51,6 +60,15 @@ describe('createRootRestrictedHost', () => {
5160 ) ;
5261 } ) ;
5362
63+ it ( 'should reject dangling symlinks pointing outside configured roots' , ( ) => {
64+ const danglingLink = join ( root1 , 'dangling-out' ) ;
65+ symlinkSync ( join ( outsideDir , 'does-not-exist' ) , danglingLink , 'junction' ) ;
66+ const host = createRootRestrictedHost ( LocalWorkspaceHost , [ root1 ] ) ;
67+
68+ expect ( ( ) => host . existsSync ( danglingLink ) ) . toThrowError ( / A c c e s s d e n i e d / ) ;
69+ expect ( ( ) => host . existsSync ( join ( danglingLink , 'nested' ) ) ) . toThrowError ( / A c c e s s d e n i e d / ) ;
70+ } ) ;
71+
5472 it ( 'should fall back to initial roots when setRoots is called with an empty array' , ( ) => {
5573 const host = createRootRestrictedHost ( LocalWorkspaceHost , [ root1 , root2 ] ) ;
5674
@@ -59,4 +77,337 @@ describe('createRootRestrictedHost', () => {
5977 expect ( host . existsSync ( join ( root1 , 'file1.txt' ) ) ) . toBeTrue ( ) ;
6078 expect ( host . existsSync ( join ( root2 , 'file2.txt' ) ) ) . toBeTrue ( ) ;
6179 } ) ;
80+
81+ describe ( 'command argument validation' , ( ) => {
82+ let baseHost : ReturnType < typeof createMockHost > ;
83+ let restrictedHost : Host ;
84+
85+ beforeEach ( ( ) => {
86+ baseHost = createMockHost ( ) ;
87+ restrictedHost = createRootRestrictedHost ( baseHost , [ root1 ] ) ;
88+ } ) ;
89+
90+ it ( 'should allow valid output-path values inside the allowed root' , async ( ) => {
91+ await restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=dist/my-app' ] , {
92+ cwd : root1 ,
93+ } ) ;
94+ await restrictedHost . executeNgCommand (
95+ [ 'build' , 'my-app' , `--output-path=${ join ( root1 , 'dist/abs' ) } ` ] ,
96+ { cwd : root1 } ,
97+ ) ;
98+
99+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 2 ) ;
100+ } ) ;
101+
102+ it ( 'should allow non-filesystem URL, enum, and boolean options' , async ( ) => {
103+ await restrictedHost . executeNgCommand (
104+ [
105+ 'build' ,
106+ 'my-app' ,
107+ '--base-href=/my-app/' ,
108+ '--deploy-url=/assets/' ,
109+ '--serve-path=/app' ,
110+ '--delete-output-path=false' ,
111+ '--output-hashing=all' ,
112+ '--output-mode=static' ,
113+ '--configuration=production' ,
114+ '--define=API_BASE=/v1' ,
115+ '--filter=/AppComponent/' ,
116+ '--route=/shell' ,
117+ '--routes=/home' ,
118+ '--profile' ,
119+ '--runner-config=false' ,
120+ '--index=false' ,
121+ '--server=true' ,
122+ '--service-worker=true' ,
123+ '--no-delete-output-path' ,
124+ '--no-runner-config' ,
125+ ] ,
126+ { cwd : root1 } ,
127+ ) ;
128+
129+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 1 ) ;
130+ } ) ;
131+
132+ it ( 'should reject absolute output-path values outside allowed roots' , ( ) => {
133+ const outsideTarget = join ( outsideDir , 'target' ) ;
134+
135+ for ( const args of [
136+ [ 'build' , 'my-app' , `--output-path=${ outsideTarget } ` ] ,
137+ [ 'build' , 'my-app' , `--outputPath=${ outsideTarget } ` ] ,
138+ [ 'build' , 'my-app' , `--output_path=${ outsideTarget } ` ] ,
139+ [ 'build' , 'my-app' , `--OUTPUT-PATH=${ outsideTarget } ` ] ,
140+ [ 'build' , 'my-app' , '--output-path' , outsideTarget ] ,
141+ [ 'build' , 'my-app' , '--output-path=dist/ok' , `--output-path=${ outsideTarget } ` ] ,
142+ [ 'build' , 'my-app' , `--no-output-path=${ outsideTarget } ` ] ,
143+ [ 'build' , 'my-app' , `--output-path.base=${ outsideTarget } ` ] ,
144+ ] ) {
145+ expect ( ( ) => restrictedHost . executeNgCommand ( args , { cwd : root1 } ) ) . toThrowError (
146+ / A c c e s s d e n i e d : o p t i o n .* i s o u t s i d e a l l o w e d r o o t s / ,
147+ ) ;
148+ }
149+
150+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
151+ } ) ;
152+
153+ it ( 'should reject relative traversal output-path values outside allowed roots' , ( ) => {
154+ expect ( ( ) =>
155+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=../outside' ] , {
156+ cwd : root1 ,
157+ } ) ,
158+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t p u t - p a t h ' p a t h ' \. \. \/ o u t s i d e ' i s o u t s i d e / ) ;
159+
160+ expect ( ( ) =>
161+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=dist/../../outside' ] , {
162+ cwd : root1 ,
163+ } ) ,
164+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t p u t - p a t h ' / ) ;
165+
166+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
167+ } ) ;
168+
169+ it ( 'should reject output-path equal to or ancestor of the workspace or root directory' , ( ) => {
170+ const monoWorkspace = join ( root1 , 'apps' , 'web' ) ;
171+ mkdirSync ( monoWorkspace , { recursive : true } ) ;
172+
173+ for ( const value of [ '' , '.' , './' , '..' , '../..' , root1 ] ) {
174+ expect ( ( ) =>
175+ restrictedHost . executeNgCommand ( [ 'build' , 'web' , `--output-path=${ value } ` ] , {
176+ cwd : monoWorkspace ,
177+ } ) ,
178+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t p u t - p a t h ' / ) ;
179+ }
180+
181+ // Bare flag without a value defaults to empty string (`cwd`).
182+ expect ( ( ) =>
183+ restrictedHost . executeNgCommand ( [ 'build' , 'web' , '--output-path' ] , { cwd : monoWorkspace } ) ,
184+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t p u t - p a t h ' / ) ;
185+
186+ // Multi-root: must not target another root directory directly.
187+ restrictedHost . setRoots ( [ root1 , root2 ] ) ;
188+ expect ( ( ) =>
189+ restrictedHost . executeNgCommand ( [ 'build' , 'web' , `--output-path=${ root2 } ` ] , {
190+ cwd : monoWorkspace ,
191+ } ) ,
192+ ) . toThrowError ( / m u s t n o t b e a n a l l o w e d r o o t d i r e c t o r y / ) ;
193+
194+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
195+ } ) ;
196+
197+ it ( 'should allow monorepo sibling output paths strictly inside the allowed root' , async ( ) => {
198+ const monoWorkspace = join ( root1 , 'apps' , 'web' ) ;
199+ mkdirSync ( monoWorkspace , { recursive : true } ) ;
200+
201+ await restrictedHost . executeNgCommand ( [ 'build' , 'web' , '--output-path=../../dist/web' ] , {
202+ cwd : monoWorkspace ,
203+ } ) ;
204+
205+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 1 ) ;
206+ } ) ;
207+
208+ it ( 'should reject symlinks inside root pointing outside roots or to workspace root' , ( ) => {
209+ const linkOut = join ( root1 , 'link-out' ) ;
210+ const danglingOut = join ( root1 , 'dangling-out' ) ;
211+ const linkSelf = join ( root1 , 'link-self' ) ;
212+ const outsideNested = join ( outsideDir , 'sub' ) ;
213+ mkdirSync ( outsideNested , { recursive : true } ) ;
214+ const linkNested = join ( root1 , 'link-nested' ) ;
215+
216+ symlinkSync ( outsideDir , linkOut , 'junction' ) ;
217+ symlinkSync ( join ( outsideDir , 'missing' ) , danglingOut , 'junction' ) ;
218+ symlinkSync ( root1 , linkSelf , 'junction' ) ;
219+ symlinkSync ( outsideNested , linkNested , 'junction' ) ;
220+
221+ expect ( ( ) =>
222+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=link-out' ] , {
223+ cwd : root1 ,
224+ } ) ,
225+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
226+
227+ expect ( ( ) =>
228+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=link-out/nested' ] , {
229+ cwd : root1 ,
230+ } ) ,
231+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
232+
233+ expect ( ( ) =>
234+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=dangling-out' ] , {
235+ cwd : root1 ,
236+ } ) ,
237+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
238+
239+ expect ( ( ) =>
240+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , '--output-path=link-self' ] , {
241+ cwd : root1 ,
242+ } ) ,
243+ ) . toThrowError ( / m u s t n o t b e t h e w o r k s p a c e d i r e c t o r y / ) ;
244+
245+ // Lexical `link-nested/../secret` must still be caught when walking via the symlink target.
246+ expect ( ( ) =>
247+ restrictedHost . executeNgCommand (
248+ [ 'build' , 'my-app' , '--output-path=link-nested/../secret' ] ,
249+ {
250+ cwd : root1 ,
251+ } ,
252+ ) ,
253+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
254+
255+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
256+ } ) ;
257+
258+ it ( 'should reject compound output-path and out-file traversal regardless of flag order' , ( ) => {
259+ const monoWorkspace = join ( root1 , 'apps' , 'web' ) ;
260+ mkdirSync ( monoWorkspace , { recursive : true } ) ;
261+
262+ expect ( ( ) =>
263+ restrictedHost . executeNgCommand (
264+ [ 'extract-i18n' , 'web' , '--output-path=../../dist' , '--out-file=../../evil.xlf' ] ,
265+ { cwd : monoWorkspace } ,
266+ ) ,
267+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t - f i l e ' / ) ;
268+
269+ expect ( ( ) =>
270+ restrictedHost . executeNgCommand (
271+ [ 'extract-i18n' , 'web' , '--out-file=../../evil.xlf' , '--output-path=../../dist' ] ,
272+ { cwd : monoWorkspace } ,
273+ ) ,
274+ ) . toThrowError ( / A c c e s s d e n i e d : o p t i o n ' - - o u t - f i l e ' / ) ;
275+
276+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
277+ } ) ;
278+
279+ it ( 'should reject other path-bearing builder and third-party options outside roots' , ( ) => {
280+ const extPath = join ( outsideDir , 'file' ) ;
281+
282+ for ( const flag of [
283+ `--out-file=${ extPath } ` ,
284+ `--output-file=${ extPath } ` ,
285+ `--ts-config=${ extPath } ` ,
286+ `--karma-config=${ extPath } ` ,
287+ `--runner-config=${ extPath } ` ,
288+ `--webpack-config=${ extPath } ` ,
289+ `--proxy-config=${ extPath } ` ,
290+ `--ssl-key=${ extPath } ` ,
291+ `--ssl-cert=${ extPath } ` ,
292+ `--providers-file=${ extPath } ` ,
293+ `--setup-files=${ extPath } ` ,
294+ `--routes-file=${ extPath } ` ,
295+ `--browser=${ extPath } ` ,
296+ `--server=${ extPath } ` ,
297+ `--main=${ extPath } ` ,
298+ `--index=${ extPath } ` ,
299+ `--service-worker=${ extPath } ` ,
300+ `--project=${ extPath } ` ,
301+ `--ngsw-config-path=${ extPath } ` ,
302+ `--resources-output-path=${ extPath } ` ,
303+ `--eslint-config=${ extPath } ` ,
304+ `--cache-location=${ extPath } ` ,
305+ `--output-dir=${ extPath } ` ,
306+ `--config-dir=${ extPath } ` ,
307+ `--dest=${ extPath } ` ,
308+ `-o=${ extPath } ` ,
309+ ] ) {
310+ expect ( ( ) =>
311+ restrictedHost . executeNgCommand ( [ 'run' , 'my-app:custom' , flag ] , { cwd : root1 } ) ,
312+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
313+ }
314+
315+ expect ( ( ) =>
316+ restrictedHost . executeNgCommand ( [ 'lint' , 'my-app' , '-o' , extPath ] , { cwd : root1 } ) ,
317+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
318+
319+ expect ( baseHost . executeNgCommand ) . not . toHaveBeenCalled ( ) ;
320+ } ) ;
321+
322+ it ( 'should validate glob-based path options while allowing normal patterns' , async ( ) => {
323+ const linkOut = join ( root1 , 'link-out' ) ;
324+ symlinkSync ( outsideDir , linkOut , 'junction' ) ;
325+
326+ await restrictedHost . executeNgCommand (
327+ [
328+ 'test' ,
329+ 'my-app' ,
330+ '--include=src/**/*.spec.ts' ,
331+ '--include=src/foo..bar.spec.ts' ,
332+ '--exclude=!src/ignored.spec.ts' ,
333+ ] ,
334+ { cwd : root1 } ,
335+ ) ;
336+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 1 ) ;
337+
338+ for ( const badPattern of [
339+ '../outside/*.spec.ts' ,
340+ join ( outsideDir , '*.spec.ts' ) ,
341+ 'src/*/../../../outside/*.spec.ts' ,
342+ `{${ outsideDir } ,src}/*.spec.ts` ,
343+ `{a,{b,c},${ outsideDir } }/*.spec.ts` ,
344+ '{link-out,src}/*.spec.ts' ,
345+ '!../outside/*.spec.ts' ,
346+ ] ) {
347+ expect ( ( ) =>
348+ restrictedHost . executeNgCommand ( [ 'test' , 'my-app' , `--include=${ badPattern } ` ] , {
349+ cwd : root1 ,
350+ } ) ,
351+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
352+ }
353+ } ) ;
354+
355+ it ( 'should validate nested runner-args and passthrough args after --' , async ( ) => {
356+ await restrictedHost . executeNgCommand (
357+ [ 'test' , 'my-app' , '--runner-args=--update' , '--runner-args=--bail=1' ] ,
358+ { cwd : root1 } ,
359+ ) ;
360+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 1 ) ;
361+
362+ const extConfig = join ( outsideDir , 'vitest.config.mjs' ) ;
363+ expect ( ( ) =>
364+ restrictedHost . executeNgCommand ( [ 'test' , 'my-app' , `--runner-args=--config=${ extConfig } ` ] , {
365+ cwd : root1 ,
366+ } ) ,
367+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
368+
369+ expect ( ( ) =>
370+ restrictedHost . executeNgCommand (
371+ [ 'test' , 'my-app' , '--runner-args=--outputFile' , `--runner-args=${ extConfig } ` ] ,
372+ { cwd : root1 } ,
373+ ) ,
374+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
375+
376+ expect ( ( ) =>
377+ restrictedHost . executeNgCommand ( [ 'test' , 'my-app' , '--' , `--config=${ extConfig } ` ] , {
378+ cwd : root1 ,
379+ } ) ,
380+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
381+ } ) ;
382+
383+ it ( 'should validate arguments in startNgProcess and allow valid devserver flags' , ( ) => {
384+ restrictedHost . startNgProcess ( [ 'serve' , 'my-app' , '--port=12345' , '-o' ] , { cwd : root1 } ) ;
385+ expect ( baseHost . startNgProcess ) . toHaveBeenCalledTimes ( 1 ) ;
386+
387+ expect ( ( ) =>
388+ restrictedHost . startNgProcess (
389+ [ 'serve' , 'my-app' , `--proxy-config=${ join ( outsideDir , 'proxy.json' ) } ` ] ,
390+ { cwd : root1 } ,
391+ ) ,
392+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
393+
394+ expect ( baseHost . startNgProcess ) . toHaveBeenCalledTimes ( 1 ) ;
395+ } ) ;
396+
397+ it ( 'should enforce updated roots after setRoots is called' , async ( ) => {
398+ const targetInRoot2 = join ( root2 , 'dist/app' ) ;
399+ expect ( ( ) =>
400+ restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , `--output-path=${ targetInRoot2 } ` ] , {
401+ cwd : root1 ,
402+ } ) ,
403+ ) . toThrowError ( / A c c e s s d e n i e d / ) ;
404+
405+ restrictedHost . setRoots ( [ root1 , root2 ] ) ;
406+ await restrictedHost . executeNgCommand ( [ 'build' , 'my-app' , `--output-path=${ targetInRoot2 } ` ] , {
407+ cwd : root1 ,
408+ } ) ;
409+
410+ expect ( baseHost . executeNgCommand ) . toHaveBeenCalledTimes ( 1 ) ;
411+ } ) ;
412+ } ) ;
62413} ) ;
0 commit comments