Skip to content

Commit 2a94c37

Browse files
dcabibdani-abib
andauthored
feat(parser): add IPv6 support for sourceIp in API Gateway schemas (#4398)
Co-authored-by: Daniel ABIB <[email protected]>
1 parent 7882d0d commit 2a94c37

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

packages/parser/src/schemas/api-gateway.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const APIGatewayEventIdentity = z.object({
3030
*
3131
* See aws-powertools/powertools-lambda-python#1562 for more information.
3232
*/
33-
sourceIp: z.union([z.ipv4(), z.literal('test-invoke-source-ip')]).optional(),
33+
sourceIp: z
34+
.union([z.ipv4(), z.ipv6(), z.literal('test-invoke-source-ip')])
35+
.optional(),
3436
user: z.string().nullish(),
3537
userAgent: z.string().nullish(),
3638
userArn: z.string().nullish(),

packages/parser/src/schemas/api-gatewayv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const APIGatewayRequestContextV2Schema = z.object({
110110
method: APIGatewayHttpMethod,
111111
path: z.string(),
112112
protocol: z.string(),
113-
sourceIp: z.ipv4(),
113+
sourceIp: z.union([z.ipv4(), z.ipv6()]),
114114
userAgent: z.string(),
115115
}),
116116
requestId: z.string(),

packages/parser/tests/unit/schema/apigw.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
APIGatewayRequestAuthorizerEventSchema,
55
APIGatewayTokenAuthorizerEventSchema,
66
} from '../../../src/schemas/index.js';
7+
import type { APIGatewayProxyEvent } from '../../../src/types/schema.js';
78
import { getTestEvent } from '../helpers/utils.js';
89

910
describe('Schema: API Gateway REST', () => {
@@ -98,6 +99,40 @@ describe('Schema: API Gateway REST', () => {
9899
// Assess
99100
expect(parsedEvent).toEqual(event);
100101
});
102+
it('parses an event with IPv6 sourceIp', () => {
103+
// Prepare
104+
const event = getTestEvent<APIGatewayProxyEvent>({
105+
eventsPath,
106+
filename: 'no-auth',
107+
});
108+
// Add IPv6 address to the event
109+
event.requestContext.identity.sourceIp =
110+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334';
111+
112+
// Act
113+
const parsedEvent = APIGatewayProxyEventSchema.parse(event);
114+
115+
// Assess
116+
expect(parsedEvent.requestContext.identity.sourceIp).toEqual(
117+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334'
118+
);
119+
});
120+
121+
it('parses an event with shortened IPv6 sourceIp', () => {
122+
// Prepare
123+
const event = getTestEvent<APIGatewayProxyEvent>({
124+
eventsPath,
125+
filename: 'no-auth',
126+
});
127+
// Add shortened IPv6 address to the event
128+
event.requestContext.identity.sourceIp = '::1';
129+
130+
// Act
131+
const parsedEvent = APIGatewayProxyEventSchema.parse(event);
132+
133+
// Assess
134+
expect(parsedEvent.requestContext.identity.sourceIp).toEqual('::1');
135+
});
101136
});
102137

103138
describe('APIGatewayRequestAuthorizerEventSchema', () => {

packages/parser/tests/unit/schema/apigwv2.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ describe('Schema: API Gateway HTTP (v2)', () => {
4545
expect(parsedEvent).toEqual(event);
4646
});
4747

48+
it('parses an event with IPv6 sourceIp', () => {
49+
// Prepare
50+
const event = getTestEvent<APIGatewayProxyEventV2>({
51+
eventsPath,
52+
filename: 'no-auth',
53+
});
54+
// Add IPv6 address to the event
55+
event.requestContext.http.sourceIp =
56+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334';
57+
58+
// Act
59+
const parsedEvent = APIGatewayProxyEventV2Schema.parse(event);
60+
61+
// Assess
62+
expect(parsedEvent.requestContext.http.sourceIp).toEqual(
63+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334'
64+
);
65+
});
66+
67+
it('parses an event with shortened IPv6 sourceIp', () => {
68+
// Prepare
69+
const event = getTestEvent<APIGatewayProxyEventV2>({
70+
eventsPath,
71+
filename: 'no-auth',
72+
});
73+
// Add shortened IPv6 address to the event
74+
event.requestContext.http.sourceIp = '::1';
75+
76+
// Act
77+
const parsedEvent = APIGatewayProxyEventV2Schema.parse(event);
78+
79+
// Assess
80+
expect(parsedEvent.requestContext.http.sourceIp).toEqual('::1');
81+
});
82+
4883
it('parses an event with a JWT authorizer', () => {
4984
// Prepare
5085
const event = getTestEvent({

0 commit comments

Comments
 (0)