Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOGL-8507 : Taken care of quotes and slash in asset name #1432

Merged
merged 22 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions C/common/asset_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <logger.h>
#include <asset_tracking.h>
#include <config_category.h>
#include "string_utils.h"

using namespace std;

Expand Down Expand Up @@ -228,6 +229,7 @@ void AssetTracker::addAssetTrackingTuple(string plugin, string asset, string eve

}

asset = escape(asset);
AssetTrackingTuple tuple(m_service, plugin, asset, event);
addAssetTrackingTuple(tuple);
}
Expand Down
11 changes: 10 additions & 1 deletion C/common/datapoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ int bscount = 0;
{
if (str[i] == '\\')
{
if (i + 1 < str.length() && (str[i + 1] == '"' || str[i + 1] == '\\' || str[i + 1] == '/'|| str[i-1] == '\\'))
{
rval += '\\';
}
else
{
rval += "\\\\";
}
bscount++;
}
else if (str[i] == '\"')
Expand All @@ -339,13 +347,14 @@ int bscount = 0;
{
rval += "\\"; // Add escape of "
}
rval += str[i];
bscount = 0;
}
else
{
rval += str[i];
bscount = 0;
}
rval += str[i];
}
return rval;
}
Expand Down
3 changes: 2 additions & 1 deletion C/common/include/datapoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <dpimage.h>
#include <databuffer.h>
#include <rapidjson/document.h>
#include "string_utils.h"

class Datapoint;
/**
Expand Down Expand Up @@ -325,7 +326,7 @@ class Datapoint {
*/
std::string toJSONProperty()
{
std::string rval = "\"" + m_name + "\":";
std::string rval = "\"" + escape(m_name) + "\":";
rval += m_value.toString();

return rval;
Expand Down
5 changes: 5 additions & 0 deletions C/common/include/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ bool IsRegex(const std::string &str);
std::string StringAround(const std::string& str, unsigned int pos,
unsigned int after = 30, unsigned int before = 10);

void replicate(std::string& StringToManage,
const std::string& StringToSearch,
const std::string& StringReplicate);

std::string escape(const std::string& str);

#endif
11 changes: 10 additions & 1 deletion C/common/reading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ int bscount = 0;
{
if (str[i] == '\\')
{
if (i + 1 < str.length() && (str[i + 1] == '"' || str[i + 1] == '\\' || str[i + 1] == '/' || str[i-1] == '\\'))
{
rval += '\\';
}
else
{
rval += "\\\\";
}
bscount++;
}
else if (str[i] == '\"')
Expand All @@ -556,13 +564,14 @@ int bscount = 0;
{
rval += "\\"; // Add escape of "
}
rval += str[i];
bscount = 0;
}
else
{
rval += str[i];
bscount = 0;
}
rval += str[i];
}
return rval;
}
Expand Down
68 changes: 68 additions & 0 deletions C/common/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,71 @@ std::string StringAround(const std::string& str, unsigned int pos,
size_t len = before + after;
return str.substr(start, len);
}

/**
* Replicate a character/substring within an string
*
* @param out StringToManage string in which apply the search and replicate
* @param StringToSearch string to search and replicate
* @param StringReplicate substitution string
*
*/
void replicate(std::string& StringToManage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replicate is a bad name for this. Probably replaceAll us better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here function is replicating a single character to double character. Also StringReplaceAll function is already there but will not work in this scenario. That's why I created new function. I will rename the function as StringReplaceAllEx to make it more intuitive.

const std::string& StringToSearch,
const std::string& StringReplicate)
{
size_t pos = 0;
while ((pos = StringToManage.find(StringToSearch, pos)) != std::string::npos)
{
StringToManage.replace(pos, StringToSearch.length(), StringReplicate);
pos += StringReplicate.length(); // Move past the last replaced substring
}

}

