Skip to content

Feature Request: parse sourceIp as ipv6 in APIGateway schemas #4348

@cbarlow1993

Description

@cbarlow1993

Expected Behavior

I would expect that the sourceIp for the APIGateway, APIGatewayV2 and the AppSync envelopes would support both IPv4 and IPv6 type ip adddress.

See:

Current Behavior

Currently only support IPv4

Code snippet

const APIGatewayEventIdentity = z.object({
  accessKey: z.string().nullish(),
  accountId: z.string().nullish(),
  apiKey: z.string().nullish(),
  apiKeyId: z.string().nullish(),
  caller: z.string().nullish(),
  cognitoAuthenticationProvider: z.string().nullish(),
  cognitoAuthenticationType: z.string().nullish(),
  cognitoIdentityId: z.string().nullish(),
  cognitoIdentityPoolId: z.string().nullish(),
  principalOrgId: z.string().nullish(),
  /**
   * When invoking the API Gateway REST API using the Test Invoke feature,
   * the sourceIp is hardcoded to `test-invoke-source-ip`. This is a stopgap
   * solution to allow customers to test their API and have successful parsing.
   *
   * See aws-powertools/powertools-lambda-python#1562 for more information.
   */
  sourceIp: z.union([z.ipv4(), z.literal('test-invoke-source-ip')]).optional(),  <-----
  user: z.string().nullish(),
  userAgent: z.string().nullish(),
  userArn: z.string().nullish(),
  clientCert: APIGatewayCert.nullish(),
});


const APIGatewayRequestContextV2Schema = z.object({
  accountId: z.string(),
  apiId: z.string(),
  authorizer: APIGatewayRequestAuthorizerV2Schema.optional(),
  authentication: z
    .object({
      clientCert: APIGatewayCert.optional(),
    })
    .nullish(),
  domainName: z.string(),
  domainPrefix: z.string(),
  http: z.object({
    method: APIGatewayHttpMethod,
    path: z.string(),
    protocol: z.string(),
    sourceIp: z.ipv4(),   <-----
    userAgent: z.string(),
  }),
  requestId: z.string(),
  routeKey: z.string(),
  stage: z.string(),
  time: z.string(),
  timeEpoch: z.number(),
});

const AppSyncIamIdentity = z.object({
  accountId: z.string(),
  cognitoIdentityPoolId: z.string().nullable(),
  cognitoIdentityId: z.string().nullable(),
  sourceIp: z.array(z.string()), <----
  username: z.string(),
  userArn: z.string(),
  cognitoIdentityAuthType: z.string().nullable(),
  cognitoIdentityAuthProvider: z.string().nullable(),
});

const AppSyncCognitoIdentity = z.object({
  sub: z.string(),
  issuer: z.string(),
  username: z.string(),
  claims: z.record(z.string(), z.unknown()),
  sourceIp: z.array(z.ipv4()), <-----
  defaultAuthStrategy: z.string().nullable(),
  groups: z.array(z.string()).nullable(),
});

Steps to Reproduce

  1. With Zod v4 and latest power tools packages. Extended the APIGatewayRequestContextV2Schema with a request body on a function URL lambda. And hit it via an IPv6 Address

Possible Solution

Support an IPv4 or IPv6

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

confirmedThe scope is clear, ready for implementationfeature-requestThis item refers to a feature request for an existing or new utilitygood-first-issueSomething that is suitable for those who want to start contributinghelp-wantedWe would really appreciate some support from community for this oneparserThis item relates to the Parser Utility

Type

No type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions