fix(go): parse -X ldflags values without CSV splitting (#11727)

This commit is contained in:
DmitriyLewen authored and GitHub committed 2026-11-26 08:07:21 +00:00
1 parent c14584a6c4
commit 87ab02caa3
2 files changed
+93 -15

No files matched your search

+30 -15
View File
@@ -224,10 +224,12 @@ func (p *Parser) ParseLDFlags(name string, flags []string) string {
fset.ParseErrorsAllowlist.UnknownFlags = true
// The shorthand name is needed here because setting the full name
// to `X` will cause the flag set to look for `--X` instead of `-X`.
// The flag can also be set multiple times, so a string slice is needed
// to handle that edge case.
var x map[string]string
fset.StringToStringVarP(&x, "", "X", nil, "")
// The flag can also be set multiple times, so a string array is needed
// to handle that edge case. Each value is kept as is (no CSV splitting)
// and split on the first `=` below, the same way the Go linker does.
// cf. https://pkg.go.dev/cmd/link (`-X importpath.name=value`)
var xFlags []string
fset.StringArrayVarP(&xFlags, "", "X", nil, "")
// Init `help` flag to avoid error in flags with `h` (e.g. `-lpthread`)
fset.BoolP("help", "h", false, "just to disable the built-in help flag")
if err := fset.Parse(flags); err != nil {
@@ -235,6 +237,30 @@ func (p *Parser) ParseLDFlags(name string, flags []string) string {
return ""
}
// The last value wins for a repeated key, as in the Go linker.
x := make(map[string]string, len(xFlags))
for _, f := range xFlags {
key, val, ok := strings.Cut(f, "=")
if !ok {
continue
}
// It's valid to set the -X flags with quotes so we trim any that might
// have been provided: Ex:
//
// -X main.version=1.0.0
// -X=main.version=1.0.0
// -X 'main.version=1.0.0'
// -X='main.version=1.0.0'
// -X="main.version=1.0.0"
// -X "main.version=1.0.0"
//
// Quotes are trimmed before the assignment so that quoted and unquoted
// forms of the same key overwrite each other.
key = strings.TrimLeft(key, `'"`)
val = strings.TrimRight(val, `'"`)
x[key] = val
}
// foundVersions contains discovered versions by type.
// foundVersions doesn't contain duplicates. Versions are filled into first corresponding category.
// Possible elements(categories):
@@ -243,17 +269,6 @@ func (p *Parser) ParseLDFlags(name string, flags []string) string {
// [2]: Other versions
var foundVersions = make([][]string, 3)
for key, val := range x {
// It's valid to set the -X flags with quotes so we trim any that might
// have been provided: Ex:
//
// -X main.version=1.0.0
// -X=main.version=1.0.0
// -X 'main.version=1.0.0'
// -X='main.version=1.0.0'
// -X="main.version=1.0.0"
// -X "main.version=1.0.0"
key = strings.TrimLeft(key, `'`)
val = strings.TrimRight(val, `'`)
if isVersionXKey(key) && isValidSemVer(val) {
classifyVersion(foundVersions, key, name, val)
}
@@ -617,6 +617,69 @@ func TestParser_ParseLDFlags(t *testing.T) {
},
want: "v1.28.6+k3s2",
},
{
name: "with multi-line flag value containing `=`",
args: args{
name: "github.com/aquasecurity/test",
flags: []string{
"-X",
"main.banner=key=value\nmore",
"-X",
"main.version=1.2.3",
},
},
want: "1.2.3",
},
{
name: "with comma in flag value containing `=`",
args: args{
name: "github.com/aquasecurity/test",
flags: []string{
"-X",
"main.banner=a=b,c",
"-X",
"main.version=1.2.3",
},
},
want: "1.2.3",
},
{
name: "with flag value without `=`",
args: args{
name: "github.com/aquasecurity/test",
flags: []string{
"-X",
"novalue",
"-X",
"main.version=1.2.3",
},
},
want: "1.2.3",
},
{
name: "with quoted and unquoted assignments to the same key",
args: args{
name: "github.com/aquasecurity/test",
flags: []string{
"-X",
`"main.version=1.2.3"`,
"-X",
"main.version=2.0.0",
},
},
want: "2.0.0",
},
{
name: "with comma-separated assignments in a single flag value",
args: args{
name: "github.com/aquasecurity/test",
flags: []string{
"-X",
"main.banner=x,main.version=1.2.3",
},
},
want: "",
},
{
name: "with no flags",
args: args{