Skip to content

Commit 3b7a2eb

Browse files
committed
fix: ensure apigateway does not parse a function url
1 parent a3195f7 commit 3b7a2eb

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/__test__/api.gateway.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Context } from 'aws-lambda';
22
import o from 'ospec';
33
import { LambdaApiGatewayRequest } from '../http/request.api.gateway.js';
4-
import { AlbExample, ApiGatewayExample, clone, CloudfrontExample } from './examples.js';
4+
import { AlbExample, ApiGatewayExample, clone, CloudfrontExample, UrlExample } from './examples.js';
55
import { fakeLog } from './log.js';
66

77
o.spec('ApiGateway', () => {
88
const fakeContext = {} as Context;
9+
910
o('should match the event', () => {
1011
o(LambdaApiGatewayRequest.is(CloudfrontExample)).equals(false);
1112
o(LambdaApiGatewayRequest.is(AlbExample)).equals(false);
1213
o(LambdaApiGatewayRequest.is(ApiGatewayExample)).equals(true);
14+
o(LambdaApiGatewayRequest.is(UrlExample)).equals(false);
1315
});
1416

1517
o('should extract headers', () => {

src/__test__/cloudfront.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { fakeLog } from './log.js';
66

77
o.spec('CloudFront', () => {
88
const fakeContext = {} as Context;
9+
910
o('should match the event', () => {
1011
o(LambdaCloudFrontRequest.is(CloudfrontExample)).equals(true);
1112
o(LambdaCloudFrontRequest.is(AlbExample)).equals(false);

src/http/request.api.gateway.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export class LambdaApiGatewayRequest<T extends Record<string, string>> extends L
1010
APIGatewayProxyResultV2
1111
> {
1212
static is(x: unknown): x is APIGatewayEvent {
13-
return isRecord(x) && isRecord(x['requestContext']) && typeof x['requestContext']['apiId'] === 'string';
13+
if (!isRecord(x)) return false;
14+
if (!isRecord(x['requestContext'])) return false;
15+
if (typeof x['requestContext']['apiId'] !== 'string') return false;
16+
if (typeof x['requestContext']['resourceId'] !== 'string') return false;
17+
if (typeof x['requestContext']['httpMethod'] !== 'string') return false;
18+
return true;
1419
}
1520

1621
toResponse(res: LambdaHttpResponse): APIGatewayProxyResultV2 {

src/http/request.url.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export class LambdaUrlRequest<T extends Record<string, string>> extends LambdaHt
7575
}
7676

7777
static is(x: unknown): x is UrlEvent {
78-
return isRecord(x) && isRecord(x['requestContext']) && isRecord(x['requestContext']['http']);
78+
if (!isRecord(x)) return false;
79+
if (!isRecord(x['requestContext'])) return false;
80+
if (!isRecord(x['requestContext']['http'])) return false;
81+
return true;
7982
}
8083
}

0 commit comments

Comments
 (0)