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
20 changes: 16 additions & 4 deletions packages/angular/ssr/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,21 @@ function validateHeaders(request: Request): void {
}

const xForwardedPrefix = getFirstHeaderValue(headers.get('x-forwarded-prefix'));
if (xForwardedPrefix && INVALID_PREFIX_REGEX.test(xForwardedPrefix)) {
throw new Error(
'Header "x-forwarded-prefix" must not start with "\\" or multiple "/" or contain ".", ".." path segments.',
);
if (xForwardedPrefix) {
let xForwardedPrefixDecoded: string;
try {
xForwardedPrefixDecoded = decodeURIComponent(xForwardedPrefix);
} catch (e) {
throw new Error(
'Header "x-forwarded-prefix" contains an invalid value and cannot be decoded.',
{ cause: e },
);
}

if (INVALID_PREFIX_REGEX.test(xForwardedPrefixDecoded)) {
throw new Error(
'Header "x-forwarded-prefix" must not start with "\\" or multiple "/" or contain ".", ".." path segments.',
);
}
}
}
25 changes: 23 additions & 2 deletions packages/angular/ssr/test/utils/validation_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,17 @@ describe('Validation Utils', () => {
);
});

it('should throw error if x-forwarded-prefix starts with a backslash or multiple slashes', () => {
const inputs = ['//evil', '\\\\evil', '/\\evil', '\\/evil', '\\evil'];
it('should throw error if x-forwarded-prefix starts with a backslash or multiple slashes including encoded', () => {
const inputs = [
'//evil',
'\\\\evil',
'/\\evil',
'\\/evil',
'\\evil',
'%5Cevil',
'%2F%2Fevil',
'%2F..%2Fevil',
];

for (const prefix of inputs) {
const request = new Request('https://example.com', {
Expand Down Expand Up @@ -213,6 +222,18 @@ describe('Validation Utils', () => {
.not.toThrow();
}
});

it('should throw error if x-forwarded-prefix contains malformed encoding', () => {
const request = new Request('https://example.com', {
headers: {
'x-forwarded-prefix': '/%invalid',
},
});

expect(() => validateRequest(request, allowedHosts, false)).toThrowError(
'Header "x-forwarded-prefix" contains an invalid value and cannot be decoded.',
);
});
});

describe('cloneRequestAndPatchHeaders', () => {
Expand Down
Loading