mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
Merge remote-tracking branch 'origin/main' into feat/1371-medication-note
# Conflicts: # CHANGELOG.md
This commit is contained in:
commit
4b500dc66c
25 files changed
+1199
-157
No files matched your search
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* How the Browser Push card reads a refusal from `pushManager.subscribe()`.
|
||||
*
|
||||
* The reported case is Brave with push messaging switched off, which rejects
|
||||
* with `AbortError: Registration failed - push service error` and used to
|
||||
* show only "Activation failed".
|
||||
*/
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
classifyWebPushSubscribeFailure,
|
||||
isBraveBrowser,
|
||||
} from "../web-push-subscribe-failure";
|
||||
|
||||
function domError(name: string, message: string): Error {
|
||||
const error = new Error(message);
|
||||
error.name = name;
|
||||
return error;
|
||||
}
|
||||
|
||||
const PUSH_SERVICE_ERROR = domError(
|
||||
"AbortError",
|
||||
"Registration failed - push service error",
|
||||
);
|
||||
|
||||
describe("classifyWebPushSubscribeFailure", () => {
|
||||
it("names the push service when the browser could not register with it", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(PUSH_SERVICE_ERROR, {
|
||||
permission: "granted",
|
||||
isBrave: false,
|
||||
}),
|
||||
).toBe("push-service");
|
||||
});
|
||||
|
||||
it("names Brave's setting when the browser is Brave", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(PUSH_SERVICE_ERROR, {
|
||||
permission: "granted",
|
||||
isBrave: true,
|
||||
}),
|
||||
).toBe("push-service-brave");
|
||||
});
|
||||
|
||||
it("reads any other AbortError from subscribe as the push service", () => {
|
||||
// Chromium also says "push service not available"; Firefox says
|
||||
// "Error retrieving push subscription." Both are the push service.
|
||||
for (const message of [
|
||||
"Registration failed - push service not available",
|
||||
"Error retrieving push subscription.",
|
||||
]) {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(domError("AbortError", message), {
|
||||
permission: "granted",
|
||||
isBrave: false,
|
||||
}),
|
||||
).toBe("push-service");
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a blocked permission as blocked, whatever the error said", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(PUSH_SERVICE_ERROR, {
|
||||
permission: "denied",
|
||||
isBrave: true,
|
||||
}),
|
||||
).toBe("denied");
|
||||
});
|
||||
|
||||
it("does not blame the push service for a dismissed permission prompt", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(
|
||||
domError("NotAllowedError", "Registration failed - permission denied"),
|
||||
{ permission: "default", isBrave: true },
|
||||
),
|
||||
).toBe("unknown");
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(
|
||||
domError("AbortError", "Registration failed - permission denied"),
|
||||
{ permission: "default", isBrave: true },
|
||||
),
|
||||
).toBe("unknown");
|
||||
});
|
||||
|
||||
it("does not blame the push service for a key the browser would not take", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(
|
||||
domError(
|
||||
"AbortError",
|
||||
"Registration failed - missing applicationServerKey, and manifest empty or missing",
|
||||
),
|
||||
{ permission: "granted", isBrave: true },
|
||||
),
|
||||
).toBe("unknown");
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(
|
||||
domError(
|
||||
"InvalidAccessError",
|
||||
"The provided applicationServerKey is not valid.",
|
||||
),
|
||||
{ permission: "granted", isBrave: false },
|
||||
),
|
||||
).toBe("unknown");
|
||||
});
|
||||
|
||||
it("keeps the generic message for anything that is not an error object", () => {
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure("boom", {
|
||||
permission: "granted",
|
||||
isBrave: false,
|
||||
}),
|
||||
).toBe("unknown");
|
||||
expect(
|
||||
classifyWebPushSubscribeFailure(new TypeError("Failed to fetch"), {
|
||||
permission: "granted",
|
||||
isBrave: false,
|
||||
}),
|
||||
).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isBraveBrowser", () => {
|
||||
const original = Object.getOwnPropertyDescriptor(globalThis, "navigator");
|
||||
|
||||
afterEach(() => {
|
||||
if (original) Object.defineProperty(globalThis, "navigator", original);
|
||||
});
|
||||
|
||||
function withNavigator(value: unknown): void {
|
||||
Object.defineProperty(globalThis, "navigator", {
|
||||
value,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
|
||||
it("is true when navigator.brave is present", () => {
|
||||
withNavigator({ brave: { isBrave: async () => true } });
|
||||
expect(isBraveBrowser()).toBe(true);
|
||||
});
|
||||
|
||||
it("is false for a Chromium without navigator.brave", () => {
|
||||
withNavigator({ userAgent: "Mozilla/5.0 Chrome/130" });
|
||||
expect(isBraveBrowser()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Why `pushManager.subscribe()` refused, in the terms the Browser Push card
|
||||
* explains to a person.
|
||||
*
|
||||
* Only the browser's own refusal is classified here. A missing VAPID key and
|
||||
* a failed save are server answers and keep their own messages in the card.
|
||||
*
|
||||
* - `denied`: notifications are blocked for this site
|
||||
* (`Notification.permission === "denied"`), whatever the error said.
|
||||
* - `push-service` / `push-service-brave`: the browser could not register
|
||||
* with its push service ("AbortError: Registration failed - push service
|
||||
* error"). The page did nothing wrong and cannot fix it; the browser has to
|
||||
* reach its push service. Brave ships with that switched off, which is the
|
||||
* common case, so Brave gets the setting named.
|
||||
* - `unknown`: anything else, which keeps the generic message. That includes
|
||||
* a dismissed permission prompt (`NotAllowedError`, or an `AbortError`
|
||||
* about permission in older Chromium builds): the permission is still
|
||||
* "default", the person can try again, and calling it a push-service fault
|
||||
* would send them to the wrong setting.
|
||||
*/
|
||||
export type WebPushSubscribeFailure =
|
||||
"denied" | "push-service" | "push-service-brave" | "unknown";
|
||||
|
||||
export interface WebPushSubscribeContext {
|
||||
/** `Notification.permission` after the attempt. */
|
||||
permission: NotificationPermission;
|
||||
/** The browser announces itself as Brave (`navigator.brave`). */
|
||||
isBrave: boolean;
|
||||
}
|
||||
|
||||
function errorName(error: unknown): string {
|
||||
return typeof error === "object" && error !== null && "name" in error
|
||||
? String((error as { name: unknown }).name)
|
||||
: "";
|
||||
}
|
||||
|
||||
function errorMessage(error: unknown): string {
|
||||
return typeof error === "object" && error !== null && "message" in error
|
||||
? String((error as { message: unknown }).message)
|
||||
: "";
|
||||
}
|
||||
|
||||
export function classifyWebPushSubscribeFailure(
|
||||
error: unknown,
|
||||
{ permission, isBrave }: WebPushSubscribeContext,
|
||||
): WebPushSubscribeFailure {
|
||||
const name = errorName(error);
|
||||
const message = errorMessage(error);
|
||||
|
||||
if (permission === "denied") return "denied";
|
||||
if (name !== "AbortError") return "unknown";
|
||||
if (/permission/i.test(message)) return "unknown";
|
||||
// A key the server handed out that the browser would not take is the
|
||||
// server's problem, not the push service's.
|
||||
if (/applicationServerKey/i.test(message)) return "unknown";
|
||||
return isBrave ? "push-service-brave" : "push-service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Brave exposes `navigator.brave` so a page can tell it apart from Chrome,
|
||||
* whose user agent it otherwise copies.
|
||||
*/
|
||||
export function isBraveBrowser(): boolean {
|
||||
if (typeof navigator === "undefined") return false;
|
||||
const brave = (navigator as Navigator & { brave?: unknown }).brave;
|
||||
return typeof brave === "object" && brave !== null;
|
||||
}
|
||||
@@ -2011,8 +2011,8 @@ export const authPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
summary: "Mint a measurement-ingest token",
|
||||
description:
|
||||
"Mints a Bearer scoped to exactly `measurements:write` and audits the mint. THE RESPONSE CARRIES THE RAW TOKEN — the only place it ever exists; it is stored as an HMAC and no path re-reveals it.\n\n" +
|
||||
"**What it can do.** `POST /api/measurements` and `POST /api/measurements/batch`, on its owner's own record. Rows it writes carry `source: EXTERNAL`, resolved from the credential rather than taken from the body, so a reading pushed by a bridge stays distinguishable from one typed in by hand and can be picked out with the source filter. A body naming ANY source is refused 422 rather than relabelled — honouring it would defeat the label, overriding it silently would hand the caller rows it did not ask for. `APPLE_HEALTH` is the one that would do real damage: it is half a dedup key the phone also writes into, it participates in the cross-source merge, and it is what decides the Apple Health card may claim a sync happened. For the same reason a write through this credential does not move the native client's sync checkpoint. The rows stay editable by their owner, unlike a connected provider's — the hardware behind the token is the user's own.\n\n" +
|
||||
"**What it cannot do.** Everything else, including the measurement reads on the same paths, the edit and delete legs, and the export — a scope grants what it names and nothing adjacent. It cannot be pointed at a shared record: a request carrying the account selector is refused 403 before any grant is read, whatever grants its holder actually has. And it cannot mint another token, this endpoint included.\n\n" +
|
||||
"**What it can do.** `POST /api/measurements` and `POST /api/measurements/batch`, on its owner's own record, and `POST /api/custom-metrics/{id}/entries` for a custom metric its owner already created (it cannot list, create, edit or delete metrics, so the id comes from the metric's page in the app). Measurement rows it writes carry `source: EXTERNAL`, resolved from the credential rather than taken from the body, so a reading pushed by a bridge stays distinguishable from one typed in by hand and can be picked out with the source filter. A body naming ANY source is refused 422 rather than relabelled — honouring it would defeat the label, overriding it silently would hand the caller rows it did not ask for. `APPLE_HEALTH` is the one that would do real damage: it is half a dedup key the phone also writes into, it participates in the cross-source merge, and it is what decides the Apple Health card may claim a sync happened. For the same reason a write through this credential does not move the native client's sync checkpoint. The rows stay editable by their owner, unlike a connected provider's — the hardware behind the token is the user's own.\n\n" +
|
||||
"**What it cannot do.** Everything else, including the measurement reads on the same paths, the custom-metric list and value reads, the edit and delete legs, and the export — a scope grants what it names and nothing adjacent. It cannot be pointed at a shared record: a request carrying the account selector is refused 403 before any grant is read, whatever grants its holder actually has. And it cannot mint another token, this endpoint included.\n\n" +
|
||||
"Minting requires a COOKIE SESSION with a fresh proof: a sign-in or `POST /api/auth/reproof` within the last five minutes (with a second factor, on an account that has one), otherwise 401 `auth.reproof.required` with `meta.methods`. No Bearer credential reaches this endpoint at any scope, wildcard included — not because a wildcard lacks the reach, but because of the lifetimes involved: a native access token lives a day and what it could mint here lives a year, so admitting one would let a short-lived compromise leave behind a credential that outlives revoking it. Gated by the operator's instance-wide API switch. Body capped at 16 KiB; 10 mints per user per minute, and at most 10 live tokens held at once. Tokens appear in `GET /api/tokens` and are revoked at `DELETE /api/tokens/{id}` like any other — revoking frees a slot against the ceiling.",
|
||||
requestBody: {
|
||||
required: true,
|
||||
|
||||
@@ -294,7 +294,8 @@ export const customMetricPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
tags: ["Custom metrics"],
|
||||
summary: "Log a value",
|
||||
description:
|
||||
"Records a value against the custom metric, snapshotting the metric's unit. Audits as `customMetricEntry.create`.",
|
||||
"Records a value against the custom metric, snapshotting the metric's unit. Audits as `customMetricEntry.create`.\n\n" +
|
||||
"**Narrow token.** Also reachable with a `measurements:write` Bearer (minted at `POST /api/tokens/measurements`), the supported way to push a custom metric's value from a script or a home-automation rule. Such a credential writes only to its owner's own live metrics: an id that is unknown, deleted, or another account's answers the same 404, and a request carrying the account selector is refused 403 before any sharing grant is read. This POST is all the scope reaches on custom metrics. The value list on this same path, the metric list, and creating, editing or deleting a metric or a value name no scope and refuse it 403, so the metric id has to come from its owner: it is the path segment after `/custom-metrics/` in the address of the metric's page in the app. The entry carries no source of its own; the audit row names the token that wrote it. Every other narrow token, `workouts:write` included, is refused 403.",
|
||||
requestParams: { path: z.object({ id: z.string() }) },
|
||||
parameters: [idempotencyKeyParameter],
|
||||
requestBody: {
|
||||
|
||||
Reference in new issue
Block a user