Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit c7e4795

Browse files
juergwcopybara-github
authored andcommitted
Validate that parsed string is a JSON Object.
Without this, parsing may crash if the input is a valid JSON value, but not a JSON Object. PiperOrigin-RevId: 621489078
1 parent 6e5d0ea commit c7e4795

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

cc/core/json_keyset_reader.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <istream>
2121
#include <iterator>
2222
#include <memory>
23-
#include <sstream>
2423
#include <string>
2524
#include <utility>
2625

@@ -34,8 +33,6 @@
3433
#include "include/rapidjson/rapidjson.h"
3534
#include "tink/keyset_reader.h"
3635
#include "tink/util/enums.h"
37-
#include "tink/util/errors.h"
38-
#include "tink/util/protobuf_helper.h"
3936
#include "tink/util/status.h"
4037
#include "tink/util/statusor.h"
4138
#include "proto/tink.pb.h"
@@ -252,6 +249,10 @@ util::StatusOr<std::unique_ptr<Keyset>> JsonKeysetReader::Read() {
252249
"Invalid JSON Keyset: Error (offset ", json_doc.GetErrorOffset(),
253250
"): ", rapidjson::GetParseError_En(json_doc.GetParseError())));
254251
}
252+
if (!json_doc.IsObject()) {
253+
return util::Status(absl::StatusCode::kInvalidArgument,
254+
"Invalid JSON Keyset: Expected object.");
255+
}
255256
return KeysetFromJson(json_doc);
256257
}
257258

cc/core/json_keyset_reader_test.cc

+9
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ TEST_F(JsonKeysetReaderTest, testReadFromString) {
195195
EXPECT_FALSE(read_result.ok());
196196
EXPECT_EQ(absl::StatusCode::kInvalidArgument, read_result.status().code());
197197
}
198+
199+
{ // A valid JSON value, but not a JSON object.
200+
auto reader_result = JsonKeysetReader::New("124");
201+
EXPECT_TRUE(reader_result.ok()) << reader_result.status();
202+
auto reader = std::move(reader_result.value());
203+
auto read_result = reader->Read();
204+
EXPECT_FALSE(read_result.ok());
205+
EXPECT_EQ(absl::StatusCode::kInvalidArgument, read_result.status().code());
206+
}
198207
}
199208

200209
TEST_F(JsonKeysetReaderTest, testReadFromStream) {

0 commit comments

Comments
 (0)