Skip to content
Merged
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
4 changes: 3 additions & 1 deletion packages/parser/src/schemas/api-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const APIGatewayEventIdentity = z.object({
*
* See aws-powertools/powertools-lambda-python#1562 for more information.
*/
sourceIp: z.union([z.ipv4(), z.literal('test-invoke-source-ip')]).optional(),
sourceIp: z
.union([z.ipv4(), z.ipv6(), z.literal('test-invoke-source-ip')])
.optional(),
user: z.string().nullish(),
userAgent: z.string().nullish(),
userArn: z.string().nullish(),
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/schemas/api-gatewayv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const APIGatewayRequestContextV2Schema = z.object({
method: APIGatewayHttpMethod,
path: z.string(),
protocol: z.string(),
sourceIp: z.ipv4(),
sourceIp: z.union([z.ipv4(), z.ipv6()]),
userAgent: z.string(),
}),
requestId: z.string(),
Expand Down
35 changes: 35 additions & 0 deletions packages/parser/tests/unit/schema/apigw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
APIGatewayRequestAuthorizerEventSchema,
APIGatewayTokenAuthorizerEventSchema,
} from '../../../src/schemas/index.js';
import type { APIGatewayProxyEvent } from '../../../src/types/schema.js';
import { getTestEvent } from '../helpers/utils.js';

describe('Schema: API Gateway REST', () => {
Expand Down Expand Up @@ -98,6 +99,40 @@ describe('Schema: API Gateway REST', () => {
// Assess
expect(parsedEvent).toEqual(event);
});
it('parses an event with IPv6 sourceIp', () => {
// Prepare
const event = getTestEvent<APIGatewayProxyEvent>({
eventsPath,
filename: 'no-auth',
});
// Add IPv6 address to the event
event.requestContext.identity.sourceIp =
'2001:0db8:85a3:0000:0000:8a2e:0370:7334';

// Act
const parsedEvent = APIGatewayProxyEventSchema.parse(event);

// Assess
expect(parsedEvent.requestContext.identity.sourceIp).toEqual(
'2001:0db8:85a3:0000:0000:8a2e:0370:7334'
);
});

it('parses an event with shortened IPv6 sourceIp', () => {
// Prepare
const event = getTestEvent<APIGatewayProxyEvent>({
eventsPath,
filename: 'no-auth',
});
// Add shortened IPv6 address to the event
event.requestContext.identity.sourceIp = '::1';

// Act
const parsedEvent = APIGatewayProxyEventSchema.parse(event);

// Assess
expect(parsedEvent.requestContext.identity.sourceIp).toEqual('::1');
});
});

describe('APIGatewayRequestAuthorizerEventSchema', () => {
Expand Down
35 changes: 35 additions & 0 deletions packages/parser/tests/unit/schema/apigwv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ describe('Schema: API Gateway HTTP (v2)', () => {
expect(parsedEvent).toEqual(event);
});

it('parses an event with IPv6 sourceIp', () => {
// Prepare
const event = getTestEvent<APIGatewayProxyEventV2>({
eventsPath,
filename: 'no-auth',
});
// Add IPv6 address to the event
event.requestContext.http.sourceIp =
'2001:0db8:85a3:0000:0000:8a2e:0370:7334';

// Act
const parsedEvent = APIGatewayProxyEventV2Schema.parse(event);

// Assess
expect(parsedEvent.requestContext.http.sourceIp).toEqual(
'2001:0db8:85a3:0000:0000:8a2e:0370:7334'
);
});

it('parses an event with shortened IPv6 sourceIp', () => {
// Prepare
const event = getTestEvent<APIGatewayProxyEventV2>({
eventsPath,
filename: 'no-auth',
});
// Add shortened IPv6 address to the event
event.requestContext.http.sourceIp = '::1';

// Act
const parsedEvent = APIGatewayProxyEventV2Schema.parse(event);

// Assess
expect(parsedEvent.requestContext.http.sourceIp).toEqual('::1');
});

it('parses an event with a JWT authorizer', () => {
// Prepare
const event = getTestEvent({
Expand Down