|
| 1 | +#include "security_auditor.hpp" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cctype> |
| 5 | + |
| 6 | +#include "config_manager.hpp" |
| 7 | + |
| 8 | +namespace flapi { |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +bool isBcryptPrefix(const std::string& password) { |
| 13 | + if (password.size() < 4) { |
| 14 | + return false; |
| 15 | + } |
| 16 | + if (password[0] != '$' || password[1] != '2' || password[3] != '$') { |
| 17 | + return false; |
| 18 | + } |
| 19 | + const char variant = password[2]; |
| 20 | + return variant == 'a' || variant == 'b' || variant == 'y'; |
| 21 | +} |
| 22 | + |
| 23 | +bool isMd5HexDigest(const std::string& password) { |
| 24 | + if (password.size() != 32) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return std::all_of(password.begin(), password.end(), [](char c) { |
| 28 | + return std::isxdigit(static_cast<unsigned char>(c)) != 0; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +void scanUsers(const std::vector<AuthUser>& users, |
| 33 | + const std::string& location, |
| 34 | + std::vector<SecurityWarning>& out) { |
| 35 | + for (const auto& user : users) { |
| 36 | + const std::string code = SecurityAuditor::classifyPassword(user.password); |
| 37 | + if (code == "AUTH_PLAINTEXT_PASSWORD") { |
| 38 | + out.push_back({ |
| 39 | + code, |
| 40 | + "User '" + user.username + "' has a plaintext password. " |
| 41 | + "Use bcrypt instead (see flapii auth hash, when available).", |
| 42 | + location |
| 43 | + }); |
| 44 | + } else if (code == "AUTH_MD5_PASSWORD") { |
| 45 | + out.push_back({ |
| 46 | + code, |
| 47 | + "User '" + user.username + "' has an MD5-hashed password. " |
| 48 | + "MD5 is cryptographically broken; migrate to bcrypt.", |
| 49 | + location |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +std::string SecurityAuditor::classifyPassword(const std::string& password) { |
| 58 | + if (password.empty()) { |
| 59 | + return {}; |
| 60 | + } |
| 61 | + if (isBcryptPrefix(password)) { |
| 62 | + return {}; |
| 63 | + } |
| 64 | + if (isMd5HexDigest(password)) { |
| 65 | + return "AUTH_MD5_PASSWORD"; |
| 66 | + } |
| 67 | + return "AUTH_PLAINTEXT_PASSWORD"; |
| 68 | +} |
| 69 | + |
| 70 | +std::vector<SecurityWarning> SecurityAuditor::audit(const ConfigManager& config) const { |
| 71 | + std::vector<SecurityWarning> warnings; |
| 72 | + |
| 73 | + for (const auto& endpoint : config.getEndpoints()) { |
| 74 | + scanUsers(endpoint.auth.users, "endpoint " + endpoint.getIdentifier(), warnings); |
| 75 | + } |
| 76 | + |
| 77 | + const auto& mcp = config.getMCPConfig(); |
| 78 | + scanUsers(mcp.auth.users, "mcp.auth", warnings); |
| 79 | + |
| 80 | + if (mcp.enabled && !mcp.auth.enabled) { |
| 81 | + const bool any_mcp_tool = std::any_of( |
| 82 | + config.getEndpoints().begin(), config.getEndpoints().end(), |
| 83 | + [](const EndpointConfig& e) { return e.isMCPTool(); }); |
| 84 | + if (any_mcp_tool) { |
| 85 | + warnings.push_back({ |
| 86 | + "MCP_UNAUTHENTICATED_TOOLS", |
| 87 | + "MCP tools are exposed without authentication (mcp.auth.enabled is false). " |
| 88 | + "Anyone reaching the server can invoke any MCP tool. " |
| 89 | + "Enable mcp.auth.enabled and configure users or JWT before exposing this server.", |
| 90 | + "mcp" |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return warnings; |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace flapi |
0 commit comments