Skip to content

Commit

Permalink
Guard against os-release panic
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Johnson <[email protected]>
  • Loading branch information
jonjohnsonjr committed Jan 21, 2025
1 parent 50e09b6 commit d518c43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/build/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log/slog"
"path/filepath"
"sort"
"strings"
"time"

osr "github.com/dominodatalab/os-release"
Expand Down Expand Up @@ -171,7 +172,18 @@ func readReleaseData(fsys fs.FS) (*osr.Data, error) {
return nil, fmt.Errorf("reading os-release: %w", err)
}

return osr.Parse(string(osReleaseData)), nil
releaseStr := string(osReleaseData)

// osr.Parse can panic if given improper input, so error early instead.
for _, line := range strings.Split(releaseStr, "\n") {
if line != "" {
if len(strings.Split(line, "=")) < 2 {
return nil, fmt.Errorf("invalid os-release line: %q", line)
}
}
}

return osr.Parse(releaseStr), nil
}

func GenerateIndexSBOM(ctx context.Context, o options.Options, ic types.ImageConfiguration, indexDigest name.Digest, imgs map[types.Architecture]oci.SignedImage) ([]types.SBOM, error) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/build/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ func TestReadReleaseData_EmptyDefaults(t *testing.T) {
require.Equal(t, "unknown", info.VersionID)
require.Equal(t, "", info.PrettyName)
}

func TestBadReleaseData(t *testing.T) {
osinfoData := `hello, world! this is not a valid os-release file
`
fsys := apkfs.NewMemFS()
require.NoError(t, fsys.MkdirAll(filepath.Dir("/etc/os-release"), os.FileMode(0o644)))
require.NoError(t, fsys.WriteFile("/etc/os-release", []byte(osinfoData), os.FileMode(0o644)))
// Bad data in file should err.
_, err := readReleaseData(fsys)
require.Error(t, err)
}

0 comments on commit d518c43

Please sign in to comment.