conninfo.test.ts
This file
Path
adapters/aws-lambda/src/conninfo.test.ts
118 lines 路 3.1 KiB 路 TypeScript
Last changed by
feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import { Context } from 'hono'import { getConnInfo } from './conninfo'
describe('getConnInfo', () => { describe('API Gateway v1', () => { it('Should return the client IP from identity.sourceIp', () => { const ip = '203.0.113.42' const c = new Context(new Request('http://localhost/'), { env: { requestContext: { identity: { sourceIp: ip, userAgent: 'test', }, accountId: '123', apiId: 'abc', authorizer: {}, domainName: 'example.com', domainPrefix: 'api', extendedRequestId: 'xxx', httpMethod: 'GET', path: '/', protocol: 'HTTP/1.1', requestId: 'req-1', requestTime: '', requestTimeEpoch: 0, resourcePath: '/', stage: 'prod', }, }, })
const info = getConnInfo(c)
expect(info.remote.address).toBe(ip) }) })
describe('API Gateway v2', () => { it('Should return the client IP from http.sourceIp', () => { const ip = '198.51.100.23' const c = new Context(new Request('http://localhost/'), { env: { requestContext: { http: { method: 'GET', path: '/', protocol: 'HTTP/1.1', sourceIp: ip, userAgent: 'test', }, accountId: '123', apiId: 'abc', authentication: null, authorizer: {}, domainName: 'example.com', domainPrefix: 'api', requestId: 'req-1', routeKey: 'GET /', stage: 'prod', time: '', timeEpoch: 0, }, }, })
const info = getConnInfo(c)
expect(info.remote.address).toBe(ip) }) })
describe('ALB', () => { it.each([ { description: 'ALB appends real client IP', xff: '10.0.0.1, 192.0.2.50', expected: '192.0.2.50', }, { description: 'attacker-controlled first IP', xff: '127.0.0.1, 192.168.1.100', expected: '192.168.1.100', }, ])('Should return the last IP from x-forwarded-for ($description)', ({ xff, expected }) => { const req = new Request('http://localhost/', { headers: { 'x-forwarded-for': xff }, }) const c = new Context(req, { env: { requestContext: { elb: { targetGroupArn: 'arn:aws:elasticloadbalancing:...', }, }, }, }) const info = getConnInfo(c) expect(info.remote.address).toBe(expected) })
it('Should return undefined when no x-forwarded-for header', () => { const c = new Context(new Request('http://localhost/'), { env: { requestContext: { elb: { targetGroupArn: 'arn:aws:elasticloadbalancing:...', }, }, }, })
const info = getConnInfo(c)
expect(info.remote.address).toBeUndefined() }) })})