Skip to content

Commit f0df552

Browse files
authored
fix(storage): more strict parsing for HttpResponse (#7707)
1 parent 1b0c0f0 commit f0df552

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

google/cloud/storage/internal/http_response.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Status AsStatus(HttpResponse const& http_response) {
173173
// structured and useful error Status. If the payload fails to parse as JSON,
174174
// we simply attach the full error payload as the Status's message string.
175175
auto json = nlohmann::json::parse(http_response.payload, nullptr, false);
176-
if (json.is_discarded()) return Status(status_code, http_response.payload);
176+
if (!json.is_object()) return Status(status_code, http_response.payload);
177177

178178
// We expect JSON that looks like the following:
179179
// {

google/cloud/storage/internal/http_response_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ TEST(HttpResponseTest, ErrorInfoInvalidJson) {
132132
EXPECT_EQ(AsStatus(HttpResponse{400, kJsonPayload, {}}), expected);
133133
}
134134

135+
TEST(HttpResponseTest, ErrorInfoInvalidOnlyString) {
136+
// Some valid json, but not what we're looking for.
137+
auto constexpr kJsonPayload = R"("uh-oh some error here")";
138+
Status expected{StatusCode::kInvalidArgument, kJsonPayload};
139+
EXPECT_EQ(AsStatus(HttpResponse{400, kJsonPayload, {}}), expected);
140+
}
141+
135142
} // namespace
136143
} // namespace internal
137144
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

0 commit comments

Comments
 (0)