Non-owner shared-space members could read the space memory provider id and internal endpoint origin via aiConsent/status, bypassing the owner-only policy that draft PR #46 applies to memory/providerConfig. Owners still see full provider disclosure; members get a generic Space memory recipient when a provider is configured, without loading settings. Co-authored-by: cynarAI <cynarAI@users.noreply.github.com>
129 lines
5.6 KiB
TypeScript
129 lines
5.6 KiB
TypeScript
import type { Actor } from "@rakazo/contracts";
|
|
import { AI_DISCLOSURE_VERSION } from "@rakazo/contracts";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("./thread-target.js", () => ({
|
|
resolveThreadTarget: async () => ({ kind: "bot", botId: "bot" }),
|
|
}));
|
|
|
|
import { aiConsentStatus, allowAiConsent } from "./ai-consent.js";
|
|
import type { RouterDeps } from "./router.js";
|
|
|
|
function setup() {
|
|
const upsert = vi.fn(async () => ({}));
|
|
const deps = {
|
|
env: { agentRuntime: "scripted" },
|
|
prisma: {
|
|
spaceModelPreference: { findMany: vi.fn(async () => []), findFirst: vi.fn(async () => null) },
|
|
secret: { findMany: vi.fn(async () => []) },
|
|
bot: { findMany: vi.fn(async () => []) },
|
|
spaceVoicePreference: {
|
|
findFirst: vi.fn(async () => ({ credential: { provider: "openai" }, voiceId: "alloy" })),
|
|
findMany: vi.fn(async () => [{ credential: { provider: "openai" } }]),
|
|
},
|
|
spaceMemoryConfig: {
|
|
findUnique: vi.fn(async () => null),
|
|
count: vi.fn(async () => 0),
|
|
},
|
|
spaceMember: { findUnique: vi.fn(async () => ({ role: "owner" })) },
|
|
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
|
aiDataConsent: { findMany: vi.fn(async () => []), upsert },
|
|
$transaction: vi.fn(async (queries) => Promise.all(queries)),
|
|
},
|
|
} as unknown as RouterDeps;
|
|
return { deps, upsert, actor: { userId: "user", spaceId: "space" } as Actor };
|
|
}
|
|
describe("consent grants", () => {
|
|
it("includes reachable helper models and cloud services for a targeted send", async () => {
|
|
const { deps, actor } = setup();
|
|
deps.env.agentRuntime = "pi";
|
|
deps.cloudAgent = {
|
|
key: "test",
|
|
spaceId: actor.spaceId,
|
|
provider: { describe: () => ({ id: "cursor", capabilities: { offline: false } }) },
|
|
} as RouterDeps["cloudAgent"];
|
|
vi.mocked(deps.prisma.spaceModelPreference.findMany).mockResolvedValue([
|
|
{
|
|
modelId: "helper-model",
|
|
isDefault: false,
|
|
credential: { provider: "anthropic", secretId: "test-secret" },
|
|
},
|
|
] as never);
|
|
const status = await aiConsentStatus(deps, actor, { botId: "bot", uses: ["model"] });
|
|
expect(status.recipients.map((recipient) => recipient.name)).toEqual(
|
|
expect.arrayContaining(["Anthropic", "Cursor"]),
|
|
);
|
|
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
|
expect(deps.prisma.bot.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: expect.objectContaining({ id: { in: ["bot"] } }) }),
|
|
);
|
|
});
|
|
it("discloses only the selected voice provider before a voice action", async () => {
|
|
const { deps, actor } = setup();
|
|
vi.mocked(deps.prisma.spaceVoicePreference.findMany).mockResolvedValue([
|
|
{ credential: { provider: "openai" } },
|
|
{ credential: { provider: "elevenlabs" } },
|
|
] as never);
|
|
const status = await aiConsentStatus(deps, actor, { uses: ["voice"] });
|
|
expect(status.recipients).toHaveLength(1);
|
|
expect(status.recipients[0]?.name).toBe("OpenAI");
|
|
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
|
});
|
|
it("discloses memory provider details to space owners", async () => {
|
|
const { deps, actor } = setup();
|
|
vi.mocked(deps.prisma.spaceMemoryConfig.findUnique).mockResolvedValue({
|
|
provider: "supermemory",
|
|
settings: { baseUrl: "http://127.0.0.1:8123" },
|
|
} as never);
|
|
const status = await aiConsentStatus(deps, actor, { uses: ["memory"] });
|
|
expect(status.recipients).toHaveLength(1);
|
|
expect(status.recipients[0]?.name).toContain("Supermemory");
|
|
expect(status.recipients[0]?.name).toContain("127.0.0.1:8123");
|
|
expect(deps.prisma.spaceMemoryConfig.count).not.toHaveBeenCalled();
|
|
});
|
|
it("redacts space memory provider details for non-owner members", async () => {
|
|
const { deps, actor } = setup();
|
|
vi.mocked(deps.prisma.spaceMember.findUnique).mockResolvedValue({ role: "member" } as never);
|
|
vi.mocked(deps.prisma.spaceMemoryConfig.count).mockResolvedValue(1);
|
|
const status = await aiConsentStatus(deps, actor, { uses: ["memory"] });
|
|
expect(status.recipients).toHaveLength(1);
|
|
expect(status.recipients[0]?.name).toBe("Space memory");
|
|
expect(JSON.stringify(status.recipients)).not.toContain("127.0.0.1");
|
|
expect(JSON.stringify(status.recipients)).not.toContain("supermemory");
|
|
expect(deps.prisma.spaceMemoryConfig.findUnique).not.toHaveBeenCalled();
|
|
});
|
|
it("returns an operator policy without requiring a hosted provider", async () => {
|
|
const { deps, actor } = setup();
|
|
deps.env.privacyPolicyUrl = "https://example.com/privacy";
|
|
expect((await aiConsentStatus(deps, actor)).privacyUrl).toBe("https://example.com/privacy");
|
|
});
|
|
it("rejects stale disclosures, unknown recipients, and a changed account or Space", async () => {
|
|
const { deps, actor, upsert } = setup();
|
|
const current = await aiConsentStatus(deps, actor);
|
|
const input = {
|
|
scope: current.scope,
|
|
version: current.version,
|
|
keys: [current.recipients[0]!.key],
|
|
};
|
|
for (const invalid of [
|
|
{ ...input, version: "old" },
|
|
{ ...input, scope: "different-account" },
|
|
{ ...input, keys: ["unknown"] },
|
|
]) {
|
|
await expect(allowAiConsent(deps, actor, invalid)).rejects.toThrow("changed");
|
|
}
|
|
expect(upsert).not.toHaveBeenCalled();
|
|
await allowAiConsent(deps, actor, input);
|
|
expect(upsert).toHaveBeenCalledExactlyOnceWith(
|
|
expect.objectContaining({
|
|
create: {
|
|
userId: actor.userId,
|
|
spaceId: actor.spaceId,
|
|
recipientKey: input.keys[0],
|
|
version: AI_DISCLOSURE_VERSION,
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
});
|