Repository navigation
Conversation
The CEL strings extension spec says that a bytes argument to the %s clause of string.format is "formatted as if string(value) was performed and any invalid UTF-8 sequences are replaced with \ufffd", with multiple adjacent invalid sequences collapsed into a single \ufffd. The v2 formatter appended the raw bytes, so "%s".format([b"\xff"]) produced a CEL string that is not valid UTF-8 (string(b"\xff") is an error), and functions such as size() then counted each stray byte as a code point. Use strings.ToValidUTF8, which replaces each run of invalid bytes with one replacement character, matching the spec. The change also covers bytes nested in lists and maps, and leaves the legacy (pre-v4) formatter unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The strings extension spec says a
bytesargument to%sis "formatted as ifstring(value)was performed and any invalid UTF-8 sequences are replaced with�", with adjacent invalid sequences collapsed into one�.appendingFormatterV2appended the raw bytes, so the result could be a CEL string that is not valid UTF-8 (details in the issue).This uses
strings.ToValidUTF8(string(argBytes), "�"), which replaces each run of invalid bytes with a single U+FFFD. Bytes nested in lists and maps go through the same code. The legacy formatter used before strings version 4 is unchanged.Fixes #1643
Three cases are added to
TestStringFormatV2: the two vectors from cel-expr/cel-spec#473 (b"\xF0abc\x8C\xF0xyz"andb"\xF0\x8C\xF0") and bytes inside a list.Local checks with Go 1.27.1 on macOS arm64,
master@ 0ef120e (Bazel is not installed here, so I usedgo test):mastergo test ./ext/ -run TestStringFormatV2with the new casesgo test ./...go vet ./ext/,gofmt -lon the changed filesThis change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.