conninfo.test.ts
This file
Path
src/adapter/aws-lambda/conninfo.test.ts
118 lines · 3.1 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–118why, one step back1import { Context } from '../../context'2import { getConnInfo } from './conninfo'34describe('getConnInfo', () => {5 describe('API Gateway v1', () => {6 it('Should return the client IP from identity.sourceIp', () => {7 const ip = '203.0.113.42'8 const c = new Context(new Request('http://localhost/'), {9 env: {10 requestContext: {11 identity: {12 sourceIp: ip,13 userAgent: 'test',14 },15 accountId: '123',16 apiId: 'abc',17 authorizer: {},18 domainName: 'example.com',19 domainPrefix: 'api',20 extendedRequestId: 'xxx',21 httpMethod: 'GET',22 path: '/',23 protocol: 'HTTP/1.1',24 requestId: 'req-1',25 requestTime: '',26 requestTimeEpoch: 0,27 resourcePath: '/',28 stage: 'prod',29 },30 },31 })3233 const info = getConnInfo(c)3435 expect(info.remote.address).toBe(ip)36 })37 })3839 describe('API Gateway v2', () => {40 it('Should return the client IP from http.sourceIp', () => {41 const ip = '198.51.100.23'42 const c = new Context(new Request('http://localhost/'), {43 env: {44 requestContext: {45 http: {46 method: 'GET',47 path: '/',48 protocol: 'HTTP/1.1',49 sourceIp: ip,50 userAgent: 'test',51 },52 accountId: '123',53 apiId: 'abc',54 authentication: null,55 authorizer: {},56 domainName: 'example.com',57 domainPrefix: 'api',58 requestId: 'req-1',59 routeKey: 'GET /',60 stage: 'prod',61 time: '',62 timeEpoch: 0,63 },64 },65 })6667 const info = getConnInfo(c)6869 expect(info.remote.address).toBe(ip)70 })71 })7273 describe('ALB', () => {74 it.each([75 {76 description: 'ALB appends real client IP',77 xff: '10.0.0.1, 192.0.2.50',78 expected: '192.0.2.50',79 },80 {81 description: 'attacker-controlled first IP',82 xff: '127.0.0.1, 192.168.1.100',83 expected: '192.168.1.100',84 },85 ])('Should return the last IP from x-forwarded-for ($description)', ({ xff, expected }) => {86 const req = new Request('http://localhost/', {87 headers: { 'x-forwarded-for': xff },88 })89 const c = new Context(req, {90 env: {91 requestContext: {92 elb: {93 targetGroupArn: 'arn:aws:elasticloadbalancing:...',94 },95 },96 },97 })98 const info = getConnInfo(c)99 expect(info.remote.address).toBe(expected)100 })101102 it('Should return undefined when no x-forwarded-for header', () => {103 const c = new Context(new Request('http://localhost/'), {104 env: {105 requestContext: {106 elb: {107 targetGroupArn: 'arn:aws:elasticloadbalancing:...',108 },109 },110 },111 })112113 const info = getConnInfo(c)114115 expect(info.remote.address).toBeUndefined()116 })117 })118})