Fix: Record policy refusals in the MCP audit log

Every policy denial - read-only mode, one of the Mcp* switches turned
off, or a host that lacks the required capability - answers -32006. The
audit log only ever showed CALL_FAIL for those, which lumped them
together with parameter errors, offline hosts and timeouts, so after the
fact there was no way to tell "nobody called this" from "it was blocked
by policy".

The hooks all live in one place: the tools/call exit in the HTTP
handler now also reads error.code and error.message from the response it
already parses, and appends a CALL_DENIED event when the code is -32006.
The original CALL_FAIL line is still written, so its statistics keep
their old meaning, and the reason text is the fixed English sentence
each branch already returns - no tool arguments or user input reach the
log. Tool names taken from the request are flattened to a single line
first, both in the denied event and in the existing CALL_OK/CALL_FAIL
lines, so a crafted name cannot forge extra audit rows.

Also refresh the stale note in the design document that still called the
client search MAX_PATH overflow unfixed; the source was fixed in
8f718d9, but already-deployed clients still carry it, which is why the
smoke test keeps its search cases opt-in.

Verified on the Debug instance: with McpFileTransfer temporarily set to
0, download_file returns -32006 and the audit log gains the expected
CALL_DENIED line right after its CALL_FAIL; with the switch restored the
same call is CALL_OK again and no denied line appears. The read-only
smoke test passes 20/20.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:28:16 +01:00
1 parent 16f23e164c
commit d9a1a9ebcb
2 files changed
+42 -5

No files matched your search

+26 -4
View File
@@ -6528,17 +6528,39 @@ void CMcpServer::HandleMcp(const httplib::Request& req, httplib::Response& res)
// 成败以返回体顶层是否有 "error" 成员为准(解析一次,避免正文里出现 "error" 字样时误判)
bool failed = false;
int errCode = 0;
std::string errMsg;
{
Json::Value parsed;
Json::CharReaderBuilder rb;
std::string perrs;
std::istringstream pis(resp);
if (Json::parseFromStream(rb, pis, &parsed, &perrs))
failed = parsed.isObject() && parsed.isMember("error");
if (Json::parseFromStream(rb, pis, &parsed, &perrs) &&
parsed.isObject() && parsed.isMember("error")) {
failed = true;
const Json::Value& err = parsed["error"];
if (err.isObject()) {
if (err["code"].isInt()) errCode = err["code"].asInt();
if (err["message"].isString()) errMsg = err["message"].asString();
}
}
}
// 单行不变量:工具名来自请求参数,控制字符一律换成空格,避免伪造出额外审计行
auto oneLine = [](std::string v) {
for (char& c : v)
if ((unsigned char)c < 0x20) c = ' ';
return v;
};
const std::string toolTag =
"tool=" + oneLine(toolName.empty() ? std::string("(empty)") : toolName);
AuditMcpEvent(failed ? "CALL_FAIL" : "CALL_OK", clientIp,
"tool=" + (toolName.empty() ? std::string("(empty)") : toolName) +
" ms=" + std::to_string(ms) + " bytes=" + std::to_string(resp.size()));
toolTag + " ms=" + std::to_string(ms) + " bytes=" + std::to_string(resp.size()));
// 策略拒绝(-32006:只读模式、功能开关关闭、主机不支持)另记一条 CALL_DENIED,
// 事后才分得清「没人调用」和「被策略拦下」。reason 全是固定的英文句式,不含
// 调用参数或用户输入,可以直接落盘。
if (errCode == -32006)
AuditMcpEvent("CALL_DENIED", clientIp, toolTag + " reason=" + oneLine(errMsg));
res.set_content(resp, "application/json");
return;