Skip to content
Myr

index.test.ts

This file

Path

src/middleware/ip-restriction/index.test.ts

217 lines · 7.5 KiB · TypeScript

Last changed by

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

lines 1–2why, one step back
1import { ipRestriction } from '.'
2import type { IPRestrictionRule } from '.'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 3–217why, one step back
3import { Context } from '../../context'
4import type { AddressType, GetConnInfo } from '../../helper/conninfo'5import { Hono } from '../../hono'67describe('ipRestriction middleware', () => {8 it('Should restrict', async () => {9 const getConnInfo: GetConnInfo = (c) => {10 return {11 remote: {12 address: c.env.ip,13 },14 }15 }16 const app = new Hono<{17 Bindings: {18 ip: string19 }20 }>()21 app.use(22 '/basic',23 ipRestriction(getConnInfo, {24 allowList: ['192.168.1.0', '192.168.2.0/24'],25 denyList: ['192.168.2.10'],26 })27 )28 app.get('/basic', (c) => c.text('Hello World!'))2930 app.use(31 '/allow-empty',32 ipRestriction(getConnInfo, {33 denyList: ['192.168.1.0'],34 })35 )36 app.get('/allow-empty', (c) => c.text('Hello World!'))3738 expect((await app.request('/basic', {}, { ip: '0.0.0.0' })).status).toBe(403)3940 expect((await app.request('/basic', {}, { ip: '192.168.1.0' })).status).toBe(200)4142 expect((await app.request('/basic', {}, { ip: '192.168.2.5' })).status).toBe(200)43 expect((await app.request('/basic', {}, { ip: '192.168.2.10' })).status).toBe(403)4445 expect((await app.request('/allow-empty', {}, { ip: '0.0.0.0' })).status).toBe(200)4647 expect((await app.request('/allow-empty', {}, { ip: '192.168.1.0' })).status).toBe(403)4849 expect((await app.request('/allow-empty', {}, { ip: '192.168.2.5' })).status).toBe(200)50 expect((await app.request('/allow-empty', {}, { ip: '192.168.2.10' })).status).toBe(200)51 })52 it('Custom onerror', async () => {53 const res = await ipRestriction(54 () => '0.0.0.0',55 { denyList: ['0.0.0.0'] },56 () => new Response('error')57 )(new Context(new Request('http://localhost/')), async () => void 0)58 expect(res).toBeTruthy()59 if (res) {60 expect(await res.text()).toBe('error')61 }62 })6364 test.each(['999.999.999.999', '2001:db8::1%eth0', '1234:::5678'])(65 'Should reject invalid remote address: %s',66 async (ip) => {67 const app = new Hono<{68 Bindings: {69 ip: string70 }71 }>()72 app.use(73 '/invalid',74 ipRestriction(75 (c) => ({76 remote: {77 address: c.env.ip,78 },79 }),80 {81 allowList: ['127.0.0.1'],82 }83 )84 )85 app.get('/invalid', (c) => c.text('Hello World!'))8687 expect((await app.request('/invalid', {}, { ip })).status).toBe(403)88 }89 )9091 it('Should not call onError for invalid remote addresses', async () => {92 const app = new Hono<{93 Bindings: {94 ip: string95 }96 }>()97 app.use(98 '/invalid',99 ipRestriction(100 (c) => ({101 remote: {102 address: c.env.ip,103 },104 }),105 {106 allowList: ['127.0.0.1'],107 },108 () => new Response('custom error', { status: 418 })109 )110 )111 app.get('/invalid', (c) => c.text('Hello World!'))112113 const res = await app.request('/invalid', {}, { ip: '1234:::5678' })114 expect(res.status).toBe(403)115 expect(await res.text()).toBe('Forbidden')116 })117})118119describe('isMatchForRule', () => {120 const isMatch = async (info: { addr: string; type: AddressType }, rule: IPRestrictionRule) => {121 const middleware = ipRestriction(122 () => ({123 remote: {124 address: info.addr,125 addressType: info.type,126 },127 }),128 {129 allowList: [rule],130 }131 )132 try {133 // eslint-disable-next-line @typescript-eslint/no-explicit-any134 await middleware(undefined as any, () => Promise.resolve())135 } catch {136 return false137 }138 return true139 }140141 test.each(['192.168.0.0/33', '::/129', '127.0.0.1/', '::ffff:127.0.0.1/129'])(142 'Should throw for invalid CIDR rule: %s',143 (rule) => {144 expect(() =>145 ipRestriction(() => '127.0.0.1', {146 allowList: [rule],147 })148 ).toThrow(`Invalid rule: ${rule}`)149 }150 )151152 it('star', async () => {153 expect(await isMatch({ addr: '192.168.2.0', type: 'IPv4' }, '*')).toBeTruthy()154 expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '*')).toBeTruthy()155 expect(await isMatch({ addr: '::0', type: 'IPv6' }, '*')).toBeTruthy()156 })157 it('CIDR Notation', async () => {158 expect(await isMatch({ addr: '192.168.2.0', type: 'IPv4' }, '192.168.2.0/24')).toBeTruthy()159 expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.0/24')).toBeTruthy()160 expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.1/32')).toBeTruthy()161 expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.2/32')).toBeFalsy()162163 expect(await isMatch({ addr: '::0', type: 'IPv6' }, '::0/1')).toBeTruthy()164 expect(await isMatch({ addr: '::1', type: 'IPv6' }, '0.0.0.0/24')).toBeFalsy()165 expect(await isMatch({ addr: '::abcd:1', type: 'IPv6' }, '127.0.0.0/8')).toBeFalsy()166 expect(await isMatch({ addr: '::1', type: 'IPv6' }, '::ffff:1.0.0.0/96')).toBeFalsy()167168 expect(169 await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::ffff:192.168.1.0/120')170 ).toBeTruthy()171 expect(172 await isMatch({ addr: '192.168.1.1', type: 'IPv4' }, '::ffff:192.168.1.0/120')173 ).toBeTruthy()174 expect(175 await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '192.168.1.0/24')176 ).toBeTruthy()177 expect(await isMatch({ addr: '::ffff:10.0.0.1', type: 'IPv6' }, '192.168.1.0/24')).toBeFalsy()178 expect(await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::/0')).toBeTruthy()179 expect(180 await isMatch({ addr: '::ffff:192.168.1.1', type: 'IPv6' }, '::ffff:0:0/95')181 ).toBeTruthy()182 })183 it('Static Rules', async () => {184 expect(await isMatch({ addr: '192.168.2.1', type: 'IPv4' }, '192.168.2.1')).toBeTruthy()185 expect(await isMatch({ addr: '1234::5678', type: 'IPv6' }, '1234::5678')).toBeTruthy()186 expect(187 await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '::ffff:127.0.0.1')188 ).toBeTruthy()189 expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '::ffff:7f00:1')).toBeTruthy()190 expect(await isMatch({ addr: '127.0.0.1', type: 'IPv4' }, '::ffff:127.0.0.1')).toBeTruthy()191 // IPv4-mapped IPv6 addresses are canonicalized to IPv4192 expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '127.0.0.1')).toBeTruthy()193 expect(await isMatch({ addr: '::ffff:127.0.0.1', type: 'IPv6' }, '127.0.0.2')).toBeFalsy()194 expect(await isMatch({ addr: '::ffff:7f00:1', type: 'IPv6' }, '127.0.0.1')).toBeTruthy()195 expect(await isMatch({ addr: '0:0:0:0:0:ffff:7f00:1', type: 'IPv6' }, '127.0.0.1')).toBeTruthy()196 // regular IPv6 is matched numerically without being treated as IPv4197 expect(await isMatch({ addr: '::1', type: 'IPv6' }, '::1')).toBeTruthy()198 expect(199 await isMatch({ addr: '2001:db8:0:0:0:0:0:1', type: 'IPv6' }, '2001:db8::1')200 ).toBeTruthy()201 expect(202 await isMatch({ addr: '2001:db8::1', type: 'IPv6' }, '2001:db8:0:0:0:0:0:1')203 ).toBeTruthy()204 expect(await isMatch({ addr: '::ffff:127.0.0.2', type: 'IPv6' }, '127.0.0.1')).toBeFalsy()205 expect(await isMatch({ addr: '::7f00:1', type: 'IPv6' }, '127.0.0.1')).toBeFalsy()206 })207 it('Function Rules', async () => {208 expect(await isMatch({ addr: '0.0.0.0', type: 'IPv4' }, () => true)).toBeTruthy()209 expect(await isMatch({ addr: '0.0.0.0', type: 'IPv4' }, () => false)).toBeFalsy()210211 const ipaddr = '93.184.216.34'212 await isMatch({ addr: ipaddr, type: 'IPv4' }, (ip) => {213 expect(ipaddr).toBe(ip.addr)214 return false215 })216 })217})