66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9+ import { IncomingMessage , ServerResponse } from 'node:http' ;
910import type { Connect , Plugin } from 'vite' ;
1011import {
1112 ComponentStyleRecord ,
@@ -17,6 +18,7 @@ import {
1718 createAngularSsrExternalMiddleware ,
1819 createAngularSsrInternalMiddleware ,
1920 createChromeDevtoolsMiddleware ,
21+ html403 ,
2022} from '../middlewares' ;
2123import { AngularMemoryOutputFiles , AngularOutputAssets } from '../utils' ;
2224
@@ -55,6 +57,8 @@ interface AngularSetupMiddlewaresPluginOptions {
5557 ssrMode : ServerSsrMode ;
5658 resetComponentUpdates : ( ) => void ;
5759 projectRoot : string ;
60+ allowedHosts : true | string [ ] ;
61+ devHost : string ;
5862}
5963
6064async function createEncapsulateStyle ( ) : Promise <
@@ -109,6 +113,36 @@ export function createAngularSetupMiddlewaresPlugin(
109113 // before the built-in HTML middleware
110114 // eslint-disable-next-line @typescript-eslint/no-misused-promises
111115 return async ( ) => {
116+ // Vite/Connect do not expose a typed stack, cast once to a precise structural type.
117+ const entry = server . middlewares . stack . find (
118+ ( { handle } ) =>
119+ typeof handle === 'function' && handle . name . startsWith ( 'hostValidationMiddleware' ) ,
120+ ) ;
121+
122+ if ( typeof entry ?. handle === 'function' ) {
123+ const originalHandle = entry . handle as Connect . NextHandleFunction ;
124+
125+ entry . handle = function angularHostValidationMiddleware (
126+ req : IncomingMessage ,
127+ res : ServerResponse ,
128+ next : ( err ?: unknown ) => void ,
129+ ) {
130+ originalHandle (
131+ req ,
132+ {
133+ writeHead : ( code ) => {
134+ res . writeHead ( code , { 'content-type' : 'text/html' } ) ;
135+ } ,
136+ end : ( ) => {
137+ const hostname = req . headers . host ?. toLowerCase ( ) . split ( ':' ) [ 0 ] ?? '' ;
138+ res . end ( html403 ( hostname ) ) ;
139+ } ,
140+ } as ServerResponse ,
141+ next ,
142+ ) ;
143+ } ;
144+ }
145+
112146 if ( ssrMode === ServerSsrMode . ExternalSsrMiddleware ) {
113147 server . middlewares . use (
114148 await createAngularSsrExternalMiddleware ( server , indexHtmlTransformer ) ,
0 commit comments