@rubytech/create-maxy-code 0.1.561 → 0.1.563
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/plugins/admin/hooks/__tests__/datetime-inject.test.sh +10 -0
- package/payload/platform/plugins/admin/hooks/__tests__/mailbox-inject.test.sh +15 -2
- package/payload/platform/plugins/admin/hooks/__tests__/preference-consult-directive.test.sh +6 -0
- package/payload/platform/plugins/admin/hooks/__tests__/prompt-optimiser-directive.test.sh +10 -0
- package/payload/platform/plugins/admin/hooks/datetime-inject.sh +10 -0
- package/payload/platform/plugins/admin/hooks/mailbox-inject.sh +14 -0
- package/payload/platform/plugins/admin/hooks/preference-consult-directive.sh +9 -0
- package/payload/platform/plugins/admin/hooks/prompt-optimiser-directive.sh +10 -0
- package/payload/platform/plugins/admin/skills/platform-architecture/SKILL.md +21 -11
- package/payload/platform/plugins/admin/skills/whats-new/SKILL.md +11 -0
- package/payload/platform/plugins/cloudflare/bin/portal-enrol.mjs +19 -5
- package/payload/platform/plugins/cloudflare/mcp/__tests__/authorize.test.ts +6 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/portal-enrol.test.ts +77 -6
- package/payload/platform/plugins/cloudflare/mcp/__tests__/portal-session-grant.test.ts +39 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/portal-visit-status-write.test.ts +176 -0
- package/payload/platform/plugins/cloudflare/mcp/__tests__/portal-visits-scope.test.ts +112 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/SKILL.md +23 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/schema.sql +94 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/_lib/session.ts +31 -4
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/visit-status.ts +136 -0
- package/payload/platform/plugins/cloudflare/skills/data-portal/template/functions/api/visits.ts +93 -0
- package/payload/platform/plugins/docs/references/deployment.md +14 -8
- package/payload/platform/plugins/docs/references/internals.md +2 -0
- package/payload/platform/plugins/docs/references/platform.md +4 -2
- package/payload/platform/scripts/__tests__/public-prompt-gate-registered.test.sh +114 -0
- package/payload/platform/scripts/smoke-boot-services.sh +14 -0
- package/payload/platform/services/claude-session-manager/dist/http-server.d.ts.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/http-server.js +26 -10
- package/payload/platform/services/claude-session-manager/dist/http-server.js.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/index.js +16 -0
- package/payload/platform/services/claude-session-manager/dist/index.js.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/public-prompt-surface.d.ts +56 -0
- package/payload/platform/services/claude-session-manager/dist/public-prompt-surface.d.ts.map +1 -0
- package/payload/platform/services/claude-session-manager/dist/public-prompt-surface.js +155 -0
- package/payload/platform/services/claude-session-manager/dist/public-prompt-surface.js.map +1 -0
- package/payload/platform/services/claude-session-manager/dist/rc-public-lockdown.d.ts +6 -0
- package/payload/platform/services/claude-session-manager/dist/rc-public-lockdown.d.ts.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/rc-public-lockdown.js +1 -1
- package/payload/platform/services/claude-session-manager/dist/rc-public-lockdown.js.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/system-prompt.d.ts +4 -0
- package/payload/platform/services/claude-session-manager/dist/system-prompt.d.ts.map +1 -1
- package/payload/platform/services/claude-session-manager/dist/system-prompt.js +45 -27
- package/payload/platform/services/claude-session-manager/dist/system-prompt.js.map +1 -1
- package/payload/platform/templates/agents/public/IDENTITY.md +3 -3
- package/payload/server/maxy-edge.js +302 -33
- package/payload/server/public/activity.html +5 -5
- package/payload/server/public/agents.html +4 -4
- package/payload/server/public/assets/{AdminLoginScreens-DLogIsJT.js → AdminLoginScreens-C6e5jf0S.js} +1 -1
- package/payload/server/public/assets/AdminLoginScreens-C6e5jf0S.js.br +0 -0
- package/payload/server/public/assets/AdminLoginScreens-C6e5jf0S.js.gz +0 -0
- package/payload/server/public/assets/AdminShell-DStA0lfm.js +3 -0
- package/payload/server/public/assets/AdminShell-DStA0lfm.js.br +0 -0
- package/payload/server/public/assets/AdminShell-DStA0lfm.js.gz +0 -0
- package/payload/server/public/assets/{activity-AVJSFESV.js → activity--yyBcNfB.js} +1 -1
- package/payload/server/public/assets/activity--yyBcNfB.js.br +0 -0
- package/payload/server/public/assets/activity--yyBcNfB.js.gz +0 -0
- package/payload/server/public/assets/{admin-eFgbld42.js → admin-Dr6Jwe7W.js} +1 -1
- package/payload/server/public/assets/admin-Dr6Jwe7W.js.br +0 -0
- package/payload/server/public/assets/admin-Dr6Jwe7W.js.gz +0 -0
- package/payload/server/public/assets/{agents-CoWLZMf7.js → agents-odyVOb5E.js} +1 -1
- package/payload/server/public/assets/agents-odyVOb5E.js.br +0 -0
- package/payload/server/public/assets/agents-odyVOb5E.js.gz +0 -0
- package/payload/server/public/assets/{browser-C6K66_fw.js → browser-D1K_MoHt.js} +1 -1
- package/payload/server/public/assets/browser-D1K_MoHt.js.br +0 -0
- package/payload/server/public/assets/browser-D1K_MoHt.js.gz +0 -0
- package/payload/server/public/assets/{calendar-Dnw_cP4b.js → calendar-jqh9U7Ic.js} +1 -1
- package/payload/server/public/assets/calendar-jqh9U7Ic.js.br +0 -0
- package/payload/server/public/assets/calendar-jqh9U7Ic.js.gz +0 -0
- package/payload/server/public/assets/chat-QQHBdVor.js +1 -0
- package/payload/server/public/assets/chat-QQHBdVor.js.br +0 -0
- package/payload/server/public/assets/chat-QQHBdVor.js.gz +0 -0
- package/payload/server/public/assets/chevron-left-DwTXgKwr.js +1 -0
- package/payload/server/public/assets/clock-DOmh9iKX.js +1 -0
- package/payload/server/public/assets/clock-DOmh9iKX.js.br +0 -0
- package/payload/server/public/assets/{copy-kuNM0Etw.js → copy-DkuHwkaB.js} +1 -1
- package/payload/server/public/assets/copy-DkuHwkaB.js.br +0 -0
- package/payload/server/public/assets/copy-DkuHwkaB.js.gz +0 -0
- package/payload/server/public/assets/data-DYmTNqrb.js +1 -0
- package/payload/server/public/assets/data-DYmTNqrb.js.br +0 -0
- package/payload/server/public/assets/data-DYmTNqrb.js.gz +0 -0
- package/payload/server/public/assets/{file-text-BWkobrcx.js → file-text-C6o_NRjj.js} +1 -1
- package/payload/server/public/assets/file-text-C6o_NRjj.js.br +0 -0
- package/payload/server/public/assets/file-text-C6o_NRjj.js.gz +0 -0
- package/payload/server/public/assets/{graph-BGjTL5Vz.js → graph-BN30v5hJ.js} +1 -1
- package/payload/server/public/assets/graph-BN30v5hJ.js.br +0 -0
- package/payload/server/public/assets/graph-BN30v5hJ.js.gz +0 -0
- package/payload/server/public/assets/{graph-labels-Dvc5Kw_r.js → graph-labels-DHFyNXQt.js} +1 -1
- package/payload/server/public/assets/graph-labels-DHFyNXQt.js.br +0 -0
- package/payload/server/public/assets/graph-labels-DHFyNXQt.js.gz +0 -0
- package/payload/server/public/assets/{operator-CzBGv_he.js → operator-DwFYA4Fp.js} +1 -1
- package/payload/server/public/assets/operator-DwFYA4Fp.js.br +0 -0
- package/payload/server/public/assets/operator-DwFYA4Fp.js.gz +0 -0
- package/payload/server/public/assets/{page-DaySqamH.js → page-BALVRssF.js} +1 -1
- package/payload/server/public/assets/page-BALVRssF.js.br +0 -0
- package/payload/server/public/assets/page-BALVRssF.js.gz +0 -0
- package/payload/server/public/assets/{page-BIY4Ku8Q.js → page-CEWIP3VF.js} +1 -1
- package/payload/server/public/assets/page-CEWIP3VF.js.br +0 -0
- package/payload/server/public/assets/page-CEWIP3VF.js.gz +0 -0
- package/payload/server/public/assets/play-Bg-BY_oj.js +1 -0
- package/payload/server/public/assets/play-Bg-BY_oj.js.br +0 -0
- package/payload/server/public/assets/play-Bg-BY_oj.js.gz +0 -0
- package/payload/server/public/assets/{public-pwDSvhBv.js → public-Ceb0n7Km.js} +1 -1
- package/payload/server/public/assets/public-Ceb0n7Km.js.br +0 -0
- package/payload/server/public/assets/public-Ceb0n7Km.js.gz +0 -0
- package/payload/server/public/assets/{rotate-ccw-B6s7tkEt.js → rotate-ccw-CDYdx91V.js} +1 -1
- package/payload/server/public/assets/rotate-ccw-CDYdx91V.js.gz +0 -0
- package/payload/server/public/assets/{routines-CFlyRdYg.js → routines-DwNTih0y.js} +1 -1
- package/payload/server/public/assets/routines-DwNTih0y.js.br +0 -0
- package/payload/server/public/assets/routines-DwNTih0y.js.gz +0 -0
- package/payload/server/public/assets/{skills-C45kfL_Q.js → skills-TIDCdOLQ.js} +1 -1
- package/payload/server/public/assets/skills-TIDCdOLQ.js.br +0 -0
- package/payload/server/public/assets/skills-TIDCdOLQ.js.gz +0 -0
- package/payload/server/public/assets/{tasks-BwoCTCu5.js → tasks-eTXvaWnd.js} +1 -1
- package/payload/server/public/assets/tasks-eTXvaWnd.js.br +0 -0
- package/payload/server/public/assets/tasks-eTXvaWnd.js.gz +0 -0
- package/payload/server/public/assets/{triangle-alert-B3bthSzx.js → triangle-alert-JwLSrj17.js} +1 -1
- package/payload/server/public/assets/triangle-alert-JwLSrj17.js.br +0 -0
- package/payload/server/public/assets/triangle-alert-JwLSrj17.js.gz +0 -0
- package/payload/server/public/assets/{useCopyFeedback-BzJAkFEb.js → useCopyFeedback-DjTkNw2p.js} +1 -1
- package/payload/server/public/assets/useCopyFeedback-DjTkNw2p.js.br +0 -0
- package/payload/server/public/assets/useCopyFeedback-DjTkNw2p.js.gz +0 -0
- package/payload/server/public/assets/useMediaQuery-BsBzbWaf.css +1 -0
- package/payload/server/public/assets/useMediaQuery-BsBzbWaf.css.br +0 -0
- package/payload/server/public/assets/useMediaQuery-BsBzbWaf.css.gz +0 -0
- package/payload/server/public/assets/useMediaQuery-D8VDA0RN.js +14 -0
- package/payload/server/public/assets/useMediaQuery-D8VDA0RN.js.br +0 -0
- package/payload/server/public/assets/useMediaQuery-D8VDA0RN.js.gz +0 -0
- package/payload/server/public/assets/{useVoiceRecorder-_qI_BAOo.js → useVoiceRecorder-B8vxObZG.js} +1 -1
- package/payload/server/public/assets/useVoiceRecorder-B8vxObZG.js.br +0 -0
- package/payload/server/public/assets/useVoiceRecorder-B8vxObZG.js.gz +0 -0
- package/payload/server/public/assets/{wrench-Bqux9jaw.js → wrench-DutUiRu7.js} +1 -1
- package/payload/server/public/assets/wrench-DutUiRu7.js.br +0 -0
- package/payload/server/public/assets/wrench-DutUiRu7.js.gz +0 -0
- package/payload/server/public/browser.html +4 -4
- package/payload/server/public/calendar.html +7 -7
- package/payload/server/public/chat.html +13 -13
- package/payload/server/public/data.html +11 -11
- package/payload/server/public/graph.html +9 -9
- package/payload/server/public/index.html +13 -13
- package/payload/server/public/operator.html +14 -14
- package/payload/server/public/public.html +13 -13
- package/payload/server/public/routines.html +7 -7
- package/payload/server/public/skills.html +5 -5
- package/payload/server/public/tasks.html +6 -6
- package/payload/server/server.js +1118 -636
- package/payload/server/public/assets/AdminLoginScreens-DLogIsJT.js.br +0 -0
- package/payload/server/public/assets/AdminLoginScreens-DLogIsJT.js.gz +0 -0
- package/payload/server/public/assets/AdminShell-B67tPTyE.js +0 -2
- package/payload/server/public/assets/AdminShell-B67tPTyE.js.br +0 -0
- package/payload/server/public/assets/AdminShell-B67tPTyE.js.gz +0 -0
- package/payload/server/public/assets/activity-AVJSFESV.js.br +0 -0
- package/payload/server/public/assets/activity-AVJSFESV.js.gz +0 -0
- package/payload/server/public/assets/admin-eFgbld42.js.br +0 -0
- package/payload/server/public/assets/admin-eFgbld42.js.gz +0 -0
- package/payload/server/public/assets/agents-CoWLZMf7.js.br +0 -0
- package/payload/server/public/assets/agents-CoWLZMf7.js.gz +0 -0
- package/payload/server/public/assets/browser-C6K66_fw.js.br +0 -0
- package/payload/server/public/assets/browser-C6K66_fw.js.gz +0 -0
- package/payload/server/public/assets/calendar-Dnw_cP4b.js.br +0 -0
- package/payload/server/public/assets/calendar-Dnw_cP4b.js.gz +0 -0
- package/payload/server/public/assets/chat-Cm0fkpg1.js +0 -1
- package/payload/server/public/assets/chat-Cm0fkpg1.js.br +0 -0
- package/payload/server/public/assets/chat-Cm0fkpg1.js.gz +0 -0
- package/payload/server/public/assets/chevron-left-CjRH-BtF.js +0 -1
- package/payload/server/public/assets/chevron-left-CjRH-BtF.js.br +0 -0
- package/payload/server/public/assets/clock-wbYEt1kM.js +0 -1
- package/payload/server/public/assets/clock-wbYEt1kM.js.br +0 -0
- package/payload/server/public/assets/clock-wbYEt1kM.js.gz +0 -0
- package/payload/server/public/assets/copy-kuNM0Etw.js.br +0 -0
- package/payload/server/public/assets/copy-kuNM0Etw.js.gz +0 -0
- package/payload/server/public/assets/data-c47T2NCd.js +0 -1
- package/payload/server/public/assets/data-c47T2NCd.js.br +0 -1
- package/payload/server/public/assets/data-c47T2NCd.js.gz +0 -0
- package/payload/server/public/assets/file-text-BWkobrcx.js.br +0 -0
- package/payload/server/public/assets/file-text-BWkobrcx.js.gz +0 -0
- package/payload/server/public/assets/graph-BGjTL5Vz.js.br +0 -0
- package/payload/server/public/assets/graph-BGjTL5Vz.js.gz +0 -0
- package/payload/server/public/assets/graph-labels-Dvc5Kw_r.js.br +0 -0
- package/payload/server/public/assets/graph-labels-Dvc5Kw_r.js.gz +0 -0
- package/payload/server/public/assets/operator-CzBGv_he.js.br +0 -0
- package/payload/server/public/assets/operator-CzBGv_he.js.gz +0 -0
- package/payload/server/public/assets/page-BIY4Ku8Q.js.br +0 -0
- package/payload/server/public/assets/page-BIY4Ku8Q.js.gz +0 -0
- package/payload/server/public/assets/page-DaySqamH.js.br +0 -0
- package/payload/server/public/assets/page-DaySqamH.js.gz +0 -0
- package/payload/server/public/assets/play-C8RA52_i.js +0 -1
- package/payload/server/public/assets/play-C8RA52_i.js.br +0 -0
- package/payload/server/public/assets/play-C8RA52_i.js.gz +0 -0
- package/payload/server/public/assets/public-pwDSvhBv.js.br +0 -0
- package/payload/server/public/assets/public-pwDSvhBv.js.gz +0 -0
- package/payload/server/public/assets/rotate-ccw-B6s7tkEt.js.br +0 -0
- package/payload/server/public/assets/rotate-ccw-B6s7tkEt.js.gz +0 -0
- package/payload/server/public/assets/routines-CFlyRdYg.js.br +0 -0
- package/payload/server/public/assets/routines-CFlyRdYg.js.gz +0 -0
- package/payload/server/public/assets/skills-C45kfL_Q.js.br +0 -0
- package/payload/server/public/assets/skills-C45kfL_Q.js.gz +0 -0
- package/payload/server/public/assets/tasks-BwoCTCu5.js.br +0 -0
- package/payload/server/public/assets/tasks-BwoCTCu5.js.gz +0 -0
- package/payload/server/public/assets/triangle-alert-B3bthSzx.js.br +0 -0
- package/payload/server/public/assets/triangle-alert-B3bthSzx.js.gz +0 -0
- package/payload/server/public/assets/useCopyFeedback-BzJAkFEb.js.br +0 -0
- package/payload/server/public/assets/useCopyFeedback-BzJAkFEb.js.gz +0 -0
- package/payload/server/public/assets/useMediaQuery-DJ6XeNN7.js +0 -14
- package/payload/server/public/assets/useMediaQuery-DJ6XeNN7.js.br +0 -0
- package/payload/server/public/assets/useMediaQuery-DJ6XeNN7.js.gz +0 -0
- package/payload/server/public/assets/useMediaQuery-DW_xRrMl.css +0 -1
- package/payload/server/public/assets/useMediaQuery-DW_xRrMl.css.br +0 -0
- package/payload/server/public/assets/useMediaQuery-DW_xRrMl.css.gz +0 -0
- package/payload/server/public/assets/useVoiceRecorder-_qI_BAOo.js.br +0 -0
- package/payload/server/public/assets/useVoiceRecorder-_qI_BAOo.js.gz +0 -0
- package/payload/server/public/assets/wrench-Bqux9jaw.js.br +0 -0
- package/payload/server/public/assets/wrench-Bqux9jaw.js.gz +0 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
MAX_STATUS_LENGTH,
|
|
4
|
+
processVisitStatus,
|
|
5
|
+
} from '../../skills/data-portal/template/functions/api/visit-status'
|
|
6
|
+
import type {
|
|
7
|
+
D1Database,
|
|
8
|
+
PortalEnv,
|
|
9
|
+
} from '../../skills/data-portal/template/functions/api/_lib/types'
|
|
10
|
+
|
|
11
|
+
interface Row {
|
|
12
|
+
accountId: string
|
|
13
|
+
ownerId: string
|
|
14
|
+
visitId: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A fake that HONOURS the INSERT ... SELECT, because the gate IS the SELECT.
|
|
19
|
+
*
|
|
20
|
+
* It applies the same account and owner filters the statement names and reports
|
|
21
|
+
* `meta.changes` as the number of rows the SELECT admitted. A fake that always
|
|
22
|
+
* reported one change would pass a handler with no gate at all.
|
|
23
|
+
*/
|
|
24
|
+
function db(rows: Row[], person: { ownerId: string; recordScope: string; accountId: string }) {
|
|
25
|
+
const inserted: unknown[][] = []
|
|
26
|
+
const database = {
|
|
27
|
+
prepare(sql: string) {
|
|
28
|
+
let bound: unknown[] = []
|
|
29
|
+
const stmt = {
|
|
30
|
+
bind(...v: unknown[]) {
|
|
31
|
+
bound = v
|
|
32
|
+
return stmt
|
|
33
|
+
},
|
|
34
|
+
async first<T>() {
|
|
35
|
+
return person as unknown as T
|
|
36
|
+
},
|
|
37
|
+
async run() {
|
|
38
|
+
if (!sql.startsWith('INSERT INTO visit_status')) return { meta: { changes: 0 } }
|
|
39
|
+
// Bind order matches the statement: the SELECT's literals first, then
|
|
40
|
+
// the WHERE's account, the optional owner, and the visitId last.
|
|
41
|
+
const acct = bound[0] as string
|
|
42
|
+
const visitId = bound[bound.length - 1] as string
|
|
43
|
+
const owner = bound[1] as string
|
|
44
|
+
let admitted = rows.filter((r) => r.accountId === acct && r.visitId === visitId)
|
|
45
|
+
if (sql.includes('AND v.ownerId = ?')) admitted = admitted.filter((r) => r.ownerId === owner)
|
|
46
|
+
if (admitted.length) inserted.push(bound)
|
|
47
|
+
return { meta: { changes: admitted.length } }
|
|
48
|
+
},
|
|
49
|
+
async all<T>() {
|
|
50
|
+
return { results: [] as T[] }
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
return stmt
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
return { database: database as unknown as D1Database, inserted }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const env = (d: D1Database): PortalEnv =>
|
|
60
|
+
({ DB: d, BUCKET: {}, PORTAL_ACCOUNT_ID: 'acc-a' }) as unknown as PortalEnv
|
|
61
|
+
|
|
62
|
+
const ROWS: Row[] = [
|
|
63
|
+
{ accountId: 'acc-a', ownerId: 'alice', visitId: 'v1' },
|
|
64
|
+
{ accountId: 'acc-a', ownerId: 'bob', visitId: 'v2' },
|
|
65
|
+
{ accountId: 'acc-a', ownerId: '', visitId: 'v3' },
|
|
66
|
+
{ accountId: 'acc-b', ownerId: 'alice', visitId: 'v4' },
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
const person = (ownerId: string, recordScope: string) => ({
|
|
70
|
+
ownerId,
|
|
71
|
+
recordScope,
|
|
72
|
+
accountId: 'acc-a',
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const body = (visitId: string, status = 'on-site') => ({ visitId, status })
|
|
76
|
+
|
|
77
|
+
describe('the visit-status write gate', () => {
|
|
78
|
+
it('accepts an own-scoped person setting status on their own visit', async () => {
|
|
79
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
80
|
+
const h = await processVisitStatus(body('v1'), 'sess', env(database), () => {}, 1)
|
|
81
|
+
expect(h.status).toBe(200)
|
|
82
|
+
expect(inserted).toHaveLength(1)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('refuses an own-scoped person setting status on someone else visit', async () => {
|
|
86
|
+
// A read leak shows someone a day that is not theirs. A write leak lets
|
|
87
|
+
// them change it, which is why the gate is the same one and is structural.
|
|
88
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
89
|
+
const h = await processVisitStatus(body('v2'), 'sess', env(database), () => {}, 1)
|
|
90
|
+
expect(h.status).toBe(404)
|
|
91
|
+
expect(inserted).toEqual([])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('refuses an own-scoped person setting status on an unbound visit', async () => {
|
|
95
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
96
|
+
const h = await processVisitStatus(body('v3'), 'sess', env(database), () => {}, 1)
|
|
97
|
+
expect(h.status).toBe(404)
|
|
98
|
+
expect(inserted).toEqual([])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('lets an account-scoped person set status on any visit for the account', async () => {
|
|
102
|
+
const { database, inserted } = db(ROWS, person('marie', 'account'))
|
|
103
|
+
const h = await processVisitStatus(body('v2'), 'sess', env(database), () => {}, 1)
|
|
104
|
+
expect(h.status).toBe(200)
|
|
105
|
+
expect(inserted).toHaveLength(1)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('refuses another account visit even at account scope', async () => {
|
|
109
|
+
const { database, inserted } = db(ROWS, person('marie', 'account'))
|
|
110
|
+
const h = await processVisitStatus(body('v4'), 'sess', env(database), () => {}, 1)
|
|
111
|
+
expect(h.status).toBe(404)
|
|
112
|
+
expect(inserted).toEqual([])
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('denies when the session does not resolve', async () => {
|
|
116
|
+
const d = {
|
|
117
|
+
prepare: () => ({ bind: () => ({ first: async () => null }) }),
|
|
118
|
+
} as unknown as D1Database
|
|
119
|
+
const h = await processVisitStatus(body('v1'), '', env(d), () => {}, 1)
|
|
120
|
+
expect(h.status).toBe(401)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('refuses a body with no visitId or no status', async () => {
|
|
124
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
125
|
+
const e = env(database)
|
|
126
|
+
expect((await processVisitStatus({ status: 'x' }, 'sess', e, () => {}, 1)).status).toBe(400)
|
|
127
|
+
expect((await processVisitStatus({ visitId: 'v1' }, 'sess', e, () => {}, 1)).status).toBe(400)
|
|
128
|
+
expect((await processVisitStatus(null, 'sess', e, () => {}, 1)).status).toBe(400)
|
|
129
|
+
expect(inserted).toEqual([])
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('refuses a status longer than the cap, rather than writing it into the graph', async () => {
|
|
133
|
+
// The value reaches :Visit.status through the pull. An unbounded free-text
|
|
134
|
+
// field written from the open web is refused at the door, not truncated:
|
|
135
|
+
// a truncated status is a different status, silently.
|
|
136
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
137
|
+
const h = await processVisitStatus(
|
|
138
|
+
body('v1', 'x'.repeat(MAX_STATUS_LENGTH + 1)),
|
|
139
|
+
'sess',
|
|
140
|
+
env(database),
|
|
141
|
+
() => {},
|
|
142
|
+
1,
|
|
143
|
+
)
|
|
144
|
+
expect(h.status).toBe(400)
|
|
145
|
+
expect(inserted).toEqual([])
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('stamps the setter own id, never one the request supplies', async () => {
|
|
149
|
+
const { database, inserted } = db(ROWS, person('alice', 'own'))
|
|
150
|
+
await processVisitStatus(
|
|
151
|
+
{ visitId: 'v1', status: 'on-site', ownerId: 'bob' },
|
|
152
|
+
'sess',
|
|
153
|
+
env(database),
|
|
154
|
+
() => {},
|
|
155
|
+
1,
|
|
156
|
+
)
|
|
157
|
+
expect(inserted[0]).toContain('alice')
|
|
158
|
+
expect(inserted[0]).not.toContain('bob')
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('logs the scope and the outcome, and no status text', async () => {
|
|
162
|
+
const lines: string[] = []
|
|
163
|
+
const { database } = db(ROWS, person('alice', 'own'))
|
|
164
|
+
await processVisitStatus(
|
|
165
|
+
body('v1', 'on-site-at-granary-court'),
|
|
166
|
+
'sess',
|
|
167
|
+
env(database),
|
|
168
|
+
(l) => lines.push(l),
|
|
169
|
+
1,
|
|
170
|
+
)
|
|
171
|
+
const joined = lines.join('\n')
|
|
172
|
+
expect(joined).toContain('scope=own')
|
|
173
|
+
expect(joined).toContain('result=ok')
|
|
174
|
+
expect(joined).not.toContain('granary')
|
|
175
|
+
})
|
|
176
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { processVisits } from '../../skills/data-portal/template/functions/api/visits'
|
|
3
|
+
import type {
|
|
4
|
+
D1Database,
|
|
5
|
+
PortalEnv,
|
|
6
|
+
} from '../../skills/data-portal/template/functions/api/_lib/types'
|
|
7
|
+
|
|
8
|
+
interface Row {
|
|
9
|
+
accountId: string
|
|
10
|
+
ownerId: string
|
|
11
|
+
visitId: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A fake that HONOURS the SQL it is handed rather than replaying a fixture.
|
|
16
|
+
*
|
|
17
|
+
* It reads the WHERE clause and the bound values and filters accordingly, so
|
|
18
|
+
* deleting the scope branch in the endpoint changes what comes back. A fake
|
|
19
|
+
* that replayed rows would pass whatever the gate did, which is the same defect
|
|
20
|
+
* shape as 1689's upload fake accepting duplicates the schema rejected.
|
|
21
|
+
*/
|
|
22
|
+
function db(rows: Row[], person: { ownerId: string; recordScope: string; accountId: string }) {
|
|
23
|
+
return {
|
|
24
|
+
prepare(sql: string) {
|
|
25
|
+
let bound: unknown[] = []
|
|
26
|
+
const stmt = {
|
|
27
|
+
bind(...v: unknown[]) {
|
|
28
|
+
bound = v
|
|
29
|
+
return stmt
|
|
30
|
+
},
|
|
31
|
+
async first<T>() {
|
|
32
|
+
return person as unknown as T
|
|
33
|
+
},
|
|
34
|
+
async run() {
|
|
35
|
+
return { meta: { changes: 0 } }
|
|
36
|
+
},
|
|
37
|
+
async all<T>() {
|
|
38
|
+
if (!sql.includes('FROM visits')) return { results: [] as T[] }
|
|
39
|
+
const [acct, owner] = bound as string[]
|
|
40
|
+
let out = rows.filter((r) => r.accountId === acct)
|
|
41
|
+
if (sql.includes('v.ownerId = ?')) out = out.filter((r) => r.ownerId === owner)
|
|
42
|
+
return { results: out as unknown as T[] }
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
return stmt
|
|
46
|
+
},
|
|
47
|
+
} as unknown as D1Database
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const env = (d: D1Database): PortalEnv =>
|
|
51
|
+
({ DB: d, BUCKET: {}, PORTAL_ACCOUNT_ID: 'acc-a' }) as unknown as PortalEnv
|
|
52
|
+
|
|
53
|
+
const ROWS: Row[] = [
|
|
54
|
+
{ accountId: 'acc-a', ownerId: 'alice', visitId: 'v1' },
|
|
55
|
+
{ accountId: 'acc-a', ownerId: 'bob', visitId: 'v2' },
|
|
56
|
+
{ accountId: 'acc-a', ownerId: '', visitId: 'v3' },
|
|
57
|
+
{ accountId: 'acc-b', ownerId: 'alice', visitId: 'v4' },
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
const ids = (h: { payload: Record<string, unknown> }) =>
|
|
61
|
+
(h.payload.visits as Row[]).map((v) => v.visitId).sort()
|
|
62
|
+
|
|
63
|
+
const asPerson = (ownerId: string, recordScope: string) => ({
|
|
64
|
+
ownerId,
|
|
65
|
+
recordScope,
|
|
66
|
+
accountId: 'acc-a',
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('the visits scope gate', () => {
|
|
70
|
+
it('shows an own-scoped person only their own visits', async () => {
|
|
71
|
+
const h = await processVisits('sess', env(db(ROWS, asPerson('alice', 'own'))), () => {}, 1)
|
|
72
|
+
expect(ids(h)).toEqual(['v1'])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('shows an account-scoped person every visit for the account', async () => {
|
|
76
|
+
const h = await processVisits('sess', env(db(ROWS, asPerson('marie', 'account'))), () => {}, 1)
|
|
77
|
+
expect(ids(h)).toEqual(['v1', 'v2', 'v3'])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('never returns another account rows, at either scope', async () => {
|
|
81
|
+
// The tenant filter is the baked-in var, not the session, and it is applied
|
|
82
|
+
// whichever scope the person holds. `v4` belongs to acc-b.
|
|
83
|
+
const own = await processVisits('sess', env(db(ROWS, asPerson('alice', 'own'))), () => {}, 1)
|
|
84
|
+
const acct = await processVisits('sess', env(db(ROWS, asPerson('marie', 'account'))), () => {}, 1)
|
|
85
|
+
expect(ids(own)).not.toContain('v4')
|
|
86
|
+
expect(ids(acct)).not.toContain('v4')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('hides an unbound visit from an own-scoped person', async () => {
|
|
90
|
+
// An unbound visit carries ownerId ''. Enrolment pins ownerId to
|
|
91
|
+
// ^[a-z0-9-]+$ (portal-enrol.mjs), so no enrolled person's id is ever '',
|
|
92
|
+
// and the own-scoped filter cannot match the unbound sentinel.
|
|
93
|
+
const h = await processVisits('sess', env(db(ROWS, asPerson('alice', 'own'))), () => {}, 1)
|
|
94
|
+
expect(ids(h)).not.toContain('v3')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('denies when the session does not resolve', async () => {
|
|
98
|
+
const d = {
|
|
99
|
+
prepare: () => ({ bind: () => ({ first: async () => null }) }),
|
|
100
|
+
} as unknown as D1Database
|
|
101
|
+
const h = await processVisits('', env(d), () => {}, 1)
|
|
102
|
+
expect(h.status).toBe(401)
|
|
103
|
+
expect(h.payload).toEqual({ ok: false, error: 'denied' })
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('logs the scope it applied, and no visit detail', async () => {
|
|
107
|
+
const lines: string[] = []
|
|
108
|
+
await processVisits('sess', env(db(ROWS, asPerson('alice', 'own'))), (l) => lines.push(l), 1)
|
|
109
|
+
expect(lines.join('\n')).toContain('scope=own')
|
|
110
|
+
expect(lines.join('\n')).toContain('visits=1')
|
|
111
|
+
})
|
|
112
|
+
})
|
|
@@ -90,6 +90,7 @@ For each person:
|
|
|
90
90
|
```bash
|
|
91
91
|
node platform/plugins/cloudflare/bin/portal-enrol.mjs --owner <ownerId> --name <name> \
|
|
92
92
|
--account <accountId> [--storage-model shared|dedicated] [--folders <a,b,c>] \
|
|
93
|
+
[--record-scope own|account] \
|
|
93
94
|
| npx wrangler d1 execute <portalDbName> --remote --file=-
|
|
94
95
|
```
|
|
95
96
|
|
|
@@ -124,6 +125,28 @@ the same upsert that rotates the passcode. The grant lives only in the portal's
|
|
|
124
125
|
below the account's exposed set and never widens past it: a portal compromise returns a person to the
|
|
125
126
|
account set, which the install re-derives and enforces regardless.
|
|
126
127
|
|
|
128
|
+
`--record-scope` is a different question from `--folders` and answers it for RECORDS rather than
|
|
129
|
+
files: `own` lets the person read only records carrying their own id, `account` lets them read every
|
|
130
|
+
record for the account. Two values and no third; a typo is refused rather than resolved to whichever
|
|
131
|
+
is nearer. Omitting it gives `own`, so nobody is granted account-wide visibility by leaving a flag
|
|
132
|
+
off. Re-enrolment replaces it, the same upsert that rotates the passcode, and that is the only way
|
|
133
|
+
to narrow a person back. The office person who schedules gets `account`; a field worker gets `own`.
|
|
134
|
+
|
|
135
|
+
The scope's only reader is `functions/api/visits.ts`, which the template ships. An account's own page
|
|
136
|
+
renders what that endpoint returns and never re-implements the filter — a gate written once per
|
|
137
|
+
client is a gate got wrong once per client.
|
|
138
|
+
|
|
139
|
+
**On a store that predates this column, run the `ALTER` once before enrolling anyone.** `IF NOT
|
|
140
|
+
EXISTS` adds no column to an existing table, so `schema.sql` will not do it for you and enrolment's
|
|
141
|
+
INSERT names the column, which means it fails loudly rather than writing a scopeless row:
|
|
142
|
+
|
|
143
|
+
```sql
|
|
144
|
+
ALTER TABLE people ADD COLUMN recordScope TEXT NOT NULL DEFAULT 'own';
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
No backfill follows it. Everyone already enrolled lands on the narrow scope, which is the intended
|
|
148
|
+
outcome, and widening one person is a re-run of enrolment for that person.
|
|
149
|
+
|
|
127
150
|
**A passcode is never stored in plaintext** — not in `data-portal.json`, not in a log line, nowhere
|
|
128
151
|
on the device. The operator conveys it to the person out of band. Rotating a passcode is a re-run of
|
|
129
152
|
the script for that person; there is no separate editor.
|
|
@@ -47,6 +47,22 @@
|
|
|
47
47
|
-- to a `people` table that predates it, and enrolment's INSERT names `folders`,
|
|
48
48
|
-- so it fails loudly on such a table. Add it by hand in that case:
|
|
49
49
|
-- ALTER TABLE people ADD COLUMN folders TEXT NOT NULL DEFAULT '';
|
|
50
|
+
-- `recordScope` is which RECORDS this person may read, as distinct from
|
|
51
|
+
-- `folders` above, which is which FILES they may browse. Two values and no
|
|
52
|
+
-- third: `own` sees only records carrying their ownerId, `account` sees every
|
|
53
|
+
-- record for the account. `functions/api/visits.ts` is its only reader, and it
|
|
54
|
+
-- is deliberately the only one — a gate written once per client is a gate got
|
|
55
|
+
-- wrong once per client.
|
|
56
|
+
--
|
|
57
|
+
-- The default is the NARROW value, and that is the whole of the migration
|
|
58
|
+
-- story. Like `accountId` and `folders`, IF NOT EXISTS will NOT add this column
|
|
59
|
+
-- to a `people` table that predates it, and enrolment's INSERT names it, so
|
|
60
|
+
-- such a table fails loudly. Add it by hand in that case:
|
|
61
|
+
-- ALTER TABLE people ADD COLUMN recordScope TEXT NOT NULL DEFAULT 'own';
|
|
62
|
+
-- Every person already enrolled then lands on the narrow scope. A default of
|
|
63
|
+
-- `account` would have handed all of them account-wide visibility by migration,
|
|
64
|
+
-- which is the one thing a scope column must never do.
|
|
65
|
+
--
|
|
50
66
|
-- The key is (accountId, ownerId), not ownerId alone. On the shared store two
|
|
51
67
|
-- clients may each enrol a person called `rob`, and a bare ownerId key would
|
|
52
68
|
-- reject the second enrolment outright. Worse, the enrolment upsert would treat
|
|
@@ -60,6 +76,7 @@ CREATE TABLE IF NOT EXISTS people (
|
|
|
60
76
|
createdAt TEXT NOT NULL,
|
|
61
77
|
accountId TEXT NOT NULL DEFAULT '',
|
|
62
78
|
folders TEXT NOT NULL DEFAULT '',
|
|
79
|
+
recordScope TEXT NOT NULL DEFAULT 'own',
|
|
63
80
|
PRIMARY KEY (accountId, ownerId)
|
|
64
81
|
);
|
|
65
82
|
|
|
@@ -228,3 +245,80 @@ CREATE TABLE IF NOT EXISTS directory_state (
|
|
|
228
245
|
accountId TEXT PRIMARY KEY,
|
|
229
246
|
currentGeneration INTEGER NOT NULL
|
|
230
247
|
);
|
|
248
|
+
|
|
249
|
+
-- One row per :Visit the device has published, per staging generation
|
|
250
|
+
-- (Task 2425). Metadata only, and only what the ontology already declares
|
|
251
|
+
-- (schema-construction.md): a job sheet's contents are files, which the
|
|
252
|
+
-- manifest above already carries.
|
|
253
|
+
--
|
|
254
|
+
-- READ THE ASYMMETRY WITH THE COLUMN NOTES ABOVE. Those warn that
|
|
255
|
+
-- IF NOT EXISTS adds no COLUMN to an existing table. It does create an absent
|
|
256
|
+
-- TABLE, so these three need no hand step on a database that predates them.
|
|
257
|
+
-- Stated because a reader who has just absorbed the column warning would
|
|
258
|
+
-- otherwise carry it across and go looking for a migration that is not needed.
|
|
259
|
+
--
|
|
260
|
+
-- `ownerId` is the enrolled portal person the visit is assigned to, resolved by
|
|
261
|
+
-- the push from an explicit `portalOwnerId` on the :Person carrying :Engineer.
|
|
262
|
+
-- It is '' when the engineer carries no such property. That row is NOT dropped:
|
|
263
|
+
-- it stays invisible to every own-scoped person, visible to account-scoped
|
|
264
|
+
-- ones, and counted by the audit as `unboundVisits`, so an unbound engineer is
|
|
265
|
+
-- found in the journal rather than by a worker opening an empty day.
|
|
266
|
+
--
|
|
267
|
+
-- `generation` and `visits_state` mirror `directory`/`directory_state` exactly,
|
|
268
|
+
-- and for the same reason: the push writes a whole generation, moves a pointer
|
|
269
|
+
-- in one forward-only statement, then sweeps what the pointer no longer names.
|
|
270
|
+
-- A listing that races a push reads the old set or the new one, never a
|
|
271
|
+
-- half-built one.
|
|
272
|
+
CREATE TABLE IF NOT EXISTS visits (
|
|
273
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
274
|
+
accountId TEXT NOT NULL,
|
|
275
|
+
visitId TEXT NOT NULL,
|
|
276
|
+
ownerId TEXT NOT NULL DEFAULT '',
|
|
277
|
+
jobId TEXT NOT NULL DEFAULT '',
|
|
278
|
+
status TEXT NOT NULL DEFAULT '',
|
|
279
|
+
startDate TEXT NOT NULL DEFAULT '',
|
|
280
|
+
purpose TEXT NOT NULL DEFAULT '',
|
|
281
|
+
generation INTEGER NOT NULL DEFAULT 0,
|
|
282
|
+
UNIQUE (accountId, visitId, generation)
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
CREATE INDEX IF NOT EXISTS visits_account ON visits (accountId);
|
|
286
|
+
-- The own-scoped read filters on both columns, so it carries its own index
|
|
287
|
+
-- rather than riding the account one.
|
|
288
|
+
CREATE INDEX IF NOT EXISTS visits_owner ON visits (accountId, ownerId);
|
|
289
|
+
|
|
290
|
+
-- Which generation of an account's visit rows is the live one. The flip of this
|
|
291
|
+
-- single row is the atomic point of a push, exactly as directory_state is.
|
|
292
|
+
-- An account with no row here has never completed one, and every reader joins
|
|
293
|
+
-- through this table, so it shows nothing rather than a staged generation that
|
|
294
|
+
-- no flip ever blessed.
|
|
295
|
+
CREATE TABLE IF NOT EXISTS visits_state (
|
|
296
|
+
accountId TEXT PRIMARY KEY,
|
|
297
|
+
currentGeneration INTEGER NOT NULL
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
-- The inbound half: a status a person set on the portal, waiting for the device
|
|
301
|
+
-- to carry it into the graph (Task 2425).
|
|
302
|
+
--
|
|
303
|
+
-- `claimed` is set only AFTER the graph write returns. Claiming first is
|
|
304
|
+
-- unrecoverable: if the write then failed, the row would read as delivered and
|
|
305
|
+
-- the person's edit would be gone with nothing saying so. Claiming after costs
|
|
306
|
+
-- a repeated MERGE on a lost response, which is idempotent.
|
|
307
|
+
--
|
|
308
|
+
-- A row whose visitId resolves to no :Visit in that account stays unclaimed and
|
|
309
|
+
-- is counted by the audit. It is never written under another id and never
|
|
310
|
+
-- dropped: a dropped edit is a promise broken to the person who made it, and it
|
|
311
|
+
-- emits nothing at the time it happens.
|
|
312
|
+
CREATE TABLE IF NOT EXISTS visit_status (
|
|
313
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
314
|
+
accountId TEXT NOT NULL,
|
|
315
|
+
visitId TEXT NOT NULL,
|
|
316
|
+
ownerId TEXT NOT NULL,
|
|
317
|
+
status TEXT NOT NULL,
|
|
318
|
+
statusAt TEXT NOT NULL,
|
|
319
|
+
claimed INTEGER NOT NULL DEFAULT 0
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
-- The pull selects unclaimed rows for one account every sixty seconds, so the
|
|
323
|
+
-- pair carries its own index or every cycle is a full scan.
|
|
324
|
+
CREATE INDEX IF NOT EXISTS visit_status_pending ON visit_status (accountId, claimed);
|
|
@@ -60,16 +60,32 @@ export async function mintSession(
|
|
|
60
60
|
return sessionId
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Which records a person may read.
|
|
65
|
+
*
|
|
66
|
+
* Two values and no third: `own` sees only records carrying their ownerId,
|
|
67
|
+
* `account` sees every record for the account. Distinct from `folders`, which
|
|
68
|
+
* governs which FILES they browse — a person can hold any combination, and
|
|
69
|
+
* neither narrows or widens the other.
|
|
70
|
+
*/
|
|
71
|
+
export type RecordScope = 'own' | 'account'
|
|
72
|
+
|
|
63
73
|
export async function resolveSession(
|
|
64
74
|
db: D1Database,
|
|
65
75
|
sessionId: string,
|
|
66
76
|
accountId: string,
|
|
67
77
|
nowMs: number,
|
|
68
|
-
): Promise<{
|
|
78
|
+
): Promise<{
|
|
79
|
+
ownerId: string
|
|
80
|
+
accountId: string
|
|
81
|
+
folders: string[]
|
|
82
|
+
recordScope: RecordScope
|
|
83
|
+
} | null> {
|
|
69
84
|
if (!sessionId) return null
|
|
70
85
|
const row = await db
|
|
71
86
|
.prepare(
|
|
72
|
-
`SELECT s.ownerId AS ownerId, p.accountId AS accountId, p.folders AS folders
|
|
87
|
+
`SELECT s.ownerId AS ownerId, p.accountId AS accountId, p.folders AS folders,
|
|
88
|
+
p.recordScope AS recordScope
|
|
73
89
|
FROM sessions s
|
|
74
90
|
JOIN people p ON p.ownerId = s.ownerId AND p.accountId = s.accountId
|
|
75
91
|
WHERE s.sessionId = ?
|
|
@@ -78,7 +94,12 @@ export async function resolveSession(
|
|
|
78
94
|
AND s.pcCheck = substr(p.hash, 1, 16)`,
|
|
79
95
|
)
|
|
80
96
|
.bind(sessionId, accountId, nowMs)
|
|
81
|
-
.first<{
|
|
97
|
+
.first<{
|
|
98
|
+
ownerId: string
|
|
99
|
+
accountId: string
|
|
100
|
+
folders: string
|
|
101
|
+
recordScope: unknown
|
|
102
|
+
}>()
|
|
82
103
|
if (!row || typeof row.ownerId !== 'string') return null
|
|
83
104
|
// The join matches on BOTH columns and the lookup is already scoped to this
|
|
84
105
|
// portal's own account, which is what stops a same-named person in another
|
|
@@ -92,7 +113,13 @@ export async function resolveSession(
|
|
|
92
113
|
// means no grant, which the two client surfaces read as no filter (full set).
|
|
93
114
|
const folders =
|
|
94
115
|
typeof row.folders === 'string' ? row.folders.split(',').map((s) => s.trim()).filter(Boolean) : []
|
|
95
|
-
|
|
116
|
+
// ONLY the exact token widens the scope. An empty string, an unknown value, a
|
|
117
|
+
// null, a legacy row with no such column: every one of them is `own`. An
|
|
118
|
+
// unreadable scope is the narrow scope, because the alternative is a read
|
|
119
|
+
// failure that hands out account-wide visibility — the one outcome this
|
|
120
|
+
// column exists to prevent.
|
|
121
|
+
const recordScope: RecordScope = row.recordScope === 'account' ? 'account' : 'own'
|
|
122
|
+
return { ownerId: row.ownerId, accountId, folders, recordScope }
|
|
96
123
|
}
|
|
97
124
|
|
|
98
125
|
/** Whether a person's folder grant admits `relPath`. An empty grant admits
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { Handler, Logger, PortalEnv } from './_lib/types'
|
|
2
|
+
import { readSessionCookie, resolveSession } from './_lib/session'
|
|
3
|
+
import { q } from './_lib/log'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The longest status a person may set.
|
|
7
|
+
*
|
|
8
|
+
* The value reaches `:Visit.status` through the device-side pull, so it is
|
|
9
|
+
* graph content arriving from the open web. It is refused at the door rather
|
|
10
|
+
* than truncated: a truncated status is a different status, silently, and the
|
|
11
|
+
* person who set it is never told.
|
|
12
|
+
*/
|
|
13
|
+
export const MAX_STATUS_LENGTH = 64
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Record a status a person set against one of their visits.
|
|
17
|
+
*
|
|
18
|
+
* THE GATE IS THE SELECT, not a check beside it. The insert is
|
|
19
|
+
* `INSERT ... SELECT` over the same `visits` join, under the same two filters
|
|
20
|
+
* the read applies — the account from the baked-in var, and the owner when and
|
|
21
|
+
* only when the person's scope is `own`. A visit the person may not see admits
|
|
22
|
+
* no row, so `meta.changes` is 0 and the handler answers 404. There is no
|
|
23
|
+
* separate authorisation step that a later edit could reorder past the write.
|
|
24
|
+
*
|
|
25
|
+
* This ships in the platform template for the same reason the read does, and
|
|
26
|
+
* more sharply. A read leak shows someone a working day that is not theirs; a
|
|
27
|
+
* write leak lets them change it. Neither gate is something to re-implement per
|
|
28
|
+
* client.
|
|
29
|
+
*
|
|
30
|
+
* `ownerId` is stamped from the resolved session and never from the body. A
|
|
31
|
+
* request that names one is ignored rather than refused, because the field
|
|
32
|
+
* carries no authority here — the session already decided who is writing.
|
|
33
|
+
*/
|
|
34
|
+
export async function processVisitStatus(
|
|
35
|
+
body: unknown,
|
|
36
|
+
sessionId: string,
|
|
37
|
+
env: PortalEnv,
|
|
38
|
+
log: Logger,
|
|
39
|
+
nowMs: number,
|
|
40
|
+
): Promise<Handler> {
|
|
41
|
+
const session = await resolveSession(env.DB, sessionId, env.PORTAL_ACCOUNT_ID, nowMs)
|
|
42
|
+
if (!session) {
|
|
43
|
+
log('[data-portal] op=visit-status owner=none result=denied')
|
|
44
|
+
return { status: 401, payload: { ok: false, error: 'denied' } }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const b = (body ?? {}) as Record<string, unknown>
|
|
48
|
+
const visitId = typeof b.visitId === 'string' ? b.visitId : ''
|
|
49
|
+
const status = typeof b.status === 'string' ? b.status : ''
|
|
50
|
+
if (!visitId || !status) {
|
|
51
|
+
log(`[data-portal] op=visit-status owner=${q(session.ownerId)} result=bad-request`)
|
|
52
|
+
return { status: 400, payload: { ok: false, error: 'visitId and status are required' } }
|
|
53
|
+
}
|
|
54
|
+
if (status.length > MAX_STATUS_LENGTH) {
|
|
55
|
+
log(`[data-portal] op=visit-status owner=${q(session.ownerId)} result=status-too-long`)
|
|
56
|
+
return { status: 400, payload: { ok: false, error: 'status too long' } }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const scoped = session.recordScope === 'own'
|
|
60
|
+
const sql =
|
|
61
|
+
`INSERT INTO visit_status (accountId, ownerId, status, statusAt, claimed, visitId)
|
|
62
|
+
SELECT ?, ?, ?, ?, 0, v.visitId
|
|
63
|
+
FROM visits v
|
|
64
|
+
JOIN visits_state s
|
|
65
|
+
ON s.accountId = v.accountId AND s.currentGeneration = v.generation
|
|
66
|
+
WHERE v.accountId = ?` +
|
|
67
|
+
(scoped ? ' AND v.ownerId = ?' : '') +
|
|
68
|
+
' AND v.visitId = ?'
|
|
69
|
+
const statusAt = new Date(nowMs).toISOString()
|
|
70
|
+
const stmt = env.DB.prepare(sql)
|
|
71
|
+
const bound = scoped
|
|
72
|
+
? stmt.bind(
|
|
73
|
+
env.PORTAL_ACCOUNT_ID,
|
|
74
|
+
session.ownerId,
|
|
75
|
+
status,
|
|
76
|
+
statusAt,
|
|
77
|
+
env.PORTAL_ACCOUNT_ID,
|
|
78
|
+
session.ownerId,
|
|
79
|
+
visitId,
|
|
80
|
+
)
|
|
81
|
+
: stmt.bind(
|
|
82
|
+
env.PORTAL_ACCOUNT_ID,
|
|
83
|
+
session.ownerId,
|
|
84
|
+
status,
|
|
85
|
+
statusAt,
|
|
86
|
+
env.PORTAL_ACCOUNT_ID,
|
|
87
|
+
visitId,
|
|
88
|
+
)
|
|
89
|
+
const result = await bound.run()
|
|
90
|
+
const changes = result.meta?.changes ?? 0
|
|
91
|
+
|
|
92
|
+
if (changes === 0) {
|
|
93
|
+
// One answer for "no such visit" and "not yours", deliberately. The two are
|
|
94
|
+
// the same fact to a caller who may not see it, and distinguishing them
|
|
95
|
+
// would let anyone enumerate the account's visit ids.
|
|
96
|
+
log(
|
|
97
|
+
`[data-portal] op=visit-status owner=${q(session.ownerId)} account=${q(session.accountId)} ` +
|
|
98
|
+
`scope=${session.recordScope} result=not-found`,
|
|
99
|
+
)
|
|
100
|
+
return { status: 404, payload: { ok: false, error: 'not found' } }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Counts, the scope and the outcome. Never the status text: it describes what
|
|
104
|
+
// a real person was doing.
|
|
105
|
+
log(
|
|
106
|
+
`[data-portal] op=visit-status owner=${q(session.ownerId)} account=${q(session.accountId)} ` +
|
|
107
|
+
`scope=${session.recordScope} queued=${changes} result=ok`,
|
|
108
|
+
)
|
|
109
|
+
return { status: 200, payload: { ok: true, queued: changes } }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface PagesContext {
|
|
113
|
+
request: Request
|
|
114
|
+
env: PortalEnv
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function onRequestPost(context: PagesContext): Promise<Response> {
|
|
118
|
+
const sessionId = readSessionCookie(context.request.headers.get('cookie')) ?? ''
|
|
119
|
+
let body: unknown = null
|
|
120
|
+
try {
|
|
121
|
+
body = await context.request.json()
|
|
122
|
+
} catch {
|
|
123
|
+
// A malformed body is a bad request, not a crash. processVisitStatus
|
|
124
|
+
// resolves the session first, so an unauthenticated caller still gets 401
|
|
125
|
+
// rather than learning that the body parse is what failed.
|
|
126
|
+
body = null
|
|
127
|
+
}
|
|
128
|
+
const { status, payload } = await processVisitStatus(
|
|
129
|
+
body,
|
|
130
|
+
sessionId,
|
|
131
|
+
context.env,
|
|
132
|
+
(line) => console.log(line),
|
|
133
|
+
Date.now(),
|
|
134
|
+
)
|
|
135
|
+
return Response.json(payload, { status })
|
|
136
|
+
}
|