/**
* Escape double quotes, forward and backword slash
*
* @param str The string to escape
* @return The escaped string
*/
std::string escape(const std::string& str)
{
size_t pos = 0;
if (str.find("\"", pos) == std::string::npos && str.find("\\", pos) == std::string::npos && str.find("/", pos) == std::string::npos)
return str; //return if none of the following character exists '"' , "\" , "/"

std::string rval;
int bscount = 0;
for (size_t i = 0; i < str.length(); i++)
{
if (str[i] == '\\')
{
if (i + 1 < str.length() && (str[i + 1] == '"' || str[i + 1] == '\\' || str[i + 1] == '/' || str[i-1] == '\\'))
{
rval += '\\';
}
else
{
rval += "\\\\";
}
bscount++;
}
else if (str[i] == '\"')
{
if ((bscount & 1) == 0) // not already escaped
{
rval += "\\"; // Add escape of "
}
rval += str[i];
bscount = 0;
}
else
{
rval += str[i];
bscount = 0;
}
}
return rval;
}

9 changes: 7 additions & 2 deletions C/plugins/storage/postgres/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/time.h>

#include "json_utils.h"
#include "string_utils.h"

#include <iostream>
#include <chrono>
Expand Down Expand Up @@ -1336,7 +1337,7 @@ SQLBuffer sql;
value.Accept(writer);

std::string buffer_escaped = "\"";
buffer_escaped.append(escape_double_quotes(buffer.GetString()));
buffer_escaped.append(escape_double_quotes(escape(buffer.GetString())));
buffer_escaped.append( "\"");

sql.append('\'');
Expand Down Expand Up @@ -1691,7 +1692,11 @@ bool add_row = false;

// Handles - asset_code
sql.append(",\'");
sql.append(asset_code);
std::string escaped_asset(asset_code);
std::string target ="'";
std::string replacement ="''";
replicate(escaped_asset, target, replacement);
sql.append(escaped_asset);
sql.append("', '");

// Handles - reading
Expand Down
4 changes: 2 additions & 2 deletions C/plugins/storage/sqlite/common/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ std::size_t arr = data.find("inserts");
}
else
{
sqlite3_bind_text(stmt, columID, escape(str).c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, columID, str, -1, SQLITE_TRANSIENT);
}
}
else if (itr->value.IsDouble()) {
Expand Down Expand Up @@ -1742,7 +1742,7 @@ bool allowZero = false;
Writer<StringBuffer> writer(buffer);
value.Accept(writer);
sql.append('\'');
sql.append(buffer.GetString());
sql.append(escape(buffer.GetString()));
sql.append('\'');
}
sql.append(")");
Expand Down
13 changes: 10 additions & 3 deletions C/plugins/storage/sqlite/common/readings_catalogue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sqlite_common.h>
#include "readings_catalogue.h"
#include <purge_configuration.h>
#include "json_utils.h"

