Skip to content

Commit

Permalink
fix: make the spdx output more complete
Browse files Browse the repository at this point in the history
sha1 checksums are commonly checked by existing scanners/tools.
Some of the fields are not set, so set them.

Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha committed Dec 16, 2023
1 parent aff21a6 commit 26b605e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
15 changes: 11 additions & 4 deletions pkg/distro/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bufio"
"compress/gzip"
"crypto/sha1" //nolint:gosec // used only to produce the sha1 checksum field
"crypto/sha256"
"encoding/base64"
"encoding/hex"
Expand Down Expand Up @@ -93,6 +94,7 @@ func ParsePackage(input, output, author, organization, license string) error {
}{
Person: apk.PkgInfo.PkgMaintainer,
},
FilesAnalyzed: true,
LicenseDeclared: pkglicense,
}

Expand Down Expand Up @@ -151,17 +153,21 @@ func ParsePackage(input, output, author, organization, license string) error {
}
}

cksum := sha256.Sum256(buf)
cksumSHA1 := sha1.Sum(buf) //nolint:gosec // used only to produce the sha1 checksum field
cksumSHA256 := sha256.Sum256(buf)

log.Info().Str("name", hdr.Name).
Int("size", bufsz).
Str("cksum", fmt.Sprintf("SHA256:%s", hex.EncodeToString(cksum[:]))).
Str("cksum", fmt.Sprintf("SHA256:%s", hex.EncodeToString(cksumSHA256[:]))).
Msg("file entry detected")

sfile := &spdx.File{
Entity: spdx.Entity{
Name: fmt.Sprintf("/%s", hdr.Name),
Checksum: map[string]string{"SHA256": hex.EncodeToString(cksum[:])},
Name: fmt.Sprintf("/%s", hdr.Name),
Checksum: map[string]string{
"SHA1": hex.EncodeToString(cksumSHA1[:]),
"SHA256": hex.EncodeToString(cksumSHA256[:]),
},
},
}
if err := spkg.AddFile(sfile); err != nil {
Expand Down Expand Up @@ -192,6 +198,7 @@ func InstalledPackage(doc *spdx.Document, pkg *IndexEntry, files []string) error
}{
Person: pkg.PackageMaintainer,
},
FilesAnalyzed: true,
LicenseDeclared: pkg.PackageLicense,
}

Expand Down
15 changes: 11 additions & 4 deletions pkg/distro/deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deb
import (
"archive/tar"
"bufio"
"crypto/sha1" //nolint:gosec // used only to produce the sha1 checksum field
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -48,6 +49,7 @@ func ParsePackage(input, output, author, organization, license string) error {
}{
Person: debfile.Control.Maintainer,
},
FilesAnalyzed: true,
LicenseDeclared: license,
}

Expand Down Expand Up @@ -87,17 +89,21 @@ func ParsePackage(input, output, author, organization, license string) error {
}
}

cksum := sha256.Sum256(buf)
cksumSHA1 := sha1.Sum(buf) //nolint:gosec // used only to produce the sha1 checksum field
cksumSHA256 := sha256.Sum256(buf)

log.Info().Str("name", hdr.Name).
Int("size", bufsz).
Str("cksum", fmt.Sprintf("SHA256:%s", hex.EncodeToString(cksum[:]))).
Str("cksum", fmt.Sprintf("SHA256:%s", hex.EncodeToString(cksumSHA256[:]))).
Msg("file entry detected")

sfile := &spdx.File{
Entity: spdx.Entity{
Name: hdr.Name[1:],
Checksum: map[string]string{"SHA256": hex.EncodeToString(cksum[:])},
Name: hdr.Name[1:],
Checksum: map[string]string{
"SHA1": hex.EncodeToString(cksumSHA1[:]),
"SHA256": hex.EncodeToString(cksumSHA256[:]),
},
},
}
if err := spkg.AddFile(sfile); err != nil {
Expand Down Expand Up @@ -277,6 +283,7 @@ func InstalledPackage(doc *spdx.Document, pkg Package, path string) error {
}{
Person: pkg.Maintainer,
},
FilesAnalyzed: true,
LicenseDeclared: "unknown",
}

Expand Down
32 changes: 26 additions & 6 deletions pkg/distro/rpm/rpm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package rpm

import (
"crypto/sha1" //nolint:gosec // used only to produce the sha1 checksum field
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -87,6 +89,7 @@ func ParsePackage(input, output, author, organization, license string) error {
}{
Organization: vendor[0],
},
FilesAnalyzed: true,
LicenseDeclared: pkglicense,
}

Expand Down Expand Up @@ -119,18 +122,34 @@ func ParsePackage(input, output, author, organization, license string) error {
}
defer fhandle.Close()

shaWriter := sha256.New()
if _, err := io.Copy(shaWriter, fhandle); err != nil {
return err
buf := make([]byte, info.Size())

var bufsz int

if bufsz, err = fhandle.Read(buf); err != nil {
if !errors.Is(err, io.EOF) {
log.Error().Err(err).Str("name", finfo.Name()).Msg("unable to read content")

return err
}
}

cksum := shaWriter.Sum(nil)
cksumSHA1 := sha1.Sum(buf) //nolint:gosec // used only to produce the sha1 checksum field
cksumSHA256 := sha256.Sum256(buf)

log.Info().Str("name", info.Name()).
Int("size", bufsz).
Str("cksum", fmt.Sprintf("SHA256:%s", hex.EncodeToString(cksumSHA256[:]))).
Msg("file entry detected")

sfile := spdx.NewFile()
sfile.SetEntity(
&spdx.Entity{
Name: finfo.Name(),
Checksum: map[string]string{"SHA256": hex.EncodeToString(cksum)},
Name: finfo.Name(),
Checksum: map[string]string{
"SHA1": hex.EncodeToString(cksumSHA1[:]),
"SHA256": hex.EncodeToString(cksumSHA256[:]),
},
},
)

Expand Down Expand Up @@ -162,6 +181,7 @@ func InstalledPackage(doc *spdx.Document, pkg *rpmdb.PackageInfo) error {
}{
Person: pkg.Vendor,
},
FilesAnalyzed: true,
LicenseDeclared: pkg.License,
}

Expand Down

0 comments on commit 26b605e

Please sign in to comment.