-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(server-core): enforce memory conversation ownership #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zcxGGmu
wants to merge
8
commits into
VoltAgent:main
Choose a base branch
from
zcxGGmu:fix/issue-1371-memory-ownership-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e64a3c
fix(server-core): enforce memory conversation ownership
MaskerFather 95ba4d9
fix: harden memory conversation ownership checks
MaskerFather 96bcbab
fix: enforce memory ownership guards in adapters
MaskerFather 8c5ff4f
fix: harden guarded memory mutation follow-ups
MaskerFather ca88a84
fix: address memory ownership release metadata
MaskerFather c7c2c28
test: assert guarded memory delete arguments
MaskerFather a4d2f31
chore: sync memory ownership lockfile
MaskerFather 2cf761b
chore: merge upstream main into memory ownership branch
MaskerFather File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
packages/server-core/src/handlers/memory.handlers.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { Memory } from "@voltagent/core"; | ||
| import type { Agent, Logger, ServerProviderDeps, VoltOpsClient } from "@voltagent/core"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { InMemoryStorageAdapter } from "../../../core/src/memory/adapters/storage/in-memory"; | ||
| import { | ||
| handleDeleteMemoryConversation, | ||
| handleGetMemoryConversation, | ||
| handleListMemoryConversationMessages, | ||
| handleUpdateMemoryConversation, | ||
| } from "./memory.handlers"; | ||
|
|
||
| function createAgentWithMemory(agentId: string, agentName: string, memory: Memory): Agent { | ||
| return { | ||
| getFullState: () => ({ | ||
| id: agentId, | ||
| name: agentName, | ||
| instructions: "", | ||
| status: "idle", | ||
| model: "test-model", | ||
| tools: [], | ||
| subAgents: [], | ||
| memory: {}, | ||
| }), | ||
| getMemory: () => memory, | ||
| } as unknown as Agent; | ||
| } | ||
|
|
||
| function createDepsWithAgents(agents: Agent[]): ServerProviderDeps { | ||
| const logger: Logger = { | ||
| trace: vi.fn(), | ||
| debug: vi.fn(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| fatal: vi.fn(), | ||
| child: vi.fn().mockReturnThis(), | ||
| level: "info", | ||
| silent: vi.fn(), | ||
| } as unknown as Logger; | ||
|
|
||
| return { | ||
| agentRegistry: { | ||
| getAgent: vi.fn((agentId: string) => | ||
| agents.find((agent) => agent.getFullState().id === agentId), | ||
| ), | ||
| getAllAgents: vi.fn().mockReturnValue(agents), | ||
| getAgentCount: vi.fn().mockReturnValue(agents.length), | ||
| removeAgent: vi.fn(), | ||
| registerAgent: vi.fn(), | ||
| getGlobalVoltOpsClient: vi.fn().mockReturnValue(undefined as unknown as VoltOpsClient), | ||
| getGlobalLogger: vi.fn().mockReturnValue(logger), | ||
| }, | ||
| workflowRegistry: { | ||
| getWorkflow: vi.fn(), | ||
| getWorkflowsForApi: vi.fn().mockReturnValue([]), | ||
| getWorkflowDetailForApi: vi.fn(), | ||
| getWorkflowCount: vi.fn().mockReturnValue(0), | ||
| on: vi.fn(), | ||
| off: vi.fn(), | ||
| activeExecutions: new Map(), | ||
| resumeSuspendedWorkflow: vi.fn(), | ||
| }, | ||
| triggerRegistry: { | ||
| list: vi.fn().mockReturnValue([]), | ||
| register: vi.fn(), | ||
| registerMany: vi.fn(), | ||
| get: vi.fn(), | ||
| getByPath: vi.fn(), | ||
| unregister: vi.fn(), | ||
| clear: vi.fn(), | ||
| } as any, | ||
| logger, | ||
| } as unknown as ServerProviderDeps; | ||
| } | ||
|
|
||
| describe("memory handlers ownership checks", () => { | ||
| let memory: Memory; | ||
| let deps: ServerProviderDeps; | ||
| const agentId = "agent-1"; | ||
| const ownerUserId = "user-alice"; | ||
| const otherUserId = "user-bob"; | ||
| const conversationId = "conv-private"; | ||
|
|
||
| beforeEach(async () => { | ||
| memory = new Memory({ | ||
| storage: new InMemoryStorageAdapter(), | ||
| }); | ||
|
|
||
| await memory.createConversation({ | ||
| id: conversationId, | ||
| resourceId: agentId, | ||
| userId: ownerUserId, | ||
| title: "Alice Private", | ||
| metadata: {}, | ||
| }); | ||
|
|
||
| await memory.addMessage( | ||
| { | ||
| id: "msg-1", | ||
| role: "user", | ||
| parts: [{ type: "text", text: "Confidential" }], | ||
| }, | ||
| ownerUserId, | ||
| conversationId, | ||
| ); | ||
|
|
||
| deps = createDepsWithAgents([createAgentWithMemory(agentId, "Agent One", memory)]); | ||
| }); | ||
|
|
||
| it("rejects reading a conversation owned by a different authenticated user", async () => { | ||
| const result = await handleGetMemoryConversation(deps, conversationId, { | ||
| agentId, | ||
| requestingUserId: otherUserId, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| expect(result.httpStatus).toBe(403); | ||
| }); | ||
|
|
||
| it("rejects listing messages for a conversation owned by a different authenticated user", async () => { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| const result = await handleListMemoryConversationMessages(deps, conversationId, { | ||
| agentId, | ||
| requestingUserId: otherUserId, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| expect(result.httpStatus).toBe(403); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it("rejects updating a conversation owned by a different authenticated user", async () => { | ||
| const result = await handleUpdateMemoryConversation(deps, conversationId, { | ||
| agentId, | ||
| requestingUserId: otherUserId, | ||
| title: "Bob title", | ||
| } as any); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| expect(result.httpStatus).toBe(403); | ||
|
|
||
| const conversation = await memory.getConversation(conversationId); | ||
| expect(conversation?.title).toBe("Alice Private"); | ||
| }); | ||
|
|
||
| it("rejects deleting a conversation owned by a different authenticated user", async () => { | ||
| const result = await handleDeleteMemoryConversation(deps, conversationId, { | ||
| agentId, | ||
| requestingUserId: otherUserId, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| expect(result.httpStatus).toBe(403); | ||
|
|
||
| await expect(memory.getConversation(conversationId)).resolves.not.toBeNull(); | ||
| }); | ||
|
|
||
| it("allows the owning authenticated user to manage the conversation", async () => { | ||
| const result = await handleGetMemoryConversation(deps, conversationId, { | ||
| agentId, | ||
| requestingUserId: ownerUserId, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (!result.success) return; | ||
| expect(result.data.conversation.userId).toBe(ownerUserId); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.