-
Notifications
You must be signed in to change notification settings - Fork 859
Expand file tree
/
Copy pathcontext.ts
More file actions
530 lines (491 loc) · 17 KB
/
context.ts
File metadata and controls
530 lines (491 loc) · 17 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import assert from 'node:assert'
import * as plc from '@did-plc/lib'
import express from 'express'
import { Redis } from 'ioredis'
import * as nodemailer from 'nodemailer'
import * as ui8 from 'uint8arrays'
import * as undici from 'undici'
import { AtpAgent } from '@atproto/api'
import { KmsKeypair, S3BlobStore } from '@atproto/aws'
import * as crypto from '@atproto/crypto'
import { IdResolver } from '@atproto/identity'
import {
AccessTokenMode,
JoseKey,
LexResolver,
OAuthProvider,
OAuthVerifier,
} from '@atproto/oauth-provider'
import { BlobStore } from '@atproto/repo'
import {
createServiceAuthHeaders,
createServiceJwt,
} from '@atproto/xrpc-server'
import {
Fetch,
isUnicastIp,
safeFetchWrap,
unicastLookup,
} from '@atproto-labs/fetch-node'
import { AccountManager } from './account-manager/account-manager'
import { OAuthStore } from './account-manager/oauth-store'
import { ScopeReferenceGetter } from './account-manager/scope-reference-getter'
import { ActorStore } from './actor-store/actor-store'
import { authPassthru, forwardedFor } from './api/proxy'
import {
AuthVerifier,
createPublicKeyObject,
createSecretKeyObject,
} from './auth-verifier'
import { BackgroundQueue } from './background'
import { BskyAppView } from './bsky-app-view'
import { ServerConfig, ServerSecrets } from './config'
import { Crawlers } from './crawlers'
import { DidSqliteCache } from './did-cache'
import { DiskBlobStore } from './disk-blobstore'
import { ImageUrlBuilder } from './image/image-url-builder'
import { fetchLogger, lexiconResolverLogger, oauthLogger } from './logger'
import { ServerMailer } from './mailer'
import { ModerationMailer } from './mailer/moderation'
import { LocalViewer, LocalViewerCreator } from './read-after-write/viewer'
import { getRedisClient } from './redis'
import { Sequencer } from './sequencer'
export type AppContextOptions = {
actorStore: ActorStore
blobstore: (did: string) => BlobStore
localViewer: LocalViewerCreator
mailer: ServerMailer
moderationMailer: ModerationMailer
didCache: DidSqliteCache
idResolver: IdResolver
plcClient: plc.Client
accountManager: AccountManager
sequencer: Sequencer
backgroundQueue: BackgroundQueue
redisScratch?: Redis
crawlers: Crawlers
bskyAppView?: BskyAppView
moderationAgent?: AtpAgent
reportingAgent?: AtpAgent
entrywayAgent?: AtpAgent
entrywayAdminAgent?: AtpAgent
proxyAgent: undici.Dispatcher
safeFetch: Fetch
oauthProvider?: OAuthProvider
authVerifier: AuthVerifier
plcRotationKey: crypto.Keypair
cfg: ServerConfig
}
export class AppContext {
public actorStore: ActorStore
public blobstore: (did: string) => BlobStore
public localViewer: LocalViewerCreator
public mailer: ServerMailer
public moderationMailer: ModerationMailer
public didCache: DidSqliteCache
public idResolver: IdResolver
public plcClient: plc.Client
public accountManager: AccountManager
public sequencer: Sequencer
public backgroundQueue: BackgroundQueue
public redisScratch?: Redis
public crawlers: Crawlers
public bskyAppView?: BskyAppView
public moderationAgent: AtpAgent | undefined
public reportingAgent: AtpAgent | undefined
public entrywayAgent: AtpAgent | undefined
public entrywayAdminAgent: AtpAgent | undefined
public proxyAgent: undici.Dispatcher
public safeFetch: Fetch
public authVerifier: AuthVerifier
public oauthProvider?: OAuthProvider
public plcRotationKey: crypto.Keypair
public cfg: ServerConfig
constructor(opts: AppContextOptions) {
this.actorStore = opts.actorStore
this.blobstore = opts.blobstore
this.localViewer = opts.localViewer
this.mailer = opts.mailer
this.moderationMailer = opts.moderationMailer
this.didCache = opts.didCache
this.idResolver = opts.idResolver
this.plcClient = opts.plcClient
this.accountManager = opts.accountManager
this.sequencer = opts.sequencer
this.backgroundQueue = opts.backgroundQueue
this.redisScratch = opts.redisScratch
this.crawlers = opts.crawlers
this.bskyAppView = opts.bskyAppView
this.moderationAgent = opts.moderationAgent
this.reportingAgent = opts.reportingAgent
this.entrywayAgent = opts.entrywayAgent
this.entrywayAdminAgent = opts.entrywayAdminAgent
this.proxyAgent = opts.proxyAgent
this.safeFetch = opts.safeFetch
this.authVerifier = opts.authVerifier
this.oauthProvider = opts.oauthProvider
this.plcRotationKey = opts.plcRotationKey
this.cfg = opts.cfg
}
static async fromConfig(
cfg: ServerConfig,
secrets: ServerSecrets,
overrides?: Partial<AppContextOptions>,
): Promise<AppContext> {
const blobstore =
cfg.blobstore.provider === 's3'
? S3BlobStore.creator({
bucket: cfg.blobstore.bucket,
region: cfg.blobstore.region,
endpoint: cfg.blobstore.endpoint,
forcePathStyle: cfg.blobstore.forcePathStyle,
credentials: cfg.blobstore.credentials,
uploadTimeoutMs: cfg.blobstore.uploadTimeoutMs,
})
: DiskBlobStore.creator(
cfg.blobstore.location,
cfg.blobstore.tempLocation,
)
const mailTransport =
cfg.email !== null
? nodemailer.createTransport(cfg.email.smtpUrl)
: nodemailer.createTransport({ jsonTransport: true })
const mailer = new ServerMailer(mailTransport, cfg)
const modMailTransport =
cfg.moderationEmail !== null
? nodemailer.createTransport(cfg.moderationEmail.smtpUrl)
: nodemailer.createTransport({ jsonTransport: true })
const moderationMailer = new ModerationMailer(modMailTransport, cfg)
const didCache = new DidSqliteCache(
cfg.db.didCacheDbLoc,
cfg.identity.cacheStaleTTL,
cfg.identity.cacheMaxTTL,
cfg.db.disableWalAutoCheckpoint,
)
await didCache.migrateOrThrow()
const idResolver = new IdResolver({
plcUrl: cfg.identity.plcUrl,
didCache,
timeout: cfg.identity.resolverTimeout,
backupNameservers: cfg.identity.handleBackupNameservers,
})
const plcClient = new plc.Client(cfg.identity.plcUrl)
const backgroundQueue = new BackgroundQueue()
const crawlers = new Crawlers(
cfg.service.hostname,
cfg.crawlers,
backgroundQueue,
)
const sequencer = new Sequencer(
cfg.db.sequencerDbLoc,
crawlers,
undefined,
cfg.db.disableWalAutoCheckpoint,
)
const redisScratch = cfg.redis
? getRedisClient(cfg.redis.address, cfg.redis.password)
: undefined
const bskyAppView = cfg.bskyAppView
? new BskyAppView(cfg.bskyAppView)
: undefined
const moderationAgent = cfg.modService
? new AtpAgent({ service: cfg.modService.url })
: undefined
const reportingAgent = cfg.reportService
? new AtpAgent({ service: cfg.reportService.url })
: undefined
const entrywayAgent = cfg.entryway
? new AtpAgent({ service: cfg.entryway.url })
: undefined
let entrywayAdminAgent: AtpAgent | undefined
if (cfg.entryway && secrets.entrywayAdminToken) {
entrywayAdminAgent = new AtpAgent({ service: cfg.entryway.url })
entrywayAdminAgent.api.setHeader(
'authorization',
basicAuthHeader('admin', secrets.entrywayAdminToken),
)
}
const jwtSecretKey = createSecretKeyObject(secrets.jwtSecret)
const jwtPublicKey = cfg.entryway
? createPublicKeyObject(cfg.entryway.jwtPublicKeyHex)
: null
const imageUrlBuilder = new ImageUrlBuilder(
cfg.service.hostname,
bskyAppView,
)
const actorStore = new ActorStore(cfg.actorStore, {
blobstore,
backgroundQueue,
})
const accountManager = new AccountManager(
idResolver,
jwtSecretKey,
cfg.service.did,
cfg.identity.serviceHandleDomains,
cfg.db,
)
await accountManager.migrateOrThrow()
const plcRotationKey =
secrets.plcRotationKey.provider === 'kms'
? await KmsKeypair.load({
keyId: secrets.plcRotationKey.keyId,
})
: await crypto.Secp256k1Keypair.import(
secrets.plcRotationKey.privateKeyHex,
)
const localViewer = LocalViewer.creator(
accountManager,
imageUrlBuilder,
bskyAppView,
)
// An agent for performing HTTP requests based on user provided URLs.
const proxyAgentBase = new undici.Agent({
allowH2: cfg.proxy.allowHTTP2, // This is experimental
headersTimeout: cfg.proxy.headersTimeout,
maxResponseSize: cfg.proxy.maxResponseSize,
bodyTimeout: cfg.proxy.bodyTimeout,
factory: cfg.proxy.disableSsrfProtection
? undefined
: (origin, opts) => {
const { protocol, hostname } =
origin instanceof URL ? origin : new URL(origin)
if (protocol !== 'https:') {
throw new Error(`Forbidden protocol "${protocol}"`)
}
if (isUnicastIp(hostname) === false) {
throw new Error('Hostname resolved to non-unicast address')
}
return new undici.Pool(origin, opts)
},
connect: {
lookup: cfg.proxy.disableSsrfProtection ? undefined : unicastLookup,
},
})
const proxyAgent =
cfg.proxy.maxRetries > 0
? new undici.RetryAgent(proxyAgentBase, {
statusCodes: [], // Only retry on socket errors
methods: ['GET', 'HEAD'],
maxRetries: cfg.proxy.maxRetries,
})
: proxyAgentBase
/**
* A fetch() function that protects against SSRF attacks, large responses &
* known bad domains. This function can safely be used to fetch user
* provided URLs (unless "disableSsrfProtection" is true, of course).
*
* @note **DO NOT** wrap `safeFetch` with any logging or other transforms as
* this might prevent the use of explicit `redirect: "follow"` init from
* working. See {@link safeFetchWrap}.
*/
const safeFetch = safeFetchWrap({
allowIpHost: false,
allowImplicitRedirect: false,
responseMaxSize: cfg.fetch.maxResponseSize,
ssrfProtection: !cfg.fetch.disableSsrfProtection,
// @NOTE Since we are using NodeJS <= 20, unicastFetchWrap would normally
// *not* be using a keep-alive agent if it we are providing a fetch
// function that is different from `globalThis.fetch`. However, since the
// fetch function below is indeed calling `globalThis.fetch` without
// altering any argument, we can safely force the use of the keep-alive
// agent. This would not be the case if we used "loggedFetch" as that
// function does wrap the input & init arguments into a Request object,
// which, on NodeJS<=20, results in init.dispatcher *not* being used.
dangerouslyForceKeepAliveAgent: true,
fetch: function (input, init) {
const method =
init?.method ?? (input instanceof Request ? input.method : 'GET')
const uri = input instanceof Request ? input.url : String(input)
fetchLogger.info({ method, uri }, 'fetch')
return globalThis.fetch.call(this, input, init)
},
})
const oauthProvider = cfg.oauth.provider
? new OAuthProvider({
issuer: cfg.oauth.issuer,
keyset: [await JoseKey.fromKeyLike(jwtSecretKey, undefined, 'HS256')],
store: new OAuthStore(
accountManager,
actorStore,
imageUrlBuilder,
backgroundQueue,
mailer,
sequencer,
plcClient,
plcRotationKey,
cfg.service.publicUrl,
cfg.identity.recoveryDidKey,
),
redis: redisScratch,
dpopSecret: secrets.dpopSecret,
inviteCodeRequired: cfg.invites.required,
availableUserDomains: cfg.identity.serviceHandleDomains,
hcaptcha: cfg.oauth.provider.hcaptcha,
branding: cfg.oauth.provider.branding,
safeFetch,
lexResolver: new LexResolver({
fetch: safeFetch,
plcDirectoryUrl: cfg.identity.plcUrl,
hooks: {
onResolveAuthority: ({ nsid }) => {
lexiconResolverLogger.debug(
{ nsid: nsid.toString() },
'Resolving lexicon DID authority',
)
// Override the lexicon did resolution to point to a custom PDS
return cfg.lexicon.didAuthority
},
onResolveAuthorityResult({ nsid, did }) {
lexiconResolverLogger.info(
{ nsid: nsid.toString(), did },
'Resolved lexicon DID',
)
},
onResolveAuthorityError({ nsid, err }) {
lexiconResolverLogger.error(
{ nsid: nsid.toString(), err },
'Lexicon DID resolution error',
)
},
onFetchResult({ uri, cid }) {
lexiconResolverLogger.info(
{ uri: uri.toString(), cid: cid.toString() },
'Fetched lexicon',
)
},
onFetchError({ err, uri }) {
lexiconResolverLogger.error(
{ uri: uri.toString(), err },
'Lexicon fetch error',
)
},
},
}),
idResolver: idResolver,
metadata: {
protected_resources: [new URL(cfg.oauth.issuer).origin],
},
// If the PDS is both an authorization server & resource server (no
// entryway), we can afford to check the token validity on every
// request. This allows revoked tokens to be rejected immediately.
// This also allows JWT to be shorter since some claims (notably the
// "scope" claim) do not need to be included in the token.
accessTokenMode: AccessTokenMode.stateful,
getClientInfo(clientId) {
return {
isTrusted: cfg.oauth.provider?.trustedClients?.includes(clientId),
}
},
})
: undefined
const scopeRefGetter = entrywayAgent
? new ScopeReferenceGetter(entrywayAgent, redisScratch)
: undefined
const oauthVerifier: OAuthVerifier =
oauthProvider ?? // OAuthProvider extends OAuthVerifier
new OAuthVerifier({
issuer: cfg.oauth.issuer,
keyset: [await JoseKey.fromKeyLike(jwtPublicKey!, undefined, 'ES256K')],
dpopSecret: secrets.dpopSecret,
redis: redisScratch,
onDecodeToken: async ({ payload, dpopProof }) => {
// @TODO drop this once oauth provider no longer accepts DPoP proof with
// query or fragment in "htu" claim.
if (dpopProof?.htu.match(/[?#]/)) {
oauthLogger.info(
{ htu: dpopProof.htu, client_id: payload.client_id },
'DPoP proof "htu" contains query or fragment',
)
}
if (scopeRefGetter) {
payload.scope = await scopeRefGetter.dereference(payload.scope)
}
return payload
},
})
const authVerifier = new AuthVerifier(
accountManager,
idResolver,
oauthVerifier,
{
publicUrl: cfg.service.publicUrl,
jwtKey: jwtPublicKey ?? jwtSecretKey,
adminPass: secrets.adminPassword,
dids: {
pds: cfg.service.did,
entryway: cfg.entryway?.did,
modService: cfg.modService?.did,
},
},
)
return new AppContext({
actorStore,
blobstore,
localViewer,
mailer,
moderationMailer,
didCache,
idResolver,
plcClient,
accountManager,
sequencer,
backgroundQueue,
redisScratch,
crawlers,
bskyAppView,
moderationAgent,
reportingAgent,
entrywayAgent,
entrywayAdminAgent,
proxyAgent,
safeFetch,
authVerifier,
oauthProvider,
plcRotationKey,
cfg,
...(overrides ?? {}),
})
}
async appviewAuthHeaders(did: string, lxm: string) {
assert(this.bskyAppView)
return this.serviceAuthHeaders(did, this.bskyAppView.did, lxm)
}
async entrywayAuthHeaders(req: express.Request, did: string, lxm: string) {
assert(this.cfg.entryway)
const headers = await this.serviceAuthHeaders(
did,
this.cfg.entryway.did,
lxm,
)
return forwardedFor(req, headers)
}
entrywayPassthruHeaders(req: express.Request) {
return forwardedFor(req, authPassthru(req))
}
async serviceAuthHeaders(did: string, aud: string, lxm: string) {
const keypair = await this.actorStore.keypair(did)
return createServiceAuthHeaders({
iss: did,
aud,
lxm,
keypair,
})
}
async serviceAuthJwt(did: string, aud: string, lxm: string) {
const keypair = await this.actorStore.keypair(did)
return createServiceJwt({
iss: did,
aud,
lxm,
keypair,
})
}
}
const basicAuthHeader = (username: string, password: string) => {
const encoded = ui8.toString(
ui8.fromString(`${username}:${password}`, 'utf8'),
'base64pad',
)
return `Basic ${encoded}`
}
export default AppContext