Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,12 @@ export async function getRoutesFromAngularRouterConfig(
const platformRef = platformServer([
{
provide: INITIAL_CONFIG,
useValue: { document, url: `${protocol}//${host}/` },
useValue: {
document,
url: `${protocol}//${host}/`,
// The app engine, build, or dev server owns the URL policy for route extraction.
allowedHosts: ['*'],
},
},
{
// An Angular Console Provider that does not print a set of predefined logs.
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/ssr/src/utils/ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export async function renderAngular(
useValue: {
url: urlToRender.href,
document: html,
// The app engine validates requests before constructing this internal platform.
allowedHosts: ['*'],
},
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/angular/ssr/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ts_project(
"//:node_modules/@angular/compiler",
"//:node_modules/@angular/core",
"//:node_modules/@angular/platform-browser",
"//:node_modules/@angular/platform-server",
"//:node_modules/@angular/router",
"//:node_modules/@types/node",
"//packages/angular/ssr",
Expand Down
15 changes: 13 additions & 2 deletions packages/angular/ssr/test/app-engine_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '@angular/compiler';
/* eslint-enable import/no-unassigned-import */

import { Component, REQUEST, inject } from '@angular/core';
import { INITIAL_CONFIG } from '@angular/platform-server';
import { destroyAngularServerApp, getOrCreateAngularServerApp } from '../src/app';
import { AngularAppEngine } from '../src/app-engine';
import { setAngularAppEngineManifest } from '../src/manifest';
Expand Down Expand Up @@ -290,9 +291,13 @@ describe('AngularAppEngine', () => {
beforeAll(() => {
@Component({
selector: 'app-home',
template: `Home works`,
template: `Home works {{ trustedHosts }}`,
})
class HomeComponent {}
class HomeComponent {
protected readonly trustedHosts = (
inject(INITIAL_CONFIG) as { allowedHosts?: readonly string[] }
).allowedHosts?.join(',');
}

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

it('marks internal platform URLs as already validated', async () => {
const request = new Request('https://example.com/home');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('Home works *');
});

it('should correctly render the content when the URL ends with "index.html"', async () => {
const request = new Request('https://example.com/home/index.html');
const response = await appEngine.handle(request);
Expand Down
33 changes: 32 additions & 1 deletion packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
import '@angular/compiler';
/* eslint-enable import/no-unassigned-import */

import { Component, InjectionToken, Injector, inject } from '@angular/core';
import {
Component,
InjectionToken,
Injector,
inject,
provideEnvironmentInitializer,
} from '@angular/core';
import { INITIAL_CONFIG } from '@angular/platform-server';
import {
Route,
Routes,
Expand All @@ -31,6 +38,30 @@ describe('extractRoutesAndCreateRouteTree', () => {
})
class DummyComponent {}

it('marks internal platform URLs as already validated', async () => {
let trustedHosts: readonly string[] | undefined;
setAngularAppTestingManifest(
[{ path: 'home', component: DummyComponent }],
[{ path: '**', renderMode: RenderMode.Server }],
'/',
{},
undefined,
undefined,
[
provideEnvironmentInitializer(() => {
trustedHosts = (inject(INITIAL_CONFIG) as { allowedHosts?: readonly string[] })
.allowedHosts;
}),
],
);

const { errors } = await extractRoutesAndCreateRouteTree({
url: new URL('http://example.com/home'),
});
expect(errors).toHaveSize(0);
expect(trustedHosts).toEqual(['*']);
});

it('should extract routes and create a route tree', async () => {
setAngularAppTestingManifest(
[
Expand Down
Loading