|
| 1 | +import { AllowlistMiddleware } from '../middleware/allowlist.middleware'; |
| 2 | +import { ForbiddenException } from '@nestjs/common'; |
| 3 | +import type { Request, Response, NextFunction } from 'express'; |
| 4 | + |
| 5 | +function mockConfig(cidrs: string) { |
| 6 | + return { get: jest.fn((key: string) => (key === 'ADMIN_ALLOWED_CIDRS' ? cidrs : undefined)) }; |
| 7 | +} |
| 8 | + |
| 9 | +function makeReq(ip: string, forwarded?: string): Request { |
| 10 | + return { |
| 11 | + ip, |
| 12 | + get: jest.fn((header: string) => { |
| 13 | + if (header === 'X-Forwarded-For') return forwarded ?? undefined; |
| 14 | + return undefined; |
| 15 | + }), |
| 16 | + socket: { remoteAddress: ip }, |
| 17 | + } as unknown as Request; |
| 18 | +} |
| 19 | + |
| 20 | +function makeRes(): Response { |
| 21 | + return { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; |
| 22 | +} |
| 23 | + |
| 24 | +describe('AllowlistMiddleware', () => { |
| 25 | + describe('empty allowlist (backward compatible)', () => { |
| 26 | + it('allows any IP when ADMIN_ALLOWED_CIDRS is empty', () => { |
| 27 | + const mw = new AllowlistMiddleware(mockConfig('') as never); |
| 28 | + const next: NextFunction = jest.fn(); |
| 29 | + mw.use(makeReq('10.0.0.1'), makeRes(), next); |
| 30 | + expect(next).toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('allows any IP when ADMIN_ALLOWED_CIDRS is unset', () => { |
| 34 | + const mw = new AllowlistMiddleware(mockConfig('') as never); |
| 35 | + const next: NextFunction = jest.fn(); |
| 36 | + mw.use(makeReq('::1'), makeRes(), next); |
| 37 | + expect(next).toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('single CIDR rule', () => { |
| 42 | + it('allows IP within the CIDR range', () => { |
| 43 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 44 | + const next: NextFunction = jest.fn(); |
| 45 | + mw.use(makeReq('10.0.0.42'), makeRes(), next); |
| 46 | + expect(next).toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('blocks IP outside the CIDR range with ForbiddenException', () => { |
| 50 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 51 | + const next: NextFunction = jest.fn(); |
| 52 | + expect(() => mw.use(makeReq('10.0.1.1'), makeRes(), next)).toThrow(ForbiddenException); |
| 53 | + expect(next).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('blocks IP with 403 — no auth processing happens', () => { |
| 57 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 58 | + const next: NextFunction = jest.fn(); |
| 59 | + try { |
| 60 | + mw.use(makeReq('192.168.1.1'), makeRes(), next); |
| 61 | + } catch (e) { |
| 62 | + expect(e).toBeInstanceOf(ForbiddenException); |
| 63 | + expect((e as ForbiddenException).getStatus()).toBe(403); |
| 64 | + expect((e as ForbiddenException).message).toBe('Access denied'); |
| 65 | + } |
| 66 | + expect(next).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('multiple CIDR rules', () => { |
| 71 | + it('allows IP matching any of the rules', () => { |
| 72 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/8,192.168.0.0/16') as never); |
| 73 | + const next: NextFunction = jest.fn(); |
| 74 | + mw.use(makeReq('10.1.2.3'), makeRes(), next); |
| 75 | + expect(next).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('blocks IP not matching any rule', () => { |
| 79 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/8,192.168.0.0/16') as never); |
| 80 | + const next: NextFunction = jest.fn(); |
| 81 | + expect(() => mw.use(makeReq('172.16.0.1'), makeRes(), next)).toThrow(ForbiddenException); |
| 82 | + expect(next).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('IPv6 support', () => { |
| 87 | + it('allows IPv6 within the CIDR range', () => { |
| 88 | + const mw = new AllowlistMiddleware(mockConfig('2001:db8::/32') as never); |
| 89 | + const next: NextFunction = jest.fn(); |
| 90 | + mw.use(makeReq('2001:db8:dead:beef::1'), makeRes(), next); |
| 91 | + expect(next).toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('blocks IPv6 outside the CIDR range', () => { |
| 95 | + const mw = new AllowlistMiddleware(mockConfig('2001:db8::/32') as never); |
| 96 | + const next: NextFunction = jest.fn(); |
| 97 | + expect(() => mw.use(makeReq('2001:db9::1'), makeRes(), next)).toThrow(ForbiddenException); |
| 98 | + expect(next).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('handles IPv6 with :: compression', () => { |
| 102 | + const mw = new AllowlistMiddleware(mockConfig('fd00::/8') as never); |
| 103 | + const next: NextFunction = jest.fn(); |
| 104 | + mw.use(makeReq('fd12:3456:789a::1'), makeRes(), next); |
| 105 | + expect(next).toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('handles ::1 loopback', () => { |
| 109 | + const mw = new AllowlistMiddleware(mockConfig('::1/128') as never); |
| 110 | + const next: NextFunction = jest.fn(); |
| 111 | + mw.use(makeReq('::1'), makeRes(), next); |
| 112 | + expect(next).toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('IPv4-mapped IPv6', () => { |
| 117 | + it('matches IPv4-mapped address against IPv4 CIDR rule', () => { |
| 118 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 119 | + const next: NextFunction = jest.fn(); |
| 120 | + mw.use(makeReq('::ffff:10.0.0.5'), makeRes(), next); |
| 121 | + expect(next).toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('blocks IPv4-mapped address outside the IPv4 CIDR', () => { |
| 125 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 126 | + const next: NextFunction = jest.fn(); |
| 127 | + expect(() => mw.use(makeReq('::ffff:172.16.0.1'), makeRes(), next)).toThrow(ForbiddenException); |
| 128 | + expect(next).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('X-Forwarded-For', () => { |
| 133 | + it('uses X-Forwarded-For header when present', () => { |
| 134 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 135 | + const next: NextFunction = jest.fn(); |
| 136 | + // req.ip is blocked but X-Forwarded-For is allowed |
| 137 | + mw.use(makeReq('192.168.1.1', '10.0.0.5'), makeRes(), next); |
| 138 | + expect(next).toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('takes first IP from X-Forwarded-For chain', () => { |
| 142 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24') as never); |
| 143 | + const next: NextFunction = jest.fn(); |
| 144 | + mw.use(makeReq('192.168.1.1', '10.0.0.5, 192.168.1.1'), makeRes(), next); |
| 145 | + expect(next).toHaveBeenCalled(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('non-leaky error message', () => { |
| 150 | + it('does not expose the allowlist config in the error', () => { |
| 151 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.0/24,192.168.0.0/16') as never); |
| 152 | + const next: NextFunction = jest.fn(); |
| 153 | + try { |
| 154 | + mw.use(makeReq('1.2.3.4'), makeRes(), next); |
| 155 | + } catch (e) { |
| 156 | + expect(e).toBeInstanceOf(ForbiddenException); |
| 157 | + expect((e as ForbiddenException).message).toBe('Access denied'); |
| 158 | + // Should not leak CIDR ranges |
| 159 | + expect((e as ForbiddenException).message).not.toContain('10.0.0.0'); |
| 160 | + expect((e as ForbiddenException).message).not.toContain('192.168.0.0'); |
| 161 | + } |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('edge cases', () => { |
| 166 | + it('allows exact /32 match', () => { |
| 167 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.42/32') as never); |
| 168 | + const next: NextFunction = jest.fn(); |
| 169 | + mw.use(makeReq('10.0.0.42'), makeRes(), next); |
| 170 | + expect(next).toHaveBeenCalled(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('blocks non-matching exact /32', () => { |
| 174 | + const mw = new AllowlistMiddleware(mockConfig('10.0.0.42/32') as never); |
| 175 | + const next: NextFunction = jest.fn(); |
| 176 | + expect(() => mw.use(makeReq('10.0.0.43'), makeRes(), next)).toThrow(ForbiddenException); |
| 177 | + }); |
| 178 | + |
| 179 | + it('allows /0 (all IPs)', () => { |
| 180 | + const mw = new AllowlistMiddleware(mockConfig('0.0.0.0/0') as never); |
| 181 | + const next: NextFunction = jest.fn(); |
| 182 | + mw.use(makeReq('8.8.8.8'), makeRes(), next); |
| 183 | + expect(next).toHaveBeenCalled(); |
| 184 | + }); |
| 185 | + |
| 186 | + it('skips invalid CIDR entries without crashing', () => { |
| 187 | + const mw = new AllowlistMiddleware(mockConfig('not-a-cidr,10.0.0.0/24') as never); |
| 188 | + const next: NextFunction = jest.fn(); |
| 189 | + mw.use(makeReq('10.0.0.1'), makeRes(), next); |
| 190 | + expect(next).toHaveBeenCalled(); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments