Skip to content
Open
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
13 changes: 13 additions & 0 deletions packages/angular/ssr/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ export function cloneRequestAndPatchHeaders(
};
};

const originalForEach = headers.forEach;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
(headers.forEach as typeof originalForEach) = function (callback, thisArg) {
originalForEach.call(
headers,
(value, key, parent) => {
validateHeader(key, value, allowedHosts, onError);
callback.call(thisArg, value, key, parent);
},
thisArg,
);
};

// Ensure for...of loops use the new patched entries
(headers[Symbol.iterator] as typeof originalEntries) = headers.entries;

Expand Down
19 changes: 19 additions & 0 deletions packages/angular/ssr/test/utils/validation_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,24 @@ describe('Validation Utils', () => {
}),
);
});

it('should validate headers when iterating with forEach()', async () => {
const req = new Request('http://example.com', {
headers: { 'host': 'evil.com' },
});
const { request: secured, onError } = cloneRequestAndPatchHeaders(req, allowedHosts);

expect(() => {
secured.headers.forEach(() => {
// access the header to trigger the validation
});
}).toThrowError('Header "host" with value "evil.com" is not allowed.');

await expectAsync(onError).toBeResolvedTo(
jasmine.objectContaining({
message: jasmine.stringMatching('Header "host" with value "evil.com" is not allowed.'),
}),
);
});
});
});
Loading