Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

subscriptions.test.ts

55 lines4.3 KB

1import { randomBytes } from 'node:crypto'
2import { expect, test } from 'vitest'
3import { SubscriptionCipher, parseSubscriptionKeyring } from '../../src/subscription-crypto.js'
4import { globExpression, globMatches, normalizeSubscription, subscriptionRoute } from '../../src/subscription-protocol.js'
5import { publicWebhookAddress } from '../../src/webhook.js'
6​
7test('subscription globs agree on zero/many segment boundaries and literal regex characters', () => {
8 const cases: [string, readonly string[], readonly string[]][] = [
9 ['events/*', ['events/a', 'events/🐘', 'events/***'], ['events', 'events/a/b', 'x/events/a']],
10 ['events/**', ['events', 'events/a', 'events/a/b'], ['event', 'x/events']],
11 ['**/tail', ['tail', 'a/tail', 'a/b/tail'], ['tail/a', 'retail']],
12 ['a/**/b', ['a/b', 'a/c/b', 'a/c/d/b'], ['a/b/c', 'a/cb']],
13 ['**', ['x', 'a/b'], []],
14 ['a.b/*', ['a.b/x'], ['axb/x']],
15 ['a/**/b/**/c', ['a/b/c', 'a/x/b/y/z/c'], ['a/b/c/d']],
16 ]
17 for (const [pattern, yes, no] of cases) {
18 const regex = new RegExp(globExpression(pattern))
19 for (const path of yes) { expect(regex.test(path), `${pattern} should match ${path}`).toBe(true); expect(globMatches(pattern, path)).toBe(true) }
20 for (const path of no) { expect(regex.test(path), `${pattern} should exclude ${path}`).toBe(false); expect(globMatches(pattern, path)).toBe(false) }
21 }
22})
23test('adversarial wildcard repetition uses bounded matching and malformed Unicode is rejected', () => {
24 expect(globMatches(`events/${'a*'.repeat(128)}z`, `events/${'a'.repeat(1024)}`)).toBe(false)
25 expect(() => normalizeSubscription({ type: 'pull-wake', wake_stream: 'wake/pool', pattern: '\ud800' })).toThrow('Invalid stream pattern')
26})
27test('normalized hashes ignore explicit-list order and duplicates and include delivery/lease policy', () => {
28 const config = { type: 'pull-wake', wake_stream: 'wake/pool', streams: ['events/a', 'events/b'] }
29 const first = normalizeSubscription(config)
30 expect(normalizeSubscription({ ...config, streams: ['events/b', 'events/a', 'events/a'] }).hash).toBe(first.hash)
31 expect(normalizeSubscription({ ...config, lease_ttl_ms: 1000 }).hash).not.toBe(first.hash)
32 expect(() => normalizeSubscription({ ...config, pattern: '**' })).toThrow('own wake channel')
33})
34test('control routing decodes an encoded explicit path once without relaxing ordinary stream paths', () => {
35 expect(subscriptionRoute(new URL('https://fixture.test/v1/stream/__ds/subscriptions/sub/streams/events%2Fa'), '/v1/stream')).toMatchObject({ action: 'member', member: 'events/a' })
36 expect(() => subscriptionRoute(new URL('https://fixture.test/v1/stream/__ds/subscriptions/sub/streams/events%252Fa'), '/v1/stream')).toThrow()
37})
38test('encrypted state is bound to its installation/purpose and supports wrapping-key rotation', () => {
39 const first = randomBytes(32), second = randomBytes(32)
40 const original = new SubscriptionCipher({ currentKeyId: 'v1', keys: { v1: first } })
41 const bytes = Buffer.from('private signing material')
42 const encrypted = original.encrypt(bytes, 'installation:signing:key')
43 expect(encrypted.includes(bytes)).toBe(false)
44 const rotated = new SubscriptionCipher({ currentKeyId: 'v2', keys: { v1: first, v2: second } })
45 expect(rotated.decrypt(encrypted, 'installation:signing:key')).toEqual(bytes)
46 expect(() => rotated.decrypt(encrypted, 'different-installation:signing:key')).toThrow()
47 expect(() => new SubscriptionCipher({ currentKeyId: 'v2', keys: { v2: second } }).decrypt(encrypted, 'installation:signing:key')).toThrow('unavailable')
48 const tampered = Buffer.from(encrypted); tampered[tampered.length - 1] = tampered[tampered.length - 1]! ^ 1
49 expect(() => rotated.decrypt(tampered, 'installation:signing:key')).toThrow()
50 expect(parseSubscriptionKeyring(JSON.stringify({ currentKeyId: 'v1', keys: { v1: first.toString('base64') } })).keys.v1).toEqual(first)
51})
52test('webhook address policy rejects non-public IPv4/IPv6 and mapped-address bypasses', () => {
53 for (const address of ['127.0.0.1', '10.1.2.3', '172.16.0.1', '192.168.1.1', '169.254.169.254', '100.64.0.1', '0.0.0.0', '::', '::1', 'fd00::1', 'fe80::1', '::ffff:127.0.0.1', '224.0.0.1']) expect(publicWebhookAddress(address), address).toBe(false)
54 for (const address of ['8.8.8.8', '1.1.1.1', '2001:4860:4860::8888']) expect(publicWebhookAddress(address), address).toBe(true)
55})