Skip to content

Commit d4f422a

Browse files
committed
fix(@angular/ssr): preserve internal URL trust
Mark both internal server platforms as trusted after their owning request, build, or development-server policy has accepted the URL.
1 parent 4a080b6 commit d4f422a

5 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎packages/angular/ssr/src/routes/ng-routes.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,12 @@ export async function getRoutesFromAngularRouterConfig(
632632
const platformRef = platformServer([
633633
{
634634
provide: INITIAL_CONFIG,
635-
useValue: { document, url: `${protocol}//${host}/` },
635+
useValue: {
636+
document,
637+
url: `${protocol}//${host}/`,
638+
// The app engine, build, or dev server owns the URL policy for route extraction.
639+
allowedHosts: ['*'],
640+
},
636641
},
637642
{
638643
// An Angular Console Provider that does not print a set of predefined logs.

‎packages/angular/ssr/src/utils/ng.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export async function renderAngular(
7474
useValue: {
7575
url: urlToRender.href,
7676
document: html,
77+
// The app engine validates requests before constructing this internal platform.
78+
allowedHosts: ['*'],
7779
},
7880
},
7981
{

‎packages/angular/ssr/test/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ts_project(
1313
"//:node_modules/@angular/compiler",
1414
"//:node_modules/@angular/core",
1515
"//:node_modules/@angular/platform-browser",
16+
"//:node_modules/@angular/platform-server",
1617
"//:node_modules/@angular/router",
1718
"//:node_modules/@types/node",
1819
"//packages/angular/ssr",

‎packages/angular/ssr/test/app-engine_spec.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '@angular/compiler';
1212
/* eslint-enable import/no-unassigned-import */
1313

1414
import { Component, REQUEST, inject } from '@angular/core';
15+
import { INITIAL_CONFIG } from '@angular/platform-server';
1516
import { destroyAngularServerApp, getOrCreateAngularServerApp } from '../src/app';
1617
import { AngularAppEngine } from '../src/app-engine';
1718
import { setAngularAppEngineManifest } from '../src/manifest';
@@ -290,9 +291,13 @@ describe('AngularAppEngine', () => {
290291
beforeAll(() => {
291292
@Component({
292293
selector: 'app-home',
293-
template: `Home works`,
294+
template: `Home works {{ trustedHosts }}`,
294295
})
295-
class HomeComponent {}
296+
class HomeComponent {
297+
protected readonly trustedHosts = (
298+
inject(INITIAL_CONFIG) as { allowedHosts?: readonly string[] }
299+
).allowedHosts?.join(',');
300+
}
296301

297302
setAngularAppEngineManifest({
298303
allowedHosts: ['example.com'],
@@ -334,6 +339,12 @@ describe('AngularAppEngine', () => {
334339
expect(await response?.text()).toContain('Home works');
335340
});
336341

342+
it('marks internal platform URLs as already validated', async () => {
343+
const request = new Request('https://example.com/home');
344+
const response = await appEngine.handle(request);
345+
expect(await response?.text()).toContain('Home works *');
346+
});
347+
337348
it('should correctly render the content when the URL ends with "index.html"', async () => {
338349
const request = new Request('https://example.com/home/index.html');
339350
const response = await appEngine.handle(request);

‎packages/angular/ssr/test/routes/ng-routes_spec.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
import '@angular/compiler';
1212
/* eslint-enable import/no-unassigned-import */
1313

14-
import { Component, InjectionToken, Injector, inject } from '@angular/core';
14+
import {
15+
Component,
16+
InjectionToken,
17+
Injector,
18+
inject,
19+
provideEnvironmentInitializer,
20+
} from '@angular/core';
21+
import { INITIAL_CONFIG } from '@angular/platform-server';
1522
import {
1623
Route,
1724
Routes,
@@ -31,6 +38,30 @@ describe('extractRoutesAndCreateRouteTree', () => {
3138
})
3239
class DummyComponent {}
3340

41+
it('marks internal platform URLs as already validated', async () => {
42+
let trustedHosts: readonly string[] | undefined;
43+
setAngularAppTestingManifest(
44+
[{ path: 'home', component: DummyComponent }],
45+
[{ path: '**', renderMode: RenderMode.Server }],
46+
'/',
47+
{},
48+
undefined,
49+
undefined,
50+
[
51+
provideEnvironmentInitializer(() => {
52+
trustedHosts = (inject(INITIAL_CONFIG) as { allowedHosts?: readonly string[] })
53+
.allowedHosts;
54+
}),
55+
],
56+
);
57+
58+
const { errors } = await extractRoutesAndCreateRouteTree({
59+
url: new URL('http://example.com/home'),
60+
});
61+
expect(errors).toHaveSize(0);
62+
expect(trustedHosts).toEqual(['*']);
63+
});
64+
3465
it('should extract routes and create a route tree', async () => {
3566
setAngularAppTestingManifest(
3667
[

0 commit comments

Comments
 (0)