44package tasks
55
66import (
7- "compress/gzip"
87 "context"
98 "database/sql"
109 "errors"
@@ -17,7 +16,6 @@ import (
1716 "time"
1817
1918 "github.com/go-gorp/gorp/v3"
20- "github.com/klauspost/compress/zstd"
2119 "github.com/opencontainers/go-digest"
2220 imagespecs "github.com/opencontainers/image-spec/specs-go/v1"
2321 "github.com/prometheus/client_golang/prometheus"
@@ -815,15 +813,12 @@ func (j *Janitor) checkPreConditionsForTrivy(ctx context.Context, account models
815813
816814 // filter media types that trivy is known to support
817815 for _ , blob := range layerBlobs {
818- if blob .MediaType == imageManifest .DockerV2SchemaLayerMediaTypeUncompressed || blob .MediaType == imageManifest .DockerV2Schema2LayerMediaType || blob .MediaType == imageManifest .DockerV2SchemaLayerMediaTypeZstd ||
819- blob .MediaType == imagespecs .MediaTypeImageLayer || blob .MediaType == imagespecs .MediaTypeImageLayerGzip || blob .MediaType == imagespecs .MediaTypeImageLayerZstd {
820- continue
816+ if blob .Compression () == models .BlobCompressionUnknown { // None = unknown compression method because we don't recognize `blob.MediaType`
817+ securityInfo .VulnerabilityStatus = models .UnsupportedVulnerabilityStatus
818+ securityInfo .Message = fmt .Sprintf ("vulnerability scanning is not supported for blob layers with media type %q" , blob .MediaType )
819+ securityInfo .NextCheckAt = Some (j .timeNow ().Add (j .addJitter (24 * time .Hour )))
820+ return false , layerBlobs , nil
821821 }
822-
823- securityInfo .VulnerabilityStatus = models .UnsupportedVulnerabilityStatus
824- securityInfo .Message = fmt .Sprintf ("vulnerability scanning is not supported for blob layers with media type %q" , blob .MediaType )
825- securityInfo .NextCheckAt = Some (j .timeNow ().Add (j .addJitter (24 * time .Hour )))
826- return false , layerBlobs , nil
827822 }
828823
829824 // can only validate when all blobs are present in the storage
@@ -845,47 +840,32 @@ func (j *Janitor) checkPreConditionsForTrivy(ctx context.Context, account models
845840 }
846841
847842 if blob .BlocksVulnScanning .IsNone () {
848- isUncompressed := blob .MediaType == imageManifest .DockerV2SchemaLayerMediaTypeUncompressed || blob .MediaType == imagespecs .MediaTypeImageLayer
849- isGzip := blob .MediaType == imageManifest .DockerV2Schema2LayerMediaType || blob .MediaType == imagespecs .MediaTypeImageLayerGzip
850- isZstd := blob .MediaType == imageManifest .DockerV2SchemaLayerMediaTypeZstd || blob .MediaType == imagespecs .MediaTypeImageLayerZstd
843+ compression := blob .Compression ()
851844
852845 // when measuring uncompressed size, use LimitReader as a simple but
853846 // effective guard against zip bombs
854847 limitBytes := int64 (1 << 30 * blobUncompressedSizeTooBigGiB )
855848 var numberBytes int64
856849
857- if isUncompressed {
850+ if compression == models . BlobCompressionNone {
858851 numberBytes = int64 (blob .SizeBytes ) //nolint:gosec
859- } else if isGzip || isZstd {
852+ } else {
860853 // uncompress the blob to check if it's too large for Trivy to handle within its allotted timeout
861854 reader , _ , err := j .sd .ReadBlob (ctx , account , blob .StorageID )
862855 if err != nil {
863856 return false , layerBlobs , fmt .Errorf ("cannot read blob %s: %w" , blob .Digest , err )
864857 }
865858 defer reader .Close ()
866859
867- if isGzip {
868- compressedReader , err := gzip .NewReader (reader )
869- if err != nil {
870- return false , layerBlobs , fmt .Errorf ("cannot create gzip reader for blob %s: %w" , blob .Digest , err )
871- }
872- defer compressedReader .Close ()
873-
874- numberBytes , err = io .Copy (io .Discard , io .LimitReader (compressedReader , limitBytes + 1 ))
875- if err != nil {
876- return false , layerBlobs , fmt .Errorf ("cannot decompress gzip blob %s: %w" , blob .Digest , err )
877- }
878- } else if isZstd {
879- compressedReader , err := zstd .NewReader (reader )
880- if err != nil {
881- return false , layerBlobs , fmt .Errorf ("cannot create zstd reader for blob %s: %w" , blob .Digest , err )
882- }
883- defer compressedReader .Close ()
884-
885- numberBytes , err = io .Copy (io .Discard , io .LimitReader (compressedReader , limitBytes + 1 ))
886- if err != nil {
887- return false , layerBlobs , fmt .Errorf ("cannot decompress zstd blob %s: %w" , blob .Digest , err )
888- }
860+ compressedReader , err := compression .Reader (reader )
861+ if err != nil {
862+ return false , layerBlobs , fmt .Errorf ("cannot create %s reader for blob %s: %w" , compression , blob .Digest , err )
863+ }
864+ defer compressedReader .Close ()
865+
866+ numberBytes , err = io .Copy (io .Discard , io .LimitReader (compressedReader , limitBytes + 1 ))
867+ if err != nil {
868+ return false , layerBlobs , fmt .Errorf ("cannot decompress %s blob %s: %w" , compression , blob .Digest , err )
889869 }
890870 }
891871
0 commit comments