-
Notifications
You must be signed in to change notification settings - Fork 189
security(mcp): fix stdio dispatcher type confusion (Z-052) #935
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 |
|---|---|---|
|
|
@@ -385,7 +385,34 @@ func (client *Client) readLoop() { | |
| client.failAll(err) | ||
| return | ||
| } | ||
| // Explicit type validation: distinguish requests/notifications (have | ||
| // method) from responses (no method, has id). Only responses may be | ||
| // delivered to pending callers. Server-initiated requests must never be | ||
| // misdelivered as a response even when the id collides with a pending | ||
| // client request (Z-052). | ||
| if message.Method != "" { | ||
| if message.ID != nil { | ||
| // Server-initiated request: route to request handler and | ||
| // reply with method-not-found so the server does not hang. | ||
| // The client does not currently handle inbound requests, so | ||
| // every method is unknown. Log for diagnostics. | ||
| _, _ = fmt.Fprintf(os.Stderr, "[mcp] server request %q (id %v) not handled: replying method not found\n", message.Method, message.ID) | ||
| client.mu.Lock() | ||
| _ = client.writer.write(rpcMessage{ | ||
| JSONRPC: "2.0", | ||
| ID: message.ID, | ||
| Error: &rpcError{Code: -32601, Message: "method not found: " + message.Method}, | ||
| }) | ||
| client.mu.Unlock() | ||
|
Comment on lines
+399
to
+406
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. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Handle failure to send the error response. Line 401 discards the write error. If stdin is closed or broken, 🤖 Prompt for AI Agents |
||
| } | ||
| // Notifications (no id) and handled server requests are not | ||
| // responses — do not dispatch. | ||
| continue | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| if message.ID == nil { | ||
| // No id and no method: invalid or stray message. Ignore but log | ||
| // for diagnostics to help trace protocol mismatches. | ||
| _, _ = fmt.Fprintf(os.Stderr, "[mcp] ignoring unexpected message without id (method %q)\n", message.Method) | ||
| continue | ||
| } | ||
| id, ok := rpcMessageID(message.ID) | ||
|
|
||
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.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Do not write peer-controlled values to stderr.
message.Methodandmessage.IDcome from the server. An ID can be an opaque credential. Remove these values from the diagnostic, or redact them before logging.As per coding guidelines, “Keep secrets out of argv, env dumps, and logs. Redact success and error paths (including stderr).”
🤖 Prompt for AI Agents
Source: Coding guidelines