mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
test(01-01): pin MCP read scope boundaries
- cover health read and wildcard positive capability paths - reject unrelated scopes before server registration and database reads
This commit is contained in:
1 parent
fb533147d3
commit
6c0f47acad
4 files changed
+229
-8
No files matched your search
@@ -14,7 +14,7 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe("resolveMcpAuthContext", () => {
|
||||
it("binds the session to <userId>:<tokenId> (REQ-SEC-11) via the canonical Bearer path", async () => {
|
||||
it("binds a health:read session to <userId>:<tokenId> via the canonical Bearer path", async () => {
|
||||
vi.mocked(resolveBearerToken).mockResolvedValue({
|
||||
user: FAKE_USER,
|
||||
tokenId: "token-9",
|
||||
@@ -24,11 +24,6 @@ describe("resolveMcpAuthContext", () => {
|
||||
|
||||
const ctx = await resolveMcpAuthContext(" hlk_abc ");
|
||||
|
||||
// Trimmed token resolved through the shared validator (no requiredPermission for reads).
|
||||
// `any-valid-token` is the one deliberate fail-open posture in the tree:
|
||||
// `/mcp` authenticates here and authorises downstream (audience binding +
|
||||
// `tokenAllowsWrite`). Asserted explicitly so a silent switch to another
|
||||
// posture — in either direction — shows up as a failing test.
|
||||
expect(resolveBearerToken).toHaveBeenCalledWith("hlk_abc", {
|
||||
kind: "any-valid-token",
|
||||
});
|
||||
@@ -39,7 +34,19 @@ describe("resolveMcpAuthContext", () => {
|
||||
expect(ctx.canWrite).toBe(false);
|
||||
});
|
||||
|
||||
it("grants write capability only when the token carries health:write", async () => {
|
||||
it("admits wildcard read capability and preserves wildcard write capability", async () => {
|
||||
vi.mocked(resolveBearerToken).mockResolvedValue({
|
||||
user: FAKE_USER,
|
||||
tokenId: "token-wild",
|
||||
permissions: ["*"],
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
const ctx = await resolveMcpAuthContext("hlk_wild");
|
||||
expect(ctx.canRead).toBe(true);
|
||||
expect(ctx.canWrite).toBe(true);
|
||||
});
|
||||
|
||||
it("does not infer read capability from health:write", async () => {
|
||||
vi.mocked(resolveBearerToken).mockResolvedValue({
|
||||
user: FAKE_USER,
|
||||
tokenId: "token-w",
|
||||
@@ -47,9 +54,29 @@ describe("resolveMcpAuthContext", () => {
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
const ctx = await resolveMcpAuthContext("hlk_xyz");
|
||||
expect(ctx.canRead).toBe(false);
|
||||
expect(ctx.canWrite).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["medication ingest", ["medication:ingest"]],
|
||||
["notification delivery", ["notifications:send"]],
|
||||
["FHIR read", ["fhir:read"]],
|
||||
["unrelated", ["profile:read"]],
|
||||
["empty", []],
|
||||
])("does not grant MCP reads to a valid %s token", async (_label, scopes) => {
|
||||
vi.mocked(resolveBearerToken).mockResolvedValue({
|
||||
user: FAKE_USER,
|
||||
tokenId: "token-narrow",
|
||||
permissions: scopes,
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
|
||||
const ctx = await resolveMcpAuthContext("hlk_narrow");
|
||||
|
||||
expect(ctx.canRead).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects an empty token without calling the validator", async () => {
|
||||
await expect(resolveMcpAuthContext(" ")).rejects.toThrow();
|
||||
expect(resolveBearerToken).not.toHaveBeenCalled();
|
||||
|
||||
@@ -1,12 +1,34 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
tokenAllowsRead,
|
||||
tokenAllowsWrite,
|
||||
SCOPE_HEALTH_READ,
|
||||
SCOPE_HEALTH_WRITE,
|
||||
SCOPE_WILDCARD,
|
||||
} from "../scopes";
|
||||
|
||||
describe("tokenAllowsRead", () => {
|
||||
it.each([
|
||||
["health:read", [SCOPE_HEALTH_READ]],
|
||||
["wildcard", [SCOPE_WILDCARD]],
|
||||
["read plus write", [SCOPE_HEALTH_READ, SCOPE_HEALTH_WRITE]],
|
||||
])("admits an explicit %s grant", (_label, permissions) => {
|
||||
expect(tokenAllowsRead(permissions)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["write-only", [SCOPE_HEALTH_WRITE]],
|
||||
["medication ingest", ["medication:ingest"]],
|
||||
["notifications", ["notifications:send"]],
|
||||
["FHIR", ["fhir:read"]],
|
||||
["unrelated", ["profile:read"]],
|
||||
["empty", []],
|
||||
])("refuses a %s scope set", (_label, permissions) => {
|
||||
expect(tokenAllowsRead(permissions)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("tokenAllowsWrite", () => {
|
||||
it("is false for a read-only token", () => {
|
||||
expect(tokenAllowsWrite([SCOPE_HEALTH_READ])).toBe(false);
|
||||
|
||||
Reference in new issue
Block a user