about summary refs log tree commit diff
path: root/bskylink/tests/index.ts
blob: 1b3d06ad1904550a1a56562802d782455dc6cb77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import assert from 'node:assert'
import {type AddressInfo} from 'node:net'
import {after, before, describe, it} from 'node:test'

import {ToolsOzoneSafelinkDefs} from '@atproto/api'

import {Database, envToCfg, LinkService, readEnv} from '../src/index.js'

describe('link service', async () => {
  let linkService: LinkService
  let baseUrl: string
  before(async () => {
    const env = readEnv()
    const cfg = envToCfg({
      ...env,
      hostnames: ['test.bsky.link'],
      appHostname: 'test.bsky.app',
      dbPostgresSchema: 'link_test',
      dbPostgresUrl: process.env.DB_POSTGRES_URL,
      safelinkEnabled: true,
      ozoneUrl: 'http://localhost:2583',
      ozoneAgentHandle: 'mod-authority.test',
      ozoneAgentPass: 'hunter2',
    })
    const migrateDb = Database.postgres({
      url: cfg.db.url,
      schema: cfg.db.schema,
    })
    await migrateDb.migrateToLatestOrThrow()
    await migrateDb.close()
    linkService = await LinkService.create(cfg)
    await linkService.start()
    const {port} = linkService.server?.address() as AddressInfo
    baseUrl = `http://localhost:${port}`

    // Ensure blocklist, whitelist, and safelink rules are set up
    const now = new Date().toISOString()
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 1,
      eventType: ToolsOzoneSafelinkDefs.ADDRULE,
      url: 'https://en.wikipedia.org/wiki/Fight_Club',
      pattern: ToolsOzoneSafelinkDefs.URL,
      action: ToolsOzoneSafelinkDefs.WARN,
      reason: ToolsOzoneSafelinkDefs.SPAM,
      createdBy: 'did:example:admin',
      createdAt: now,
      comment: 'Do not talk about Fight Club',
    })
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 2,
      eventType: ToolsOzoneSafelinkDefs.ADDRULE,
      url: 'https://gist.github.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a',
      pattern: ToolsOzoneSafelinkDefs.URL,
      action: ToolsOzoneSafelinkDefs.BLOCK,
      reason: ToolsOzoneSafelinkDefs.SPAM,
      createdBy: 'did:example:admin',
      createdAt: now,
      comment: 'All Bs',
    })
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 3,
      eventType: ToolsOzoneSafelinkDefs.ADDRULE,
      url: 'https://en.wikipedia.org',
      pattern: ToolsOzoneSafelinkDefs.DOMAIN,
      action: ToolsOzoneSafelinkDefs.WHITELIST,
      reason: ToolsOzoneSafelinkDefs.NONE,
      createdBy: 'did:example:admin',
      createdAt: now,
      comment: 'Whitelisting the knowledge base of the internet',
    })
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 4,
      eventType: ToolsOzoneSafelinkDefs.ADDRULE,
      url: 'https://www.instagram.com/teamseshbones/?hl=en',
      pattern: ToolsOzoneSafelinkDefs.URL,
      action: ToolsOzoneSafelinkDefs.BLOCK,
      reason: ToolsOzoneSafelinkDefs.SPAM,
      createdBy: 'did:example:admin',
      createdAt: now,
      comment: 'BONES has been erroneously blocked for the sake of this test',
    })
    const later = new Date(Date.now() + 1000).toISOString()
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 5,
      eventType: ToolsOzoneSafelinkDefs.REMOVERULE,
      url: 'https://www.instagram.com/teamseshbones/?hl=en',
      pattern: ToolsOzoneSafelinkDefs.URL,
      action: ToolsOzoneSafelinkDefs.REMOVERULE,
      reason: ToolsOzoneSafelinkDefs.NONE,
      createdBy: 'did:example:admin',
      createdAt: later,
      comment:
        'BONES has been resurrected to bring good music to the world once again',
    })
    linkService.ctx.cfg.eventCache.smartUpdate({
      $type: 'tools.ozone.safelink.defs#event',
      id: 6,
      eventType: ToolsOzoneSafelinkDefs.ADDRULE,
      url: 'https://www.leagueoflegends.com/en-us/',
      pattern: ToolsOzoneSafelinkDefs.URL,
      action: ToolsOzoneSafelinkDefs.WARN,
      reason: ToolsOzoneSafelinkDefs.SPAM,
      createdBy: 'did:example:admin',
      createdAt: now,
      comment:
        'Could be quite the mistake to get into this addicting game, but we will warn instead of block',
    })
  })
  after(async () => {
    await linkService?.destroy()
  })

  it('creates a starter pack link', async () => {
    const link = await getLink('/start/did:example:alice/xxx')
    const url = new URL(link)
    assert.strictEqual(url.origin, 'https://test.bsky.link')
    assert.match(url.pathname, /^\/[a-z0-9]+$/i)
  })

  it('normalizes input paths and provides same link each time.', async () => {
    const link1 = await getLink('/start/did%3Aexample%3Abob/yyy')
    const link2 = await getLink('/start/did:example:bob/yyy/')
    assert.strictEqual(link1, link2)
  })

  it('serves permanent redirect, preserving query params.', async () => {
    const link = await getLink('/start/did:example:carol/zzz/')
    const [status, location] = await getRedirect(`${link}?a=b`)
    assert.strictEqual(status, 301)
    const locationUrl = new URL(location)
    assert.strictEqual(
      locationUrl.pathname + locationUrl.search,
      '/start/did:example:carol/zzz?a=b',
    )
  })

  it('returns json object with url when requested', async () => {
    const link = await getLink('/start/did:example:carol/zzz/')
    const [status, json] = await getJsonRedirect(link)
    assert.strictEqual(status, 200)
    assert(json.url)
    const url = new URL(json.url)
    assert.strictEqual(url.pathname, '/start/did:example:carol/zzz')
  })

  it('returns 404 for unknown link when requesting json', async () => {
    const [status, json] = await getJsonRedirect(
      'https://test.bsky.link/unknown',
    )
    assert(json.error)
    assert(json.message)
    assert.strictEqual(status, 404)
    assert.strictEqual(json.error, 'NotFound')
    assert.strictEqual(json.message, 'Link not found')
  })

  it('League of Legends warned', async () => {
    const urlToRedirect = 'https://www.leagueoflegends.com/en-us/'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    // League of Legends is set to WARN, not BLOCK, so expect a warning (blocked-site div present)
    assert.match(
      html,
      /Warning: Malicious Link/,
      'Expected warning not found in HTML',
    )
  })

  it('Wikipedia whitelisted, url restricted. Redirect safely since wikipedia is whitelisted', async () => {
    const urlToRedirect = 'https://en.wikipedia.org/wiki/Fight_Club'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(html, /meta http-equiv="refresh"/)
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    // Wikipedia domain is whitelisted, so no blocked-site div should be present
    assert.doesNotMatch(html, /"blocked-site"/)
  })

  it('Unsafe redirect with block rule, due to the content of webpage.', async () => {
    const urlToRedirect =
      'https://gist.github.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    assert.match(
      html,
      /"blocked-site"/,
      'Expected blocked-site div not found in HTML',
    )
  })

  it('Rule adjustment, safe redirect, 200 response for Instagram Account of teamsesh Bones', async () => {
    // Retrieve the latest event after all updates
    const result = linkService.ctx.cfg.eventCache.smartGet(
      'https://www.instagram.com/teamseshbones/?hl=en',
    )
    assert(result, 'Expected event not found in eventCache')
    assert.strictEqual(result.eventType, ToolsOzoneSafelinkDefs.REMOVERULE)
    const urlToRedirect = 'https://www.instagram.com/teamseshbones/?hl=en'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(html, /meta http-equiv="refresh"/)
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
  })

  async function getRedirect(link: string): Promise<[number, string]> {
    const url = new URL(link)
    const base = new URL(baseUrl)
    url.protocol = base.protocol
    url.host = base.host
    const res = await fetch(url, {redirect: 'manual'})
    await res.arrayBuffer() // drain
    assert(
      res.status === 301 || res.status === 303,
      'response was not a redirect',
    )
    return [res.status, res.headers.get('location') ?? '']
  }

  async function getJsonRedirect(
    link: string,
  ): Promise<[number, {url?: string; error?: string; message?: string}]> {
    const url = new URL(link)
    const base = new URL(baseUrl)
    url.protocol = base.protocol
    url.host = base.host
    const res = await fetch(url, {
      redirect: 'manual',
      headers: {accept: 'application/json,text/html'},
    })
    assert(
      res.headers.get('content-type')?.startsWith('application/json'),
      'content type was not json',
    )
    const json = await res.json()
    return [res.status, json]
  }

  async function getLink(path: string): Promise<string> {
    const res = await fetch(new URL('/link', baseUrl), {
      method: 'post',
      headers: {'content-type': 'application/json'},
      body: JSON.stringify({path}),
    })
    assert.strictEqual(res.status, 200)
    const payload = await res.json()
    assert(typeof payload.url === 'string')
    return payload.url
  }
})

