Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -281,9 +281,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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: Is there any risk of double encoding potentially causing an issue? Some case like: %252e%252e?

I feel like we're ok since you're not returning the xForwardedPrefixedDecoded, and only using it for validation (it wouldn't error but also wouldn't compromise). But just wanted to call out that situation in case there's something I'm not seeing.

} 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 @@ -154,8 +154,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',
Comment thread
alan-agius4 marked this conversation as resolved.
];

for (const prefix of inputs) {
const request = new Request('https://example.com', {
Expand Down Expand Up @@ -220,6 +229,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