Skip to content

Commit 4b24c47

Browse files
authored
feat: validate cloudfront request type (#4)
1 parent bec1a9a commit 4b24c47

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@commitlint/config-conventional": "^17.4.2",
3333
"@osaas/client-core": "^0.15.1",
3434
"@osaas/client-web": "^0.5.1",
35+
"@types/aws-lambda": "^8.10.147",
3536
"@types/cose-js": "^0.8.3",
3637
"@types/jest": "^29.5.14",
3738
"@typescript-eslint/eslint-plugin": "^5.51.0",

src/validators/http.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,34 @@ describe('HTTP Request CAT Validator', () => {
188188
const result = await httpValidatorOptional.validateHttpRequest(request);
189189
expect(result.status).toBe(200);
190190
});
191+
192+
test('can handle request of CloudFront request type', async () => {
193+
const httpValidator = new HttpValidator({
194+
keys: [
195+
{
196+
kid: 'Symmetric256',
197+
key: Buffer.from(
198+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
199+
'hex'
200+
)
201+
}
202+
],
203+
issuer: 'eyevinn'
204+
});
205+
const result = await httpValidator.validateCloudFrontRequest({
206+
clientIp: 'dummy',
207+
method: 'GET',
208+
uri: '/index.html',
209+
querystring: '',
210+
headers: {
211+
'cta-common-access-token': [
212+
{
213+
value:
214+
'2D3RhEOhAQWhBFBha2FtYWlfa2V5X2hzMjU2U6MEGnUCOrsGGmfXRKwFGmfXRKxYIOM6yRx830uqAamWFv1amFYRa5vaV2z5lIQTqFEvFh8z'
215+
}
216+
]
217+
}
218+
});
219+
expect(result.status).toBe(200);
220+
});
191221
});

src/validators/http.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
KeyNotFoundError,
77
TokenExpiredError
88
} from '../errors';
9+
import { CloudFrontRequest } from 'aws-lambda';
910

1011
interface HttpValidatorKey {
1112
kid: string;
@@ -65,6 +66,26 @@ export class HttpValidator {
6566
this.opts.tokenMandatory = opts.tokenMandatory ?? true;
6667
}
6768

69+
public async validateCloudFrontRequest(
70+
cfRequest: CloudFrontRequest
71+
): Promise<HttpResponse> {
72+
const requestLike: Pick<IncomingMessage, 'headers'> = {
73+
headers: {}
74+
};
75+
76+
if (cfRequest.headers) {
77+
Object.entries(cfRequest.headers).forEach(([name, header]) => {
78+
if (header && header.length > 0) {
79+
requestLike.headers[name.toLowerCase()] = header
80+
.map((h) => h.value)
81+
.join(',');
82+
}
83+
});
84+
}
85+
86+
return await this.validateHttpRequest(requestLike as IncomingMessage);
87+
}
88+
6889
public async validateHttpRequest(
6990
request: IncomingMessage
7091
): Promise<HttpResponse> {

0 commit comments

Comments
 (0)