@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4
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/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +39 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +52 -3
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +2 -2
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +1 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
3
|
+
|
|
4
|
+
import type { CloudPolicyContext } from './keys'
|
|
5
|
+
|
|
6
|
+
type WorkspacePolicyTarget = { workspaceId: string } | { documentId: string }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The policy context for a workspace, or for a document by its workspace: its owner's plan, which
|
|
10
|
+
* every workspace they own takes, so creating more workspaces adds no allowance. Null when the
|
|
11
|
+
* workspace or document is gone.
|
|
12
|
+
*/
|
|
13
|
+
export async function workspacePolicyContext(
|
|
14
|
+
database: Kysely<CloudDatabase> | Transaction<CloudDatabase>,
|
|
15
|
+
target: WorkspacePolicyTarget,
|
|
16
|
+
context: Pick<CloudPolicyContext, 'actorId' | 'deploymentMode'>
|
|
17
|
+
): Promise<(CloudPolicyContext & { workspaceId: string }) | null> {
|
|
18
|
+
const row =
|
|
19
|
+
'documentId' in target
|
|
20
|
+
? await database
|
|
21
|
+
.selectFrom('document')
|
|
22
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
23
|
+
.select(['workspace.id as workspaceId', 'workspace.ownerId'])
|
|
24
|
+
.where('document.id', '=', target.documentId)
|
|
25
|
+
.executeTakeFirst()
|
|
26
|
+
: await database
|
|
27
|
+
.selectFrom('workspace')
|
|
28
|
+
.select(['id as workspaceId', 'ownerId'])
|
|
29
|
+
.where('id', '=', target.workspaceId)
|
|
30
|
+
.executeTakeFirst()
|
|
31
|
+
if (!row) return null
|
|
32
|
+
const policyContext: CloudPolicyContext & { workspaceId: string } = {
|
|
33
|
+
targetingKey: row.ownerId,
|
|
34
|
+
subjectType: 'user',
|
|
35
|
+
workspaceId: row.workspaceId,
|
|
36
|
+
deploymentMode: context.deploymentMode
|
|
37
|
+
}
|
|
38
|
+
if (context.actorId) policyContext.actorId = context.actorId
|
|
39
|
+
if ('documentId' in target) policyContext.documentId = target.documentId
|
|
40
|
+
return policyContext
|
|
41
|
+
}
|
|
@@ -28,6 +28,8 @@ export function createStorageReconciliationService(database: Kysely<CloudDatabas
|
|
|
28
28
|
.select(['storageObject.id', 'storageObject.byteSize'])
|
|
29
29
|
.distinct()
|
|
30
30
|
.where('document.workspaceId', '=', workspace.id)
|
|
31
|
+
// A document deleted forever came off the usage then, though its files wait out a grace.
|
|
32
|
+
.where('document.purgedAt', 'is', null)
|
|
31
33
|
// Images are the workspace's once, whichever revisions use them.
|
|
32
34
|
.union((union) =>
|
|
33
35
|
union
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
2
3
|
import type { Kysely, Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export class StorageQuotaExceededError extends Error {
|
|
@@ -12,37 +13,66 @@ export type StorageQuotaSnapshot = {
|
|
|
12
13
|
|
|
13
14
|
type StorageDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Takes the lock an owner's storage allowance is checked under, until the transaction ends.
|
|
18
|
+
* Reservations hold it while they count and reserve; an ownership transfer holds it for both
|
|
19
|
+
* owners, so no reservation counts a workspace under an owner it is leaving or joining.
|
|
20
|
+
*/
|
|
21
|
+
export function lockStorageAllowance(
|
|
22
|
+
transaction: Transaction<CloudDatabase>,
|
|
23
|
+
ownerId: string
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
return lockForTransaction(transaction, `storage-quota:${ownerId}`)
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
16
|
-
async function
|
|
29
|
+
async function ownerOf(executor: StorageDatabase, workspaceId: string): Promise<string | null> {
|
|
30
|
+
const workspace = await executor
|
|
31
|
+
.selectFrom('workspace')
|
|
32
|
+
.select('ownerId')
|
|
33
|
+
.where('id', '=', workspaceId)
|
|
34
|
+
.executeTakeFirst()
|
|
35
|
+
return workspace?.ownerId ?? null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What `ownerId` holds and has reserved across every workspace they own, which all draw from
|
|
40
|
+
* the owner's one allowance. Both totals come from one statement, so an upload committing
|
|
41
|
+
* meanwhile counts once, as committed or as reserved.
|
|
42
|
+
*/
|
|
43
|
+
async function usage(
|
|
17
44
|
executor: StorageDatabase,
|
|
18
|
-
|
|
45
|
+
ownerId: string,
|
|
19
46
|
now = new Date()
|
|
20
47
|
): Promise<StorageQuotaSnapshot> {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const owned = executor.selectFrom('workspace').select('id').where('ownerId', '=', ownerId)
|
|
49
|
+
const row = await executor
|
|
50
|
+
.selectNoFrom((expression) => [
|
|
51
|
+
expression
|
|
52
|
+
.selectFrom('workspaceStorageUsage')
|
|
53
|
+
.select((sum) =>
|
|
54
|
+
sum.fn.coalesce(sum.fn.sum('committedBytes'), sum.val(0)).as('committedBytes')
|
|
55
|
+
)
|
|
56
|
+
.where('workspaceId', 'in', owned)
|
|
57
|
+
.as('committedBytes'),
|
|
58
|
+
expression
|
|
59
|
+
.selectFrom('uploadStorageReservation')
|
|
60
|
+
.select((sum) => sum.fn.coalesce(sum.fn.sum('bytes'), sum.val(0)).as('reservedBytes'))
|
|
61
|
+
.where('workspaceId', 'in', owned)
|
|
62
|
+
.where('committedAt', 'is', null)
|
|
63
|
+
.where('releasedAt', 'is', null)
|
|
64
|
+
.where('expiresAt', '>', now)
|
|
65
|
+
.as('reservedBytes')
|
|
66
|
+
])
|
|
67
|
+
.executeTakeFirstOrThrow()
|
|
38
68
|
return {
|
|
39
|
-
committedBytes: Number(
|
|
40
|
-
reservedBytes: Number(
|
|
69
|
+
committedBytes: Number(row.committedBytes ?? 0),
|
|
70
|
+
reservedBytes: Number(row.reservedBytes ?? 0)
|
|
41
71
|
}
|
|
42
72
|
}
|
|
43
73
|
|
|
44
74
|
async function reserve(
|
|
45
|
-
executor:
|
|
75
|
+
executor: Transaction<CloudDatabase>,
|
|
46
76
|
input: {
|
|
47
77
|
workspaceId: string
|
|
48
78
|
uploadId: string
|
|
@@ -51,21 +81,25 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
51
81
|
maximumBytes: number | null
|
|
52
82
|
}
|
|
53
83
|
): Promise<void> {
|
|
84
|
+
// The allowance spans the owner's workspaces, so the owner is what reservations queue on.
|
|
85
|
+
// An ownership transfer holds the same lock, so the owner read once it is held stays put.
|
|
86
|
+
let ownerId = await ownerOf(executor, input.workspaceId)
|
|
87
|
+
for (;;) {
|
|
88
|
+
if (ownerId === null) throw new Error('Workspace not found')
|
|
89
|
+
await lockStorageAllowance(executor, ownerId)
|
|
90
|
+
const current = await ownerOf(executor, input.workspaceId)
|
|
91
|
+
if (current === ownerId) break
|
|
92
|
+
ownerId = current
|
|
93
|
+
}
|
|
54
94
|
await executor
|
|
55
95
|
.insertInto('workspaceStorageUsage')
|
|
56
96
|
.values({ workspaceId: input.workspaceId, committedBytes: 0 })
|
|
57
97
|
.onConflict((conflict) => conflict.column('workspaceId').doNothing())
|
|
58
98
|
.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)
|
|
99
|
+
const held = await usage(executor, ownerId)
|
|
66
100
|
if (
|
|
67
101
|
input.maximumBytes !== null &&
|
|
68
|
-
|
|
102
|
+
held.committedBytes + held.reservedBytes + input.bytes > input.maximumBytes
|
|
69
103
|
) {
|
|
70
104
|
throw new StorageQuotaExceededError('Workspace storage quota exceeded')
|
|
71
105
|
}
|
|
@@ -119,7 +153,11 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
119
153
|
}
|
|
120
154
|
|
|
121
155
|
return {
|
|
122
|
-
|
|
156
|
+
/** The pooled usage of the workspace's owner, or null when the workspace is gone. */
|
|
157
|
+
async snapshot(workspaceId: string): Promise<StorageQuotaSnapshot | null> {
|
|
158
|
+
const ownerId = await ownerOf(database, workspaceId)
|
|
159
|
+
return ownerId === null ? null : usage(database, ownerId)
|
|
160
|
+
},
|
|
123
161
|
|
|
124
162
|
async reserve(input: {
|
|
125
163
|
workspaceId: string
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { INVITATION_BUDGET } from '#cloud/server/invitations'
|
|
2
3
|
import type { Kysely } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export function createRateLimitCleanupService(database: Kysely<CloudDatabase>) {
|
|
5
6
|
return {
|
|
6
7
|
async cleanupExpired(before: Date): Promise<number> {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const [limits, invitations] = await Promise.all([
|
|
9
|
+
database
|
|
10
|
+
.deleteFrom('apiRateLimit')
|
|
11
|
+
.where('windowStartedAt', '<', before)
|
|
12
|
+
.executeTakeFirst(),
|
|
13
|
+
// Never a send the invitation budget still counts, whatever cutoff the sweep passes.
|
|
14
|
+
database
|
|
15
|
+
.deleteFrom('invitationSend')
|
|
16
|
+
.where(
|
|
17
|
+
'sentAt',
|
|
18
|
+
'<',
|
|
19
|
+
new Date(Math.min(before.getTime(), Date.now() - INVITATION_BUDGET.windowMs))
|
|
20
|
+
)
|
|
21
|
+
.executeTakeFirst()
|
|
22
|
+
])
|
|
23
|
+
return Number(limits.numDeletedRows) + Number(invitations.numDeletedRows)
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
@@ -44,7 +44,7 @@ export const CLOUD_RATE_LIMITS = {
|
|
|
44
44
|
},
|
|
45
45
|
uploadCreation: { namespace: 'upload-creation', windowMs: 60_000, limit: 120, actorLimit: 600 },
|
|
46
46
|
authenticatedMutation: { namespace: 'authenticated-mutation', windowMs: 60_000, limit: 120 },
|
|
47
|
-
|
|
47
|
+
servicesToken: { namespace: 'services-token', windowMs: 60_000, limit: 30 },
|
|
48
48
|
adminRead: { namespace: 'admin-read', windowMs: 60_000, limit: 300 },
|
|
49
49
|
adminMutation: { namespace: 'admin-mutation', windowMs: 60_000, limit: 30 }
|
|
50
50
|
} as const satisfies Record<string, RateLimitPolicy | ResourceRateLimitPolicy>
|
|
@@ -220,6 +220,6 @@ function setBudgetHeaders(context: Context, use: RateLimitUse, windowMs: number)
|
|
|
220
220
|
export function routeParamResource(name: string) {
|
|
221
221
|
return (context: Context): string =>
|
|
222
222
|
// Lowercased as validation lowercases UUIDs, so one row's spellings share a budget.
|
|
223
|
-
// eslint-disable-next-line open-pencil/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
223
|
+
// eslint-disable-next-line @open-pencil/security/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
224
224
|
context.req.param(name)?.toLowerCase() ?? 'unknown'
|
|
225
225
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { parseFileDocumentReport } from '#cloud/contract'
|
|
2
|
+
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
3
|
+
import { shareActor } from '#cloud/server/sharing/actor'
|
|
4
|
+
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
5
|
+
import { shareParams, validatedJSON } from '#cloud/server/validation'
|
|
6
|
+
import { Hono } from 'hono'
|
|
7
|
+
|
|
8
|
+
import { DocumentReportLimitError, type DocumentReportService } from './service'
|
|
9
|
+
|
|
10
|
+
/** Reports about documents shared by link, which anyone the link lets in may file. */
|
|
11
|
+
export function createPublicReportRoutes(
|
|
12
|
+
service: DocumentReportService,
|
|
13
|
+
resolveSession?: CloudSessionResolver
|
|
14
|
+
) {
|
|
15
|
+
return new Hono().post(
|
|
16
|
+
'/shares/:shareId/reports',
|
|
17
|
+
shareParams,
|
|
18
|
+
validatedJSON(parseFileDocumentReport),
|
|
19
|
+
async (context) => {
|
|
20
|
+
try {
|
|
21
|
+
return await sharingRoute(context, async () => {
|
|
22
|
+
await service.file(
|
|
23
|
+
context.req.valid('param').shareId,
|
|
24
|
+
context.req.valid('json'),
|
|
25
|
+
await shareActor(context.req.raw, resolveSession)
|
|
26
|
+
)
|
|
27
|
+
return context.json({ received: true as const }, 201)
|
|
28
|
+
})
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (error instanceof DocumentReportLimitError) {
|
|
31
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
32
|
+
}
|
|
33
|
+
throw error
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { FileDocumentReportInput } from '#cloud/contract'
|
|
2
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
3
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
5
|
+
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
6
|
+
import type { Kysely } from 'kysely'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How many reports one person, or guest, may file about one link in a day, and how many a link
|
|
10
|
+
* may gather from everyone, since a guest names themselves and could otherwise flood the queue.
|
|
11
|
+
*/
|
|
12
|
+
export const DOCUMENT_REPORT_BUDGET = {
|
|
13
|
+
windowMs: 24 * 60 * 60 * 1000,
|
|
14
|
+
perReporter: 3,
|
|
15
|
+
perShare: 50
|
|
16
|
+
} as const
|
|
17
|
+
|
|
18
|
+
/** How much of a description an administrator's email quotes; the console shows all of it. */
|
|
19
|
+
const EMAIL_DESCRIPTION_CHARACTERS = 500
|
|
20
|
+
|
|
21
|
+
/** A report past the reporter's or the link's daily budget. */
|
|
22
|
+
export class DocumentReportLimitError extends Error {
|
|
23
|
+
override readonly name = 'DocumentReportLimitError'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type DocumentReportServiceOptions = {
|
|
27
|
+
sharing: Pick<DocumentSharingService, 'resolveShare'>
|
|
28
|
+
email?: TransactionalEmailService
|
|
29
|
+
/** Who hears of a new report: the deployment's administrators. */
|
|
30
|
+
adminRecipients: readonly string[]
|
|
31
|
+
publicURL: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createDocumentReportService(
|
|
35
|
+
database: Kysely<CloudDatabase>,
|
|
36
|
+
options: DocumentReportServiceOptions
|
|
37
|
+
) {
|
|
38
|
+
return {
|
|
39
|
+
/**
|
|
40
|
+
* Files a report about the document a share link opens, for someone the link lets in, and
|
|
41
|
+
* tells the deployment's administrators. It answers nothing about the document, so a report
|
|
42
|
+
* never tells its reporter more than the link already did.
|
|
43
|
+
*/
|
|
44
|
+
async file(shareId: string, input: FileDocumentReportInput, actor?: CloudActor): Promise<void> {
|
|
45
|
+
const resolved = await options.sharing.resolveShare(
|
|
46
|
+
shareId,
|
|
47
|
+
{ secret: input.secret, guestId: input.guestId },
|
|
48
|
+
actor
|
|
49
|
+
)
|
|
50
|
+
const reporterUserId = actor?.userId ?? null
|
|
51
|
+
const reporterGuestId = actor ? null : (input.guestId ?? null)
|
|
52
|
+
const since = new Date(Date.now() - DOCUMENT_REPORT_BUDGET.windowMs)
|
|
53
|
+
await database.transaction().execute(async (transaction) => {
|
|
54
|
+
const [byReporter, byShare] = await Promise.all([
|
|
55
|
+
transaction
|
|
56
|
+
.selectFrom('documentReport')
|
|
57
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
58
|
+
.where('shareId', '=', shareId)
|
|
59
|
+
.where('createdAt', '>', since)
|
|
60
|
+
.where((expression) => {
|
|
61
|
+
if (reporterUserId) return expression('reporterUserId', '=', reporterUserId)
|
|
62
|
+
if (reporterGuestId) return expression('reporterGuestId', '=', reporterGuestId)
|
|
63
|
+
return expression.and([
|
|
64
|
+
expression('reporterUserId', 'is', null),
|
|
65
|
+
expression('reporterGuestId', 'is', null)
|
|
66
|
+
])
|
|
67
|
+
})
|
|
68
|
+
.executeTakeFirstOrThrow(),
|
|
69
|
+
transaction
|
|
70
|
+
.selectFrom('documentReport')
|
|
71
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
72
|
+
.where('shareId', '=', shareId)
|
|
73
|
+
.where('createdAt', '>', since)
|
|
74
|
+
.executeTakeFirstOrThrow()
|
|
75
|
+
])
|
|
76
|
+
if (
|
|
77
|
+
Number(byReporter.count) >= DOCUMENT_REPORT_BUDGET.perReporter ||
|
|
78
|
+
Number(byShare.count) >= DOCUMENT_REPORT_BUDGET.perShare
|
|
79
|
+
) {
|
|
80
|
+
throw new DocumentReportLimitError()
|
|
81
|
+
}
|
|
82
|
+
const id = crypto.randomUUID()
|
|
83
|
+
await transaction
|
|
84
|
+
.insertInto('documentReport')
|
|
85
|
+
.values({
|
|
86
|
+
id,
|
|
87
|
+
documentId: resolved.documentId,
|
|
88
|
+
shareId,
|
|
89
|
+
reporterUserId,
|
|
90
|
+
reporterGuestId,
|
|
91
|
+
replyEmail: input.replyEmail ?? actor?.email ?? null,
|
|
92
|
+
reason: input.reason,
|
|
93
|
+
description: input.description
|
|
94
|
+
})
|
|
95
|
+
.execute()
|
|
96
|
+
if (!options.email) return
|
|
97
|
+
const document = await transaction
|
|
98
|
+
.selectFrom('document')
|
|
99
|
+
.select('name')
|
|
100
|
+
.where('id', '=', resolved.documentId)
|
|
101
|
+
.executeTakeFirstOrThrow()
|
|
102
|
+
for (const recipientEmail of options.adminRecipients) {
|
|
103
|
+
await options.email.enqueue(
|
|
104
|
+
{
|
|
105
|
+
idempotencyKey: `document-report/${id}/${recipientEmail}`,
|
|
106
|
+
kind: 'document-report',
|
|
107
|
+
recipientEmail,
|
|
108
|
+
payload: {
|
|
109
|
+
documentName: document.name,
|
|
110
|
+
reason: input.reason,
|
|
111
|
+
description: input.description.slice(0, EMAIL_DESCRIPTION_CHARACTERS),
|
|
112
|
+
actionURL: new URL('/admin/reports', options.publicURL).href
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
transaction
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export type DocumentReportService = ReturnType<typeof createDocumentReportService>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ServicesNotEntitledError } from './errors'
|
|
2
|
+
export { importServicesSigningKey, loadServicesSigningKey, type ServicesSigningKey } from './keys'
|
|
3
|
+
export { createServicesRoutes, type ServicesRouteEnvironment } from './routes'
|
|
4
|
+
export {
|
|
5
|
+
createServicesTokenService,
|
|
6
|
+
type ServicesTokenService,
|
|
7
|
+
type ServicesTokenServiceOptions
|
|
8
|
+
} from './tokens'
|
|
@@ -3,18 +3,18 @@ import { calculateJwkThumbprint, exportJWK, importPKCS8, type JWK } from 'jose'
|
|
|
3
3
|
|
|
4
4
|
const ALGORITHM = 'ES256'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type ServicesSigningKey = {
|
|
7
7
|
privateKey: CryptoKey
|
|
8
8
|
/** The public half, as published in the JWKS. */
|
|
9
9
|
publicJWK: JWK & { kid: string }
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Imports the P-256 key that signs
|
|
13
|
+
* Imports the P-256 key that signs service tokens. Its `kid` is the public key's thumbprint,
|
|
14
14
|
* so replacing the key changes the `kid`, and a service that caches the JWKS refetches it on the
|
|
15
15
|
* first token it cannot match. Tokens the old key signed fail once and are re-requested.
|
|
16
16
|
*/
|
|
17
|
-
export async function
|
|
17
|
+
export async function importServicesSigningKey(pem: string): Promise<ServicesSigningKey> {
|
|
18
18
|
const privateKey = await importPKCS8(pem, ALGORITHM, { extractable: true })
|
|
19
19
|
const { kty, crv, x, y } = await exportJWK(privateKey)
|
|
20
20
|
const publicJWK = { kty, crv, x, y }
|
|
@@ -27,17 +27,17 @@ export async function importAISigningKey(pem: string): Promise<AISigningKey> {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Imports the configured signing key at startup, so a key that cannot sign stops the runtime
|
|
30
|
-
* instead of failing every token request after discovery has advertised
|
|
30
|
+
* instead of failing every token request after discovery has advertised services.
|
|
31
31
|
*/
|
|
32
|
-
export async function
|
|
33
|
-
config: Pick<CloudServerConfig, '
|
|
34
|
-
): Promise<
|
|
35
|
-
if (!config.
|
|
32
|
+
export async function loadServicesSigningKey(
|
|
33
|
+
config: Pick<CloudServerConfig, 'servicesSigningKey'>
|
|
34
|
+
): Promise<ServicesSigningKey | undefined> {
|
|
35
|
+
if (!config.servicesSigningKey) return undefined
|
|
36
36
|
try {
|
|
37
|
-
return await
|
|
37
|
+
return await importServicesSigningKey(config.servicesSigningKey)
|
|
38
38
|
} catch {
|
|
39
|
-
throw new CloudConfigError('
|
|
39
|
+
throw new CloudConfigError('Services signing key must be a PKCS #8 P-256 private key')
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export const
|
|
43
|
+
export const SERVICES_SIGNING_ALGORITHM = ALGORITHM
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { parseCreateServicesToken } from '#cloud/contract'
|
|
2
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
3
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
+
import { DocumentNotFoundError } from '#cloud/server/documents'
|
|
5
|
+
import { CLOUD_RATE_LIMITS, createRouteRateLimiter } from '#cloud/server/rate-limit'
|
|
6
|
+
import { validatedJSON } from '#cloud/server/validation'
|
|
7
|
+
import { Hono } from 'hono'
|
|
8
|
+
import type { Kysely } from 'kysely'
|
|
9
|
+
|
|
10
|
+
import { ServicesNotEntitledError } from './errors'
|
|
11
|
+
import type { ServicesTokenService } from './tokens'
|
|
12
|
+
|
|
13
|
+
export type ServicesRouteEnvironment = { Variables: { actor: CloudActor } }
|
|
14
|
+
|
|
15
|
+
/** Without a service, the deployment offers no services and the token route is not found. */
|
|
16
|
+
export function createServicesRoutes(
|
|
17
|
+
service: ServicesTokenService | undefined,
|
|
18
|
+
rateLimit?: { database: Kysely<CloudDatabase>; secret: string }
|
|
19
|
+
) {
|
|
20
|
+
const router = new Hono<ServicesRouteEnvironment>()
|
|
21
|
+
if (service && rateLimit) {
|
|
22
|
+
router.use(
|
|
23
|
+
'/services/token',
|
|
24
|
+
createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.servicesToken)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
return router.post(
|
|
28
|
+
'/services/token',
|
|
29
|
+
validatedJSON(parseCreateServicesToken),
|
|
30
|
+
async (context) => {
|
|
31
|
+
if (!service) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
32
|
+
try {
|
|
33
|
+
return context.json(
|
|
34
|
+
await service.issue(context.get('actor'), context.req.valid('json').workspaceId)
|
|
35
|
+
)
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error instanceof DocumentNotFoundError) {
|
|
38
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
39
|
+
}
|
|
40
|
+
if (error instanceof ServicesNotEntitledError) {
|
|
41
|
+
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
42
|
+
}
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
}
|
|
@@ -1,59 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CLOUD_SERVICES_TOKEN_AUDIENCE, type ServicesToken } from '#cloud/contract'
|
|
2
2
|
import type { CloudActor } from '#cloud/server/auth'
|
|
3
3
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
4
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
5
5
|
import { workspaceRole } from '#cloud/server/documents/repository'
|
|
6
6
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
7
7
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
8
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
8
9
|
import { SignJWT } from 'jose'
|
|
9
10
|
import type { Kysely } from 'kysely'
|
|
10
11
|
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
12
|
+
import { ServicesNotEntitledError } from './errors'
|
|
13
|
+
import { SERVICES_SIGNING_ALGORITHM, type ServicesSigningKey } from './keys'
|
|
13
14
|
|
|
14
|
-
export type
|
|
15
|
+
export type ServicesTokenServiceOptions = {
|
|
15
16
|
database: Kysely<CloudDatabase>
|
|
16
17
|
policy: CloudPolicy
|
|
17
18
|
deploymentMode: 'official' | 'self-hosted'
|
|
18
19
|
/** The Cloud public URL, which services check as the token's `iss`. */
|
|
19
20
|
issuer: string
|
|
20
|
-
signingKey:
|
|
21
|
+
signingKey: ServicesSigningKey
|
|
21
22
|
lifetimeSeconds: number
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export function
|
|
25
|
+
export function createServicesTokenService(options: ServicesTokenServiceOptions) {
|
|
25
26
|
const { privateKey, publicJWK } = options.signingKey
|
|
26
27
|
|
|
27
28
|
return {
|
|
28
29
|
/**
|
|
29
|
-
* Signs a token that lets a workspace member reach the
|
|
30
|
-
* Entitlements are read for every token, so a plan change reaches the service within one
|
|
30
|
+
* Signs a token that lets a workspace member reach the services on the workspace's behalf,
|
|
31
|
+
* spending its owner's credits. Entitlements are read for every token, so a plan change reaches the service within one
|
|
31
32
|
* token lifetime.
|
|
32
33
|
*/
|
|
33
|
-
async issue(actor: CloudActor, workspaceId: string): Promise<
|
|
34
|
+
async issue(actor: CloudActor, workspaceId: string): Promise<ServicesToken> {
|
|
34
35
|
if (!(await workspaceRole(options.database, actor.userId, workspaceId))) {
|
|
35
36
|
throw new DocumentNotFoundError()
|
|
36
37
|
}
|
|
37
|
-
const context =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
const context = await workspacePolicyContext(
|
|
39
|
+
options.database,
|
|
40
|
+
{ workspaceId },
|
|
41
|
+
{ actorId: actor.userId, deploymentMode: options.deploymentMode }
|
|
42
|
+
)
|
|
43
|
+
if (!context) throw new DocumentNotFoundError()
|
|
43
44
|
const [enabled, allowance] = await Promise.all([
|
|
44
|
-
options.policy.boolean(CLOUD_FEATURE_KEYS.
|
|
45
|
+
options.policy.boolean(CLOUD_FEATURE_KEYS.cloudServices, false, context),
|
|
45
46
|
options.policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context)
|
|
46
47
|
])
|
|
47
|
-
if (!enabled) throw new
|
|
48
|
+
if (!enabled) throw new ServicesNotEntitledError()
|
|
48
49
|
const issuedAt = Math.floor(Date.now() / 1000)
|
|
49
50
|
const expiresAt = issuedAt + options.lifetimeSeconds
|
|
50
51
|
const token = await new SignJWT({
|
|
51
52
|
workspaceId,
|
|
53
|
+
// Credits are the owner's, shared by every workspace they own.
|
|
54
|
+
accountId: context.targetingKey,
|
|
52
55
|
monthlyCreditAllowance: allowance < 0 ? null : allowance
|
|
53
56
|
})
|
|
54
|
-
.setProtectedHeader({ alg:
|
|
57
|
+
.setProtectedHeader({ alg: SERVICES_SIGNING_ALGORITHM, kid: publicJWK.kid, typ: 'JWT' })
|
|
55
58
|
.setIssuer(options.issuer)
|
|
56
|
-
.setAudience(
|
|
59
|
+
.setAudience(CLOUD_SERVICES_TOKEN_AUDIENCE)
|
|
57
60
|
.setSubject(actor.userId)
|
|
58
61
|
.setIssuedAt(issuedAt)
|
|
59
62
|
.setExpirationTime(expiresAt)
|
|
@@ -67,4 +70,4 @@ export function createAITokenService(options: AITokenServiceOptions) {
|
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
export type
|
|
73
|
+
export type ServicesTokenService = ReturnType<typeof createServicesTokenService>
|