@open-mercato/web-research 0.9.1-develop.7574.1.837288eec1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,35 @@ const failingLookup: LookupFn = async () => {
7
7
  throw new Error('ENOTFOUND')
8
8
  }
9
9
 
10
+ const localUseTranslationAddresses = [
11
+ '64:ff9b:1::',
12
+ '64:ff9b:1::a00:1',
13
+ '64:ff9b:1::808:808',
14
+ '0064:FF9B:0001:1234:5678:90AB:CDEF:1234',
15
+ '64:ff9b:1:ffff:ffff:ffff:ffff:ffff',
16
+ ]
17
+ const private6to4Addresses = [
18
+ '2002:0000:0000::1',
19
+ '2002:0a01:0203::1',
20
+ '2002:6440:0001::1',
21
+ '2002:7f00:0001::1',
22
+ '2002:a9fe:a9fe::1',
23
+ '2002:ac10:0001::1',
24
+ '2002:c000:0001::1',
25
+ '2002:c0a8:0101::1',
26
+ '2002:c612:0001::1',
27
+ '2002:e000:0001::1',
28
+ '2002:f000:0001::1',
29
+ '2002:0A00:0001:0000:0000:0000:0808:0808',
30
+ ]
31
+ const blockedTransitionAddresses = [...localUseTranslationAddresses, ...private6to4Addresses]
32
+ const publicIpv6Addresses = [
33
+ '2606:2800:220:1:248:1893:25c8:1946',
34
+ '64:ff9b::808:808',
35
+ '2002:0808:0808:abcd::1',
36
+ '2002:5db8:d822::a00:1',
37
+ ]
38
+
10
39
  describe('isPrivateAddress', () => {
11
40
  it.each([
12
41
  '127.0.0.1',
@@ -34,8 +63,16 @@ describe('isPrivateAddress', () => {
34
63
  },
35
64
  )
36
65
 
37
- it('allows a public IPv6 address', () => {
38
- expect(isPrivateAddress('2606:2800:220:1:248:1893:25c8:1946')).toBe(false)
66
+ it.each(publicIpv6Addresses)('allows public IPv6 %s', (address) => {
67
+ expect(isPrivateAddress(address)).toBe(false)
68
+ })
69
+
70
+ it.each(localUseTranslationAddresses)('blocks the local-use translation address %s', (address) => {
71
+ expect(isPrivateAddress(address)).toBe(true)
72
+ })
73
+
74
+ it.each(private6to4Addresses)('blocks 6to4 with non-public embedded IPv4 %s', (address) => {
75
+ expect(isPrivateAddress(address)).toBe(true)
39
76
  })
40
77
 
41
78
  // Regression: `new URL('http://[::ffff:127.0.0.1]/').hostname` is '::ffff:7f00:1',
@@ -105,6 +142,34 @@ describe('assertPublicUrl', () => {
105
142
  ).rejects.toThrow(/non-public address/)
106
143
  })
107
144
 
145
+ it.each(blockedTransitionAddresses)('rejects the IPv6 literal %s before DNS', async (address) => {
146
+ const lookup = jest.fn<ReturnType<LookupFn>, [string]>()
147
+ await expect(assertPublicUrl(`http://[${address}]/`, 'test', { lookup })).rejects.toMatchObject({
148
+ code: 'ssrf_blocked',
149
+ })
150
+ expect(lookup).not.toHaveBeenCalled()
151
+ })
152
+
153
+ it.each(blockedTransitionAddresses)('rejects a mixed DNS answer containing %s', async (address) => {
154
+ await expect(
155
+ assertPublicUrl('https://translated.example.com/', 'test', {
156
+ lookup: async () => [
157
+ { address: '93.184.216.34', family: 4 },
158
+ { address, family: 6 },
159
+ ],
160
+ }),
161
+ ).rejects.toMatchObject({ code: 'ssrf_blocked' })
162
+ })
163
+
164
+ it.each(publicIpv6Addresses)('vets and retains public IPv6 %s for pinning', async (address) => {
165
+ const literal = await assertPublicUrl(`https://[${address}]/`, 'test')
166
+ expect(literal.addresses).toEqual([new URL(`https://[${address}]/`).hostname.slice(1, -1)])
167
+ const resolved = await assertPublicUrl('https://public.example.com/', 'test', {
168
+ lookup: async () => [{ address, family: 6 }],
169
+ })
170
+ expect(resolved.addresses).toEqual([address])
171
+ })
172
+
108
173
  it('fails closed on DNS failure by default, because it cannot pin without addresses', async () => {
109
174
  await expect(
110
175
  assertPublicUrl('https://example.com/', 'test', { lookup: failingLookup }),
@@ -125,6 +190,28 @@ describe('assertPublicUrl', () => {
125
190
  })
126
191
 
127
192
  describe('allowPrivateHosts', () => {
193
+ it.each(blockedTransitionAddresses)('permits %s only for explicitly named hosts', async (address) => {
194
+ const options = {
195
+ lookup: async () => [{ address, family: 6 }],
196
+ allowPrivateHosts: ['internal.example.com'],
197
+ }
198
+ const vetted = await assertPublicUrl('http://search.internal.example.com/', 'test', options)
199
+ expect(vetted.addresses).toEqual([address])
200
+ await expect(
201
+ assertPublicUrl('http://notinternal.example.com/', 'test', options),
202
+ ).rejects.toMatchObject({ code: 'ssrf_blocked' })
203
+ })
204
+
205
+ it.each(['64:ff9b:1::a00:1', '2002:a00:1::1'])(
206
+ 'permits the explicitly named IPv6 literal %s',
207
+ async (address) => {
208
+ const vetted = await assertPublicUrl(`http://[${address}]/`, 'test', {
209
+ allowPrivateHosts: [address],
210
+ })
211
+ expect(vetted.addresses).toEqual([address])
212
+ },
213
+ )
214
+
128
215
  it('lets a named host resolve privately', async () => {
129
216
  const vetted = await assertPublicUrl('http://searxng:8080/search', 'test', {
130
217
  lookup: async () => [{ address: '10.1.2.3', family: 4 }],
package/src/net/client.ts CHANGED
@@ -349,6 +349,7 @@ export function createHttpClient(options: HttpClientOptions = {}): HttpClient {
349
349
  if (outOfTime()) {
350
350
  throw new WebResearchError('timeout', `Request to ${rawUrl} exceeded its ${maxTotalMs}ms budget`)
351
351
  }
352
+ await init.assertUrlAllowed?.(currentUrl)
352
353
  const vetted = await assertPublicUrl(currentUrl, 'web research fetch', {
353
354
  ...(options.lookup ? { lookup: options.lookup } : {}),
354
355
  ...(options.allowPrivateHosts ? { allowPrivateHosts: options.allowPrivateHosts } : {}),
package/src/net/ssrf.ts CHANGED
@@ -101,6 +101,7 @@ function embeddedIpv4(hextets: readonly number[]): string | null {
101
101
  const nat64 =
102
102
  first === 0x0064 && second === 0xff9b && third === 0 && fourth === 0 && fifth === 0 && sixth === 0
103
103
  if (nat64) return hextetsToIpv4(seventh, eighth)
104
+ if (first === 0x2002) return hextetsToIpv4(second, third)
104
105
  return null
105
106
  }
106
107
 
@@ -109,6 +110,7 @@ function isPrivateIpv6(address: string): boolean {
109
110
  if (normalized === '::1' || normalized === '::') return true
110
111
  const hextets = expandIpv6(normalized)
111
112
  if (hextets === null) return true
113
+ if (hextets[0] === 0x0064 && hextets[1] === 0xff9b && hextets[2] === 1) return true
112
114
  const embedded = embeddedIpv4(hextets)
113
115
  if (embedded !== null) return isPrivateIpv4(embedded)
114
116
  const first = hextets[0]
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod'
2
+ import { defineAdapterModule } from '../../contract/adapter'
3
+ import { describeAdapterContract } from '../contract'
4
+ import { createFakeAdapter } from '../fakeAdapter'
5
+
6
+ const versionOneAdapter = defineAdapterModule({
7
+ id: 'version-one',
8
+ kind: 'api',
9
+ contractVersion: 1,
10
+ optionsSchema: z.object({}),
11
+ createAdapter: () => createFakeAdapter({ id: 'version-one', results: [{ url: 'https://example.test/page' }] }),
12
+ })
13
+
14
+ describeAdapterContract(versionOneAdapter, { configuredOptions: {} }, { describe, it, expect })
@@ -1,6 +1,6 @@
1
1
  import type { AdapterContext, AdapterModule, SearchAdapter } from '../contract/adapter'
2
2
  import { silentLogger, type HttpClient, type HttpResponse } from '../contract/http'
3
- import { CONTRACT_VERSION } from '../contract/version'
3
+ import { isSupportedContractVersion } from '../contract/version'
4
4
  import type { SearchRequest } from '../contract/results'
5
5
 
6
6
  export type StubResponse = Partial<HttpResponse> & { readonly body: string }
@@ -80,7 +80,7 @@ export function describeAdapterContract<TOptions>(
80
80
 
81
81
  describe(`${module.id} adapter contract`, () => {
82
82
  it('declares the contract version it was built against', () => {
83
- expect(module.contractVersion).toBe(CONTRACT_VERSION)
83
+ expect(isSupportedContractVersion(module.contractVersion)).toBe(true)
84
84
  })
85
85
 
86
86
  it('exposes a stable id and kind', () => {