Skip to content

Commit

Permalink
internal/encoding: don't interpret file as UTF8 with binary encoding
Browse files Browse the repository at this point in the history
Using, for example, `@embed(type=binary)` CUE would interpret the file's
contents as UTF8. Since not all byte-sequences are valid UTF8, some will not be
interpretable. Bytes sequences that fail to be properly interpreted will not
raise an evaluation error but instead yield erroneus bytes to the evaluator.

This change makes it so the file's contents will be passed to the evaluator
verbatim by skipping the UTF8 decoder and reading directly from the filereader.

Signed-off-by: nichtsundniemand <rufus.schaefing@gmail.com>
  • Loading branch information
nichtsundniemand committed Feb 22, 2025
1 parent 1a57169 commit 3c3696f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ func NewDecoder(ctx *cue.Context, f *build.File, cfg *Config) *Decoder {
i.err = err
i.expr = ast.NewString(string(b))
case build.Binary:
b, err := io.ReadAll(r)
// Binary files should not generally be treated as UTF-8. Don't use the
// [transform.Reader] created above but read directly from the file instead.
b, err := io.ReadAll(srcr)
i.err = err
s := literal.Bytes.WithTabIndent(1).Quote(string(b))
i.expr = ast.NewLit(token.STRING, s)
Expand Down

0 comments on commit 3c3696f

Please sign in to comment.