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

[backport] fix: JSON truncation caused by escaped zero byte (#1594) #1596

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions core/processor/ProcessorParseJsonNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ bool ProcessorParseJsonNative::JsonLogLineParser(LogEvent& sourceEvent,

std::string ProcessorParseJsonNative::RapidjsonValueToString(const rapidjson::Value& value) {
if (value.IsString())
return value.GetString();
return std::string(value.GetString(), value.GetStringLength());
else if (value.IsBool())
return ToString(value.GetBool());
else if (value.IsInt())
Expand All @@ -193,7 +193,7 @@ std::string ProcessorParseJsonNative::RapidjsonValueToString(const rapidjson::Va
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
value.Accept(writer);
return buffer.GetString();
return std::string(buffer.GetString(), buffer.GetLength());
}
}

Expand Down
60 changes: 60 additions & 0 deletions core/unittest/processor/ProcessorParseJsonNativeUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ProcessorParseJsonNativeUnittest : public ::testing::Test {

void TestInit();
void TestProcessJson();
void TestProcessJsonEscapedNullByte();
void TestAddLog();
void TestProcessEventKeepUnmatch();
void TestProcessEventDiscardUnmatch();
Expand All @@ -46,6 +47,8 @@ UNIT_TEST_CASE(ProcessorParseJsonNativeUnittest, TestAddLog);

UNIT_TEST_CASE(ProcessorParseJsonNativeUnittest, TestProcessJson);

UNIT_TEST_CASE(ProcessorParseJsonNativeUnittest, TestProcessJsonEscapedNullByte);

UNIT_TEST_CASE(ProcessorParseJsonNativeUnittest, TestProcessEventKeepUnmatch);

UNIT_TEST_CASE(ProcessorParseJsonNativeUnittest, TestProcessEventDiscardUnmatch);
Expand Down Expand Up @@ -258,6 +261,63 @@ void ProcessorParseJsonNativeUnittest::TestAddLog() {
processor.GetContext().GetProcessProfile().logGroupSize);
}

void ProcessorParseJsonNativeUnittest::TestProcessJsonEscapedNullByte() {
// make config
Json::Value config;
config["SourceKey"] = "content";
config["KeepingSourceWhenParseFail"] = true;
config["KeepingSourceWhenParseSucceed"] = false;
config["CopingRawLog"] = false;
config["RenamedSourceKey"] = "rawLog";

// make events
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::string inJson = R"({
"events" :
[
{
"contents" :
{
"content" : "{\"level\":\"ERROR\",\"time\":\"2024-07-04T06:59:23.078Z\",\"msg\":\"expect { or n, but found \\u0000, error found in #0 byte of ...\"}"
},
"timestampNanosecond" : 0,
"timestamp" : 12345678901,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson);
// run function
ProcessorParseJsonNative& processor = *(new ProcessorParseJsonNative);
std::string pluginId = "testID";
ProcessorInstance processorInstance(&processor, pluginId);
APSARA_TEST_TRUE_FATAL(processorInstance.Init(config, mContext));
std::vector<PipelineEventGroup> eventGroupList;
eventGroupList.emplace_back(std::move(eventGroup));
processorInstance.Process(eventGroupList);
// judge result
std::string expectJson = R"({
"events" :
[
{
"contents" :
{
"level": "ERROR",
"msg": "expect { or n, but found \u0000, error found in #0 byte of ...",
"time": "2024-07-04T06:59:23.078Z"
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroupList[0].ToJsonString();
APSARA_TEST_STREQ_FATAL(CompactJson(expectJson).c_str(), CompactJson(outJson).c_str());
APSARA_TEST_GT_FATAL(processorInstance.mProcTimeMS->GetValue(), uint64_t(0));
}

void ProcessorParseJsonNativeUnittest::TestProcessJson() {
// make config
Json::Value config;
Expand Down
Loading