This commit is contained in:
ANIRUDDHA ADAK 2026-09-26 06:34:42 +05:30 • committed by GitHub
commit fc8b51c27b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 5 deletions

View file

@ -210,3 +210,29 @@ describe("ClaudeMemoryTool str_replace replacement literalness", () => {
expect(stored).toContain(`price is ${dollarSequence} today`)
})
})
describe("ClaudeMemoryTool path traversal", () => {
let tool: ClaudeMemoryTool
beforeEach(() => {
documentsListMock.mockReset()
documentsGetMock.mockReset()
addMock.mockReset()
mockDocument(FILE_CONTENT)
tool = new ClaudeMemoryTool("test-api-key")
})
it.each(["/memories/..", "/memories/foo/..", "/memories/../secrets.txt"])(
"rejects parent-directory path %s",
async (path) => {
const result = await tool.handleCommand({
command: "view",
path,
})
expect(result.success).toBe(false)
expect(result.error).toContain("Invalid path")
expect(documentsListMock).not.toHaveBeenCalled()
},
)
})

View file

@ -805,11 +805,15 @@ export class ClaudeMemoryTool {
* Validate that path starts with /memories for security
*/
private isValidPath(path: string): boolean {
return (
(path.startsWith("/memories/") || path === "/memories") &&
!path.includes("../") &&
!path.includes("..\\")
)
if (!(path.startsWith("/memories/") || path === "/memories")) {
return false
}
if (path.includes("..\\")) {
return false
}
// Reject any parent-directory segment, including trailing "/.." which
// the previous "../" substring check missed (e.g. "/memories/..").
return !path.split("/").some((segment) => segment === "..")
}
}