From 777d6c5da86a78eb3999cf52053239a0dfa74009 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 20 Sep 2024 15:20:02 -0500 Subject: [PATCH] fix: Output formatting edge-cases found during fuzz test - 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). --- internal/visitor/template_comments.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/visitor/template_comments.go b/internal/visitor/template_comments.go index 909ec27..7ef09b4 100644 --- a/internal/visitor/template_comments.go +++ b/internal/visitor/template_comments.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "text/template" + "unicode/utf8" "github.com/clevyr/yampl/internal/comment" "github.com/clevyr/yampl/internal/config" @@ -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 { @@ -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()