Skip to content
Open
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
11 changes: 7 additions & 4 deletions common/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ func (s String) ConvertToType(typeVal ref.Type) ref.Val {
return durationOf(d)
}
case TimestampType:
if t, err := time.Parse(time.RFC3339, s.Value().(string)); err == nil {
if t.Unix() < minUnixTime || t.Unix() > maxUnixTime {
return celErrTimestampOverflow
str := s.Value().(string)
if strictRFC3339Pattern.MatchString(str) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer early return on an error for an invalid format as this will provide a more helpful message to the user (or agent).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The pattern check now returns early with invalid RFC 3339 timestamp "<value>" rather than falling through to the generic conversion error, and the regression test asserts the exact message for each rejected form. go test ./... still passes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a little benchmark to see the perf impact of the regex?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added BenchmarkStringConvertToTimestamp over the conversion path. On an M4, converting a valid 2025-01-01T12:34:56.123456789Z runs around 600 ns/op at 1 alloc/op. The regex MatchString is the bulk of that (~500 ns/op, 0 allocs) against ~90 ns/op for the bare time.Parse, so the gate adds no allocations but does roughly 6x the parse cost. Happy to swap the regex for a small hand-rolled scan if that overhead matters on the hot path.

if t, err := time.Parse(time.RFC3339, str); err == nil {
if t.Unix() < minUnixTime || t.Unix() > maxUnixTime {
return celErrTimestampOverflow
}
return timestampOf(t)
}
return timestampOf(t)
}
case StringType:
return s
Expand Down
29 changes: 29 additions & 0 deletions common/types/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,35 @@ func TestStringConvertToType(t *testing.T) {
}
}

func TestStringConvertToTimestampStrict(t *testing.T) {
valid := []string{
"2025-01-17T01:00:00.001Z",
"2025-01-01T12:34:56Z",
"2025-01-01T12:34:56.123456789Z",
"2025-01-01T12:34:56+05:30",
"2025-01-01T12:34:56-08:00",
"2025-01-01T12:34:56+14:00",
}
for _, s := range valid {
if IsError(String(s).ConvertToType(TimestampType)) {
t.Errorf("String(%q).ConvertToType(TimestampType) errored, wanted a timestamp", s)
}
}
// RFC 3339 violations that time.Parse accepts loosely.
invalid := []string{
"2025-01-17T01:00:00,001Z", // ',' fractional separator
"2025-01-17T1:00:00Z", // single-digit hour
"2025-01-17T01:5:00Z", // single-digit minute
"2025-01-18T01:01:01.001+24:01", // offset hour out of range
"2025-01-17T01:01:01.001+00:60", // offset minute out of range
}
for _, s := range invalid {
if !IsError(String(s).ConvertToType(TimestampType)) {
t.Errorf("String(%q).ConvertToType(TimestampType) succeeded, wanted an error", s)
}
}
}

func TestStringEqual(t *testing.T) {
if !String("hello").Equal(String("hello")).(Bool) {
t.Error("Two equivalent strings were not equal")
Expand Down
10 changes: 10 additions & 0 deletions common/types/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package types
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -52,6 +53,15 @@ const (
maxUnixTime int64 = 253402300799
)

// strictRFC3339Pattern gates the strings accepted by the `timestamp()` overload.
// time.Parse accepts inputs that RFC 3339 forbids: a ',' fractional-second
// separator, single-digit time fields, and numeric offsets whose hours exceed
// 23 or minutes exceed 59. Those slip past unnoticed and shift the parsed
// instant, so they are rejected before time.Parse runs. The remaining calendar
// validation (month, day, leap year) is left to time.Parse.
var strictRFC3339Pattern = regexp.MustCompile(
`^\d{4}-\d{2}-\d{2}[Tt]([01]\d|2[0-3]):[0-5]\d:([0-5]\d|60)(\.\d+)?([Zz]|[+-]([01]\d|2[0-3]):[0-5]\d)$`)

// Add implements traits.Adder.Add.
func (t Timestamp) Add(other ref.Val) ref.Val {
switch other.Type() {
Expand Down