Skip to content

Commit

Permalink
fix: Output formatting edge-cases found during fuzz test
Browse files Browse the repository at this point in the history
- Ensure multiline strings starting with a tab are output as a single line and quoted.
- Quote base64-encoded binary data to prevent misinterpretation (e.g., "\xdfA7" output as "30E3", then parsed as 30,000).
  • Loading branch information
gabe565 committed Sep 20, 2024
1 parent a543c06 commit 777d6c5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"text/template"
"unicode/utf8"

"github.com/clevyr/yampl/internal/comment"
"github.com/clevyr/yampl/internal/config"
Expand Down Expand Up @@ -122,8 +123,9 @@ func (t TemplateComments) Template(name string, n *yaml.Node, tmplSrc string, tm
return NewNodeError(err, name, n)
}

if buf.String() != n.Value {
log.Debug().Str("to", buf.String()).Msg("updating value")
str := buf.String()
if str != n.Value {
log.Debug().Str("to", str).Msg("updating value")
n.Style = 0

switch tmplTag {
Expand All @@ -139,7 +141,12 @@ func (t TemplateComments) Template(name string, n *yaml.Node, tmplSrc string, tm
n.Kind = content.Kind
n.Value = content.Value
default:
n.SetString(buf.String())
n.SetString(str)
switch {
case n.Style != yaml.LiteralStyle && !utf8.ValidString(str),
strings.HasPrefix(str, "\t"):
n.Style = yaml.DoubleQuotedStyle
}
}

n.Tag = tmplTag.ToYaml()
Expand Down

0 comments on commit 777d6c5

Please sign in to comment.