describe('link service no safelink', async () => {
  let linkService: LinkService
  let baseUrl: string
  before(async () => {
    const env = readEnv()
    const cfg = envToCfg({
      ...env,
      hostnames: ['test.bsky.link'],
      appHostname: 'test.bsky.app',
      dbPostgresSchema: 'link_test',
      dbPostgresUrl: process.env.DB_POSTGRES_URL,
      safelinkEnabled: false,
      ozoneUrl: 'http://localhost:2583',
      ozoneAgentHandle: 'mod-authority.test',
      ozoneAgentPass: 'hunter2',
    })
    const migrateDb = Database.postgres({
      url: cfg.db.url,
      schema: cfg.db.schema,
    })
    await migrateDb.migrateToLatestOrThrow()
    await migrateDb.close()
    linkService = await LinkService.create(cfg)
    await linkService.start()
    const {port} = linkService.server?.address() as AddressInfo
    baseUrl = `http://localhost:${port}`
  })
  after(async () => {
    await linkService?.destroy()
  })
  it('Wikipedia whitelisted, url restricted. Safelink is disabled, so redirect is always safe', async () => {
    const urlToRedirect = 'https://en.wikipedia.org/wiki/Fight_Club'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(html, /meta http-equiv="refresh"/)
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    // No blocked-site div, always safe
    assert.doesNotMatch(html, /"blocked-site"/)
  })

  it('Unsafe redirect with block rule, but safelink is disabled so redirect is always safe', async () => {
    const urlToRedirect =
      'https://gist.github.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(html, /meta http-equiv="refresh"/)
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    // No blocked-site div, always safe
    assert.doesNotMatch(html, /"blocked-site"/)
  })

  it('Rule adjustment, safe redirect, safelink is disabled so always safe', async () => {
    const urlToRedirect = 'https://www.instagram.com/teamseshbones/?hl=en'
    const url = new URL(`${baseUrl}/redirect`)
    url.searchParams.set('u', urlToRedirect)
    const res = await fetch(url, {redirect: 'manual'})
    assert.strictEqual(res.status, 200)
    const html = await res.text()
    assert.match(html, /meta http-equiv="refresh"/)
    assert.match(
      html,
      new RegExp(urlToRedirect.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
    )
    // No blocked-site div, always safe
    assert.doesNotMatch(html, /"blocked-site"/)
  })
})