Skip to content

Commit

Permalink
fix: escape single quotes in sqlite manager
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasTurina committed Jan 14, 2025
1 parent 55e1a9c commit acc88b4
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/agent/persistence/src/sqlite_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <SQLiteCpp/SQLiteCpp.h>
#include <fmt/format.h>
#include <map>
#include <regex>

const std::map<ColumnType, std::string> MAP_COL_TYPE_STRING {
{ColumnType::INTEGER, "INTEGER"}, {ColumnType::TEXT, "TEXT"}, {ColumnType::REAL, "REAL"}};
Expand All @@ -14,6 +15,18 @@ const std::map<OrderType, std::string> MAP_ORDER_STRING {{OrderType::ASC, "ASC"}

SQLiteManager::~SQLiteManager() = default;

namespace
{
const std::string& TO_SEARCH = "'";
const std::string& TO_REPLACE = "''";

/// @brief Escapes single quotes in a string.
std::string EscapeSingleQuotes(const std::string& str)
{
return std::regex_replace(str, std::regex(TO_SEARCH), TO_REPLACE);
}
} // namespace

ColumnType SQLiteManager::ColumnTypeFromSQLiteType(const int type) const
{
if (type == SQLite::INTEGER)
Expand Down Expand Up @@ -79,7 +92,8 @@ void SQLiteManager::Insert(const std::string& tableName, const Row& cols)
names.push_back(col.Name);
if (col.Type == ColumnType::TEXT)
{
values.push_back(fmt::format("'{}'", col.Value));
auto escapedValue = EscapeSingleQuotes(col.Value);
values.push_back(fmt::format("'{}'", escapedValue));
}
else
values.push_back(col.Value);
Expand Down Expand Up @@ -107,7 +121,8 @@ void SQLiteManager::Update(const std::string& tableName,
{
if (col.Type == ColumnType::TEXT)
{
setFields.push_back(fmt::format("{}='{}'", col.Name, col.Value));
auto escapedValue = EscapeSingleQuotes(col.Value);
setFields.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
Expand All @@ -124,7 +139,8 @@ void SQLiteManager::Update(const std::string& tableName,
{
if (col.Type == ColumnType::TEXT)
{
conditions.push_back(fmt::format("{}='{}'", col.Name, col.Value));
auto escapedValue = EscapeSingleQuotes(col.Value);
conditions.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
Expand All @@ -149,7 +165,8 @@ void SQLiteManager::Remove(const std::string& tableName, const Criteria& selCrit
{
if (col.Type == ColumnType::TEXT)
{
critFields.push_back(fmt::format("{}='{}'", col.Name, col.Value));
auto escapedValue = EscapeSingleQuotes(col.Value);
critFields.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
Expand Down Expand Up @@ -217,9 +234,14 @@ std::vector<Row> SQLiteManager::Select(const std::string& tableName,
for (const auto& col : selCriteria)
{
if (col.Type == ColumnType::TEXT)
conditions.push_back(fmt::format("{}='{}'", col.Name, col.Value));
{
auto escapedValue = EscapeSingleQuotes(col.Value);
conditions.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
conditions.push_back(fmt::format("{}={}", col.Name, col.Value));
}
}
condition = fmt::format("WHERE {}", fmt::join(conditions, fmt::format(" {} ", MAP_LOGOP_STRING.at(logOp))));
}
Expand Down Expand Up @@ -280,9 +302,14 @@ int SQLiteManager::GetCount(const std::string& tableName, const Criteria& selCri
for (const auto& col : selCriteria)
{
if (col.Type == ColumnType::TEXT)
conditions.push_back(fmt::format("{}='{}'", col.Name, col.Value));
{
auto escapedValue = EscapeSingleQuotes(col.Value);
conditions.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
conditions.push_back(fmt::format("{}={}", col.Name, col.Value));
}
}
condition = fmt::format("WHERE {}", fmt::join(conditions, fmt::format(" {} ", MAP_LOGOP_STRING.at(logOp))));
}
Expand Down Expand Up @@ -340,9 +367,14 @@ size_t SQLiteManager::GetSize(const std::string& tableName,
for (const auto& col : selCriteria)
{
if (col.Type == ColumnType::TEXT)
conditions.push_back(fmt::format("{}='{}'", col.Name, col.Value));
{
auto escapedValue = EscapeSingleQuotes(col.Value);
conditions.push_back(fmt::format("{}='{}'", col.Name, escapedValue));
}
else
{
conditions.push_back(fmt::format("{}={}", col.Name, col.Value));
}
}
condition = fmt::format("WHERE {}", fmt::join(conditions, fmt::format(" {} ", MAP_LOGOP_STRING.at(logOp))));
}
Expand Down

0 comments on commit acc88b4

Please sign in to comment.