Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/test-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export const TEST_SUBNETS = {
PRIVATE_172: '172.16.0.0/12', //NOSONAR
PRIVATE_192: '192.168.1.0/24', //NOSONAR
PRIVATE_192_16: '192.168.0.0/16', //NOSONAR
PRIVATE_192_2_24: '192.168.2.0/24', //NOSONAR
LOCALHOST: '127.0.0.0/8',
ANY: '0.0.0.0/0',
// IPv6 subnets
Expand All @@ -301,6 +302,11 @@ export const TEST_SUBNETS = {
UNIQUE_LOCAL_V6: 'fc00::/7', //NOSONAR
DOCUMENTATION_V6: '2001:db8::/32', //NOSONAR
DOCUMENTATION_V6_64: '2001:db8::/64', //NOSONAR
// Session-store client and network keys, as the policy writes them
DOCUMENTATION_V6_CLIENT_KEY_64: '2001:db8:0:0::/64', //NOSONAR
LOCALHOST_V6_CLIENT_KEY_64: '0:0:0:0::/64', //NOSONAR
DOCUMENTATION_V6_SUBNET_48: '2001:db8:1::/48', //NOSONAR
DOCUMENTATION_V6_SUBNET_ALT_48: '2001:db8:ffff::/48', //NOSONAR
IPV4_MAPPED_V6: '::ffff:0:0/96', //NOSONAR
NAT64_PRIVATE_10_V6: '64:ff9b::a00:0/104', //NOSONAR - NAT64 prefix with 10.0.0.0/8 embedded
ANY_V6: '::/0' //NOSONAR
Expand Down Expand Up @@ -335,6 +341,7 @@ export const TEST_IPS = {
DOCUMENTATION_V6_ALT: '2001:db8::2', //NOSONAR
DOCUMENTATION_V6_FFFF: '2001:db8::ffff', //NOSONAR
DOCUMENTATION_V6_SUBNET: '2001:db8:1::1', //NOSONAR
DOCUMENTATION_V6_EXPANDED_UPPER: '2001:0DB8:0000:0000:1:2:3:4', //NOSONAR - DOCUMENTATION_V6's /64
DOCUMENTATION_V6_SUBNET_ALT: '2001:db8:ffff::1', //NOSONAR
DOCUMENTATION_V6_DIFF: '2001:db9::1', //NOSONAR
UNIQUE_LOCAL_V6: 'fd00::5', //NOSONAR
Expand Down
24 changes: 13 additions & 11 deletions tests/unit/middleware/session-store-policy.vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
clientLimitsFor,
networkLimitsFor,
} from '../../../app/middleware/session-store-policy.js'
import { TEST_IPS } from '../../test-constants.js'
import { TEST_IPS, TEST_SUBNETS } from '../../test-constants.js'
import { networkKeyFor } from '../../../app/utils/client-key.js'

const limits = resolveSessionStoreLimits({ provisionalTtlMs: 1000, maxSessionBytes: 100 })
Expand Down Expand Up @@ -99,12 +99,14 @@ describe('networkLimitsFor', () => {

describe('networkKeyFor', () => {
it('groups IPv4 by /24 and IPv6 by /48', () => {
expect(networkKeyFor(TEST_IPS.PRIVATE_192_100)).toBe('192.168.1.0/24')
expect(networkKeyFor(TEST_IPS.PRIVATE_192_101)).toBe('192.168.1.0/24')
expect(networkKeyFor(TEST_IPS.PRIVATE_192_2_1)).toBe('192.168.2.0/24')
expect(networkKeyFor(`::ffff:${TEST_IPS.PRIVATE_10}`)).toBe('10.0.0.0/24')
expect(networkKeyFor(TEST_IPS.DOCUMENTATION_V6_SUBNET)).toBe('2001:db8:1::/48')
expect(networkKeyFor(TEST_IPS.DOCUMENTATION_V6_SUBNET_ALT)).toBe('2001:db8:ffff::/48')
expect(networkKeyFor(TEST_IPS.PRIVATE_192_100)).toBe(TEST_SUBNETS.PRIVATE_192)
expect(networkKeyFor(TEST_IPS.PRIVATE_192_101)).toBe(TEST_SUBNETS.PRIVATE_192)
expect(networkKeyFor(TEST_IPS.PRIVATE_192_2_1)).toBe(TEST_SUBNETS.PRIVATE_192_2_24)
expect(networkKeyFor(`::ffff:${TEST_IPS.PRIVATE_10}`)).toBe(TEST_SUBNETS.PRIVATE_10_24)
expect(networkKeyFor(TEST_IPS.DOCUMENTATION_V6_SUBNET))
.toBe(TEST_SUBNETS.DOCUMENTATION_V6_SUBNET_48)
expect(networkKeyFor(TEST_IPS.DOCUMENTATION_V6_SUBNET_ALT))
.toBe(TEST_SUBNETS.DOCUMENTATION_V6_SUBNET_ALT_48)
})
})

Expand All @@ -116,19 +118,19 @@ describe('clientKeyFor', () => {

it('counts IPv6 addresses by their /64', () => {
const key = clientKeyFor(TEST_IPS.DOCUMENTATION_V6)
expect(key).toBe('2001:db8:0:0::/64')
expect(key).toBe(TEST_SUBNETS.DOCUMENTATION_V6_CLIENT_KEY_64)
expect(clientKeyFor(TEST_IPS.DOCUMENTATION_V6_ALT)).toBe(key)
expect(clientKeyFor(TEST_IPS.DOCUMENTATION_V6_FFFF)).toBe(key)
expect(clientKeyFor('2001:0DB8:0000:0000:1:2:3:4')).toBe(key)
expect(clientKeyFor(TEST_IPS.DOCUMENTATION_V6_EXPANDED_UPPER)).toBe(key)
expect(clientKeyFor(TEST_IPS.DOCUMENTATION_V6_SUBNET)).not.toBe(key)
expect(clientKeyFor(TEST_IPS.LOCALHOST_V6)).toBe('0:0:0:0::/64')
expect(clientKeyFor(TEST_IPS.LOCALHOST_V6)).toBe(TEST_SUBNETS.LOCALHOST_V6_CLIENT_KEY_64)
})

it('records the /64 of an established session', () => {
const placed = placeSession(
{ data: { sshLoginVerified: true, sshLoginClient: TEST_IPS.DOCUMENTATION_V6_ALT }, bytes: 1, cookieExpiresAt: null, now: 0 },
DEFAULT_SESSION_STORE_LIMITS
)
expect(placed.ok && placed.value.client).toBe('2001:db8:0:0::/64')
expect(placed.ok && placed.value.client).toBe(TEST_SUBNETS.DOCUMENTATION_V6_CLIENT_KEY_64)
})
})
13 changes: 9 additions & 4 deletions tests/unit/services/connection-limits.vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,18 @@ describe('a peer that looks like a proxy, with http.trustProxy unset', () => {
TEST_IPS.PRIVATE_172,
TEST_IPS.PRIVATE_192,
`::ffff:${TEST_IPS.PRIVATE_10}`,
'::1',
'fd12:3456::1',
'fe80::1%eth0'
TEST_IPS.LOCALHOST_V6,
TEST_IPS.UNIQUE_LOCAL_V6,
TEST_IPS.LINK_LOCAL_V6_ZONE
]) {
expect(isLocalOrPrivateAddress(address)).toBe(true)
}
for (const address of [TEST_NETWORK.FORWARDED_IP, `::ffff:${TEST_NETWORK.FORWARDED_IP}`, '2001:db8::1', 'nonsense']) {
for (const address of [
TEST_NETWORK.FORWARDED_IP,
`::ffff:${TEST_NETWORK.FORWARDED_IP}`,
TEST_IPS.DOCUMENTATION_V6,
'nonsense'
]) {
expect(isLocalOrPrivateAddress(address)).toBe(false)
}
})
Expand Down
Loading