Skip to content

Commit 8383136

Browse files
an-taoCopilot
andauthored
Update orm_lib/src/mysql_impl/MysqlConnection.cc
Co-authored-by: Copilot <[email protected]>
1 parent 8540d9c commit 8383136

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

orm_lib/src/mysql_impl/MysqlConnection.cc

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,47 @@ MysqlConnection::MysqlConnection(trantor::EventLoop *loop,
5959
static MysqlEnv env;
6060
static thread_local MysqlThreadEnv threadEnv;
6161
mysql_init(mysqlPtr_.get());
62+
// Parse SSL parameters from connection string
63+
std::string ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher;
64+
auto connParams = parseConnString(connInfo);
65+
for (auto const &kv : connParams)
66+
{
67+
auto key = kv.first;
68+
auto value = kv.second;
69+
std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c) { return tolower(c); });
70+
if (key == "ssl_key")
71+
{
72+
ssl_key = value;
73+
}
74+
else if (key == "ssl_cert")
75+
{
76+
ssl_cert = value;
77+
}
78+
else if (key == "ssl_ca")
79+
{
80+
ssl_ca = value;
81+
}
82+
else if (key == "ssl_capath")
83+
{
84+
ssl_capath = value;
85+
}
86+
else if (key == "ssl_cipher")
87+
{
88+
ssl_cipher = value;
89+
}
90+
}
91+
// If all SSL parameters are empty, log a warning about certificate verification
92+
if (ssl_key.empty() && ssl_cert.empty() && ssl_ca.empty() && ssl_capath.empty() && ssl_cipher.empty())
93+
{
94+
LOG_WARN << "SSL is enabled for MySQL connection, but no certificate parameters are set. "
95+
<< "This disables certificate verification and may allow man-in-the-middle attacks.";
96+
}
6297
mysql_ssl_set(mysqlPtr_.get(),
63-
nullptr, // key
64-
nullptr, // cert
65-
nullptr, // CA
66-
nullptr, // CApath
67-
nullptr); // cipher
98+
ssl_key.empty() ? nullptr : ssl_key.c_str(),
99+
ssl_cert.empty() ? nullptr : ssl_cert.c_str(),
100+
ssl_ca.empty() ? nullptr : ssl_ca.c_str(),
101+
ssl_capath.empty() ? nullptr : ssl_capath.c_str(),
102+
ssl_cipher.empty() ? nullptr : ssl_cipher.c_str());
68103
mysql_options(mysqlPtr_.get(), MYSQL_OPT_NONBLOCK, nullptr);
69104
#ifdef HAS_MYSQL_OPTIONSV
70105
mysql_optionsv(mysqlPtr_.get(), MYSQL_OPT_RECONNECT, &reconnect_);

0 commit comments

Comments
 (0)