|
| 1 | +/** |
| 2 | + * Regression test for GHSA-wrr4-782v-jhwh / |
| 3 | + * docs/security/advisories/2026-05-21-relationship-endpoint-tenant-isolation.md |
| 4 | + * |
| 5 | + * Invariant: getRelationshipsForEntity and getRelationshipsByType MUST apply |
| 6 | + * an .eq("user_id", userId) filter on the relationship_snapshots query when |
| 7 | + * a userId is provided. Without this filter, callers can read relationship |
| 8 | + * edges belonging to other users on the same instance. |
| 9 | + * |
| 10 | + * The test records the chained calls against a mock db and asserts the |
| 11 | + * expected filter clauses are present. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 15 | + |
| 16 | +type Call = { method: string; args: unknown[] }; |
| 17 | +const recordedCalls: Call[] = []; |
| 18 | + |
| 19 | +function makeChainable() { |
| 20 | + const proxy: any = new Proxy( |
| 21 | + {}, |
| 22 | + { |
| 23 | + get(_target, prop: string) { |
| 24 | + if (prop === "then" || typeof prop === "symbol") return undefined; |
| 25 | + return (...args: unknown[]) => { |
| 26 | + recordedCalls.push({ method: prop, args }); |
| 27 | + if (prop === "order") { |
| 28 | + // terminal: resolve with empty data |
| 29 | + return Promise.resolve({ data: [], error: null }); |
| 30 | + } |
| 31 | + return proxy; |
| 32 | + }; |
| 33 | + }, |
| 34 | + } |
| 35 | + ); |
| 36 | + return proxy; |
| 37 | +} |
| 38 | + |
| 39 | +vi.mock("../../src/db.js", () => ({ |
| 40 | + db: { |
| 41 | + from: (table: string) => { |
| 42 | + recordedCalls.push({ method: "from", args: [table] }); |
| 43 | + return makeChainable(); |
| 44 | + }, |
| 45 | + }, |
| 46 | +})); |
| 47 | + |
| 48 | +import { relationshipsService } from "../../src/services/relationships.js"; |
| 49 | + |
| 50 | +describe("relationships tenant isolation (GHSA-wrr4-782v-jhwh)", () => { |
| 51 | + beforeEach(() => { |
| 52 | + recordedCalls.length = 0; |
| 53 | + }); |
| 54 | + |
| 55 | + it("getRelationshipsForEntity outgoing applies user_id filter when userId provided", async () => { |
| 56 | + await relationshipsService.getRelationshipsForEntity( |
| 57 | + "ent_abc", |
| 58 | + "outgoing", |
| 59 | + true, // includeDeleted to skip the deletion-observation second query |
| 60 | + "user-1" |
| 61 | + ); |
| 62 | + |
| 63 | + const eqCalls = recordedCalls.filter((c) => c.method === "eq"); |
| 64 | + const hasUserIdEq = eqCalls.some( |
| 65 | + (c) => c.args[0] === "user_id" && c.args[1] === "user-1" |
| 66 | + ); |
| 67 | + expect(hasUserIdEq).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it("getRelationshipsForEntity incoming applies user_id filter when userId provided", async () => { |
| 71 | + await relationshipsService.getRelationshipsForEntity( |
| 72 | + "ent_abc", |
| 73 | + "incoming", |
| 74 | + true, |
| 75 | + "user-1" |
| 76 | + ); |
| 77 | + |
| 78 | + const eqCalls = recordedCalls.filter((c) => c.method === "eq"); |
| 79 | + const hasUserIdEq = eqCalls.some( |
| 80 | + (c) => c.args[0] === "user_id" && c.args[1] === "user-1" |
| 81 | + ); |
| 82 | + expect(hasUserIdEq).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it("getRelationshipsForEntity both directions applies user_id filter when userId provided", async () => { |
| 86 | + await relationshipsService.getRelationshipsForEntity( |
| 87 | + "ent_abc", |
| 88 | + "both", |
| 89 | + true, |
| 90 | + "user-1" |
| 91 | + ); |
| 92 | + |
| 93 | + const eqCalls = recordedCalls.filter((c) => c.method === "eq"); |
| 94 | + const hasUserIdEq = eqCalls.some( |
| 95 | + (c) => c.args[0] === "user_id" && c.args[1] === "user-1" |
| 96 | + ); |
| 97 | + expect(hasUserIdEq).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it("getRelationshipsByType applies user_id filter when userId provided", async () => { |
| 101 | + await relationshipsService.getRelationshipsByType( |
| 102 | + "WORKS_AT" as any, |
| 103 | + true, |
| 104 | + "user-1" |
| 105 | + ); |
| 106 | + |
| 107 | + const eqCalls = recordedCalls.filter((c) => c.method === "eq"); |
| 108 | + const hasUserIdEq = eqCalls.some( |
| 109 | + (c) => c.args[0] === "user_id" && c.args[1] === "user-1" |
| 110 | + ); |
| 111 | + expect(hasUserIdEq).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it("does NOT apply user_id filter when userId omitted (back-compat for internal callers)", async () => { |
| 115 | + await relationshipsService.getRelationshipsForEntity("ent_abc", "outgoing", true); |
| 116 | + |
| 117 | + const eqCalls = recordedCalls.filter((c) => c.method === "eq"); |
| 118 | + const hasUserIdEq = eqCalls.some((c) => c.args[0] === "user_id"); |
| 119 | + expect(hasUserIdEq).toBe(false); |
| 120 | + }); |
| 121 | +}); |
0 commit comments