Repository navigation
Expand file tree
/
Copy pathbrowser-quick-note.ts
More file actions
317 lines (315 loc) · 10.5 KB
/
Copy pathbrowser-quick-note.ts
File metadata and controls
317 lines (315 loc) · 10.5 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
import { randomBytes } from "node:crypto";
import { z } from "zod";
import type { DB, Schema } from "@db/index.js";
import type { Transaction } from "kysely";
import { transact } from "@db/transactions.js";
import { lockAIUser } from "@core/modules/ai/config.js";
import { authorize } from "@core/modules/access/queries.js";
import { authenticateBrowserConnection } from "@core/modules/browser-clipper/access.js";
import { createDocuments } from "@core/modules/collaboration/documents.js";
import type { EditorInput } from "@core/modules/collaboration/protocol.js";
import { preparePlainTextAppend } from "@core/modules/documents/append-plain-text.js";
import { AppError, fail } from "@core/shared/errors.js";
import { tokenHash } from "@core/modules/identity/passwords.js";
import { publicProfile, validateClipperTarget } from "./browser-clipper.js";
export const quickNoteInput = z
.object({
requestId: z.string().uuid(),
profileId: z.string().uuid(),
profileRevision: z.number().int().positive(),
text: z
.string()
.min(1)
.max(64 * 1024),
})
.strict()
.superRefine((value, ctx) => {
if (!value.text.trim() || Buffer.byteLength(value.text) > 64 * 1024)
ctx.addIssue({ code: "custom", message: "Invalid plain text" });
});
export const quickNoteCommit = z
.object({
requestId: z.string().uuid(),
ticket: z.string().regex(/^[a-f0-9]{64}$/),
})
.strict();
type NoteInput = z.infer<typeof quickNoteInput>;
type NoteTicket = {
ticket: string;
userId: string;
connectionId: string;
authorizationHash: string;
input: NoteInput;
targetId: string;
format: string;
update: EditorInput;
expiresAt: number;
};
/** No draft, grant or receipt format is introduced: tickets live only in this process. */
export function createBrowserQuickNote(
db: DB,
origin: string,
notify: (id: string) => Promise<unknown>,
options: {
now?: () => number;
lifetimeMs?: number;
maxTickets?: number;
} = {},
) {
const tickets = new Map<string, NoteTicket>();
const preparing = new Map<
string,
{ input: string; promise: Promise<ReturnType<typeof prepared>> }
>();
const now = options.now ?? Date.now;
const lifetime = options.lifetimeMs ?? 5 * 60_000;
const maxTickets = options.maxTickets ?? 1000;
const key = (userId: string, connectionId: string, requestId: string) =>
`${userId}/${connectionId}/${requestId}`;
function prune() {
for (const [id, ticket] of tickets)
if (ticket.expiresAt <= now()) tickets.delete(id);
}
const cleanup = setInterval(prune, 60_000);
cleanup.unref();
async function profileTarget(
tx: DB,
actor: Awaited<ReturnType<typeof authenticateBrowserConnection>>["actor"],
profileId: string,
revision: number,
) {
const profile = await tx
.selectFrom("browser_clipper_profiles")
.selectAll()
.where("id", "=", profileId)
.where("user_id", "=", actor.id)
.executeTakeFirst();
if (!profile || !profile.enabled || profile.revision !== revision)
fail(409, "clipper_profile_changed");
if (profile.strategy !== "append" || !profile.target_id)
fail(403, "quick_note_append_profile_required");
await validateClipperTarget(tx, actor, publicProfile(profile));
const { resource } = await authorize(tx, actor, profile.target_id, 3);
return { profile, resource };
}
async function locked<T>(
authorization: string | undefined,
work: (
tx: Transaction<Schema>,
auth: Awaited<ReturnType<typeof authenticateBrowserConnection>>,
) => Promise<T>,
) {
const candidate = await authenticateBrowserConnection(db, authorization);
return transact(db, async (tx) => {
await lockAIUser(tx, candidate.actor.id);
const current = await authenticateBrowserConnection(tx, authorization);
if (
current.actor.id !== candidate.actor.id ||
current.connection.id !== candidate.connection.id
)
fail(401, "clipper_login_required");
return work(tx, current);
});
}
function prepared(ticket: NoteTicket) {
return {
requestId: ticket.input.requestId,
ticket: ticket.ticket,
targetId: ticket.targetId,
format: ticket.format,
expiresAt: new Date(ticket.expiresAt).toISOString(),
};
}
return {
async targets(authorization: string | undefined) {
const { actor } = await authenticateBrowserConnection(db, authorization);
const profiles = await db
.selectFrom("browser_clipper_profiles")
.selectAll()
.where("user_id", "=", actor.id)
.where("enabled", "=", 1)
.where("strategy", "=", "append")
.orderBy("created_at")
.execute();
const items = [];
for (const p of profiles) {
try {
const { resource } = await profileTarget(db, actor, p.id, p.revision);
items.push({
profileId: p.id,
profileRevision: p.revision,
profileName: p.name,
isDefault: !!p.is_default,
targetId: resource.id,
title: resource.title,
format: resource.format,
});
} catch (error) {
if (
!(error instanceof AppError) ||
![400, 403, 404, 409].includes(error.status)
)
throw error;
}
}
return { items };
},
async prepare(authorization: string | undefined, input: NoteInput) {
const candidate = await authenticateBrowserConnection(db, authorization);
const prepareKey = key(
candidate.actor.id,
candidate.connection.id,
input.requestId,
);
const signature = JSON.stringify([
tokenHash(authorization!),
input.profileId,
input.profileRevision,
input.text,
]);
const inFlight = preparing.get(prepareKey);
if (inFlight) {
if (inFlight.input !== signature)
fail(409, "quick_note_request_conflict");
return inFlight.promise;
}
prune();
if (!tickets.has(prepareKey)) {
const reserved = [...preparing.keys()].filter((id) => !tickets.has(id));
const userPrefix = `${candidate.actor.id}/`;
if (
tickets.size + reserved.length >= maxTickets ||
[...tickets.keys(), ...reserved].filter((id) =>
id.startsWith(userPrefix),
).length >= 30
)
fail(429, "quick_note_busy");
}
const promise = (async () => {
const saved = await locked(
authorization,
async (tx, { actor, connection }) => {
const { resource } = await profileTarget(
tx,
actor,
input.profileId,
input.profileRevision,
);
prune();
const cacheKey = key(actor.id, connection.id, input.requestId);
const existing = tickets.get(cacheKey);
if (existing) {
if (existing.authorizationHash !== tokenHash(authorization!))
fail(409, "quick_note_signin_changed");
if (
JSON.stringify(existing.input) !== JSON.stringify(input) ||
existing.targetId !== resource.id
)
fail(409, "quick_note_request_conflict");
return existing;
}
// A missing/expired ticket must never regenerate a previously accepted request.
if (
await tx
.selectFrom(
resource.format === "markdown"
? "markdown_receipts"
: "editor_receipts",
)
.select("message_id")
.where("resource_id", "=", resource.id)
.where("message_id", "=", input.requestId)
.executeTakeFirst()
)
fail(409, "quick_note_request_already_used");
if (
tickets.size >= maxTickets ||
[...tickets.values()].filter((t) => t.userId === actor.id)
.length >= 30
)
fail(429, "quick_note_busy");
const ticket: NoteTicket = {
ticket: randomBytes(32).toString("hex"),
userId: actor.id,
connectionId: connection.id,
authorizationHash: tokenHash(authorization!),
input: { ...input },
targetId: resource.id,
format: resource.format,
update: await preparePlainTextAppend(
tx,
actor,
resource.id,
input.text,
input.requestId,
),
expiresAt: now() + lifetime,
};
return ticket;
},
);
// Publish only after the handshake transaction commits.
tickets.set(
key(saved.userId, saved.connectionId, saved.input.requestId),
saved,
);
return prepared(saved);
})();
preparing.set(prepareKey, { input: signature, promise });
try {
return await promise;
} finally {
preparing.delete(prepareKey);
}
},
async commit(
authorization: string | undefined,
input: z.infer<typeof quickNoteCommit>,
) {
const saved = await locked(
authorization,
async (tx, { actor, connection }) => {
prune();
const ticket = tickets.get(
key(actor.id, connection.id, input.requestId),
);
if (!ticket || ticket.ticket !== input.ticket)
fail(409, "quick_note_ticket_missing");
if (ticket.authorizationHash !== tokenHash(authorization!))
fail(409, "quick_note_signin_changed");
const { resource } = await profileTarget(
tx,
actor,
ticket.input.profileId,
ticket.input.profileRevision,
);
if (
resource.id !== ticket.targetId ||
resource.format !== ticket.format
)
fail(409, "clipper_profile_changed");
const result = await createDocuments(tx).exchange(
actor,
resource.id,
ticket.update,
);
return {
requestId: input.requestId,
targetId: resource.id,
url: `${origin}/#/r/${resource.id}`,
seq: result.seq,
epochId: result.epochId,
changed: result.changed,
};
},
);
await notify(saved.targetId).catch(() => {});
return saved;
},
close() {
clearInterval(cleanup);
tickets.clear();
preparing.clear();
},
};
}