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

Fix SanitizeJson when string contains escaped quotes (or nested json) #173

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions LogMonitor/LogMonitorTests/UtilityTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ namespace UtilityTests
expect = L"C:\\\\Drive\\\\XX";
Utility::SanitizeJson(str);
Assert::IsTrue(str == expect, L"should escape \\");

str = L"{\"foo\":\"bar\",\"json_in_json\":\"{\\\"key_inner\\\":\\\"value_inner\\\"}\"}";
expect = L"{\\\"foo\\\":\\\"bar\\\",\\\"json_in_json\\\":\\\"{\\\\\\\"key_inner\\\\\\\":\\\\\\\"value_inner\\\\\\\"}\\\"}";
Utility::SanitizeJson(str);
Assert::IsTrue(str == expect, L"should properly escape json inside json");

}
};
}
1 change: 0 additions & 1 deletion LogMonitor/src/LogMonitor/JsonFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ JsonFileParser::ParseNumber()
//
// End of string.
//
offset++;
AdvanceBufferPointer(offset);

m_doubleValue = negativeValue ? -parsedValue : parsedValue;
Expand Down
52 changes: 17 additions & 35 deletions LogMonitor/src/LogMonitor/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,43 +265,25 @@ bool Utility::isJsonNumber(_In_ std::wstring& str)
///
void Utility::SanitizeJson(_Inout_ std::wstring& str)
{
size_t i = 0;
while (i < str.size()) {
size_t num_esc = 0;
for (size_t i = 0; i < str.size(); i++) {
auto sub = str.substr(i, 1);
if (sub == L"\"") {
if ((i > 0 && str.substr(i - 1, 1) != L"\\")
|| i == 0)
{
str.replace(i, 1, L"\\\"");
i++;
}
if (sub == L"\"" || sub == L"\\" || sub == L"\n" || sub == L"\r") {
num_esc++;
}
else if (sub == L"\\") {
if ((i < str.size() - 1 && str.substr(i + 1, 1) != L"\\")
|| i == str.size() - 1)
{
str.replace(i, 1, L"\\\\");
i++;
}
else {
i += 2;
}
}
else if (sub == L"\n") {
if ((i > 0 && str.substr(i - 1, 1) != L"\\")
|| i == 0)
{
str.replace(i, 1, L"\\n");
i++;
}
}
else if (sub == L"\r") {
if ((i > 0 && str.substr(i - 1, 1) != L"\\")
|| i == 0)
{
str.replace(i, 1, L"\\r");
i++;
}
}

str.reserve(str.size() + num_esc);
for (size_t i = 0; i < str.size(); i++) {
auto sub = str.substr(i, 1);
if (sub == L"\n") {
str.replace(i, 1, L"\\n");
} else if (sub == L"\r") {
str.replace(i, 1, L"\\r");
} else if (sub == L"\"" || sub == L"\\") {
str.insert(i, L"\\");
} else {
continue;
}
i++;
}
Expand Down
Loading