Skip to content

Commit

Permalink
re-map authorization header when using secured function urls (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanchildressporsche authored May 16, 2024
1 parent 3a9a84f commit 2d9c5a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lambdas/sign-fn-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ describe('LambdaOriginRequestIamAuth', () => {
const event = getFakePageRequest();
const request = event.Records[0].cf.request;
await signRequest(request);
const securityHeaders = ['x-amz-date', 'x-amz-security-token', 'x-amz-content-sha256', 'authorization'];
const securityHeaders = [
'x-amz-date',
'x-amz-security-token',
'x-amz-content-sha256',
'authorization',
'origin-authorization',
];
const hasSignedHeaders = securityHeaders.every((h) => h in request.headers);
expect(hasSignedHeaders).toBe(true);
});
Expand All @@ -35,6 +41,12 @@ function getFakePageRequest(): CloudFrontRequestEvent {
request: {
clientIp: '1.1.1.1',
headers: {
authorization: [
{
key: 'Authorization',
value: 'Bearer token',
},
],
host: [
{
key: 'Host',
Expand Down
5 changes: 5 additions & 0 deletions src/lambdas/sign-fn-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export function cfHeadersToHeaderBag(headers: CloudFrontHeaders): Bag {
// not destructured) is case sensitive. we arbitrarily use case insensitive key
for (const [headerKey, [{ value }]] of Object.entries(headers)) {
headerBag[headerKey] = value;
// if there is an authorization from CloudFront, move it as
// it will be overwritten when the headers are signed
if (headerKey === 'authorization') {
headerBag['origin-authorization'] = value;
}
}
return headerBag;
}
Expand Down

0 comments on commit 2d9c5a3

Please sign in to comment.