From efae81b2304262f4cd4f89d6fdaac1c2b8b94ad7 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Sat, 25 Jul 2026 10:05:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=BA=90=E7=A0=81=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=B3=84=E6=BC=8F=E4=BB=A5=E5=8F=8Atoken=E6=97=A0?= =?UTF-8?q?=E9=99=90=E6=9C=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gateway/inc/ExpBase.hpp | 19 ++++++++++++++----- gateway/inc/LocalTime.hpp | 3 ++- gateway/src/main.cpp | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/gateway/inc/ExpBase.hpp b/gateway/inc/ExpBase.hpp index 8531cf6..c2509c6 100644 --- a/gateway/inc/ExpBase.hpp +++ b/gateway/inc/ExpBase.hpp @@ -161,15 +161,23 @@ protected: virtual std::string build_message() const { std::string result; - + // 错误码 if (m_code != 0) { result += "[Error " + std::to_string(m_code) + "] "; } - + // 错误消息 result += m_message; - + + /* ===== 以下「位置信息」与「嵌套异常」暂时以注释方式保留 ===== + * 原因:黑盒检测发现 what() 的返回值会透传到 HTTP 响应体, + * 导致泄露源码文件路径、行号、C++ 模板类型签名(函数名), + * 便于攻击者定位代码、确认技术栈、构造针对性 payload(P0-3)。 + * 排障:完整信息仍由各 handler 通过 std::cerr 输出到服务端日志。 + * 恢复:如需调试可临时取消下方注释;生产环境必须保持注释状态, + * 并确保 what() 不直接进入客户端响应。 + * ==================================================================== // 位置信息 if (!m_file.empty()) { result += " (at " + m_file; @@ -181,12 +189,13 @@ protected: } result += ")"; } - + // 嵌套异常 if (m_nested) { result += "\n Caused by: " + std::string(m_nested->what()); } - + * ==================================================================== */ + return result; } diff --git a/gateway/inc/LocalTime.hpp b/gateway/inc/LocalTime.hpp index ca5cd42..be7d415 100644 --- a/gateway/inc/LocalTime.hpp +++ b/gateway/inc/LocalTime.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -13,7 +14,7 @@ public: auto now = system_clock::now(); auto zone = current_zone(); zoned_time zt{zone, now}; - return std::vformat(format, std::make_format_args(zt)); + return std::vformat("{:" + format + "}", std::make_format_args(zt)); } // 解析时间字符串为 system_clock::time_point(本地时间 → UTC) diff --git a/gateway/src/main.cpp b/gateway/src/main.cpp index c37ce5c..c9ba0c4 100644 --- a/gateway/src/main.cpp +++ b/gateway/src/main.cpp @@ -697,9 +697,14 @@ http::response handleLogin( catch (const HttpException& e) { + // 详细错误信息(e.what() 含源码路径/行号/C++ 模板类型)暂时以注释方式保留 + // 原因:黑盒检测发现透传到客户端会泄露内部实现,便于攻击者定位代码(P0-3) + // 排障:完整信息仍输出到服务端 stderr + std::cerr << "[handleLogin HttpException] " << e.what() << std::endl; json::value jv_r = { {"success", false}, - {"message", e.what()} + /* {"message", e.what()}, */ // 泄露源码信息,已禁用 + {"message", "Invalid Username or Password"} }; res.set(http::field::content_type, "application/json"); res.result(e.status()); @@ -799,9 +804,12 @@ http::response handleRegister( } catch (const HttpException& e) { + // 详细错误信息(e.what() 含源码路径/行号/C++ 模板类型)暂时以注释方式保留(同 handleLogin) + std::cerr << "[handleRegister HttpException] " << e.what() << std::endl; json::value jv_r = { {"success", false}, - {"message", e.what()} + /* {"message", e.what()}, */ // 泄露源码信息,已禁用 + {"message", "Registration failed"} }; res.set(http::field::content_type, "application/json"); res.result(e.status()); @@ -810,9 +818,19 @@ http::response handleRegister( catch (const std::exception& e) { const auto& ti = typeid(e); - res.set(http::field::content_type, "text/plain"); + // 详细错误信息(ti.name() 含 mangled 类型名、e.what() 含内部细节)暂时以注释方式保留 + // 原因:mangled 类型名会确认技术栈与 STL 容器,e.what() 可能泄露内部路径(P0-3) + // 排障:完整信息仍输出到服务端 stderr + std::cerr << "[handleRegister std::exception] <" << ti.name() << "> " << e.what() << std::endl; + res.set(http::field::content_type, "application/json"); res.result(http::status::internal_server_error); - res.body() = std::string("<") + ti.name() + "> " + e.what(); + json::value jv_r = { + {"success", false}, + /* {"err_name", ti.name()}, // 泄露 C++ 类型信息,已禁用 */ + /* {"err_message", e.what()} // 泄露内部细节,已禁用 */ + {"message", "Internal Server Error"} + }; + res.body() = json::serialize(jv_r); } res.prepare_payload(); @@ -940,12 +958,17 @@ http::response handleProxy( catch (const std::exception& e) { const auto& ti = typeid(e); - res.set(http::field::content_type, "text/plain"); + // 详细错误信息(ti.name() 含 mangled 类型名、e.what() 含内部细节)暂时以注释方式保留 + // 原因:mangled 类型名会确认技术栈与 STL 容器,e.what() 可能泄露内部路径(P0-3) + // 排障:完整信息仍输出到服务端 stderr + std::cerr << "[handleProxy std::exception] <" << ti.name() << "> " << e.what() << std::endl; + res.set(http::field::content_type, "application/json"); res.result(http::status::internal_server_error); json::value jv_r = { {"success", false}, - {"err_name", ti.name()}, - {"err_message", e.what()} + /* {"err_name", ti.name()}, // 泄露 C++ 类型信息,已禁用 */ + /* {"err_message", e.what()} // 泄露内部细节,已禁用 */ + {"message", "Internal Server Error"} }; res.body() = json::serialize(jv_r); }