using namespace std;
using namespace rapidjson;
Expand Down Expand Up @@ -2025,7 +2026,10 @@ ReadingsCatalogue::tyReadingReference ReadingsCatalogue::getReadingReference(Co

string msg;
bool success;

std::string escaped_asset = std::string(asset_code);
std::string target ="\"";
std::string replacement ="\"\"";
replicate(escaped_asset, target, replacement);
int startReadingsId;
tyReadingsAvailable readingsAvailable;

Expand Down Expand Up @@ -2153,14 +2157,14 @@ ReadingsCatalogue::tyReadingReference ReadingsCatalogue::getReadingReference(Co
"INSERT INTO " READINGS_DB ".asset_reading_catalogue (table_id, db_id, asset_code) VALUES ("
+ to_string(ref.tableId) + ","
+ to_string(ref.dbId) + ","
+ "\"" + asset_code + "\")";
+ "\"" + escaped_asset + "\")";

Logger::getLogger()->debug("getReadingReference - allocate a new reading table for the asset '%s' db Id %d readings Id %d ", asset_code, ref.dbId, ref.tableId);

}
else
{
sql_cmd = " UPDATE " READINGS_DB ".asset_reading_catalogue SET asset_code ='" + string(asset_code) + "'" +
sql_cmd = " UPDATE " READINGS_DB ".asset_reading_catalogue SET asset_code ='" + string(escaped_asset) + "'" +
" WHERE db_id = " + to_string(ref.dbId) + " AND table_id = " + to_string(ref.tableId) + ";";

Logger::getLogger()->debug("getReadingReference - Use empty table %readings_%d_%d: ",ref.dbId,ref.tableId);
Expand Down Expand Up @@ -2539,6 +2543,9 @@ string ReadingsCatalogue::sqlConstructMultiDb(string &sqlCmdBase, vector<string

dbName = generateDbName(item.second.getDatabase());
dbReadingsName = generateReadingsName(item.second.getDatabase(), item.second.getTable());
std::string target ="\"";
std::string replacement ="\"\"";
replicate(assetCode, target, replacement);

StringReplaceAll(sqlCmdTmp, "_assetcode_", assetCode);
StringReplaceAll (sqlCmdTmp, ".assetcode.", "asset_code");
Expand Down
4 changes: 2 additions & 2 deletions C/plugins/storage/sqlitelb/common/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ std::size_t arr = data.find("inserts");
}
else
{
sqlite3_bind_text(stmt, columID, escape(str).c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, columID, str, -1, SQLITE_TRANSIENT);
}
}
else if (itr->value.IsDouble()) {
Expand Down Expand Up @@ -1854,7 +1854,7 @@ vector<string> asset_codes;
Writer<StringBuffer> writer(buffer);
value.Accept(writer);
sql.append('\'');
sql.append(buffer.GetString());
sql.append(escape(buffer.GetString()));
sql.append('\'');
}
sql.append(")");
Expand Down
3 changes: 3 additions & 0 deletions C/services/south/ingest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <thread>
#include <logger.h>
#include <set>
#include "string_utils.h"
#include <ingest_rate.h>

using namespace std;
Expand Down Expand Up @@ -622,6 +623,7 @@ void Ingest::processQueue()
{
Reading *reading = *it;
string assetName = reading->getAssetName();
assetName = escape(assetName);
const std::vector<Datapoint *> dpVec = reading->getReadingData();
std::string temp;
std::set<std::string> tempSet;
Expand Down Expand Up @@ -870,6 +872,7 @@ void Ingest::processQueue()
{
Reading *reading = *it;
string assetName = reading->getAssetName();
assetName = escape(assetName);
const std::vector<Datapoint *> dpVec = reading->getReadingData();
std::string temp;
std::set<std::string> tempSet;
Expand Down
6 changes: 4 additions & 2 deletions C/tasks/statistics_history/stats_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <csignal>
#include <time.h>
#include <sys/time.h>
#include "string_utils.h"

#define DATETIME_MAX_LEN 52
#define MICROSECONDS_FORMAT_LEN 10
Expand Down Expand Up @@ -137,7 +138,8 @@ void StatsHistory::processKey(const std::string& key, std::vector<InsertValues>
// Insert the row into the statistics history
// create an object of InsertValues and push in historyValues vector
// for batch insertion
iValue.push_back(InsertValue("key", key.c_str()));
string escaped_key = escape(key);
iValue.push_back(InsertValue("key", escaped_key));
iValue.push_back(InsertValue("value", val - prev));
iValue.push_back(InsertValue("history_ts", dateTimeStr));

Expand All @@ -147,7 +149,7 @@ void StatsHistory::processKey(const std::string& key, std::vector<InsertValues>
// create an object of InsertValue and push in updateValues vector
// for batch updation
InsertValue *updateValue = new InsertValue("previous_value", val);
Where *wKey = new Where("key", Equals, key);
Where *wKey = new Where("key", Equals, escaped_key);
updateValues.emplace_back(updateValue, wKey);
}

Expand Down