Skip to content

Commit

Permalink
fix: do not allow empty items in deb.provides (#606)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <[email protected]>

Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Jan 24, 2023
1 parent 656cf86 commit daae772
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
13 changes: 12 additions & 1 deletion deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ Installed-Size: {{.InstalledSize}}
{{- with .Info.Replaces}}
Replaces: {{join .}}
{{- end }}
{{- with .Info.Provides}}
{{- with nonEmpty .Info.Provides}}
Provides: {{join .}}
{{- end }}
{{- with .Info.Depends}}
Expand Down Expand Up @@ -833,6 +833,17 @@ func writeControl(w io.Writer, data controlData) error {
ret := strings.ReplaceAll(strs, "\n", "\n ")
return strings.Trim(ret, " \n")
},
"nonEmpty": func(strs []string) []string {
var result []string
for _, s := range strs {
s := strings.TrimSpace(s)
if s == "" {
continue
}
result = append(result, s)
}
return result
},
})
return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
}
16 changes: 16 additions & 0 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,3 +1397,19 @@ func TestGlob(t *testing.T) {
},
}), io.Discard))
}

func TestBadProvides(t *testing.T) {
var w bytes.Buffer
info := exampleInfo()
info.Provides = []string{" "}
require.NoError(t, writeControl(&w, controlData{
Info: nfpm.WithDefaults(info),
}))
golden := "testdata/bad_provides.golden"
if *update {
require.NoError(t, os.WriteFile(golden, w.Bytes(), 0o600))
}
bts, err := os.ReadFile(golden) //nolint:gosec
require.NoError(t, err)
require.Equal(t, string(bts), w.String())
}
14 changes: 14 additions & 0 deletions deb/testdata/bad_provides.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: foo
Version: 1.0.0
Section: default
Priority: extra
Architecture: amd64
Maintainer: Carlos A Becker <[email protected]>
Installed-Size: 0
Replaces: svn
Depends: bash
Recommends: git
Suggests: bash
Conflicts: zsh
Homepage: http://carlosbecker.com
Description: Foo does things

0 comments on commit daae772

Please sign in to comment.