@@ -290,140 +290,87 @@ describe('AngularAppEngine', () => {
290290 describe ( 'Invalid host headers' , ( ) => {
291291 let consoleErrorSpy : jasmine . Spy ;
292292
293- describe ( 'with allowed hosts configured' , ( ) => {
294- beforeAll ( ( ) => {
295- setAngularAppEngineManifest ( {
296- allowedHosts : [ 'example.com' ] ,
297- entryPoints : {
298- '' : async ( ) => {
299- setAngularAppTestingManifest (
300- [ { path : 'home' , component : TestHomeComponent } ] ,
301- [ { path : '**' , renderMode : RenderMode . Server } ] ,
302- ) ;
303-
304- return {
305- ɵgetOrCreateAngularServerApp : getOrCreateAngularServerApp ,
306- ɵdestroyAngularServerApp : destroyAngularServerApp ,
307- } ;
308- } ,
309- } ,
310- basePath : '/' ,
311- supportedLocales : { 'en-US' : '' } ,
312- } ) ;
313-
314- appEngine = new AngularAppEngine ( ) ;
315- } ) ;
316-
317- beforeEach ( ( ) => {
318- consoleErrorSpy = spyOn ( console , 'error' ) ;
319- } ) ;
320-
321- it ( 'should return 400 when disallowed host' , async ( ) => {
322- const request = new Request ( 'https://evil.com' ) ;
323- const response = await appEngine . handle ( request ) ;
324- expect ( response ) . not . toBeNull ( ) ;
325- expect ( response ?. status ) . toBe ( 400 ) ;
326- expect ( await response ?. text ( ) ) . toContain ( 'URL with hostname "evil.com" is not allowed.' ) ;
327- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
328- jasmine . stringMatching ( 'URL with hostname "evil.com" is not allowed.' ) ,
329- ) ;
330- } ) ;
331-
332- it ( 'should return 400 when disallowed host header' , async ( ) => {
333- const request = new Request ( 'https://example.com/home' , {
334- headers : { 'host' : 'evil.com' } ,
335- } ) ;
336- const response = await appEngine . handle ( request ) ;
337- expect ( response ) . not . toBeNull ( ) ;
338- expect ( response ?. status ) . toBe ( 400 ) ;
339- expect ( await response ?. text ( ) ) . toContain (
340- 'Header "host" with value "evil.com" is not allowed.' ,
341- ) ;
342- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
343- jasmine . stringMatching ( 'Header "host" with value "evil.com" is not allowed.' ) ,
344- ) ;
345- } ) ;
293+ beforeAll ( ( ) => {
294+ setAngularAppEngineManifest ( {
295+ allowedHosts : [ 'example.com' ] ,
296+ entryPoints : {
297+ '' : async ( ) => {
298+ setAngularAppTestingManifest (
299+ [ { path : 'home' , component : TestHomeComponent } ] ,
300+ [ { path : '**' , renderMode : RenderMode . Server } ] ,
301+ ) ;
346302
347- it ( 'should return 400 when disallowed x-forwarded-host header' , async ( ) => {
348- const request = new Request ( 'https://example.com/home' , {
349- headers : { 'x-forwarded-host' : 'evil.com' } ,
350- } ) ;
351- const response = await appEngine . handle ( request ) ;
352- expect ( response ) . not . toBeNull ( ) ;
353- expect ( response ?. status ) . toBe ( 400 ) ;
354- expect ( await response ?. text ( ) ) . toContain (
355- 'Header "x-forwarded-host" with value "evil.com" is not allowed.' ,
356- ) ;
357- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
358- jasmine . stringMatching ( 'Header "x-forwarded-host" with value "evil.com" is not allowed.' ) ,
359- ) ;
303+ return {
304+ ɵgetOrCreateAngularServerApp : getOrCreateAngularServerApp ,
305+ ɵdestroyAngularServerApp : destroyAngularServerApp ,
306+ } ;
307+ } ,
308+ } ,
309+ basePath : '/' ,
310+ supportedLocales : { 'en-US' : '' } ,
360311 } ) ;
361312
362- it ( 'should return 400 when host with path separator' , async ( ) => {
363- const request = new Request ( 'https://example.com/home' , {
364- headers : { 'host' : 'example.com/evil' } ,
365- } ) ;
366- const response = await appEngine . handle ( request ) ;
367- expect ( response ) . not . toBeNull ( ) ;
368- expect ( response ?. status ) . toBe ( 400 ) ;
369- expect ( await response ?. text ( ) ) . toContain (
370- 'Header "host" contains characters that are not allowed.' ,
371- ) ;
372- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
373- jasmine . stringMatching ( 'Header "host" contains characters that are not allowed.' ) ,
374- ) ;
375- } ) ;
313+ appEngine = new AngularAppEngine ( ) ;
376314 } ) ;
377315
378- describe ( 'without allowed hosts configured' , ( ) => {
379- beforeAll ( ( ) => {
380- setAngularAppEngineManifest ( {
381- allowedHosts : [ ] ,
382- entryPoints : {
383- '' : async ( ) => {
384- setAngularAppTestingManifest (
385- [ { path : 'home' , component : TestHomeComponent } ] ,
386- [ { path : '**' , renderMode : RenderMode . Server } ] ,
387- ) ;
388-
389- return {
390- ɵgetOrCreateAngularServerApp : getOrCreateAngularServerApp ,
391- ɵdestroyAngularServerApp : destroyAngularServerApp ,
392- } ;
393- } ,
394- } ,
395- basePath : '/' ,
396- supportedLocales : { 'en-US' : '' } ,
397- } ) ;
316+ beforeEach ( ( ) => {
317+ consoleErrorSpy = spyOn ( console , 'error' ) ;
318+ } ) ;
398319
399- appEngine = new AngularAppEngine ( ) ;
400- } ) ;
320+ it ( 'should return 400 when disallowed host' , async ( ) => {
321+ const request = new Request ( 'https://evil.com' ) ;
322+ const response = await appEngine . handle ( request ) ;
323+ expect ( response ) . not . toBeNull ( ) ;
324+ expect ( response ?. status ) . toBe ( 400 ) ;
325+ expect ( await response ?. text ( ) ) . toContain ( 'URL with hostname "evil.com" is not allowed.' ) ;
326+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
327+ jasmine . stringMatching ( 'URL with hostname "evil.com" is not allowed.' ) ,
328+ ) ;
329+ } ) ;
401330
402- beforeEach ( ( ) => {
403- consoleErrorSpy = spyOn ( console , 'error' ) ;
331+ it ( 'should return 400 when disallowed host header' , async ( ) => {
332+ const request = new Request ( 'https://example.com/home' , {
333+ headers : { 'host' : 'evil.com' } ,
404334 } ) ;
335+ const response = await appEngine . handle ( request ) ;
336+ expect ( response ) . not . toBeNull ( ) ;
337+ expect ( response ?. status ) . toBe ( 400 ) ;
338+ expect ( await response ?. text ( ) ) . toContain (
339+ 'Header "host" with value "evil.com" is not allowed.' ,
340+ ) ;
341+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
342+ jasmine . stringMatching ( 'Header "host" with value "evil.com" is not allowed.' ) ,
343+ ) ;
344+ } ) ;
405345
406- it ( 'should log error and fallback to CSR when disallowed host' , async ( ) => {
407- const request = new Request ( 'https://example.com' ) ;
408- const response = await appEngine . handle ( request ) ;
409- expect ( response ) . not . toBeNull ( ) ;
410- expect ( await response ?. text ( ) ) . toContain ( '<title>CSR page</title>' ) ;
411- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
412- jasmine . stringMatching ( 'URL with hostname "example.com" is not allowed.' ) ,
413- ) ;
346+ it ( 'should return 400 when disallowed x-forwarded-host header' , async ( ) => {
347+ const request = new Request ( 'https://example.com/home' , {
348+ headers : { 'x-forwarded-host' : 'evil.com' } ,
414349 } ) ;
350+ const response = await appEngine . handle ( request ) ;
351+ expect ( response ) . not . toBeNull ( ) ;
352+ expect ( response ?. status ) . toBe ( 400 ) ;
353+ expect ( await response ?. text ( ) ) . toContain (
354+ 'Header "x-forwarded-host" with value "evil.com" is not allowed.' ,
355+ ) ;
356+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
357+ jasmine . stringMatching ( 'Header "x-forwarded-host" with value "evil.com" is not allowed.' ) ,
358+ ) ;
359+ } ) ;
415360
416- it ( 'should log error and fallback to CSR when host with path separator' , async ( ) => {
417- const request = new Request ( 'https://example.com/home' , {
418- headers : { 'host' : 'example.com/evil' } ,
419- } ) ;
420- const response = await appEngine . handle ( request ) ;
421- expect ( response ) . not . toBeNull ( ) ;
422- expect ( await response ?. text ( ) ) . toContain ( '<title>CSR page</title>' ) ;
423- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
424- jasmine . stringMatching ( 'Header "host" contains characters that are not allowed.' ) ,
425- ) ;
361+ it ( 'should return 400 when host with path separator' , async ( ) => {
362+ const request = new Request ( 'https://example.com/home' , {
363+ headers : { 'host' : 'example.com/evil' } ,
426364 } ) ;
365+ const response = await appEngine . handle ( request ) ;
366+ expect ( response ) . not . toBeNull ( ) ;
367+ expect ( response ?. status ) . toBe ( 400 ) ;
368+ expect ( await response ?. text ( ) ) . toContain (
369+ 'Header "host" contains characters that are not allowed.' ,
370+ ) ;
371+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
372+ jasmine . stringMatching ( 'Header "host" contains characters that are not allowed.' ) ,
373+ ) ;
427374 } ) ;
428375 } ) ;
429376} ) ;
0 commit comments