-
Notifications
You must be signed in to change notification settings - Fork 140
fix: add untrusted data wrapper around logs response MCP-197 #548
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,10 @@ | ||||||||||
import { expect, it } from "vitest"; | ||||||||||
import { validateToolMetadata, validateThrowsForInvalidArguments, getResponseElements } from "../../../helpers.js"; | ||||||||||
import { | ||||||||||
validateToolMetadata, | ||||||||||
validateThrowsForInvalidArguments, | ||||||||||
getResponseElements, | ||||||||||
getDataFromUntrustedContent, | ||||||||||
} from "../../../helpers.js"; | ||||||||||
import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js"; | ||||||||||
|
||||||||||
describeWithMongoDB("logs tool", (integration) => { | ||||||||||
|
@@ -36,13 +41,27 @@ describeWithMongoDB("logs tool", (integration) => { | |||||||||
|
||||||||||
const elements = getResponseElements(response); | ||||||||||
|
||||||||||
expect(elements).toHaveLength(2); | ||||||||||
expect(elements[1]?.text).toContain("<untrusted-user-data-"); | ||||||||||
|
||||||||||
const logs = getDataFromUntrustedContent(elements[1]?.text ?? "").split("\n"); | ||||||||||
// Default limit is 50 | ||||||||||
expect(elements.length).toBeLessThanOrEqual(51); | ||||||||||
expect(logs.length).toBeLessThanOrEqual(50); | ||||||||||
|
||||||||||
// Expect at least one log entry | ||||||||||
expect(logs.length).toBeGreaterThan(1); | ||||||||||
|
||||||||||
expect(elements[0]?.text).toMatch(/Found: \d+ messages/); | ||||||||||
const totalMessages = parseInt(elements[0]?.text.match(/Found: (\d+) messages/)?.[1] ?? "0", 10); | ||||||||||
expect(totalMessages).toBeGreaterThanOrEqual(logs.length); | ||||||||||
|
||||||||||
if (totalMessages > logs.length) { | ||||||||||
expect(elements[0]?.text).toContain(`(showing only the first ${logs.length})`); | ||||||||||
} | ||||||||||
|
||||||||||
for (let i = 1; i < elements.length; i++) { | ||||||||||
for (const message of logs) { | ||||||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||||||||||
const log = JSON.parse(elements[i]?.text ?? "{}"); | ||||||||||
const log = JSON.parse(message ?? "{}"); | ||||||||||
expect(log).toHaveProperty("t"); | ||||||||||
expect(log).toHaveProperty("msg"); | ||||||||||
} | ||||||||||
|
@@ -58,9 +77,18 @@ describeWithMongoDB("logs tool", (integration) => { | |||||||||
}); | ||||||||||
|
||||||||||
const elements = getResponseElements(response); | ||||||||||
expect(elements.length).toBeLessThanOrEqual(51); | ||||||||||
for (let i = 1; i < elements.length; i++) { | ||||||||||
const log = JSON.parse(elements[i]?.text ?? "{}") as { tags: string[] }; | ||||||||||
expect(elements).toHaveLength(2); | ||||||||||
expect(elements[1]?.text).toContain("<untrusted-user-data-"); | ||||||||||
|
||||||||||
const logs = getDataFromUntrustedContent(elements[1]?.text ?? "").split("\n"); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as above - the split('\n') operation may produce empty strings that could break JSON parsing. Consider filtering out empty strings:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
// Default limit is 50 | ||||||||||
expect(logs.length).toBeLessThanOrEqual(50); | ||||||||||
|
||||||||||
// Expect at least one log entry | ||||||||||
expect(logs.length).toBeGreaterThan(1); | ||||||||||
|
||||||||||
for (const message of logs) { | ||||||||||
const log = JSON.parse(message ?? "{}") as { tags: string[] }; | ||||||||||
expect(log).toHaveProperty("t"); | ||||||||||
expect(log).toHaveProperty("msg"); | ||||||||||
expect(log).toHaveProperty("tags"); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The split('\n') operation may produce empty strings at the end if the logs data ends with newlines. This could cause the length checks and JSON parsing to fail. Consider filtering out empty strings:
getDataFromUntrustedContent(elements[1]?.text ?? "").split("\n").filter(line => line.trim())
Copilot uses AI. Check for mistakes.