mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-11-16 09:22:28 +00:00
fix(mcp): enforce write-tag permission on save-memory and add_memory
effectiveContainerTagAccess() computes per-tag read/write permission from the session's RBAC state (restricted member roles, scoped OAuth grants), but it was only ever consumed by select-space, guided-save, and upload-file -- all three use it purely to build the list of tags to offer in a picker/dropdown widget. The two tools that actually perform a write, save-memory and add_memory, accept a containerTag argument directly from the caller and never check it against the session's permissions before calling createMemory / forgetMemory. Since containerTags are discoverable (listSpaces / listContainerTags returns them) and not secret, any client can call these tools directly -- bypassing the widget entirely -- with a containerTag it only has read access to (a restricted member) or that isn't in its assigned set (a scoped OAuth grant), and the write still goes through today. Add assertWriteAccess(containerTag, session) in rbac.ts, built on the existing effectiveContainerTagAccess, and call it at the start of both tools before any client call is made. A denied call now returns a normal tool error instead of writing. Covered by 5 new rbac.test.ts cases (full access, restricted read-only, scoped-out tag, scoped read-only own tag, restricted explicit write). apps/mcp's pre-existing e2e/auth.test.ts failures are unrelated -- they hit a live OAuth origin this sandbox doesn't have and fail identically on main before this change.
This commit is contained in:
parent
9ccd1b64c3
commit
823fa95579
4 changed files with 85 additions and 1 deletions
|
|
@ -1,6 +1,10 @@
|
|||
import { describe, expect, it } from "vitest"
|
||||
import type { SessionInfo } from "../../shared/types"
|
||||
import { effectiveContainerTagAccess } from "./rbac"
|
||||
import {
|
||||
assertWriteAccess,
|
||||
ContainerTagAccessError,
|
||||
effectiveContainerTagAccess,
|
||||
} from "./rbac"
|
||||
|
||||
const baseSession: SessionInfo = {
|
||||
user: { id: "user_test" },
|
||||
|
|
@ -47,3 +51,53 @@ describe("effectiveContainerTagAccess", () => {
|
|||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("assertWriteAccess", () => {
|
||||
it("does not throw when the session has write access to the tag", () => {
|
||||
expect(() => assertWriteAccess("one", baseSession)).not.toThrow()
|
||||
})
|
||||
|
||||
it("throws ContainerTagAccessError for a restricted read-only tag", () => {
|
||||
const session: SessionInfo = {
|
||||
...baseSession,
|
||||
accessType: "restricted",
|
||||
containerTags: [{ containerTag: "one", permission: "read" }],
|
||||
}
|
||||
|
||||
expect(() => assertWriteAccess("one", session)).toThrow(
|
||||
ContainerTagAccessError,
|
||||
)
|
||||
})
|
||||
|
||||
it("throws for a tag outside a scoped session's assigned tags", () => {
|
||||
const session: SessionInfo = {
|
||||
...baseSession,
|
||||
scope: { type: "scoped", permission: "write", tags: ["allowed"] },
|
||||
}
|
||||
|
||||
expect(() => assertWriteAccess("other", session)).toThrow(
|
||||
ContainerTagAccessError,
|
||||
)
|
||||
})
|
||||
|
||||
it("throws for a read-only scoped session even on its own tag", () => {
|
||||
const session: SessionInfo = {
|
||||
...baseSession,
|
||||
scope: { type: "scoped", permission: "read", tags: ["one"] },
|
||||
}
|
||||
|
||||
expect(() => assertWriteAccess("one", session)).toThrow(
|
||||
ContainerTagAccessError,
|
||||
)
|
||||
})
|
||||
|
||||
it("does not throw for a restricted member with explicit write access", () => {
|
||||
const session: SessionInfo = {
|
||||
...baseSession,
|
||||
accessType: "restricted",
|
||||
containerTags: [{ containerTag: "one", permission: "write" }],
|
||||
}
|
||||
|
||||
expect(() => assertWriteAccess("one", session)).not.toThrow()
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -32,3 +32,25 @@ export function effectiveContainerTagAccess(
|
|||
return { containerTag, permission }
|
||||
})
|
||||
}
|
||||
|
||||
// Tools that write (save/forget a memory, upload a file) must call this
|
||||
// before performing the write. effectiveContainerTagAccess above is used
|
||||
// elsewhere only to build picker/dropdown option lists for the widgets --
|
||||
// nothing stops a client from calling a write tool directly with an
|
||||
// arbitrary containerTag, bypassing whatever the UI would have offered.
|
||||
export class ContainerTagAccessError extends Error {
|
||||
constructor(containerTag: string) {
|
||||
super(`You do not have write access to space "${containerTag}".`)
|
||||
this.name = "ContainerTagAccessError"
|
||||
}
|
||||
}
|
||||
|
||||
export function assertWriteAccess(
|
||||
containerTag: string,
|
||||
session: SessionInfo,
|
||||
): void {
|
||||
const [access] = effectiveContainerTagAccess([containerTag], session)
|
||||
if (access?.permission !== "write") {
|
||||
throw new ContainerTagAccessError(containerTag)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { z } from "zod"
|
||||
import { assertWriteAccess } from "../auth/rbac"
|
||||
import { optionalContainerTagSchema } from "../container-tag"
|
||||
import { MEMORY_TOOL_ANNOTATIONS } from "./annotations"
|
||||
import { addMemoryOutputSchema, type AddMemoryOutput } from "./output-schemas"
|
||||
|
|
@ -26,6 +27,9 @@ export function register(deps: ToolDeps) {
|
|||
async (args) => {
|
||||
try {
|
||||
const effectiveTag = await deps.resolveContainerTag(args.containerTag)
|
||||
const session = await deps.getSession()
|
||||
assertWriteAccess(effectiveTag, session)
|
||||
|
||||
const client = deps.getClient(effectiveTag)
|
||||
|
||||
if (args.action === "forget") {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { z } from "zod"
|
||||
import { saveSuccessViewSchema, type ViewMessage } from "../../shared/types"
|
||||
import { appResultMeta, appToolMeta } from "../app-metadata"
|
||||
import { assertWriteAccess } from "../auth/rbac"
|
||||
import { containerTagSchema } from "../container-tag"
|
||||
import { ADDITIVE_MEMORY_TOOL_ANNOTATIONS } from "./annotations"
|
||||
import { textContent, type ToolDeps } from "./types"
|
||||
|
|
@ -21,6 +22,9 @@ export function register(deps: ToolDeps) {
|
|||
},
|
||||
async (args) => {
|
||||
try {
|
||||
const session = await deps.getSession()
|
||||
assertWriteAccess(args.containerTag, session)
|
||||
|
||||
const viewId = args.viewId ?? crypto.randomUUID()
|
||||
const client = deps.getClient(args.containerTag)
|
||||
const result = await client.createMemory(args.content)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue