@open-pencil/cloud 0.0.0-stage → 0.16.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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
3
|
+
|
|
4
|
+
export class StorageQuotaExceededError extends Error {
|
|
5
|
+
override readonly name = 'StorageQuotaExceededError'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type StorageQuotaSnapshot = {
|
|
9
|
+
committedBytes: number
|
|
10
|
+
reservedBytes: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type StorageDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
14
|
+
|
|
15
|
+
export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
16
|
+
async function snapshot(
|
|
17
|
+
executor: StorageDatabase,
|
|
18
|
+
workspaceId: string,
|
|
19
|
+
now = new Date()
|
|
20
|
+
): Promise<StorageQuotaSnapshot> {
|
|
21
|
+
const [usage, reservation] = await Promise.all([
|
|
22
|
+
executor
|
|
23
|
+
.selectFrom('workspaceStorageUsage')
|
|
24
|
+
.select('committedBytes')
|
|
25
|
+
.where('workspaceId', '=', workspaceId)
|
|
26
|
+
.executeTakeFirst(),
|
|
27
|
+
executor
|
|
28
|
+
.selectFrom('uploadStorageReservation')
|
|
29
|
+
.select((expression) =>
|
|
30
|
+
expression.fn.coalesce(expression.fn.sum('bytes'), expression.val(0)).as('reservedBytes')
|
|
31
|
+
)
|
|
32
|
+
.where('workspaceId', '=', workspaceId)
|
|
33
|
+
.where('committedAt', 'is', null)
|
|
34
|
+
.where('releasedAt', 'is', null)
|
|
35
|
+
.where('expiresAt', '>', now)
|
|
36
|
+
.executeTakeFirstOrThrow()
|
|
37
|
+
])
|
|
38
|
+
return {
|
|
39
|
+
committedBytes: Number(usage?.committedBytes ?? 0),
|
|
40
|
+
reservedBytes: Number(reservation.reservedBytes)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function reserve(
|
|
45
|
+
executor: StorageDatabase,
|
|
46
|
+
input: {
|
|
47
|
+
workspaceId: string
|
|
48
|
+
uploadId: string
|
|
49
|
+
bytes: number
|
|
50
|
+
expiresAt: Date
|
|
51
|
+
maximumBytes: number | null
|
|
52
|
+
}
|
|
53
|
+
): Promise<void> {
|
|
54
|
+
await executor
|
|
55
|
+
.insertInto('workspaceStorageUsage')
|
|
56
|
+
.values({ workspaceId: input.workspaceId, committedBytes: 0 })
|
|
57
|
+
.onConflict((conflict) => conflict.column('workspaceId').doNothing())
|
|
58
|
+
.execute()
|
|
59
|
+
await executor
|
|
60
|
+
.selectFrom('workspaceStorageUsage')
|
|
61
|
+
.select('workspaceId')
|
|
62
|
+
.where('workspaceId', '=', input.workspaceId)
|
|
63
|
+
.forUpdate()
|
|
64
|
+
.executeTakeFirstOrThrow()
|
|
65
|
+
const usage = await snapshot(executor, input.workspaceId)
|
|
66
|
+
if (
|
|
67
|
+
input.maximumBytes !== null &&
|
|
68
|
+
usage.committedBytes + usage.reservedBytes + input.bytes > input.maximumBytes
|
|
69
|
+
) {
|
|
70
|
+
throw new StorageQuotaExceededError('Workspace storage quota exceeded')
|
|
71
|
+
}
|
|
72
|
+
await executor
|
|
73
|
+
.insertInto('uploadStorageReservation')
|
|
74
|
+
.values({
|
|
75
|
+
id: crypto.randomUUID(),
|
|
76
|
+
workspaceId: input.workspaceId,
|
|
77
|
+
uploadId: input.uploadId,
|
|
78
|
+
bytes: input.bytes,
|
|
79
|
+
expiresAt: input.expiresAt,
|
|
80
|
+
committedAt: null,
|
|
81
|
+
releasedAt: null
|
|
82
|
+
})
|
|
83
|
+
.onConflict((conflict) => conflict.column('uploadId').doNothing())
|
|
84
|
+
.execute()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function commit(executor: StorageDatabase, uploadId: string): Promise<void> {
|
|
88
|
+
const reservation = await executor
|
|
89
|
+
.selectFrom('uploadStorageReservation')
|
|
90
|
+
.select(['id', 'workspaceId', 'bytes', 'committedAt', 'releasedAt'])
|
|
91
|
+
.where('uploadId', '=', uploadId)
|
|
92
|
+
.forUpdate()
|
|
93
|
+
.executeTakeFirst()
|
|
94
|
+
if (!reservation || reservation.committedAt || reservation.releasedAt) return
|
|
95
|
+
await executor
|
|
96
|
+
.updateTable('workspaceStorageUsage')
|
|
97
|
+
.set({
|
|
98
|
+
committedBytes: (expression) =>
|
|
99
|
+
expression('committedBytes', '+', Number(reservation.bytes)),
|
|
100
|
+
updatedAt: new Date()
|
|
101
|
+
})
|
|
102
|
+
.where('workspaceId', '=', reservation.workspaceId)
|
|
103
|
+
.execute()
|
|
104
|
+
await executor
|
|
105
|
+
.updateTable('uploadStorageReservation')
|
|
106
|
+
.set({ committedAt: new Date() })
|
|
107
|
+
.where('id', '=', reservation.id)
|
|
108
|
+
.execute()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function release(executor: StorageDatabase, uploadId: string): Promise<void> {
|
|
112
|
+
await executor
|
|
113
|
+
.updateTable('uploadStorageReservation')
|
|
114
|
+
.set({ releasedAt: new Date() })
|
|
115
|
+
.where('uploadId', '=', uploadId)
|
|
116
|
+
.where('committedAt', 'is', null)
|
|
117
|
+
.where('releasedAt', 'is', null)
|
|
118
|
+
.execute()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
snapshot: (workspaceId: string) => snapshot(database, workspaceId),
|
|
123
|
+
|
|
124
|
+
async reserve(input: {
|
|
125
|
+
workspaceId: string
|
|
126
|
+
uploadId: string
|
|
127
|
+
bytes: number
|
|
128
|
+
expiresAt: Date
|
|
129
|
+
maximumBytes: number | null
|
|
130
|
+
}): Promise<void> {
|
|
131
|
+
await database.transaction().execute((transaction) => reserve(transaction, input))
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
reserveInTransaction(
|
|
135
|
+
transaction: Transaction<CloudDatabase>,
|
|
136
|
+
input: {
|
|
137
|
+
workspaceId: string
|
|
138
|
+
uploadId: string
|
|
139
|
+
bytes: number
|
|
140
|
+
expiresAt: Date
|
|
141
|
+
maximumBytes: number | null
|
|
142
|
+
}
|
|
143
|
+
): Promise<void> {
|
|
144
|
+
return reserve(transaction, input)
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
async commit(uploadId: string): Promise<void> {
|
|
148
|
+
await database.transaction().execute((transaction) => commit(transaction, uploadId))
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
commitInTransaction(transaction: Transaction<CloudDatabase>, uploadId: string): Promise<void> {
|
|
152
|
+
return commit(transaction, uploadId)
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
async release(uploadId: string): Promise<void> {
|
|
156
|
+
await release(database, uploadId)
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
releaseInTransaction(transaction: Transaction<CloudDatabase>, uploadId: string): Promise<void> {
|
|
160
|
+
return release(transaction, uploadId)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export type StorageQuotaService = ReturnType<typeof createStorageQuotaService>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
|
|
4
|
+
export function createRateLimitCleanupService(database: Kysely<CloudDatabase>) {
|
|
5
|
+
return {
|
|
6
|
+
async cleanupExpired(before: Date): Promise<number> {
|
|
7
|
+
const result = await database
|
|
8
|
+
.deleteFrom('apiRateLimit')
|
|
9
|
+
.where('windowStartedAt', '<', before)
|
|
10
|
+
.executeTakeFirst()
|
|
11
|
+
return Number(result.numDeletedRows)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type RateLimitCleanupService = ReturnType<typeof createRateLimitCleanupService>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { createRateLimitCleanupService, type RateLimitCleanupService } from './cleanup'
|
|
2
|
+
export { rateLimitKey, trustedClientIP, type ClientIdentityOptions } from './keys'
|
|
3
|
+
export {
|
|
4
|
+
CLOUD_RATE_LIMITS,
|
|
5
|
+
createActorRateLimiter,
|
|
6
|
+
createCloudRateLimiter,
|
|
7
|
+
createRouteRateLimiter,
|
|
8
|
+
createTrustedIPRateLimiter,
|
|
9
|
+
routeHasOwnBudget,
|
|
10
|
+
type RateLimitPolicy
|
|
11
|
+
} from './middleware'
|
|
12
|
+
export { PostgresRateLimitStore } from './postgres'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
2
|
+
import type { Context } from 'hono'
|
|
3
|
+
|
|
4
|
+
export type ClientIdentityOptions = {
|
|
5
|
+
trustedHeaders: string[]
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The client address a trusted proxy reported. List-valued headers such as `X-Forwarded-For` carry
|
|
10
|
+
* whatever the client sent followed by what each proxy appended, so only the right-most entry,
|
|
11
|
+
* the one the trusted proxy wrote, identifies the client.
|
|
12
|
+
*/
|
|
13
|
+
export function trustedClientIP(context: Context, options: ClientIdentityOptions): string | null {
|
|
14
|
+
for (const header of options.trustedHeaders) {
|
|
15
|
+
const value = context.req
|
|
16
|
+
.header(header)
|
|
17
|
+
?.split(',')
|
|
18
|
+
.map((entry) => entry.trim())
|
|
19
|
+
.findLast((entry) => entry.length > 0)
|
|
20
|
+
if (value) return value
|
|
21
|
+
}
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const rateLimitKey = {
|
|
26
|
+
email(email: string): string {
|
|
27
|
+
return `email:${email.trim().toLowerCase()}`
|
|
28
|
+
},
|
|
29
|
+
ip(ip: string): string {
|
|
30
|
+
return `ip:${ip}`
|
|
31
|
+
},
|
|
32
|
+
actor(actor: CloudActor): string {
|
|
33
|
+
return `actor:${actor.userId}`
|
|
34
|
+
},
|
|
35
|
+
resource(resourceId: string, identity: string): string {
|
|
36
|
+
return `resource:${resourceId}:${identity}`
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { CloudAPIEnvironment } from '#cloud/server/api/types'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import type { Context, Env, Input } from 'hono'
|
|
4
|
+
import { rateLimiter, type RateLimitExceededEventHandler } from 'hono-rate-limiter'
|
|
5
|
+
import { matchedRoutes } from 'hono/route'
|
|
6
|
+
import type { Kysely } from 'kysely'
|
|
7
|
+
|
|
8
|
+
import { trustedClientIP, type ClientIdentityOptions } from './keys'
|
|
9
|
+
import { PostgresRateLimitStore } from './postgres'
|
|
10
|
+
|
|
11
|
+
export type RateLimitPolicy = {
|
|
12
|
+
namespace: string
|
|
13
|
+
windowMs: number
|
|
14
|
+
limit: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const CLOUD_RATE_LIMITS = {
|
|
18
|
+
publicCapability: { namespace: 'public-capability', windowMs: 60_000, limit: 30 },
|
|
19
|
+
collaborationTicket: { namespace: 'collaboration-ticket', windowMs: 60_000, limit: 60 },
|
|
20
|
+
invitationMutation: { namespace: 'invitation-mutation', windowMs: 60_000, limit: 30 },
|
|
21
|
+
workspaceCreation: { namespace: 'workspace-creation', windowMs: 60_000, limit: 30 },
|
|
22
|
+
documentCreation: { namespace: 'document-creation', windowMs: 60_000, limit: 60 },
|
|
23
|
+
uploadCreation: { namespace: 'upload-creation', windowMs: 60_000, limit: 120 },
|
|
24
|
+
authenticatedMutation: { namespace: 'authenticated-mutation', windowMs: 60_000, limit: 120 },
|
|
25
|
+
aiToken: { namespace: 'ai-token', windowMs: 60_000, limit: 30 },
|
|
26
|
+
adminRead: { namespace: 'admin-read', windowMs: 60_000, limit: 300 },
|
|
27
|
+
adminMutation: { namespace: 'admin-mutation', windowMs: 60_000, limit: 30 }
|
|
28
|
+
} as const satisfies Record<string, RateLimitPolicy>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The limiters routes mount for their own budget, each with which requests it counts. A general
|
|
32
|
+
* limiter leaves those requests to it rather than counting them twice; the routes themselves say
|
|
33
|
+
* which have one, so the general limiter never matches paths.
|
|
34
|
+
*/
|
|
35
|
+
const routeBudgets = new WeakMap<object, (context: Context) => boolean>()
|
|
36
|
+
|
|
37
|
+
/** Whether a limiter the request's route mounts for its own budget counts this request. */
|
|
38
|
+
export function routeHasOwnBudget(context: Context): boolean {
|
|
39
|
+
return matchedRoutes(context).some((route) => routeBudgets.get(route.handler)?.(context))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function ownBudget<Limiter extends object>(
|
|
43
|
+
limiter: Limiter,
|
|
44
|
+
skip?: (context: Context) => boolean
|
|
45
|
+
): Limiter {
|
|
46
|
+
routeBudgets.set(limiter, (context) => !skip?.(context))
|
|
47
|
+
return limiter
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type RateLimitOptions<E extends Env, P extends string, I extends Input> = {
|
|
51
|
+
database: Kysely<CloudDatabase>
|
|
52
|
+
secret: string
|
|
53
|
+
policy: RateLimitPolicy
|
|
54
|
+
keyGenerator(context: Context<E, P, I>): string
|
|
55
|
+
handler?: RateLimitExceededEventHandler<E, P, I>
|
|
56
|
+
skip?: (context: Context<E, P, I>) => boolean
|
|
57
|
+
standardHeaders?: boolean | 'draft-6' | 'draft-7'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function createCloudRateLimiter<
|
|
61
|
+
E extends Env = Env,
|
|
62
|
+
P extends string = string,
|
|
63
|
+
I extends Input = Input
|
|
64
|
+
>(options: RateLimitOptions<E, P, I>) {
|
|
65
|
+
return rateLimiter<E, P, I>({
|
|
66
|
+
windowMs: options.policy.windowMs,
|
|
67
|
+
limit: options.policy.limit,
|
|
68
|
+
standardHeaders: options.standardHeaders ?? 'draft-7',
|
|
69
|
+
store: new PostgresRateLimitStore(options.database, options.secret, options.policy.namespace),
|
|
70
|
+
keyGenerator: (context) => options.keyGenerator(context),
|
|
71
|
+
handler: options.handler
|
|
72
|
+
? (context, next, used) => options.handler?.(context, next, used)
|
|
73
|
+
: undefined,
|
|
74
|
+
skip: options.skip
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function createTrustedIPRateLimiter(
|
|
79
|
+
database: Kysely<CloudDatabase>,
|
|
80
|
+
secret: string,
|
|
81
|
+
policy: RateLimitPolicy,
|
|
82
|
+
client: ClientIdentityOptions,
|
|
83
|
+
handler?: RateLimitExceededEventHandler,
|
|
84
|
+
options?: {
|
|
85
|
+
resource?: (context: Context) => string
|
|
86
|
+
skip?: (context: Context) => boolean
|
|
87
|
+
/** The limiter is a route's own budget, which general limiters leave to it. */
|
|
88
|
+
route?: boolean
|
|
89
|
+
}
|
|
90
|
+
) {
|
|
91
|
+
const limiter = createCloudRateLimiter({
|
|
92
|
+
database,
|
|
93
|
+
secret,
|
|
94
|
+
policy,
|
|
95
|
+
keyGenerator: (context) => {
|
|
96
|
+
const ip = trustedClientIP(context, client) ?? 'unavailable'
|
|
97
|
+
return options?.resource ? `resource:${options.resource(context)}:ip:${ip}` : `ip:${ip}`
|
|
98
|
+
},
|
|
99
|
+
skip: (context) =>
|
|
100
|
+
trustedClientIP(context, client) === null || Boolean(options?.skip?.(context)),
|
|
101
|
+
handler
|
|
102
|
+
})
|
|
103
|
+
return options?.route ? ownBudget(limiter, options.skip) : limiter
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createActorRateLimiter(
|
|
107
|
+
database: Kysely<CloudDatabase>,
|
|
108
|
+
secret: string,
|
|
109
|
+
policy: RateLimitPolicy,
|
|
110
|
+
skip?: (context: Context) => boolean,
|
|
111
|
+
resource?: (context: Context<CloudAPIEnvironment, '*'>) => string
|
|
112
|
+
) {
|
|
113
|
+
return createCloudRateLimiter<CloudAPIEnvironment, '*'>({
|
|
114
|
+
database,
|
|
115
|
+
secret,
|
|
116
|
+
policy,
|
|
117
|
+
keyGenerator: (context) =>
|
|
118
|
+
resource
|
|
119
|
+
? `resource:${resource(context)}:actor:${context.get('actor').userId}`
|
|
120
|
+
: `actor:${context.get('actor').userId}`,
|
|
121
|
+
skip
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A limiter a route mounts for its own budget, such as document creation; general limiters leave
|
|
127
|
+
* the requests it counts to it.
|
|
128
|
+
*/
|
|
129
|
+
export function createRouteRateLimiter(
|
|
130
|
+
database: Kysely<CloudDatabase>,
|
|
131
|
+
secret: string,
|
|
132
|
+
policy: RateLimitPolicy,
|
|
133
|
+
skip?: (context: Context) => boolean,
|
|
134
|
+
resource?: (context: Context<CloudAPIEnvironment, '*'>) => string
|
|
135
|
+
) {
|
|
136
|
+
return ownBudget(createActorRateLimiter(database, secret, policy, skip, resource), skip)
|
|
137
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
3
|
+
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
4
|
+
import type { ClientRateLimitInfo, Store } from 'hono-rate-limiter'
|
|
5
|
+
import type { Kysely } from 'kysely'
|
|
6
|
+
|
|
7
|
+
export class PostgresRateLimitStore implements Store {
|
|
8
|
+
readonly localKeys = false
|
|
9
|
+
readonly prefix: string
|
|
10
|
+
private windowMs = 60_000
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
private readonly database: Kysely<CloudDatabase>,
|
|
14
|
+
private readonly secret: string,
|
|
15
|
+
namespace: string,
|
|
16
|
+
options: { windowMs?: number } = {}
|
|
17
|
+
) {
|
|
18
|
+
this.prefix = `${namespace}:`
|
|
19
|
+
this.windowMs = options.windowMs ?? this.windowMs
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
init(options: { windowMs: number }): void {
|
|
23
|
+
this.windowMs = options.windowMs
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private hash(key: string): string {
|
|
27
|
+
return bytesToHex(sha256(utf8ToBytes(`${this.secret}:${this.prefix}:${key}`)))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async increment(key: string): Promise<ClientRateLimitInfo> {
|
|
31
|
+
const now = new Date()
|
|
32
|
+
const resetTime = new Date(
|
|
33
|
+
Math.floor(now.getTime() / this.windowMs) * this.windowMs + this.windowMs
|
|
34
|
+
)
|
|
35
|
+
const row = await this.database
|
|
36
|
+
.insertInto('apiRateLimit')
|
|
37
|
+
.values({
|
|
38
|
+
keyHash: this.hash(key),
|
|
39
|
+
windowStartedAt: new Date(resetTime.getTime() - this.windowMs),
|
|
40
|
+
requestCount: 1,
|
|
41
|
+
updatedAt: now
|
|
42
|
+
})
|
|
43
|
+
.onConflict((conflict) =>
|
|
44
|
+
conflict.column('keyHash').doUpdateSet((expression) => ({
|
|
45
|
+
requestCount: expression
|
|
46
|
+
.case()
|
|
47
|
+
.whenRef('apiRateLimit.windowStartedAt', '=', 'excluded.windowStartedAt')
|
|
48
|
+
.then(expression('apiRateLimit.requestCount', '+', 1))
|
|
49
|
+
.else(1)
|
|
50
|
+
.end(),
|
|
51
|
+
windowStartedAt: expression.ref('excluded.windowStartedAt'),
|
|
52
|
+
updatedAt: expression.ref('excluded.updatedAt')
|
|
53
|
+
}))
|
|
54
|
+
)
|
|
55
|
+
.returning(['requestCount', 'windowStartedAt'])
|
|
56
|
+
.executeTakeFirstOrThrow()
|
|
57
|
+
return {
|
|
58
|
+
totalHits: Number(row.requestCount),
|
|
59
|
+
resetTime: new Date(new Date(row.windowStartedAt).getTime() + this.windowMs)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async decrement(key: string): Promise<void> {
|
|
64
|
+
await this.database
|
|
65
|
+
.updateTable('apiRateLimit')
|
|
66
|
+
.set((expression) => ({
|
|
67
|
+
requestCount: expression.fn('greatest', [
|
|
68
|
+
expression('requestCount', '-', 1),
|
|
69
|
+
expression.val(0)
|
|
70
|
+
])
|
|
71
|
+
}))
|
|
72
|
+
.where('keyHash', '=', this.hash(key))
|
|
73
|
+
.execute()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async resetKey(key: string): Promise<void> {
|
|
77
|
+
await this.database.deleteFrom('apiRateLimit').where('keyHash', '=', this.hash(key)).execute()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
3
|
+
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
4
|
+
import type { Kysely } from 'kysely'
|
|
5
|
+
|
|
6
|
+
export async function requireSharingAccess(
|
|
7
|
+
database: Kysely<CloudDatabase>,
|
|
8
|
+
userId: string,
|
|
9
|
+
documentId: string
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
const access = await resolveDocumentAccess(database, userId, documentId)
|
|
12
|
+
if (!access) throw new DocumentNotFoundError()
|
|
13
|
+
if (!access.canManageSharing) throw new DocumentForbiddenError()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Ends every collaboration ticket and room key issued for the document so far: removing someone's
|
|
18
|
+
* access must also remove them from its live room.
|
|
19
|
+
*/
|
|
20
|
+
export async function advanceCollaborationEpoch(
|
|
21
|
+
database: Kysely<CloudDatabase>,
|
|
22
|
+
documentId: string
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
await database
|
|
25
|
+
.updateTable('document')
|
|
26
|
+
.set({ collaborationEpoch: (expression) => expression('collaborationEpoch', '+', 1) })
|
|
27
|
+
.where('id', '=', documentId)
|
|
28
|
+
.executeTakeFirst()
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CloudActor, CloudSessionResolver } from '#cloud/server/auth'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Who follows a link: the person signed in to the server, if any, whom the server lets in where
|
|
5
|
+
* it turns guests away.
|
|
6
|
+
*/
|
|
7
|
+
export async function shareActor(
|
|
8
|
+
request: Request,
|
|
9
|
+
resolveSession?: CloudSessionResolver
|
|
10
|
+
): Promise<CloudActor | undefined> {
|
|
11
|
+
return resolveSession ? ((await resolveSession(request)) ?? undefined) : undefined
|
|
12
|
+
}
|