Skip to content

Commit ec603ba

Browse files
committed
fix(@angular/ssr): disallow x-forwarded-prefix starting with a backslash
Updated the INVALID_PREFIX_REGEX to ensure that prefixes starting with a backslash are considered invalid. Previously, only multiple slashes or dot segments were explicitly disallowed at the start. Also updated the associated validation error message and unit tests to reflect this change.
1 parent cb8d4d9 commit ec603ba

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

packages/angular/ssr/src/utils/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const VALID_HOST_REGEX = /^[a-z0-9.:-]+$/i;
2929
/**
3030
* Regular expression to validate that the prefix is valid.
3131
*/
32-
const INVALID_PREFIX_REGEX = /^[/\\]{2}|(?:^|[/\\])\.\.?(?:[/\\]|$)/;
32+
const INVALID_PREFIX_REGEX = /^(?:\\|\/[/\\])|(?:^|[/\\])\.\.?(?:[/\\]|$)/;
3333

3434
/**
3535
* Extracts the first value from a multi-value header string.
@@ -270,7 +270,7 @@ function validateHeaders(request: Request): void {
270270
const xForwardedPrefix = getFirstHeaderValue(headers.get('x-forwarded-prefix'));
271271
if (xForwardedPrefix && INVALID_PREFIX_REGEX.test(xForwardedPrefix)) {
272272
throw new Error(
273-
'Header "x-forwarded-prefix" must not start with multiple "/" or "\\" or contain ".", ".." path segments.',
273+
'Header "x-forwarded-prefix" must not start with "\\" or multiple "/" or contain ".", ".." path segments.',
274274
);
275275
}
276276
}

packages/angular/ssr/test/utils/validation_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ describe('Validation Utils', () => {
147147
);
148148
});
149149

150-
it('should throw error if x-forwarded-prefix starts with multiple slashes or backslashes', () => {
151-
const inputs = ['//evil', '\\\\evil', '/\\evil', '\\/evil'];
150+
it('should throw error if x-forwarded-prefix starts with a backslash or multiple slashes', () => {
151+
const inputs = ['//evil', '\\\\evil', '/\\evil', '\\/evil', '\\evil'];
152152

153153
for (const prefix of inputs) {
154154
const request = new Request('https://example.com', {
@@ -160,7 +160,7 @@ describe('Validation Utils', () => {
160160
expect(() => validateRequest(request, allowedHosts, false))
161161
.withContext(`Prefix: "${prefix}"`)
162162
.toThrowError(
163-
'Header "x-forwarded-prefix" must not start with multiple "/" or "\\" or contain ".", ".." path segments.',
163+
'Header "x-forwarded-prefix" must not start with "\\" or multiple "/" or contain ".", ".." path segments.',
164164
);
165165
}
166166
});
@@ -193,7 +193,7 @@ describe('Validation Utils', () => {
193193
expect(() => validateRequest(request, allowedHosts, false))
194194
.withContext(`Prefix: "${prefix}"`)
195195
.toThrowError(
196-
'Header "x-forwarded-prefix" must not start with multiple "/" or "\\" or contain ".", ".." path segments.',
196+
'Header "x-forwarded-prefix" must not start with "\\" or multiple "/" or contain ".", ".." path segments.',
197197
);
198198
}
199199
});

0 commit comments

Comments
 (0)