Skip to content

Commit 5efcf88

Browse files
committed
refactor(@angular/build): send error status code on invalid external style component ID
If an invalid component identifier is provided to the development server for an external stylesheet requiring encapsulation, both a console message and a 400 status response will now be sent. This removes the potential for invalid styles to be sent to the browser.
1 parent 505521e commit 5efcf88

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

packages/angular/build/src/tools/vite/middlewares/assets-middleware.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,31 @@ export function createAngularAssetsMiddleware(
9898
// Shim the stylesheet if a component ID is provided
9999
if (componentId.length > 0) {
100100
// Validate component ID
101-
if (/^[_.\-\p{Letter}\d]+-c\d+$/u.test(componentId)) {
102-
loadEsmModule<typeof import('@angular/compiler')>('@angular/compiler')
103-
.then((compilerModule) => {
104-
const encapsulatedData = compilerModule.encapsulateStyle(
105-
new TextDecoder().decode(data),
106-
componentId,
107-
);
108-
109-
res.setHeader('Content-Type', 'text/css');
110-
res.setHeader('Cache-Control', 'no-cache');
111-
res.setHeader('ETag', etag);
112-
res.end(encapsulatedData);
113-
})
114-
.catch((e) => next(e));
101+
if (!/^[_.\-\p{Letter}\d]+-c\d+$/u.test(componentId)) {
102+
const message = 'Invalid component stylesheet ID request: ' + componentId;
103+
// eslint-disable-next-line no-console
104+
console.error(message);
105+
res.statusCode = 400;
106+
res.end(message);
115107

116108
return;
117-
} else {
118-
// eslint-disable-next-line no-console
119-
console.error('Invalid component stylesheet ID request: ' + componentId);
120109
}
110+
111+
loadEsmModule<typeof import('@angular/compiler')>('@angular/compiler')
112+
.then((compilerModule) => {
113+
const encapsulatedData = compilerModule.encapsulateStyle(
114+
new TextDecoder().decode(data),
115+
componentId,
116+
);
117+
118+
res.setHeader('Content-Type', 'text/css');
119+
res.setHeader('Cache-Control', 'no-cache');
120+
res.setHeader('ETag', etag);
121+
res.end(encapsulatedData);
122+
})
123+
.catch((e) => next(e));
124+
125+
return;
121126
}
122127
}
123128
}

0 commit comments

Comments
 (0)