mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix: validate account access payloads
- Share the closed access schema with OpenAPI\n- Fail closed on malformed or stale record metadata
This commit is contained in:
1 parent
c6b5d5df63
commit
60f91d27d7
4 files changed
+104
-81
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import type { AccountAccess } from "./account-access-view";
|
||||
import { SHARE_DOMAINS } from "./scope";
|
||||
|
||||
const accountAccessLevelSchema = z.enum(["read", "write", "manage"]);
|
||||
const accountRecordKindSchema = z.enum(["shared", "managed"]);
|
||||
const shareSectionsSchema = z.array(z.enum(SHARE_DOMAINS)).nullable();
|
||||
|
||||
/** The browser's resolved account-access payload, shared with OpenAPI. */
|
||||
export const accountAccessEntrySchema = z
|
||||
.object({
|
||||
accountId: z.string().min(1),
|
||||
username: z.string().min(1),
|
||||
displayName: z.string().nullable(),
|
||||
access: z.enum(["read", "write"]),
|
||||
level: accountAccessLevelSchema,
|
||||
recordKind: accountRecordKindSchema,
|
||||
sections: shareSectionsSchema,
|
||||
canWrite: z.boolean(),
|
||||
})
|
||||
.superRefine((entry, ctx) => {
|
||||
const uniqueSections = new Set(entry.sections ?? []);
|
||||
if (uniqueSections.size !== (entry.sections?.length ?? 0)) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Account access sections must not repeat a domain",
|
||||
path: ["sections"],
|
||||
});
|
||||
}
|
||||
|
||||
const isManage = entry.level === "manage";
|
||||
if (isManage && (entry.access !== "write" || entry.sections !== null)) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Manage access must use legacy write access and whole scope",
|
||||
});
|
||||
}
|
||||
|
||||
if (entry.canWrite !== (entry.level !== "read")) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Account access write flag disagrees with its level",
|
||||
path: ["canWrite"],
|
||||
});
|
||||
}
|
||||
|
||||
if (!isManage && entry.access !== entry.level) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Legacy access must agree with the resolved level",
|
||||
path: ["access"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const accountAccessBlockSchema = z.object({
|
||||
accounts: z.array(accountAccessEntrySchema),
|
||||
active: accountAccessEntrySchema.nullable(),
|
||||
recordKind: z.enum(["self", "shared", "managed"]),
|
||||
canSwitch: z.boolean(),
|
||||
});
|
||||
|
||||
/** Parse the server-resolved access block as a closed presentation contract. */
|
||||
export function parseAccountAccess(value: unknown): AccountAccess | null {
|
||||
const parsed = accountAccessBlockSchema.safeParse(value);
|
||||
if (!parsed.success) return null;
|
||||
|
||||
const { accounts, active, recordKind, canSwitch } = parsed.data;
|
||||
const accountIds = new Set(accounts.map((entry) => entry.accountId));
|
||||
if (
|
||||
accountIds.size !== accounts.length ||
|
||||
canSwitch !== accounts.length > 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (active === null) {
|
||||
return recordKind === "self"
|
||||
? { accounts, active: null, recordKind, canSwitch }
|
||||
: null;
|
||||
}
|
||||
|
||||
if (!accountIds.has(active.accountId) || recordKind !== active.recordKind) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const canonicalActive = accounts.find(
|
||||
(entry) => entry.accountId === active.accountId,
|
||||
);
|
||||
if (!canonicalActive) return null;
|
||||
|
||||
return { accounts, active: canonicalActive, recordKind, canSwitch };
|
||||
}
|
||||
@@ -28,6 +28,7 @@ export type LegacyAccountAccessLevel = "read" | "write";
|
||||
|
||||
/** The server-resolved kind of record a presentation is naming. */
|
||||
export type AccountRecordKind = "self" | "shared" | "managed";
|
||||
export type SharedAccountRecordKind = Exclude<AccountRecordKind, "self">;
|
||||
|
||||
/**
|
||||
* Read the portion of an account-access entry that an older whole-record
|
||||
@@ -68,7 +69,7 @@ export interface AccountAccessEntry {
|
||||
* What kind of record this is. This is presentation metadata only; it is
|
||||
* resolved by the server and never accepted as an authorization input.
|
||||
*/
|
||||
recordKind: AccountRecordKind;
|
||||
recordKind: SharedAccountRecordKind;
|
||||
/**
|
||||
* v1.37.0 — which sections of the record this grant opens, or `null` for
|
||||
* the entire record.
|
||||
|
||||
Reference in new issue
Block a user