Skip to content

Commit e9e97ee

Browse files
maskri17copybara-github
authored andcommitted
Add validation for out-of-bounds durations and timestamps in ConvertToJson.
PiperOrigin-RevId: 950916413
1 parent ff55aa8 commit e9e97ee

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

common/values/duration_value.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ absl::Status DurationValue::ConvertToJson(
7575
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
7676
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
7777

78+
CEL_RETURN_IF_ERROR(cel::internal::ValidateDuration(NativeValue()));
79+
7880
ValueReflection value_reflection;
7981
CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
8082
value_reflection.SetStringValueFromDuration(json, NativeValue());

common/values/duration_value_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "common/values/duration_value.h"
16+
1517
#include <sstream>
1618
#include <utility>
1719

20+
#include "absl/status/status.h"
1821
#include "absl/status/status_matchers.h"
1922
#include "absl/time/time.h"
2023
#include "common/native_type.h"
@@ -66,6 +69,16 @@ TEST_F(DurationValueTest, ConvertToJson) {
6669
EXPECT_THAT(*message, EqualsValueTextProto(R"pb(string_value: "0s")pb"));
6770
}
6871

72+
TEST_F(DurationValueTest, ConvertToJsonOutOfBounds) {
73+
auto* message = NewArenaValueMessage();
74+
EXPECT_THAT(UnsafeDurationValue(absl::InfiniteDuration())
75+
.ConvertToJson(descriptor_pool(), message_factory(), message),
76+
absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
77+
EXPECT_THAT(UnsafeDurationValue(-absl::InfiniteDuration())
78+
.ConvertToJson(descriptor_pool(), message_factory(), message),
79+
absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
80+
}
81+
6982
TEST_F(DurationValueTest, NativeTypeId) {
7083
EXPECT_EQ(NativeTypeId::Of(DurationValue(absl::Seconds(1))),
7184
NativeTypeId::For<DurationValue>());

common/values/timestamp_value.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ absl::Status TimestampValue::ConvertToJson(
7575
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
7676
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
7777

78+
CEL_RETURN_IF_ERROR(cel::internal::ValidateTimestamp(NativeValue()));
79+
7880
ValueReflection value_reflection;
7981
CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
8082
value_reflection.SetStringValueFromTimestamp(json, NativeValue());

common/values/timestamp_value_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "common/values/timestamp_value.h"
16+
1517
#include <sstream>
1618

19+
#include "absl/status/status.h"
1720
#include "absl/status/status_matchers.h"
1821
#include "absl/time/time.h"
1922
#include "common/native_type.h"
@@ -56,6 +59,16 @@ TEST_F(TimestampValueTest, ConvertToJson) {
5659
R"pb(string_value: "1970-01-01T00:00:00Z")pb"));
5760
}
5861

62+
TEST_F(TimestampValueTest, ConvertToJsonOutOfBounds) {
63+
auto* message = NewArenaValueMessage();
64+
EXPECT_THAT(UnsafeTimestampValue(absl::InfiniteFuture())
65+
.ConvertToJson(descriptor_pool(), message_factory(), message),
66+
absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
67+
EXPECT_THAT(UnsafeTimestampValue(absl::InfinitePast())
68+
.ConvertToJson(descriptor_pool(), message_factory(), message),
69+
absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
70+
}
71+
5972
TEST_F(TimestampValueTest, NativeTypeId) {
6073
EXPECT_EQ(
6174
NativeTypeId::Of(TimestampValue(absl::UnixEpoch() + absl::Seconds(1))),

0 commit comments

Comments
 (0)