@crombolo/server 0.62.0 → 0.64.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.
- package/package.json +14 -14
- package/src/access.ts +5 -5
- package/src/adapters/content.ts +79 -12
- package/src/adapters/documents.ts +6 -2
- package/src/adapters/file-system.ts +27 -3
- package/src/adapters/schema-files.ts +19 -27
- package/src/adapters/template-files.ts +10 -0
- package/src/adapters/users.ts +55 -84
- package/src/approvals.ts +2 -2
- package/src/bundle-admin.ts +21 -1
- package/src/bundle-api.ts +1 -3
- package/src/bundle-install.ts +28 -5
- package/src/bundle-sections.ts +10 -0
- package/src/config.ts +32 -1
- package/src/content-notifications.ts +6 -3
- package/src/content-operations.ts +4 -3
- package/src/content-query.ts +2 -4
- package/src/core.ts +11 -3
- package/src/email-notice.ts +2 -2
- package/src/email-settings.ts +394 -0
- package/src/email.ts +9 -4
- package/src/form-workflows.ts +3 -3
- package/src/grant-policy.ts +23 -3
- package/src/grant-trust.ts +73 -0
- package/src/index.ts +3 -0
- package/src/marketplace.ts +14 -6
- package/src/ports.ts +37 -18
- package/src/provenance.ts +13 -1
- package/src/remote.ts +23 -8
- package/src/schema-transfer.ts +472 -0
- package/src/seed/values.ts +126 -85
- package/src/server.ts +116 -11
- package/src/settings.ts +9 -0
- package/src/site-public.ts +2 -1
- package/src/site-trust.ts +16 -0
- package/src/status.ts +3 -0
- package/src/telemetry.ts +9 -2
- package/src/transfer-dashboard.ts +3 -0
- package/src/zip.ts +86 -3
package/src/email-notice.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* The alternative is an operator finding out when an invitation never arrives,
|
|
5
5
|
* which is the worst moment and the hardest to diagnose: nothing errored.
|
|
6
6
|
*/
|
|
7
|
-
import type
|
|
7
|
+
import { EMAIL_SETUP_HINT, type EmailAvailability } from './email.ts'
|
|
8
8
|
|
|
9
9
|
export interface EmailNoticeLog {
|
|
10
10
|
info(message: string, properties?: Record<string, unknown>): void
|
|
@@ -28,7 +28,7 @@ export function noticeAboutEmail(
|
|
|
28
28
|
const message = 'No e-mail provider is configured, so {affected} unavailable. {hint}'
|
|
29
29
|
const properties = {
|
|
30
30
|
affected: `${availability.affects.join(', ')} ${availability.affects.length === 1 ? 'is' : 'are'}`,
|
|
31
|
-
hint:
|
|
31
|
+
hint: `Set one up in Settings, under E-mail, or: ${EMAIL_SETUP_HINT}`,
|
|
32
32
|
}
|
|
33
33
|
// In development this is the expected state and not worth a warning.
|
|
34
34
|
if (options.development) log.info(message, properties)
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E-mail set up in Settings (story 340): kept in `key_value`, its key sealed by
|
|
3
|
+
* `CROMBOLO_SECRET_KEY`, and used only when the site's configuration sets no
|
|
4
|
+
* e-mail. The configuration wins, because whoever sets e-mail decides where
|
|
5
|
+
* password-reset links go. docs/configuration.md, "E-mail".
|
|
6
|
+
*/
|
|
7
|
+
import { isValidEmail } from '@crombolo/core'
|
|
8
|
+
import { type Db, now } from '@crombolo/data'
|
|
9
|
+
import { recordAudit } from './adapters/audit.ts'
|
|
10
|
+
import {
|
|
11
|
+
type EmailAvailability,
|
|
12
|
+
type EmailMessage,
|
|
13
|
+
type EmailPort,
|
|
14
|
+
emailAvailability,
|
|
15
|
+
emailFromEnvironment,
|
|
16
|
+
logEmail,
|
|
17
|
+
resolveEmailPort,
|
|
18
|
+
} from './email.ts'
|
|
19
|
+
import { logger } from './logging.ts'
|
|
20
|
+
import { openSecret, sealSecret, usableSecretKey } from './secrets.ts'
|
|
21
|
+
|
|
22
|
+
const KEY = 'crombolo:email'
|
|
23
|
+
/** How stale another node's copy may be before it reads again. */
|
|
24
|
+
export const EMAIL_SETTINGS_TTL_MS = 30_000
|
|
25
|
+
|
|
26
|
+
export const STORED_PROVIDERS = ['resend', 'postmark', 'ses', 'custom'] as const
|
|
27
|
+
export type StoredProvider = (typeof STORED_PROVIDERS)[number]
|
|
28
|
+
|
|
29
|
+
interface Stored {
|
|
30
|
+
provider: StoredProvider
|
|
31
|
+
from: string
|
|
32
|
+
fromName?: string
|
|
33
|
+
url?: string
|
|
34
|
+
postmarkStream?: string
|
|
35
|
+
sesRegion?: string
|
|
36
|
+
sesAccessKeyId?: string
|
|
37
|
+
/** The API key, or SES's secret access key. */
|
|
38
|
+
keySealed?: string
|
|
39
|
+
/** Where the key was entered for; it goes nowhere else (as story 305's client secret). */
|
|
40
|
+
keyFor?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** What an administrator sends; `key` is set only when a new one is entered. */
|
|
44
|
+
export type EmailSettingsInput = Partial<Omit<Stored, 'keySealed' | 'keyFor'>> & { key?: string }
|
|
45
|
+
|
|
46
|
+
interface Site {
|
|
47
|
+
email?: EmailPort | null
|
|
48
|
+
development?: boolean
|
|
49
|
+
secretKey?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Env = Record<string, string | undefined>
|
|
53
|
+
|
|
54
|
+
/** `config` when the site's configuration sets e-mail, `off` when it turns it off. */
|
|
55
|
+
export function configuredEmail(config: Site, env: Env = Bun.env): 'config' | 'off' | undefined {
|
|
56
|
+
if (config.email === null) return 'off'
|
|
57
|
+
if (config.email) return 'config'
|
|
58
|
+
const kind = (env.CROMBOLO_EMAIL_PROVIDER ?? '').trim().toLowerCase()
|
|
59
|
+
if (kind === 'none') return 'off'
|
|
60
|
+
return kind ? 'config' : undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const targetOf = (s: Pick<Stored, 'provider' | 'url' | 'sesAccessKeyId'>) =>
|
|
64
|
+
[s.provider, s.url ?? '', s.sesAccessKeyId ?? ''].join('|')
|
|
65
|
+
|
|
66
|
+
async function read(db: Db): Promise<Stored | undefined> {
|
|
67
|
+
const row = await db.q
|
|
68
|
+
.selectFrom('key_value')
|
|
69
|
+
.select('value')
|
|
70
|
+
.where('key', '=', KEY)
|
|
71
|
+
.executeTakeFirst()
|
|
72
|
+
try {
|
|
73
|
+
return row?.value ? (JSON.parse(row.value) as Stored) : undefined
|
|
74
|
+
} catch {
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function write(db: Db, stored: Stored | undefined): Promise<void> {
|
|
80
|
+
if (!stored) {
|
|
81
|
+
await db.q.deleteFrom('key_value').where('key', '=', KEY).execute()
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
const value = JSON.stringify(stored)
|
|
85
|
+
const updated = await db.q
|
|
86
|
+
.updateTable('key_value')
|
|
87
|
+
.set({ value, update_date: now(db) })
|
|
88
|
+
.where('key', '=', KEY)
|
|
89
|
+
.returning('key')
|
|
90
|
+
.execute()
|
|
91
|
+
if (updated.length === 0)
|
|
92
|
+
await db.q
|
|
93
|
+
.insertInto('key_value')
|
|
94
|
+
.values({ key: KEY, value, update_date: now(db) })
|
|
95
|
+
.execute()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function keyOf(stored: Stored, secretKey: string | undefined): string {
|
|
99
|
+
const key = usableSecretKey(secretKey)
|
|
100
|
+
if (!stored.keySealed || !key || stored.keyFor !== targetOf(stored)) return ''
|
|
101
|
+
try {
|
|
102
|
+
return openSecret(stored.keySealed, key)
|
|
103
|
+
} catch {
|
|
104
|
+
return ''
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The adapter stored settings describe, through the same checks as the environment's. */
|
|
109
|
+
function portOf(
|
|
110
|
+
stored: Stored | undefined,
|
|
111
|
+
secretKey: string | undefined,
|
|
112
|
+
log: { warning(message: string, properties?: Record<string, unknown>): void } = logger('email'),
|
|
113
|
+
): EmailPort | undefined {
|
|
114
|
+
if (!stored) return undefined
|
|
115
|
+
const key = keyOf(stored, secretKey)
|
|
116
|
+
return emailFromEnvironment(
|
|
117
|
+
{
|
|
118
|
+
CROMBOLO_EMAIL_PROVIDER: stored.provider,
|
|
119
|
+
CROMBOLO_EMAIL_FROM: stored.from,
|
|
120
|
+
CROMBOLO_EMAIL_FROM_NAME: stored.fromName,
|
|
121
|
+
CROMBOLO_EMAIL_URL: stored.url,
|
|
122
|
+
CROMBOLO_EMAIL_POSTMARK_STREAM: stored.postmarkStream,
|
|
123
|
+
CROMBOLO_EMAIL_SES_REGION: stored.sesRegion,
|
|
124
|
+
CROMBOLO_EMAIL_SES_ACCESS_KEY_ID: stored.sesAccessKeyId,
|
|
125
|
+
...(stored.provider === 'ses'
|
|
126
|
+
? { CROMBOLO_EMAIL_SES_SECRET_ACCESS_KEY: key }
|
|
127
|
+
: { CROMBOLO_EMAIL_API_KEY: key }),
|
|
128
|
+
},
|
|
129
|
+
log,
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The site's e-mail, read again from Settings as it changes. */
|
|
134
|
+
export interface LiveEmail extends EmailPort {
|
|
135
|
+
/** Reads the settings now, on the node that saved them. */
|
|
136
|
+
refresh(): Promise<void>
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The port a site sends through. The configuration's when it sets e-mail;
|
|
141
|
+
* otherwise one that follows Settings, re-read when older than the TTL, so a
|
|
142
|
+
* change reaches every node without a restart. With nothing set up it reports
|
|
143
|
+
* `none`, or is the console in development.
|
|
144
|
+
*/
|
|
145
|
+
export async function siteEmail(
|
|
146
|
+
db: Db,
|
|
147
|
+
config: Site,
|
|
148
|
+
options: { env?: Env; ttlMs?: number } = {},
|
|
149
|
+
): Promise<EmailPort | undefined> {
|
|
150
|
+
const env = options.env ?? Bun.env
|
|
151
|
+
if (configuredEmail(config, env)) return resolveEmailPort(config, env)
|
|
152
|
+
const ttl = options.ttlMs ?? EMAIL_SETTINGS_TTL_MS
|
|
153
|
+
const fallback = config.development ? logEmail() : undefined
|
|
154
|
+
let current: EmailPort | undefined
|
|
155
|
+
let readAt = 0
|
|
156
|
+
let reading: Promise<void> | undefined
|
|
157
|
+
const load = () => {
|
|
158
|
+
reading ??= read(db)
|
|
159
|
+
.then((stored) => {
|
|
160
|
+
current = portOf(stored, config.secretKey) ?? fallback
|
|
161
|
+
readAt = Date.now()
|
|
162
|
+
})
|
|
163
|
+
.catch(() => {})
|
|
164
|
+
.finally(() => {
|
|
165
|
+
reading = undefined
|
|
166
|
+
})
|
|
167
|
+
return reading
|
|
168
|
+
}
|
|
169
|
+
// A stale copy answers at once while the next read is under way.
|
|
170
|
+
const fresh = () => {
|
|
171
|
+
if (Date.now() - readAt >= ttl) void load()
|
|
172
|
+
return current
|
|
173
|
+
}
|
|
174
|
+
await load()
|
|
175
|
+
const live: LiveEmail = {
|
|
176
|
+
get description() {
|
|
177
|
+
return fresh()?.description ?? 'not set up'
|
|
178
|
+
},
|
|
179
|
+
get provider() {
|
|
180
|
+
return fresh()?.provider ?? 'none'
|
|
181
|
+
},
|
|
182
|
+
async send(message: EmailMessage) {
|
|
183
|
+
if (Date.now() - readAt >= ttl) await load()
|
|
184
|
+
return current
|
|
185
|
+
? current.send(message)
|
|
186
|
+
: { ok: false as const, error: 'This site has no e-mail set up.' }
|
|
187
|
+
},
|
|
188
|
+
refresh: load,
|
|
189
|
+
}
|
|
190
|
+
return live
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** What the card shows: never a key, only whether one is kept. */
|
|
194
|
+
export interface EmailSettingsView {
|
|
195
|
+
/** `config`/`off`: set by the site's configuration, read-only here. */
|
|
196
|
+
source: 'config' | 'off' | 'settings' | 'none'
|
|
197
|
+
provider: string | null
|
|
198
|
+
from: string | null
|
|
199
|
+
fromName: string | null
|
|
200
|
+
url: string | null
|
|
201
|
+
postmarkStream: string | null
|
|
202
|
+
sesRegion: string | null
|
|
203
|
+
sesAccessKeyId: string | null
|
|
204
|
+
keyKept: boolean
|
|
205
|
+
/** Whether a key can be kept at all: the site has a secret key to seal it with. */
|
|
206
|
+
canKeepKeys: boolean
|
|
207
|
+
status: EmailAvailability
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export interface EmailActor {
|
|
211
|
+
id: string | null
|
|
212
|
+
name: string
|
|
213
|
+
email: string | null
|
|
214
|
+
ipAddress?: string | null
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export type EmailOutcome = { ok: true } | { ok: false; status: number; message: string }
|
|
218
|
+
|
|
219
|
+
const text = (value: unknown) => (typeof value === 'string' ? value.trim() : '')
|
|
220
|
+
|
|
221
|
+
/** Settings → E-mail, as an administrator uses it. */
|
|
222
|
+
export function createEmailSettings(
|
|
223
|
+
db: Db,
|
|
224
|
+
config: Site,
|
|
225
|
+
options: { port: () => EmailPort | undefined; env?: Env },
|
|
226
|
+
) {
|
|
227
|
+
const env = options.env ?? Bun.env
|
|
228
|
+
const audit = (actor: EmailActor, action: string, detail: string) =>
|
|
229
|
+
recordAudit(db, {
|
|
230
|
+
category: 'settings',
|
|
231
|
+
action,
|
|
232
|
+
entityType: 'E-mail',
|
|
233
|
+
entityName: 'E-mail',
|
|
234
|
+
detail,
|
|
235
|
+
source: 'request',
|
|
236
|
+
userKey: actor.id,
|
|
237
|
+
userName: actor.name,
|
|
238
|
+
ipAddress: actor.ipAddress ?? null,
|
|
239
|
+
forwardedFor: null,
|
|
240
|
+
})
|
|
241
|
+
const refresh = async () => {
|
|
242
|
+
const port = options.port() as Partial<LiveEmail> | undefined
|
|
243
|
+
await port?.refresh?.()
|
|
244
|
+
}
|
|
245
|
+
const locked = (): EmailOutcome | undefined =>
|
|
246
|
+
configuredEmail(config, env)
|
|
247
|
+
? {
|
|
248
|
+
ok: false,
|
|
249
|
+
status: 409,
|
|
250
|
+
message: "E-mail is set by the site's configuration, so it can't be changed here.",
|
|
251
|
+
}
|
|
252
|
+
: undefined
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
async view(): Promise<EmailSettingsView> {
|
|
256
|
+
const status = emailAvailability(options.port())
|
|
257
|
+
const canKeepKeys = Boolean(usableSecretKey(config.secretKey))
|
|
258
|
+
const fixed = configuredEmail(config, env)
|
|
259
|
+
if (fixed) {
|
|
260
|
+
const fromEnv = !config.email && fixed === 'config'
|
|
261
|
+
return {
|
|
262
|
+
source: fixed,
|
|
263
|
+
provider:
|
|
264
|
+
fixed === 'off' ? null : (config.email?.provider ?? text(env.CROMBOLO_EMAIL_PROVIDER)),
|
|
265
|
+
from: fromEnv ? text(env.CROMBOLO_EMAIL_FROM) || null : null,
|
|
266
|
+
fromName: fromEnv ? text(env.CROMBOLO_EMAIL_FROM_NAME) || null : null,
|
|
267
|
+
url: fromEnv ? text(env.CROMBOLO_EMAIL_URL) || null : null,
|
|
268
|
+
postmarkStream: null,
|
|
269
|
+
sesRegion: fromEnv ? text(env.CROMBOLO_EMAIL_SES_REGION) || null : null,
|
|
270
|
+
sesAccessKeyId: null,
|
|
271
|
+
keyKept: false,
|
|
272
|
+
canKeepKeys,
|
|
273
|
+
status,
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const stored = await read(db)
|
|
277
|
+
return {
|
|
278
|
+
source: stored ? 'settings' : 'none',
|
|
279
|
+
provider: stored?.provider ?? null,
|
|
280
|
+
from: stored?.from ?? null,
|
|
281
|
+
fromName: stored?.fromName ?? null,
|
|
282
|
+
url: stored?.url ?? null,
|
|
283
|
+
postmarkStream: stored?.postmarkStream ?? null,
|
|
284
|
+
sesRegion: stored?.sesRegion ?? null,
|
|
285
|
+
sesAccessKeyId: stored?.sesAccessKeyId ?? null,
|
|
286
|
+
keyKept: Boolean(stored && keyOf(stored, config.secretKey)),
|
|
287
|
+
canKeepKeys,
|
|
288
|
+
status,
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
async save(actor: EmailActor, input: EmailSettingsInput): Promise<EmailOutcome> {
|
|
293
|
+
const refused = locked()
|
|
294
|
+
if (refused) return refused
|
|
295
|
+
const provider = text(input.provider) as StoredProvider
|
|
296
|
+
if (!STORED_PROVIDERS.includes(provider))
|
|
297
|
+
return { ok: false, status: 400, message: 'Choose a provider.' }
|
|
298
|
+
const from = text(input.from)
|
|
299
|
+
if (!isValidEmail(from))
|
|
300
|
+
return { ok: false, status: 400, message: 'Enter the address e-mail is sent from.' }
|
|
301
|
+
const next: Stored = {
|
|
302
|
+
provider,
|
|
303
|
+
from,
|
|
304
|
+
...(text(input.fromName) ? { fromName: text(input.fromName) } : {}),
|
|
305
|
+
}
|
|
306
|
+
if (provider === 'custom' || text(input.url)) {
|
|
307
|
+
const url = text(input.url)
|
|
308
|
+
if (provider === 'custom' && !url)
|
|
309
|
+
return { ok: false, status: 400, message: 'Enter the address messages are sent to.' }
|
|
310
|
+
if (url) next.url = url
|
|
311
|
+
}
|
|
312
|
+
if (provider === 'postmark' && text(input.postmarkStream))
|
|
313
|
+
next.postmarkStream = text(input.postmarkStream)
|
|
314
|
+
if (provider === 'ses') {
|
|
315
|
+
next.sesRegion = text(input.sesRegion)
|
|
316
|
+
next.sesAccessKeyId = text(input.sesAccessKeyId)
|
|
317
|
+
if (!next.sesRegion)
|
|
318
|
+
return { ok: false, status: 400, message: 'Enter the Amazon SES region.' }
|
|
319
|
+
if (!next.sesAccessKeyId)
|
|
320
|
+
return { ok: false, status: 400, message: 'Enter the access key ID.' }
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const key = text(input.key)
|
|
324
|
+
const secretKey = usableSecretKey(config.secretKey)
|
|
325
|
+
if (key) {
|
|
326
|
+
if (!secretKey)
|
|
327
|
+
return {
|
|
328
|
+
ok: false,
|
|
329
|
+
status: 400,
|
|
330
|
+
message:
|
|
331
|
+
"This site can't keep e-mail keys until whoever hosts it gives it a secret key.",
|
|
332
|
+
}
|
|
333
|
+
next.keySealed = sealSecret(key, secretKey)
|
|
334
|
+
next.keyFor = targetOf(next)
|
|
335
|
+
} else {
|
|
336
|
+
const before = await read(db)
|
|
337
|
+
// A kept key stays only while it is for the same provider and address.
|
|
338
|
+
if (before?.keySealed && before.keyFor === targetOf(next)) {
|
|
339
|
+
next.keySealed = before.keySealed
|
|
340
|
+
next.keyFor = before.keyFor
|
|
341
|
+
} else if (provider !== 'custom')
|
|
342
|
+
return {
|
|
343
|
+
ok: false,
|
|
344
|
+
status: 400,
|
|
345
|
+
message: before?.keySealed
|
|
346
|
+
? 'Enter the key again: a key is only used with the provider and address it was entered for.'
|
|
347
|
+
: 'Enter the key from your provider.',
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const problems: string[] = []
|
|
352
|
+
const built = portOf(next, config.secretKey, {
|
|
353
|
+
warning: (_message, properties) => problems.push(String(properties?.detail ?? '')),
|
|
354
|
+
})
|
|
355
|
+
if (!built)
|
|
356
|
+
return {
|
|
357
|
+
ok: false,
|
|
358
|
+
status: 400,
|
|
359
|
+
message: `Those settings can't be used. ${problems.filter(Boolean).join(' ')}`.trim(),
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
await write(db, next)
|
|
363
|
+
await audit(actor, 'Save', `${provider}, from ${from}${key ? ', key changed' : ''}`)
|
|
364
|
+
await refresh()
|
|
365
|
+
return { ok: true }
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
async clear(actor: EmailActor): Promise<EmailOutcome> {
|
|
369
|
+
const refused = locked()
|
|
370
|
+
if (refused) return refused
|
|
371
|
+
await write(db, undefined)
|
|
372
|
+
await audit(actor, 'Clear', 'e-mail settings removed')
|
|
373
|
+
await refresh()
|
|
374
|
+
return { ok: true }
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
/** A message to the administrator, through whatever is in force now. */
|
|
378
|
+
async test(actor: EmailActor): Promise<EmailOutcome> {
|
|
379
|
+
if (!actor.email || !isValidEmail(actor.email))
|
|
380
|
+
return { ok: false, status: 400, message: 'Your account has no e-mail address to send to.' }
|
|
381
|
+
const port = options.port()
|
|
382
|
+
if (!port || port.provider === 'none')
|
|
383
|
+
return { ok: false, status: 409, message: 'Set up e-mail first.' }
|
|
384
|
+
const result = await port.send({
|
|
385
|
+
to: [{ email: actor.email, name: actor.name }],
|
|
386
|
+
subject: 'A test e-mail from your site',
|
|
387
|
+
text: 'This message shows that your site can send e-mail.',
|
|
388
|
+
})
|
|
389
|
+
return result.ok
|
|
390
|
+
? { ok: true }
|
|
391
|
+
: { ok: false, status: 502, message: `The test e-mail was not sent: ${result.error}` }
|
|
392
|
+
},
|
|
393
|
+
}
|
|
394
|
+
}
|
package/src/email.ts
CHANGED
|
@@ -52,7 +52,8 @@ export interface EmailMessage {
|
|
|
52
52
|
export type EmailResult = { ok: true; id: string | null } | { ok: false; error: string }
|
|
53
53
|
|
|
54
54
|
/** Which adapter is in play; named in logs and in the availability report. */
|
|
55
|
-
|
|
55
|
+
/** `none`: a port set up in Settings (story 340) that has nothing set up yet. */
|
|
56
|
+
export type EmailProvider = 'resend' | 'postmark' | 'ses' | 'custom' | 'log' | 'none'
|
|
56
57
|
|
|
57
58
|
export interface EmailPort {
|
|
58
59
|
/** Named in the boot banner and in logs: `resend`, `postmark (broadcast)`, … */
|
|
@@ -224,13 +225,17 @@ export const EMAIL_DEPENDENT_FEATURES = [
|
|
|
224
225
|
export const EMAIL_SETUP_HINT =
|
|
225
226
|
'Set CROMBOLO_EMAIL_PROVIDER to resend, postmark or custom with its key and CROMBOLO_EMAIL_FROM, or pass `email` in crombolo.config.ts. See docs/configuration.md.'
|
|
226
227
|
|
|
228
|
+
/** Whether there is a port that would take a message, the console's included. */
|
|
229
|
+
export const emailReady = (port: EmailPort | undefined): port is EmailPort =>
|
|
230
|
+
port !== undefined && port.provider !== 'none'
|
|
231
|
+
|
|
227
232
|
export function emailAvailability(port: EmailPort | undefined): EmailAvailability {
|
|
228
|
-
if (!port)
|
|
233
|
+
if (!emailReady(port))
|
|
229
234
|
return {
|
|
230
235
|
available: false,
|
|
231
236
|
provider: null,
|
|
232
237
|
description: null,
|
|
233
|
-
reason:
|
|
238
|
+
reason: 'No e-mail provider is configured. Set one up in Settings, under E-mail.',
|
|
234
239
|
affects: [...EMAIL_DEPENDENT_FEATURES],
|
|
235
240
|
}
|
|
236
241
|
if (port.provider === 'log')
|
|
@@ -239,7 +244,7 @@ export function emailAvailability(port: EmailPort | undefined): EmailAvailabilit
|
|
|
239
244
|
provider: 'log',
|
|
240
245
|
description: port.description,
|
|
241
246
|
reason:
|
|
242
|
-
|
|
247
|
+
"E-mail is written to the server's log in development, not sent. Set up a provider before relying on it.",
|
|
243
248
|
affects: [],
|
|
244
249
|
}
|
|
245
250
|
return {
|
package/src/form-workflows.ts
CHANGED
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
type NodeSchemaState,
|
|
30
30
|
} from '@crombolo/data'
|
|
31
31
|
import type { EmailField, EmailProps, EmailRenderResult } from '@crombolo/render'
|
|
32
|
-
import type
|
|
32
|
+
import { type EmailAttachment, type EmailPort, emailReady } from './email.ts'
|
|
33
33
|
import { htmlToText } from './email-text.ts'
|
|
34
34
|
import type { FormWorkflowTypeDefinition } from './form-types.ts'
|
|
35
35
|
import { logger } from './logging.ts'
|
|
@@ -234,7 +234,7 @@ async function sendEmail(
|
|
|
234
234
|
context: WorkflowContext,
|
|
235
235
|
deps: WorkflowDeps,
|
|
236
236
|
): Promise<WorkflowResult> {
|
|
237
|
-
if (!deps.email)
|
|
237
|
+
if (!emailReady(deps.email))
|
|
238
238
|
return {
|
|
239
239
|
ok: false,
|
|
240
240
|
// Retrying cannot help: this is configuration, not weather.
|
|
@@ -638,7 +638,7 @@ async function runBundleWorkflow(
|
|
|
638
638
|
attempt: context.attempt ?? 1,
|
|
639
639
|
fetch: (input, init) => (deps.fetch ?? fetch)(input, { signal, ...init }),
|
|
640
640
|
sendEmail: async (message) =>
|
|
641
|
-
deps.email
|
|
641
|
+
emailReady(deps.email)
|
|
642
642
|
? deps.email.send(message)
|
|
643
643
|
: { ok: false, error: 'This site has no e-mail set up.' },
|
|
644
644
|
log: {
|
package/src/grant-policy.ts
CHANGED
|
@@ -16,9 +16,26 @@ export type UnrestrictedFrom = 'backoffice' | 'config'
|
|
|
16
16
|
export const unrestrictedFrom = (value: string | undefined): UnrestrictedFrom =>
|
|
17
17
|
value?.trim().toLowerCase() === 'config' ? 'config' : 'backoffice'
|
|
18
18
|
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* A package someone has vouched for: signed by a signer this site trusts (story
|
|
21
|
+
* 335), vouched for by the host, or built here.
|
|
22
|
+
*/
|
|
20
23
|
export const vouchedFor = (provenance: ProvenanceResult | undefined): boolean =>
|
|
21
|
-
provenance?.state === 'signed'
|
|
24
|
+
(provenance?.state === 'signed' && provenance.trusted === true) ||
|
|
25
|
+
provenance?.state === 'vouched' ||
|
|
26
|
+
provenance?.state === 'local'
|
|
27
|
+
|
|
28
|
+
/** Who signed it, in words: a key by its name and fingerprint, or the repository that built it. */
|
|
29
|
+
export function signerWords(provenance: ProvenanceResult): string | undefined {
|
|
30
|
+
if (provenance.key)
|
|
31
|
+
return `the key ${provenance.key.comment ? `${provenance.key.comment} ` : ''}(${provenance.key.fingerprint})`
|
|
32
|
+
if (provenance.source) return `a build of github.com/${provenance.source.repository}`
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Why a signature that checks out still does not vouch: its signer is not one this site trusts. */
|
|
37
|
+
export const untrustedSigner = (provenance: ProvenanceResult): string =>
|
|
38
|
+
`It is signed by ${signerWords(provenance) ?? 'someone'}, which this site doesn't trust, so it can't be turned on here. An administrator can add the signer in Settings → Trusted signers.`
|
|
22
39
|
|
|
23
40
|
export const ADMINISTRATOR_GRANTS = 'An administrator can turn this on.'
|
|
24
41
|
export const NOBODY_VOUCHED =
|
|
@@ -52,6 +69,9 @@ export function grantRefusal(options: {
|
|
|
52
69
|
if (!options.grants) return undefined
|
|
53
70
|
if (options.level === 'unrestricted' && options.from === 'config') return CONFIG_GRANTS
|
|
54
71
|
if (!options.isAdmin) return ADMINISTRATOR_GRANTS
|
|
55
|
-
if (isUpperLevel(options.level) && !vouchedFor(options.provenance))
|
|
72
|
+
if (isUpperLevel(options.level) && !vouchedFor(options.provenance))
|
|
73
|
+
return options.provenance?.state === 'signed'
|
|
74
|
+
? untrustedSigner(options.provenance)
|
|
75
|
+
: NOBODY_VOUCHED
|
|
56
76
|
return undefined
|
|
57
77
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Story 335: an extension granted Acts as you or Unrestricted keeps that only
|
|
3
|
+
* while someone this site trusts vouches for it. Checked at every start, before
|
|
4
|
+
* extensions run, so an upgrade, or a signer taken off Trusted signers, switches
|
|
5
|
+
* off what no longer qualifies.
|
|
6
|
+
*/
|
|
7
|
+
import { type ExtensionRegistry, isUpperLevel, levelOf } from '@crombolo/backoffice-host/registry'
|
|
8
|
+
import type { Db } from '@crombolo/data'
|
|
9
|
+
import { recordAudit } from './adapters/audit.ts'
|
|
10
|
+
import { APPROVED_BY_CODE, type BundleGrants } from './bundle-grants.ts'
|
|
11
|
+
import type { CromboloConfig } from './config.ts'
|
|
12
|
+
import { signerWords, vouchedFor } from './grant-policy.ts'
|
|
13
|
+
import { logger } from './logging.ts'
|
|
14
|
+
import { createMarketplace } from './marketplace.ts'
|
|
15
|
+
import { signerTrusted } from './site-trust.ts'
|
|
16
|
+
|
|
17
|
+
/** Who switches them off, as the audit log records it. */
|
|
18
|
+
export const SWITCHED_OFF_BY = 'crombolo'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Withdraws each upper-level grant recorded here that nobody this site trusts
|
|
22
|
+
* vouches for, so it is granted again only by the rule; the ids withdrawn. A
|
|
23
|
+
* grant the site's code makes is the developer's, and kept.
|
|
24
|
+
*/
|
|
25
|
+
export async function switchOffUntrusted(
|
|
26
|
+
db: Db,
|
|
27
|
+
config: CromboloConfig,
|
|
28
|
+
extensions: ExtensionRegistry,
|
|
29
|
+
grants: Pick<BundleGrants, 'granted' | 'isOn' | 'state' | 'forget'>,
|
|
30
|
+
): Promise<string[]> {
|
|
31
|
+
const marketplace = createMarketplace({
|
|
32
|
+
registry: config.npmRegistry,
|
|
33
|
+
keyword: config.packageKeyword,
|
|
34
|
+
marketplaceUrl: config.marketplaceUrl,
|
|
35
|
+
componentsDir: config.componentsDir,
|
|
36
|
+
extensions,
|
|
37
|
+
siteDir: config.siteDir,
|
|
38
|
+
trustSigner: (provenance) => signerTrusted(db, config, provenance),
|
|
39
|
+
})
|
|
40
|
+
const packages = new Map(extensions.bundles().bundles.map((pkg) => [pkg.id, pkg]))
|
|
41
|
+
const switched: string[] = []
|
|
42
|
+
for (const item of await marketplace.installed()) {
|
|
43
|
+
const pkg = packages.get(item.id)
|
|
44
|
+
if (!pkg || !grants.isOn(item.id)) continue
|
|
45
|
+
if (grants.state(pkg).grant?.grantedBy === APPROVED_BY_CODE) continue
|
|
46
|
+
if (!isUpperLevel(levelOf(grants.granted(item.id)))) continue
|
|
47
|
+
// A signature that can't be checked right now is left alone, not judged.
|
|
48
|
+
if (item.provenance.state === 'unchecked' || vouchedFor(item.provenance)) continue
|
|
49
|
+
await grants.forget(pkg.id)
|
|
50
|
+
const signer = signerWords(item.provenance)
|
|
51
|
+
const why = signer
|
|
52
|
+
? `signed by ${signer}, which this site doesn't trust`
|
|
53
|
+
: 'nobody has vouched for who built it'
|
|
54
|
+
await recordAudit(db, {
|
|
55
|
+
category: 'system',
|
|
56
|
+
action: 'Disable',
|
|
57
|
+
entityType: 'Bundle',
|
|
58
|
+
entityName: `${pkg.packageName}@${pkg.version}`,
|
|
59
|
+
detail: `switched off at start: it acts as you or runs unrestricted, and is ${why}; it needs granting again`,
|
|
60
|
+
source: 'boot',
|
|
61
|
+
userKey: null,
|
|
62
|
+
userName: SWITCHED_OFF_BY,
|
|
63
|
+
ipAddress: null,
|
|
64
|
+
forwardedFor: null,
|
|
65
|
+
})
|
|
66
|
+
logger('server').warn(
|
|
67
|
+
'{extension} is switched off: it acts as you or runs unrestricted, and is {why}. Trust its signer in Settings → Trusted signers, then grant it again.',
|
|
68
|
+
{ extension: pkg.packageName, why },
|
|
69
|
+
)
|
|
70
|
+
switched.push(item.id)
|
|
71
|
+
}
|
|
72
|
+
return switched
|
|
73
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -61,6 +61,7 @@ export * from './email-notice.ts'
|
|
|
61
61
|
export * from './email-postmark.ts'
|
|
62
62
|
export * from './email-resend.ts'
|
|
63
63
|
export * from './email-ses.ts'
|
|
64
|
+
export * from './email-settings.ts'
|
|
64
65
|
export * from './email-text.ts'
|
|
65
66
|
export {
|
|
66
67
|
activityCsv,
|
|
@@ -95,6 +96,7 @@ export * from './git.ts'
|
|
|
95
96
|
export * from './git-github.ts'
|
|
96
97
|
export * from './git-routes.ts'
|
|
97
98
|
export * from './grant-policy.ts'
|
|
99
|
+
export * from './grant-trust.ts'
|
|
98
100
|
export * from './host-token.ts'
|
|
99
101
|
export * from './identity-provider-overrides.ts'
|
|
100
102
|
export * from './identity-providers.ts'
|
|
@@ -134,6 +136,7 @@ export * from './routes.ts'
|
|
|
134
136
|
export * from './schema.ts'
|
|
135
137
|
export * from './schema-picks.ts'
|
|
136
138
|
export * from './schema-store.ts'
|
|
139
|
+
export * from './schema-transfer.ts'
|
|
137
140
|
export * from './secrets.ts'
|
|
138
141
|
export * from './seed/index.ts'
|
|
139
142
|
export * from './seo.ts'
|
package/src/marketplace.ts
CHANGED
|
@@ -176,6 +176,8 @@ export interface MarketplaceOptions {
|
|
|
176
176
|
policy?: ProvenancePolicy
|
|
177
177
|
/** Checks a package directory's signature; `verifyProvenance`, unless a test stands in. */
|
|
178
178
|
verify?: (dir: string) => Promise<ProvenanceResult>
|
|
179
|
+
/** Whether a signature's signer is on this site's Trusted signers list (story 335). */
|
|
180
|
+
trustSigner?: (provenance: ProvenanceResult) => Promise<boolean>
|
|
179
181
|
}
|
|
180
182
|
|
|
181
183
|
export interface InstallOutcome {
|
|
@@ -336,7 +338,13 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
336
338
|
|
|
337
339
|
const declaresAt = (name: string, wanted?: string) =>
|
|
338
340
|
declaresAtRegistry(options.registry, name, wanted, call)
|
|
339
|
-
const
|
|
341
|
+
const verifyOnly = options.verify ?? ((dir: string) => verifyProvenance(dir))
|
|
342
|
+
/** A signature with whether its signer is trusted, asked fresh each time, since the list changes. */
|
|
343
|
+
const withTrust = async (result: ProvenanceResult): Promise<ProvenanceResult> =>
|
|
344
|
+
result.state === 'signed'
|
|
345
|
+
? { ...result, trusted: (await options.trustSigner?.(result)) ?? false }
|
|
346
|
+
: result
|
|
347
|
+
const verify = async (dir: string) => withTrust(await verifyOnly(dir))
|
|
340
348
|
const provenanceAt = (name: string, dist: Parameters<typeof provenanceAtRegistry>[1]) =>
|
|
341
349
|
provenanceAtRegistry(name, dist, { fetch: call, verify })
|
|
342
350
|
|
|
@@ -379,7 +387,7 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
379
387
|
const key = `${bundle.packageName}@${bundle.version}`
|
|
380
388
|
let found = installedProvenance.get(key)
|
|
381
389
|
if (!found) {
|
|
382
|
-
found =
|
|
390
|
+
found = verifyOnly(bundle.directory).catch((error: unknown) => ({
|
|
383
391
|
state: 'unchecked' as const,
|
|
384
392
|
reason: error instanceof Error ? error.message : String(error),
|
|
385
393
|
}))
|
|
@@ -388,7 +396,7 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
388
396
|
const result = await found
|
|
389
397
|
// Not kept: the next look may be able to check it.
|
|
390
398
|
if (result.state === 'unchecked') installedProvenance.delete(key)
|
|
391
|
-
return result
|
|
399
|
+
return withTrust(result)
|
|
392
400
|
}
|
|
393
401
|
|
|
394
402
|
/** What the registry says this package's latest version carries, when it is a valid bundle. */
|
|
@@ -618,11 +626,11 @@ export function createMarketplace(options: MarketplaceOptions): Marketplace {
|
|
|
618
626
|
const discard = () => {
|
|
619
627
|
if (kept.copied) rmSync(join(options.siteDir, kept.path), { force: true })
|
|
620
628
|
}
|
|
621
|
-
const provenance = await
|
|
622
|
-
(error: unknown) => ({
|
|
629
|
+
const provenance = await withTrust(
|
|
630
|
+
await verifyPackage(join(options.siteDir, kept.path)).catch((error: unknown) => ({
|
|
623
631
|
state: 'unchecked' as const,
|
|
624
632
|
reason: error instanceof Error ? error.message : String(error),
|
|
625
|
-
}),
|
|
633
|
+
})),
|
|
626
634
|
)
|
|
627
635
|
const refused =
|
|
628
636
|
refusedBySignature(declared.name, provenance) ??
|