Skip to content

Commit

Permalink
drop exceptions from the json sanitizer
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Marantz <[email protected]>
  • Loading branch information
jmarantz committed Sep 6, 2024
1 parent 9386d34 commit 88b9581
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/common/json/json_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ ObjectSharedPtr Factory::loadFromProtobufStruct(const ProtobufWkt::Struct& proto

std::string Factory::serialize(absl::string_view str) {
nlohmann::json j(str);
return j.dump();
return j.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace);
}

std::vector<uint8_t> Factory::jsonToMsgpack(const std::string& json_string) {
Expand Down
7 changes: 3 additions & 4 deletions source/common/json/json_sanitizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "source/common/json/json_internal.h"

#include "absl/strings/str_format.h"
#include "utf8_validity.h"

namespace Envoy {
namespace Json {
Expand Down Expand Up @@ -65,7 +66,7 @@ absl::string_view sanitize(std::string& buffer, absl::string_view str) {
if (need_slow == 0) {
return str; // Fast path, should be executed most of the time.
}
TRY_ASSERT_MAIN_THREAD {
if (utf8_range::IsStructurallyValid(str)) {
// The Nlohmann JSON library supports serialization and is not too slow. A
// hand-rolled sanitizer can be a little over 2x faster at the cost of added
// production complexity. The main drawback is that this code cannot be used
Expand All @@ -74,9 +75,7 @@ absl::string_view sanitize(std::string& buffer, absl::string_view str) {
// adds complexity to the production code base.
buffer = Nlohmann::Factory::serialize(str);
return stripDoubleQuotes(buffer);
}
END_TRY
catch (std::exception&) {
} else {
// If Nlohmann throws an error, emit a hex escape for any character
// requiring it. This can occur for invalid utf-8 sequences, and we don't
// want to crash the server if such a sequence makes its way into a string
Expand Down

0 comments on commit 88b9581

Please sign in to comment.