@rubytech/create-maxy-code 0.1.459 → 0.1.460
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 +1 -1
- package/payload/platform/lib/storage-broker/dist/__tests__/cf-exec.test.js +116 -6
- package/payload/platform/lib/storage-broker/dist/__tests__/cf-exec.test.js.map +1 -1
- package/payload/platform/lib/storage-broker/dist/__tests__/object-limits.test.js +20 -0
- package/payload/platform/lib/storage-broker/dist/__tests__/object-limits.test.js.map +1 -1
- package/payload/platform/lib/storage-broker/dist/__tests__/resource-limits.test.d.ts +2 -0
- package/payload/platform/lib/storage-broker/dist/__tests__/resource-limits.test.d.ts.map +1 -0
- package/payload/platform/lib/storage-broker/dist/__tests__/resource-limits.test.js +41 -0
- package/payload/platform/lib/storage-broker/dist/__tests__/resource-limits.test.js.map +1 -0
- package/payload/platform/lib/storage-broker/dist/cf-exec.d.ts +1 -0
- package/payload/platform/lib/storage-broker/dist/cf-exec.d.ts.map +1 -1
- package/payload/platform/lib/storage-broker/dist/cf-exec.js +116 -48
- package/payload/platform/lib/storage-broker/dist/cf-exec.js.map +1 -1
- package/payload/platform/lib/storage-broker/dist/index.d.ts +1 -0
- package/payload/platform/lib/storage-broker/dist/index.d.ts.map +1 -1
- package/payload/platform/lib/storage-broker/dist/index.js +1 -0
- package/payload/platform/lib/storage-broker/dist/index.js.map +1 -1
- package/payload/platform/lib/storage-broker/dist/object-limits.d.ts +27 -0
- package/payload/platform/lib/storage-broker/dist/object-limits.d.ts.map +1 -1
- package/payload/platform/lib/storage-broker/dist/object-limits.js +27 -7
- package/payload/platform/lib/storage-broker/dist/object-limits.js.map +1 -1
- package/payload/platform/lib/storage-broker/dist/resource-limits.d.ts +102 -0
- package/payload/platform/lib/storage-broker/dist/resource-limits.d.ts.map +1 -0
- package/payload/platform/lib/storage-broker/dist/resource-limits.js +105 -0
- package/payload/platform/lib/storage-broker/dist/resource-limits.js.map +1 -0
- package/payload/platform/lib/storage-broker/src/__tests__/cf-exec.test.ts +130 -7
- package/payload/platform/lib/storage-broker/src/__tests__/object-limits.test.ts +30 -0
- package/payload/platform/lib/storage-broker/src/__tests__/resource-limits.test.ts +49 -0
- package/payload/platform/lib/storage-broker/src/cf-exec.ts +129 -56
- package/payload/platform/lib/storage-broker/src/index.ts +1 -0
- package/payload/platform/lib/storage-broker/src/object-limits.ts +26 -6
- package/payload/platform/lib/storage-broker/src/resource-limits.ts +105 -0
- package/payload/platform/plugins/admin/skills/platform-architecture/SKILL.md +5 -4
- package/payload/platform/plugins/cloudflare/PLUGIN.md +3 -1
- package/payload/platform/plugins/cloudflare/mcp/__tests__/auth-route.test.ts +206 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/authorize.test.ts +201 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/file-routes.test.ts +178 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/harness.test.ts +32 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/passcode.test.ts +74 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/ratelimit.test.ts +77 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/skill-contract.test.ts +46 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/template-config.test.ts +55 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/upload-route.test.ts +255 -0
- package/payload/platform/plugins/cloudflare/mcp/package.json +5 -2
- package/payload/platform/plugins/cloudflare/references/r2-object-storage.md +46 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/SKILL.md +123 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/authorize.ts +32 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/log.ts +12 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/passcode.ts +73 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/ratelimit.ts +55 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/session.ts +85 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/types.ts +44 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/auth.ts +113 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/delete.ts +89 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/download.ts +70 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/files.ts +41 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/upload.ts +151 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/public/index.html +42 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/public/portal.css +113 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/public/portal.js +155 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/schema.sql +64 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/wrangler.toml +21 -0
- package/payload/platform/plugins/cloudflare/skills/site-deploy/SKILL.md +8 -0
- package/payload/platform/plugins/docs/references/outlook-guide.md +3 -3
- package/payload/platform/plugins/docs/references/voice-mirror-guide.md +1 -0
- package/payload/platform/plugins/filesystem/PLUGIN.md +0 -1
- package/payload/platform/plugins/filesystem/mcp/dist/lib/scope.d.ts +14 -10
- package/payload/platform/plugins/filesystem/mcp/dist/lib/scope.d.ts.map +1 -1
- package/payload/platform/plugins/filesystem/mcp/dist/lib/scope.js +14 -10
- package/payload/platform/plugins/filesystem/mcp/dist/lib/scope.js.map +1 -1
- package/payload/platform/plugins/outlook/.claude-plugin/plugin.json +1 -1
- package/payload/platform/plugins/outlook/PLUGIN.md +3 -5
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/detach.test.d.ts +2 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/detach.test.d.ts.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/detach.test.js +75 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/detach.test.js.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit-removal.test.d.ts +2 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit-removal.test.d.ts.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit-removal.test.js +187 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit-removal.test.js.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit.test.js +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/draft-edit.test.js.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/index.js +3 -2
- package/payload/platform/plugins/outlook/mcp/dist/index.js.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/lib/attachment-list.d.ts +43 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/attachment-list.d.ts.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/attachment-list.js +65 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/attachment-list.js.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/detach.d.ts +65 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/detach.d.ts.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/detach.js +99 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/detach.js.map +1 -0
- package/payload/platform/plugins/outlook/mcp/dist/lib/outbound-attachments.d.ts +6 -1
- package/payload/platform/plugins/outlook/mcp/dist/lib/outbound-attachments.d.ts.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/lib/outbound-attachments.js.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/tools/draft-edit.d.ts +42 -22
- package/payload/platform/plugins/outlook/mcp/dist/tools/draft-edit.d.ts.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/tools/draft-edit.js +59 -23
- package/payload/platform/plugins/outlook/mcp/dist/tools/draft-edit.js.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/tools/mail-attachment.d.ts +15 -16
- package/payload/platform/plugins/outlook/mcp/dist/tools/mail-attachment.d.ts.map +1 -1
- package/payload/platform/plugins/outlook/mcp/dist/tools/mail-attachment.js +8 -41
- package/payload/platform/plugins/outlook/mcp/dist/tools/mail-attachment.js.map +1 -1
- package/payload/platform/plugins/outlook/references/auth.md +1 -1
- package/payload/platform/plugins/outlook/references/graph-surfaces.md +7 -3
- package/payload/platform/plugins/outlook/skills/outlook/SKILL.md +1 -1
- package/payload/platform/plugins/storage-broker/PLUGIN.md +1 -1
- package/payload/platform/plugins/voice-mirror/PLUGIN.md +19 -7
- package/payload/platform/plugins/voice-mirror/mcp/dist/index.d.ts.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/index.js +35 -16
- package/payload/platform/plugins/voice-mirror/mcp/dist/index.js.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/lib/admin-user.d.ts +31 -0
- package/payload/platform/plugins/voice-mirror/mcp/dist/lib/admin-user.d.ts.map +1 -0
- package/payload/platform/plugins/voice-mirror/mcp/dist/lib/admin-user.js +87 -0
- package/payload/platform/plugins/voice-mirror/mcp/dist/lib/admin-user.js.map +1 -0
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-distil-profile.d.ts.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-distil-profile.js +13 -3
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-distil-profile.js.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-ingest-session-text.d.ts.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-ingest-session-text.js +2 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-ingest-session-text.js.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-record-feedback.d.ts.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-record-feedback.js +2 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-record-feedback.js.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-tag-content.d.ts +2 -2
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-tag-content.d.ts.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-tag-content.js +12 -3
- package/payload/platform/plugins/voice-mirror/mcp/dist/tools/voice-tag-content.js.map +1 -1
- package/payload/platform/plugins/voice-mirror/mcp/scripts/smoke.mjs +334 -0
- package/payload/platform/plugins/voice-mirror/skills/voice-mirror/SKILL.md +13 -0
- package/payload/platform/scripts/voice-author-key-audit.sh +155 -0
- package/payload/platform/services/claude-session-manager/dist/mcp-descriptor-resolve.d.ts +17 -0
- package/payload/platform/services/claude-session-manager/dist/mcp-descriptor-resolve.d.ts.map +1 -0
- package/payload/platform/services/claude-session-manager/dist/mcp-descriptor-resolve.js +88 -0
- package/payload/platform/services/claude-session-manager/dist/mcp-descriptor-resolve.js.map +1 -0
- package/payload/platform/services/claude-session-manager/dist/pty-spawner.d.ts +0 -1
- package/payload/platform/services/claude-session-manager/dist/pty-spawner.d.ts.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/pty-spawner.js +34 -23
- package/payload/platform/services/claude-session-manager/dist/pty-spawner.js.map +1 -1
- package/payload/server/{chunk-Q6W4U6HL.js → chunk-JXWFVE5X.js} +83 -39
- package/payload/server/server.js +213 -38
- package/payload/server/{src-3I2RYZFB.js → src-4F37OHLK.js} +11 -1
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/pkce-flow.test.d.ts +0 -2
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/pkce-flow.test.js +0 -215
- package/payload/platform/plugins/outlook/mcp/dist/__tests__/pkce-flow.test.js.map +0 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { processUpload } from '../../skills/data-portal/template/functions/api/upload'
|
|
3
|
+
import type { PortalEnv } from '../../skills/data-portal/template/functions/api/_lib/types'
|
|
4
|
+
|
|
5
|
+
// The fake must model the constraints the schema actually declares, not just
|
|
6
|
+
// the happy path. `objectKey TEXT UNIQUE` is what makes a re-upload an upsert
|
|
7
|
+
// rather than a second row; a fake that accepts duplicates would let the
|
|
8
|
+
// "exactly one manifest row" assertion pass while the real D1 threw.
|
|
9
|
+
function makeEnv() {
|
|
10
|
+
const manifestRows: unknown[][] = []
|
|
11
|
+
const objects = new Map<string, Uint8Array>()
|
|
12
|
+
const env = {
|
|
13
|
+
DB: {
|
|
14
|
+
prepare(query: string) {
|
|
15
|
+
let bound: unknown[] = []
|
|
16
|
+
const stmt = {
|
|
17
|
+
bind(...v: unknown[]) {
|
|
18
|
+
bound = v
|
|
19
|
+
return stmt
|
|
20
|
+
},
|
|
21
|
+
async run() {
|
|
22
|
+
if (/INSERT INTO manifest/i.test(query)) {
|
|
23
|
+
const objectKey = bound[3] as string
|
|
24
|
+
const existing = manifestRows.findIndex((r) => r[3] === objectKey)
|
|
25
|
+
if (existing >= 0) {
|
|
26
|
+
if (!/ON CONFLICT\s*\(\s*objectKey\s*\)\s*DO UPDATE/i.test(query)) {
|
|
27
|
+
throw new Error('UNIQUE constraint failed: manifest.objectKey')
|
|
28
|
+
}
|
|
29
|
+
manifestRows[existing] = bound
|
|
30
|
+
} else {
|
|
31
|
+
manifestRows.push(bound)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { meta: { changes: 1 } }
|
|
35
|
+
},
|
|
36
|
+
async all() {
|
|
37
|
+
return { results: [] }
|
|
38
|
+
},
|
|
39
|
+
async first<T>() {
|
|
40
|
+
if (/FROM sessions/i.test(query)) {
|
|
41
|
+
return (bound[0] === 'sess-a' ? { ownerId: 'alice' } : null) as T | null
|
|
42
|
+
}
|
|
43
|
+
if (/FROM manifest/i.test(query)) {
|
|
44
|
+
const key = bound[0] as string
|
|
45
|
+
return (objects.has(key) ? { fileId: 'x' } : null) as T | null
|
|
46
|
+
}
|
|
47
|
+
return null as T | null
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
return stmt
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
BUCKET: {
|
|
54
|
+
async put(key: string, body: Uint8Array) {
|
|
55
|
+
objects.set(key, body)
|
|
56
|
+
return {}
|
|
57
|
+
},
|
|
58
|
+
async get(key: string) {
|
|
59
|
+
const v = objects.get(key)
|
|
60
|
+
return v ? { body: null, size: v.length } : null
|
|
61
|
+
},
|
|
62
|
+
async head(key: string) {
|
|
63
|
+
const v = objects.get(key)
|
|
64
|
+
return v ? { size: v.length } : null
|
|
65
|
+
},
|
|
66
|
+
async delete(key: string) {
|
|
67
|
+
objects.delete(key)
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
} as unknown as PortalEnv
|
|
71
|
+
return { env, manifestRows, objects }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('processUpload', () => {
|
|
75
|
+
it('writes the object under the owner prefix and exactly one manifest row', async () => {
|
|
76
|
+
const { env, manifestRows, objects } = makeEnv()
|
|
77
|
+
const lines: string[] = []
|
|
78
|
+
const res = await processUpload(
|
|
79
|
+
'sess-a',
|
|
80
|
+
'invoice.pdf',
|
|
81
|
+
new Uint8Array([1, 2, 3]),
|
|
82
|
+
env,
|
|
83
|
+
(l) => lines.push(l),
|
|
84
|
+
() => 'up-1',
|
|
85
|
+
() => 1_000_000,
|
|
86
|
+
)
|
|
87
|
+
expect(res.status).toBe(200)
|
|
88
|
+
expect([...objects.keys()]).toEqual(['alice/invoice.pdf'])
|
|
89
|
+
expect(manifestRows.length).toBe(1)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('writes the manifest row with ingested=0', async () => {
|
|
93
|
+
const { env, manifestRows } = makeEnv()
|
|
94
|
+
await processUpload(
|
|
95
|
+
'sess-a',
|
|
96
|
+
'invoice.pdf',
|
|
97
|
+
new Uint8Array([1]),
|
|
98
|
+
env,
|
|
99
|
+
() => {},
|
|
100
|
+
() => 'up-1',
|
|
101
|
+
() => 1_000_000,
|
|
102
|
+
)
|
|
103
|
+
// Column order: fileId, ownerId, filename, objectKey, size, uploadedAt, ingested
|
|
104
|
+
expect(manifestRows[0][6]).toBe(0)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('emits the full lifeline correlated by uploadId, ending in a verified post-condition', async () => {
|
|
108
|
+
const { env } = makeEnv()
|
|
109
|
+
const lines: string[] = []
|
|
110
|
+
await processUpload(
|
|
111
|
+
'sess-a',
|
|
112
|
+
'invoice.pdf',
|
|
113
|
+
new Uint8Array([1]),
|
|
114
|
+
env,
|
|
115
|
+
(l) => lines.push(l),
|
|
116
|
+
() => 'up-1',
|
|
117
|
+
() => 1_000_000,
|
|
118
|
+
)
|
|
119
|
+
const joined = lines.join('\n')
|
|
120
|
+
expect(joined).toContain('op=request uploadId=up-1')
|
|
121
|
+
expect(joined).toContain('op=r2-put uploadId=up-1')
|
|
122
|
+
expect(joined).toContain('op=manifest-write uploadId=up-1')
|
|
123
|
+
expect(joined).toMatch(/op=complete uploadId=up-1 .*objectPresent=true rowPresent=true/)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('denies an unauthenticated caller and writes nothing', async () => {
|
|
127
|
+
const { env, manifestRows, objects } = makeEnv()
|
|
128
|
+
const res = await processUpload(
|
|
129
|
+
'',
|
|
130
|
+
'invoice.pdf',
|
|
131
|
+
new Uint8Array([1]),
|
|
132
|
+
env,
|
|
133
|
+
() => {},
|
|
134
|
+
() => 'up-1',
|
|
135
|
+
() => 1_000_000,
|
|
136
|
+
)
|
|
137
|
+
expect(res.status).toBe(401)
|
|
138
|
+
expect(objects.size).toBe(0)
|
|
139
|
+
expect(manifestRows.length).toBe(0)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('rejects a filename that escapes the owner prefix, and writes nothing', async () => {
|
|
143
|
+
const { env, objects, manifestRows } = makeEnv()
|
|
144
|
+
const res = await processUpload(
|
|
145
|
+
'sess-a',
|
|
146
|
+
'../bob/evil.pdf',
|
|
147
|
+
new Uint8Array([1]),
|
|
148
|
+
env,
|
|
149
|
+
() => {},
|
|
150
|
+
() => 'up-1',
|
|
151
|
+
() => 1_000_000,
|
|
152
|
+
)
|
|
153
|
+
expect(res.status).toBe(400)
|
|
154
|
+
expect(objects.size).toBe(0)
|
|
155
|
+
expect(manifestRows.length).toBe(0)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('rejects an empty filename', async () => {
|
|
159
|
+
const { env } = makeEnv()
|
|
160
|
+
expect(
|
|
161
|
+
(
|
|
162
|
+
await processUpload(
|
|
163
|
+
'sess-a',
|
|
164
|
+
'',
|
|
165
|
+
new Uint8Array([1]),
|
|
166
|
+
env,
|
|
167
|
+
() => {},
|
|
168
|
+
() => 'up-1',
|
|
169
|
+
() => 1_000_000,
|
|
170
|
+
)
|
|
171
|
+
).status,
|
|
172
|
+
).toBe(400)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('caps the upload size and says so, rather than OOMing the isolate silently', async () => {
|
|
176
|
+
const { env, objects, manifestRows } = makeEnv()
|
|
177
|
+
const lines: string[] = []
|
|
178
|
+
const tooBig = new Uint8Array(25 * 1024 * 1024 + 1)
|
|
179
|
+
const res = await processUpload(
|
|
180
|
+
'sess-a',
|
|
181
|
+
'huge.bin',
|
|
182
|
+
tooBig,
|
|
183
|
+
env,
|
|
184
|
+
(l) => lines.push(l),
|
|
185
|
+
() => 'up-1',
|
|
186
|
+
() => 1_000_000,
|
|
187
|
+
)
|
|
188
|
+
expect(res.status).toBe(413)
|
|
189
|
+
expect(objects.size).toBe(0)
|
|
190
|
+
expect(manifestRows.length).toBe(0)
|
|
191
|
+
expect(lines.join('\n')).toContain('result=too-large')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('measures elapsed time rather than printing a fabricated zero', async () => {
|
|
195
|
+
const { env } = makeEnv()
|
|
196
|
+
const lines: string[] = []
|
|
197
|
+
let t = 1_000_000
|
|
198
|
+
await processUpload(
|
|
199
|
+
'sess-a',
|
|
200
|
+
'invoice.pdf',
|
|
201
|
+
new Uint8Array([1]),
|
|
202
|
+
env,
|
|
203
|
+
(l) => lines.push(l),
|
|
204
|
+
() => 'up-1',
|
|
205
|
+
() => (t += 5),
|
|
206
|
+
)
|
|
207
|
+
// A hardcoded ms=0 reads to an operator as "took no time", not "was never
|
|
208
|
+
// measured". With a real clock the value moves.
|
|
209
|
+
expect(lines.join('\n')).toMatch(/op=complete uploadId=up-1 ms=([1-9]\d*)/)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
// R2 overwrites an existing key unconditionally, so a re-upload of the same
|
|
213
|
+
// filename REPLACES the object whatever the manifest does. The manifest must
|
|
214
|
+
// therefore replace its row too, or it ends up describing a file that no
|
|
215
|
+
// longer exists. Re-sending a corrected invoice.pdf is the single most
|
|
216
|
+
// ordinary thing this portal will ever be asked to do.
|
|
217
|
+
describe('re-uploading the same filename', () => {
|
|
218
|
+
it('replaces the object, keeps exactly one row, and succeeds', async () => {
|
|
219
|
+
const { env, manifestRows, objects } = makeEnv()
|
|
220
|
+
await processUpload('sess-a', 'invoice.pdf', new Uint8Array([1]), env, () => {}, () => 'up-1', () => 1_000)
|
|
221
|
+
const second = await processUpload(
|
|
222
|
+
'sess-a',
|
|
223
|
+
'invoice.pdf',
|
|
224
|
+
new Uint8Array([9, 9, 9]),
|
|
225
|
+
env,
|
|
226
|
+
() => {},
|
|
227
|
+
() => 'up-2',
|
|
228
|
+
() => 2_000,
|
|
229
|
+
)
|
|
230
|
+
expect(second.status).toBe(200)
|
|
231
|
+
expect(manifestRows.length).toBe(1)
|
|
232
|
+
expect(objects.get('alice/invoice.pdf')?.length).toBe(3)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it('updates the row to describe the new object, not the old one', async () => {
|
|
236
|
+
const { env, manifestRows } = makeEnv()
|
|
237
|
+
await processUpload('sess-a', 'invoice.pdf', new Uint8Array([1]), env, () => {}, () => 'up-1', () => 1_000)
|
|
238
|
+
await processUpload('sess-a', 'invoice.pdf', new Uint8Array([9, 9, 9]), env, () => {}, () => 'up-2', () => 2_000)
|
|
239
|
+
// Column order: fileId, ownerId, filename, objectKey, size, uploadedAt, ingested
|
|
240
|
+
expect(manifestRows[0][4]).toBe(3)
|
|
241
|
+
expect(manifestRows[0][5]).toBe(new Date(2_000).toISOString())
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
it('re-arms ingestion by resetting ingested to 0', async () => {
|
|
245
|
+
// The load-bearing part. If the first file was already swept to
|
|
246
|
+
// ingested=1, a replacement that left the flag set would never be
|
|
247
|
+
// ingested, and nothing would ever say so.
|
|
248
|
+
const { env, manifestRows } = makeEnv()
|
|
249
|
+
await processUpload('sess-a', 'invoice.pdf', new Uint8Array([1]), env, () => {}, () => 'up-1', () => 1_000)
|
|
250
|
+
manifestRows[0][6] = 1 // the device-side sweep marks it ingested
|
|
251
|
+
await processUpload('sess-a', 'invoice.pdf', new Uint8Array([9, 9, 9]), env, () => {}, () => 'up-2', () => 2_000)
|
|
252
|
+
expect(manifestRows[0][6]).toBe(0)
|
|
253
|
+
})
|
|
254
|
+
})
|
|
255
|
+
})
|
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"start": "node dist/index.js"
|
|
9
|
+
"start": "node dist/index.js",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"typecheck:templates": "tsc --noEmit --strict --target es2022 --module esnext --moduleResolution bundler --lib es2022,dom ../skills/*/template/functions/api/*.ts ../skills/*/template/functions/api/_lib/*.ts"
|
|
10
12
|
},
|
|
11
13
|
"dependencies": {
|
|
12
14
|
"@modelcontextprotocol/sdk": "^1.12.1"
|
|
13
15
|
},
|
|
14
16
|
"devDependencies": {
|
|
15
17
|
"@types/node": "^22.0.0",
|
|
16
|
-
"typescript": "^5.7.0"
|
|
18
|
+
"typescript": "^5.7.0",
|
|
19
|
+
"vitest": "^4.1.10"
|
|
17
20
|
}
|
|
18
21
|
}
|
|
@@ -37,3 +37,49 @@ When a bucket is created or reused, log the establishment decision so a reuse is
|
|
|
37
37
|
```
|
|
38
38
|
op=storage-provision resource=r2 purpose=<purpose> account=<accountId> name=<canonical-name> action=create|reuse
|
|
39
39
|
```
|
|
40
|
+
|
|
41
|
+
## Bind the bucket in wrangler.toml
|
|
42
|
+
|
|
43
|
+
The binding name (`BUCKET` below) is the variable the Pages Function reads off `env`. Keep `wrangler.toml` in the site's project root; it carries no secret, only the bucket's name.
|
|
44
|
+
|
|
45
|
+
```toml
|
|
46
|
+
name = "<project>"
|
|
47
|
+
pages_build_output_dir = "public"
|
|
48
|
+
|
|
49
|
+
[[r2_buckets]]
|
|
50
|
+
binding = "BUCKET"
|
|
51
|
+
bucket_name = "<accountId>-<purpose>" # from bin/cf-store-name.sh
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**R2 binds by name and has no `database_id` analogue.** That is the one asymmetry with the `[[d1_databases]]` block in `d1-data-capture.md` § Bind the database, where a `database_id` from `wrangler d1 create` must also be recorded. An R2 bucket needs no id, so there is no second value to carry into the tree.
|
|
55
|
+
|
|
56
|
+
The deploy-time token needs **no** R2 scope: the binding is declarative config carried in the deploy payload, covered by Pages Write. `wrangler r2 bucket create` **does** need `Workers R2 Storage Write` — the `storage` scope from `bin/cf-token.sh`. Reaching for an R2 token at deploy time is a sign the binding is being confused with the provision.
|
|
57
|
+
|
|
58
|
+
## The Pages Function
|
|
59
|
+
|
|
60
|
+
The bound bucket arrives on `env` and exposes `put` / `get` / `delete` / `head`. Keep the handler a thin wrapper over a pure function taking the env injected, so the logic is verifiable without the Pages runtime — the shape `d1-data-capture.md` § The Pages Function prescribes for D1.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
interface R2Bucket {
|
|
64
|
+
put: (key: string, value: ArrayBuffer | Uint8Array) => Promise<unknown>
|
|
65
|
+
get: (key: string) => Promise<{ body: ReadableStream | null; size: number } | null>
|
|
66
|
+
delete: (key: string) => Promise<void>
|
|
67
|
+
head: (key: string) => Promise<{ size: number } | null>
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function storeObject(
|
|
71
|
+
env: { BUCKET: R2Bucket },
|
|
72
|
+
key: string,
|
|
73
|
+
bytes: Uint8Array,
|
|
74
|
+
log: (line: string) => void,
|
|
75
|
+
): Promise<boolean> {
|
|
76
|
+
await env.BUCKET.put(key, bytes)
|
|
77
|
+
// Re-read rather than assume: a put that stored nothing emits no error, and
|
|
78
|
+
// an object with no manifest row is invisible to whatever later reads it.
|
|
79
|
+
const present = (await env.BUCKET.head(key)) !== null
|
|
80
|
+
log(`[r2-store] op=put key=${key} present=${present}`)
|
|
81
|
+
return present
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Declaring the interface structurally, rather than importing `@cloudflare/workers-types`, is what lets a test inject a fake bucket. No Workers-specific test tooling ships in this repo and none is needed for a Function shaped this way.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: data-portal
|
|
3
|
+
description: Stand up a per-account gated file-drop portal on its own custom domain, where named people each sign in with their own passcode and can upload, list, download and delete only their own files. Files land in one R2 bucket under a per-person prefix and every upload writes a D1 manifest row the agent later ingests. Use when an account needs somewhere for named people to drop photos, invoices or spreadsheets — "set up a file drop for <client>", "somewhere for <person> to send us their invoices", "a place clients can upload documents".
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Data portal
|
|
7
|
+
|
|
8
|
+
Assemble and deploy one account's file-drop portal. This is an outcome contract, not a copy of the
|
|
9
|
+
Pages mechanics. The deploy, custom-domain attach, token discipline, and live done-gate are owned by
|
|
10
|
+
`site-deploy`; the R2 store rules are owned by `references/r2-object-storage.md`. Read those
|
|
11
|
+
references with `plugin-read` before issuing their commands.
|
|
12
|
+
|
|
13
|
+
## What this skill produces, and what it does not
|
|
14
|
+
|
|
15
|
+
It produces a Pages project on a custom domain, an R2 bucket holding one prefix per enrolled person,
|
|
16
|
+
and a D1 database holding the manifest, the enrolment records, and the sessions. It does not ingest
|
|
17
|
+
the dropped files: a separate device-side sweep reads un-ingested manifest rows and pulls them into
|
|
18
|
+
the graph. This skill delivers the manifest that sweep consumes.
|
|
19
|
+
|
|
20
|
+
## Why the portal is on Pages and not the install
|
|
21
|
+
|
|
22
|
+
Uploads must keep succeeding while the device is offline, which is the same reason the booking page
|
|
23
|
+
went to Pages. The install's magic-link gate cannot be reused: it gates `/api/chat` on the install
|
|
24
|
+
and its sessions live in memory, so it does not cross origins.
|
|
25
|
+
|
|
26
|
+
## Provisioning is two sessions, and the order matters
|
|
27
|
+
|
|
28
|
+
The storage broker registers a bucket to **whoever creates it**, and that ownership is set on create
|
|
29
|
+
and never reassigned. There is no delegation.
|
|
30
|
+
|
|
31
|
+
1. **In the client account's own session**, create the bucket with `storage-r2-bucket-create`, named
|
|
32
|
+
from `bin/cf-store-name.sh <accountId> <purpose>`. It registers to that account.
|
|
33
|
+
2. **In the admin session**, do everything else: the D1 database, the assembly, and the deploy.
|
|
34
|
+
|
|
35
|
+
If the admin creates the bucket instead, it registers to the house, and the client account's own
|
|
36
|
+
ingestion sweep is denied `cross-account` against its own files — permanently, because ownership is
|
|
37
|
+
never reassigned. The portal would appear to work while nothing on the client side could read what
|
|
38
|
+
it stored.
|
|
39
|
+
|
|
40
|
+
## Enrol the people first
|
|
41
|
+
|
|
42
|
+
Collect from the operator the portal's custom domain and the list of people, then write
|
|
43
|
+
`<accountDir>/data-portal.json` carrying `portalDomain`, `portalDbName`, `bucketName`, and a
|
|
44
|
+
`people` array of `{ name, ownerId, prefix }`.
|
|
45
|
+
|
|
46
|
+
For each person, generate a high-entropy passcode, print it to the operator **once**, and insert the
|
|
47
|
+
person into D1 with only a PBKDF2 salt and hash. **A passcode is never stored in plaintext** — not
|
|
48
|
+
in `data-portal.json`, not in a log line, nowhere on the device. The operator conveys it to the
|
|
49
|
+
person out of band. Rotating a passcode is a re-run of this step for that person; there is no
|
|
50
|
+
separate editor.
|
|
51
|
+
|
|
52
|
+
Rotation also revokes: a session records a digest of the person's passcode hash at sign-in, and the
|
|
53
|
+
session lookup requires it to still match, so re-running enrolment kills every live session for that
|
|
54
|
+
person immediately. There is no cleanup step to remember and no window to wait out — which is the
|
|
55
|
+
point, because a leaked passcode is exactly the case where a procedure you have to remember fails.
|
|
56
|
+
Deleting the person's row revokes their sessions the same way.
|
|
57
|
+
|
|
58
|
+
The `ownerId` must match `[a-z0-9-]+`. This is not cosmetic: the whole isolation rule is that a key
|
|
59
|
+
starts with `<ownerId>/`, so an `ownerId` containing a separator would let one person read another's
|
|
60
|
+
files. The gate enforces it too, but do not enrol a name it will reject.
|
|
61
|
+
|
|
62
|
+
## Assemble the canonical tree
|
|
63
|
+
|
|
64
|
+
Copy `template/` to `<accountDir>/pages/<project>/` and fill `wrangler.toml`'s `__PROJECT_NAME__`,
|
|
65
|
+
`__D1_DATABASE_NAME__`, `__D1_DATABASE_ID__`, and `__R2_BUCKET_NAME__`. **Leave no `__...__`
|
|
66
|
+
placeholder in the assembled tree.**
|
|
67
|
+
|
|
68
|
+
Name the D1 database with the same `bin/cf-store-name.sh` scheme as the bucket, and apply
|
|
69
|
+
`schema.sql` to the remote database. It is `IF NOT EXISTS` throughout, so re-applying is safe.
|
|
70
|
+
|
|
71
|
+
## Deploy through site-deploy
|
|
72
|
+
|
|
73
|
+
Hand the assembled tree to `cloudflare:site-deploy`. Both bindings — D1 and R2 — are declarative
|
|
74
|
+
config in `wrangler.toml`, carried in the deploy payload under the Pages scope; the deploy needs no
|
|
75
|
+
R2 token. Only the bucket *creation* in step 1 needs R2 write, and the broker holds that.
|
|
76
|
+
|
|
77
|
+
## The portal needs a paid Cloudflare plan
|
|
78
|
+
|
|
79
|
+
Check this before promising a date. Signing in costs one PBKDF2 derivation at the OWASP floor of
|
|
80
|
+
600,000 iterations, measured at roughly 250ms of CPU. Workers Free allows **10ms of CPU per
|
|
81
|
+
request**, so on a free plan authentication cannot succeed — and no OWASP-compliant setting fits
|
|
82
|
+
either, since even 100,000 iterations costs about 40ms. Paid plans allow far more and are fine.
|
|
83
|
+
|
|
84
|
+
The cost is tunable without a migration if it ever needs to be: `iterations` is stored per person
|
|
85
|
+
row, so it is the enrolment-time value that binds, not a constant baked into the deployed code.
|
|
86
|
+
|
|
87
|
+
## Done-gate
|
|
88
|
+
|
|
89
|
+
The portal is not done until all five hold on the live domain:
|
|
90
|
+
|
|
91
|
+
1. A cache-busted request to the portal domain returns `HTTP/2 200`.
|
|
92
|
+
2. Signing in with the right passcode returns `200` **within the CPU budget** rather than timing
|
|
93
|
+
out — this is the check that catches a free-plan deployment, and it fails nowhere else.
|
|
94
|
+
3. A wrong passcode returns `401`; the **sixth** rapid wrong passcode returns `429` (five are
|
|
95
|
+
allowed per fifteen minutes, per person, per IP).
|
|
96
|
+
4. An upload as one person returns `200`, and the object is present in R2 under that person's
|
|
97
|
+
prefix with exactly one manifest row at `ingested=0`. Re-uploading the same filename also
|
|
98
|
+
returns `200`, replaces the object, and leaves exactly one row, now back at `ingested=0`.
|
|
99
|
+
5. That person's browser cannot list, download, or delete another person's file.
|
|
100
|
+
|
|
101
|
+
A `502` on `/api/upload` with `op=r2-put result=failed` on every attempt is the expected signature of
|
|
102
|
+
an R2 binding that did not deploy — check `wrangler.toml` reached the tree with its placeholder
|
|
103
|
+
filled. An auth request that hangs or 500s while everything else works is the free-plan signature
|
|
104
|
+
above, not a code fault.
|
|
105
|
+
|
|
106
|
+
Uploads are capped at 25 MiB and a larger one returns `413` with `result=too-large`.
|
|
107
|
+
|
|
108
|
+
## Observability
|
|
109
|
+
|
|
110
|
+
Every step of an upload emits one `[data-portal]` line correlated by `uploadId`, ending in
|
|
111
|
+
`op=complete` with `objectPresent` and `rowPresent` **re-read** rather than assumed. Passcodes,
|
|
112
|
+
hashes, and object bodies are never logged.
|
|
113
|
+
|
|
114
|
+
Read one upload's full lifeline by filtering the brand journal on its `uploadId`.
|
|
115
|
+
|
|
116
|
+
The standing audit that would reconcile R2 objects against manifest rows is not built yet. Until it
|
|
117
|
+
is, an object with no manifest row and a row with no object are both invisible: neither emits an
|
|
118
|
+
event at request time.
|
|
119
|
+
|
|
120
|
+
## Tool discipline
|
|
121
|
+
|
|
122
|
+
Never write a token into the tree, never commit one, never echo one. The bucket name and database
|
|
123
|
+
name come from `bin/cf-store-name.sh`, never hand-built.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The isolation rule.
|
|
2
|
+
//
|
|
3
|
+
// Every file op authorizes owner-vs-key here BEFORE any R2 call. The
|
|
4
|
+
// per-person key prefix is a consequence of this check, never the check
|
|
5
|
+
// itself: a caller could otherwise reach another person's files by naming
|
|
6
|
+
// them, and prefix convention alone would not stop it.
|
|
7
|
+
|
|
8
|
+
const SEP = '/'
|
|
9
|
+
|
|
10
|
+
// The whole gate rests on an ownerId that cannot contain the separator. If two
|
|
11
|
+
// people were ever enrolled as 'alice' and 'alice/2024', then
|
|
12
|
+
// 'alice/2024/invoice.pdf' starts with 'alice/' and alice would read the other
|
|
13
|
+
// person's files. Enrolment is an agent writing rows, so this invariant cannot
|
|
14
|
+
// live only in the skill's prose — it is enforced here, where the decision is
|
|
15
|
+
// actually made, and it fails closed.
|
|
16
|
+
const OWNER_ID = /^[a-z0-9-]+$/
|
|
17
|
+
|
|
18
|
+
export function ownerPrefix(ownerId: string): string {
|
|
19
|
+
return `${ownerId}${SEP}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function authorizeKey(ownerId: string, key: string): boolean {
|
|
23
|
+
if (!ownerId || !key) return false
|
|
24
|
+
if (!OWNER_ID.test(ownerId)) return false
|
|
25
|
+
// Reject traversal and absolute forms outright rather than normalising them:
|
|
26
|
+
// a normaliser is one more thing that can be wrong, and no legitimate key
|
|
27
|
+
// contains these.
|
|
28
|
+
if (key.includes('..') || key.startsWith(SEP)) return false
|
|
29
|
+
// startsWith(ownerId) alone would authorize 'alice-evil/...' for 'alice'.
|
|
30
|
+
// The separator is part of the rule.
|
|
31
|
+
return key.startsWith(ownerPrefix(ownerId))
|
|
32
|
+
}
|
package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/log.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The log grammar is `[tag] op=<verb> key=value …`, space-separated. Any field
|
|
2
|
+
// carrying free text has to be quoted, because the values here are not ours:
|
|
3
|
+
// `ownerId` is unvalidated JSON off the wire at auth time, and `filename` comes
|
|
4
|
+
// straight from a multipart part name. Unquoted, a newline forges whole log
|
|
5
|
+
// lines and an embedded `result=ok` corrupts the line's parse — in a portal
|
|
6
|
+
// where the lifeline IS the diagnostic contract, and whose deferred reconcile
|
|
7
|
+
// audit is specified to read these lines back.
|
|
8
|
+
|
|
9
|
+
/** Render a free-text value as a quoted, single-line log field. */
|
|
10
|
+
export function q(value: string): string {
|
|
11
|
+
return `"${value.replace(/[\\"]/g, '\\$&').replace(/[\r\n\t]/g, ' ')}"`
|
|
12
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Passcode hashing for the data portal. PBKDF2-SHA256 via Web Crypto, which
|
|
2
|
+
// exists identically in the Workers runtime and in Node's webcrypto, so these
|
|
3
|
+
// functions are tested against real crypto rather than a fake.
|
|
4
|
+
//
|
|
5
|
+
// constantTimeEqual is hand-rolled deliberately: crypto.subtle.timingSafeEqual
|
|
6
|
+
// is a Cloudflare extension with no Node equivalent, so depending on it would
|
|
7
|
+
// make the comparison untestable here.
|
|
8
|
+
|
|
9
|
+
export const PBKDF2_ITERATIONS = 600_000
|
|
10
|
+
|
|
11
|
+
export function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
12
|
+
// Length is not secret; an early return on it leaks nothing a caller cannot
|
|
13
|
+
// already see. The byte loop below never short-circuits.
|
|
14
|
+
if (a.length !== b.length) return false
|
|
15
|
+
let diff = 0
|
|
16
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i]
|
|
17
|
+
return diff === 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function randomSaltHex(bytes: Uint8Array): string {
|
|
21
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Returns Uint8Array<ArrayBuffer>, not the bare Uint8Array: the bare form
|
|
25
|
+
// widens to ArrayBufferLike, which Web Crypto will not accept as a BufferSource
|
|
26
|
+
// because a SharedArrayBuffer could back it.
|
|
27
|
+
function hexToBytes(hex: string): Uint8Array<ArrayBuffer> | null {
|
|
28
|
+
if (hex.length === 0 || hex.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(hex)) return null
|
|
29
|
+
const out = new Uint8Array(hex.length / 2)
|
|
30
|
+
for (let i = 0; i < out.length; i++) out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
|
|
31
|
+
return out
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function hashPasscode(
|
|
35
|
+
passcode: string,
|
|
36
|
+
saltHex: string,
|
|
37
|
+
iterations: number,
|
|
38
|
+
): Promise<string> {
|
|
39
|
+
const salt = hexToBytes(saltHex)
|
|
40
|
+
if (!salt) throw new Error('salt must be hex')
|
|
41
|
+
const key = await crypto.subtle.importKey(
|
|
42
|
+
'raw',
|
|
43
|
+
new TextEncoder().encode(passcode),
|
|
44
|
+
'PBKDF2',
|
|
45
|
+
false,
|
|
46
|
+
['deriveBits'],
|
|
47
|
+
)
|
|
48
|
+
const bits = await crypto.subtle.deriveBits(
|
|
49
|
+
{ name: 'PBKDF2', salt, iterations, hash: 'SHA-256' },
|
|
50
|
+
key,
|
|
51
|
+
256,
|
|
52
|
+
)
|
|
53
|
+
return randomSaltHex(new Uint8Array(bits))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function verifyPasscode(
|
|
57
|
+
passcode: string,
|
|
58
|
+
saltHex: string,
|
|
59
|
+
expectedHashHex: string,
|
|
60
|
+
iterations: number,
|
|
61
|
+
): Promise<boolean> {
|
|
62
|
+
const expected = hexToBytes(expectedHashHex)
|
|
63
|
+
if (!expected) return false
|
|
64
|
+
let actualHex: string
|
|
65
|
+
try {
|
|
66
|
+
actualHex = await hashPasscode(passcode, saltHex, iterations)
|
|
67
|
+
} catch {
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
const actual = hexToBytes(actualHex)
|
|
71
|
+
if (!actual) return false
|
|
72
|
+
return constantTimeEqual(actual, expected)
|
|
73
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { D1Database } from './types'
|
|
2
|
+
|
|
3
|
+
// Fixed-window attempt counter in D1.
|
|
4
|
+
//
|
|
5
|
+
// Every rate limiter elsewhere in this platform is an in-memory Map in a
|
|
6
|
+
// long-lived Node process (ui/server/routes/client-error.ts). None of that
|
|
7
|
+
// ports here: Pages Functions are stateless and spread across edge locations,
|
|
8
|
+
// so the counter must live in storage the whole edge shares.
|
|
9
|
+
//
|
|
10
|
+
// D1 has no atomic increment, so two concurrent requests can read the same
|
|
11
|
+
// count and both be allowed. This is a speed bump against passcode guessing,
|
|
12
|
+
// not a hard limit, and the docs say so rather than implying a guarantee.
|
|
13
|
+
|
|
14
|
+
export const RATE_WINDOW_MS = 900_000 // 15 minutes
|
|
15
|
+
export const RATE_MAX_ATTEMPTS = 5
|
|
16
|
+
|
|
17
|
+
/** The counter's key. The client IP is part of it, not decoration: keyed on the
|
|
18
|
+
* owner alone, the limiter is a weapon rather than a shield — six
|
|
19
|
+
* unauthenticated requests naming a person lock that person out of their own
|
|
20
|
+
* portal for the window, from anywhere, repeatably. Keyed on owner+IP, an
|
|
21
|
+
* attacker throttles only themselves. The cost is that a distributed guesser
|
|
22
|
+
* gets RATE_MAX_ATTEMPTS per address, which the passcodes' entropy carries. */
|
|
23
|
+
export function rateScope(ownerId: string, clientIp: string): string {
|
|
24
|
+
return `${ownerId}|${clientIp}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Clear a scope's counter. Called on a successful sign-in, so a person doing
|
|
28
|
+
* ordinary work does not lock themselves out. */
|
|
29
|
+
export async function clearAttempts(db: D1Database, scope: string): Promise<void> {
|
|
30
|
+
await db.prepare('DELETE FROM auth_attempts WHERE scope = ?').bind(scope).run()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function checkAndRecordAttempt(
|
|
34
|
+
db: D1Database,
|
|
35
|
+
scope: string,
|
|
36
|
+
nowMs: number,
|
|
37
|
+
): Promise<{ allowed: boolean; count: number }> {
|
|
38
|
+
const windowStart = Math.floor(nowMs / RATE_WINDOW_MS) * RATE_WINDOW_MS
|
|
39
|
+
|
|
40
|
+
await db
|
|
41
|
+
.prepare(
|
|
42
|
+
`INSERT INTO auth_attempts (scope, windowStart, count) VALUES (?, ?, 1)
|
|
43
|
+
ON CONFLICT (scope, windowStart) DO UPDATE SET count = count + 1`,
|
|
44
|
+
)
|
|
45
|
+
.bind(scope, windowStart)
|
|
46
|
+
.run()
|
|
47
|
+
|
|
48
|
+
const row = await db
|
|
49
|
+
.prepare('SELECT count FROM auth_attempts WHERE scope = ? AND windowStart = ?')
|
|
50
|
+
.bind(scope, windowStart)
|
|
51
|
+
.first<{ count: number }>()
|
|
52
|
+
|
|
53
|
+
const count = typeof row?.count === 'number' ? row.count : 1
|
|
54
|
+
return { allowed: count <= RATE_MAX_ATTEMPTS, count }
|
|
55
|
+
}
|