Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Package.resolved swift files support #4932

Merged
merged 9 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
c\+\+
elixir
dart
swift

os
lang
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,7 @@ require (
// oras 1.2.2 is incompatible with github.com/docker/docker v24.0.2
// cf. https://github.com/oras-project/oras-go/pull/527
replace oras.land/oras-go => oras.land/oras-go v1.2.4-0.20230801060855-932dd06d38af

replace github.com/aquasecurity/trivy-db => github.com/DmitriyLewen/trivy-db v0.0.0-20230802111732-8c360541bf36

replace github.com/aquasecurity/go-dep-parser => github.com/DmitriyLewen/go-dep-parser v0.0.0-20230823055443-ec6151858b68
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/CycloneDX/cyclonedx-go v0.7.2-0.20230625092137-07e2f29defc3 h1:NqeV+ZMqpcosu0Xg2VW14Ru9ayBs/toe2oihS7sN6Xo=
github.com/CycloneDX/cyclonedx-go v0.7.2-0.20230625092137-07e2f29defc3/go.mod h1:fGXSp1lCDfMQ8KR1EjxT4ewc5HHhGczRF2pWhLSWohs=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DmitriyLewen/go-dep-parser v0.0.0-20230823055443-ec6151858b68 h1:fg9DCKZx1y60lYEUqHd0+LXlGEuPdstB2gzVsghG2V0=
github.com/DmitriyLewen/go-dep-parser v0.0.0-20230823055443-ec6151858b68/go.mod h1:0+GvQF0gL4YEAAUPpNeLeGpFDxMvvIHLMd7vk9bpwko=
github.com/DmitriyLewen/trivy-db v0.0.0-20230802111732-8c360541bf36 h1:UOikjBd2rqcrUQe0Fjk++p0zVfwAITf1krvRjrg4fow=
github.com/DmitriyLewen/trivy-db v0.0.0-20230802111732-8c360541bf36/go.mod h1:st35g9O0ecDGBqnRHNFFz8imlDD3GWTCwo1WyJixI2c=
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible h1:juIaKLLVhqzP55d8x4cSVgwyQv76Z55/fRv/UBr2KkQ=
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible/go.mod h1:BB1eHdMLYEFuFdBlRMb0N7YGVdM5s6Pt0njxgvfbGGs=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
Expand Down Expand Up @@ -325,8 +329,6 @@ github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986 h1:2a30
github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986/go.mod h1:NT+jyeCzXk6vXR5MTkdn4z64TgGfE5HMLC8qfj5unl8=
github.com/aquasecurity/defsec v0.91.1 h1:dBIPm6Tva9I+ZTQv+6t9wob3ZlMSu8NFqMJr4mgJC5A=
github.com/aquasecurity/defsec v0.91.1/go.mod h1:l/srzxtuuyb6c6FlqUvMp3xw2ZbvuZ0l9972MNJM7V8=
github.com/aquasecurity/go-dep-parser v0.0.0-20230816082938-c86bfd152132 h1:SiiJwsijT2zgXJLGAPc5xXYH6QAnZjfsegm6vi2h/qo=
github.com/aquasecurity/go-dep-parser v0.0.0-20230816082938-c86bfd152132/go.mod h1:0+GvQF0gL4YEAAUPpNeLeGpFDxMvvIHLMd7vk9bpwko=
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce h1:QgBRgJvtEOBtUXilDb1MLi1p1MWoyFDXAu5DEUl5nwM=
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce/go.mod h1:HXgVzOPvXhVGLJs4ZKO817idqr/xhwsTcj17CLYY74s=
github.com/aquasecurity/go-mock-aws v0.0.0-20230328195059-5bf52338aec3 h1:Vt9y1gZS5JGY3tsL9zc++Cg4ofX51CG7PaMyC5SXWPg=
Expand All @@ -345,8 +347,6 @@ github.com/aquasecurity/testdocker v0.0.0-20230111101738-e741bda259da h1:pj/adfN
github.com/aquasecurity/testdocker v0.0.0-20230111101738-e741bda259da/go.mod h1:852lbQLpK2nCwlR4ZLYIccxYCfoQao6q9Nl6tjz54v8=
github.com/aquasecurity/tml v0.6.1 h1:y2ZlGSfrhnn7t4ZJ/0rotuH+v5Jgv6BDDO5jB6A9gwo=
github.com/aquasecurity/tml v0.6.1/go.mod h1:OnYMWY5lvI9ejU7yH9LCberWaaTBW7hBFsITiIMY2yY=
github.com/aquasecurity/trivy-db v0.0.0-20230817085355-15ce04b6527c h1:vsWd8uL6C1MoeYhEaJFQU+Rcx93Qv1eFe6N1BO2LjUs=
github.com/aquasecurity/trivy-db v0.0.0-20230817085355-15ce04b6527c/go.mod h1:iJSGMMclPEhkYeyiN9i+gzjV9jhEv+XfPzfVgFhfvTE=
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728 h1:0eS+V7SXHgqoT99tV1mtMW6HL4HdoB9qGLMCb1fZp8A=
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728/go.mod h1:Ldya37FLi0e/5Cjq2T5Bty7cFkzUDwTcPeQua+2M8i8=
github.com/aquasecurity/trivy-kubernetes v0.5.7-0.20230814115812-7afa52705226 h1:fL4BpAfnLFruHqkomRDAB7Lv8yv3zuKdg71mZk9y61c=
Expand Down
9 changes: 9 additions & 0 deletions integration/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ func TestRepository(t *testing.T) {
},
golden: "testdata/dotnet.json.golden",
},
{
name: "swift",
args: args{
scanner: types.VulnerabilityScanner,
listAllPkgs: true,
input: "testdata/fixtures/repo/swift",
},
golden: "testdata/swift.json.golden",
},
{
name: "cocoapods",
args: args{
Expand Down
14 changes: 14 additions & 0 deletions integration/testdata/fixtures/db/swift.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- bucket: "swift::GitHub Security Advisory Swift"
pairs:
- bucket: github.com/apple/swift-nio
pairs:
- key: CVE-2022-3215
value:
PatchedVersions:
- "2.29.1"
- "2.39.1"
- "2.42.0"
VulnerableVersions:
- "< 2.29.1"
- ">= 2.39.0, < 2.39.1"
- ">= 2.41.0, < 2.42.0"
18 changes: 18 additions & 0 deletions integration/testdata/fixtures/db/vulnerability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,24 @@
- https://github.com/advisories/GHSA-4rgh-jx4f-qfcq
PublishedDate: "2022-05-24T17:37:16Z"
LastModifiedDate: "2022-10-06T20:26:08Z"
- key: CVE-2022-3215
value:
Title: "SwiftNIO vulnerable to Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')"
Description: "`NIOHTTP1` and projects using it for generating HTTP responses, including SwiftNIO, can be subject to a HTTP Response Injection attack..."
Severity: MEDIUM
VendorSeverity:
ghsa: 2
CVSS:
ghsa:
V3Vector: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N"
V3Score: 5.3
References:
- https://github.com/apple/swift-nio/security/advisories/GHSA-7fj7-39wj-c64f
- https://nvd.nist.gov/vuln/detail/CVE-2022-3215
- https://github.com/apple/swift-nio/commit/a16e2f54a25b2af217044e5168997009a505930f
- https://github.com/advisories/GHSA-7fj7-39wj-c64f
PublishedDate: "2023-06-07T16:01:53Z"
LastModifiedDate: "2023-06-19T16:45:07Z"
- key: CVE-2022-24775
value:
Title: "Improper Input Validation in guzzlehttp/psr7"
Expand Down
25 changes: 25 additions & 0 deletions integration/testdata/fixtures/repo/swift/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
"state": {
"branch": null,
"revision": "6c89474e62719ddcc1e9614989fff2f68208fe10",
"version": "1.1.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio",
"state": {
"branch": null,
"revision": "ece5057615d1bee848341eceafdf04ca54d60177",
"version": "2.41.0"
}
}
]
},
"version": 1
}
76 changes: 76 additions & 0 deletions integration/testdata/swift.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"SchemaVersion": 2,
"ArtifactName": "testdata/fixtures/repo/swift",
"ArtifactType": "repository",
"Metadata": {
"ImageConfig": {
"architecture": "",
"created": "0001-01-01T00:00:00Z",
"os": "",
"rootfs": {
"type": "",
"diff_ids": null
},
"config": {}
}
},
"Results": [
{
"Target": "Package.resolved",
"Class": "lang-pkgs",
"Type": "swift",
"Packages": [
{
"Name": "github.com/apple/swift-atomics",
"Version": "1.1.0",
"Layer": {},
"Locations": [
{
"StartLine": 4,
"EndLine": 12
}
]
},
{
"Name": "github.com/apple/swift-nio",
"Version": "2.41.0",
"Layer": {},
"Locations": [
{
"StartLine": 13,
"EndLine": 21
}
]
}
],
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2022-3215",
"PkgName": "github.com/apple/swift-nio",
"InstalledVersion": "2.41.0",
"FixedVersion": "2.29.1, 2.39.1, 2.42.0",
"Status": "fixed",
"Layer": {},
"PrimaryURL": "https://avd.aquasec.com/nvd/cve-2022-3215",
"Title": "SwiftNIO vulnerable to Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')",
"Description": "`NIOHTTP1` and projects using it for generating HTTP responses, including SwiftNIO, can be subject to a HTTP Response Injection attack...",
"Severity": "MEDIUM",
"CVSS": {
"ghsa": {
"V3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N",
"V3Score": 5.3
}
},
"References": [
"https://github.com/apple/swift-nio/security/advisories/GHSA-7fj7-39wj-c64f",
"https://nvd.nist.gov/vuln/detail/CVE-2022-3215",
"https://github.com/apple/swift-nio/commit/a16e2f54a25b2af217044e5168997009a505930f",
"https://github.com/advisories/GHSA-7fj7-39wj-c64f"
],
"PublishedDate": "2023-06-07T16:01:53Z",
"LastModifiedDate": "2023-06-19T16:45:07Z"
}
]
}
]
}
5 changes: 5 additions & 0 deletions pkg/detector/library/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func NewDriver(libType string) (Driver, bool) {
// Only semver can be used for version ranges
// https://docs.conan.io/en/latest/versioning/version_ranges.html
comparer = compare.GenericComparer{}
case ftypes.Swift:
// Swift uses semver
// https://www.swift.org/package-manager/#importing-dependencies
ecosystem = vulnerability.Swift
comparer = compare.GenericComparer{}
case ftypes.Cocoapods:
log.Logger.Warn("CocoaPods is supported for SBOM, not for vulnerability scanning")
return Driver{}, false
Expand Down
1 change: 1 addition & 0 deletions pkg/fanal/analyzer/all/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language/rust/binary"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language/rust/cargo"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language/swift/cocoapods"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language/swift/swift"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/licensing"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os/alpine"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os/amazonlinux"
Expand Down
1 change: 1 addition & 0 deletions pkg/fanal/analyzer/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const (
TypeMixLock Type = "mix-lock"

// Swift
TypeSwift Type = "swift"
TypeCocoaPods Type = "cocoapods"

// Dart
Expand Down
45 changes: 45 additions & 0 deletions pkg/fanal/analyzer/language/swift/swift/swift.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package swift

import (
"context"
"os"

"github.com/aquasecurity/go-dep-parser/pkg/swift/swift"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
"github.com/aquasecurity/trivy/pkg/fanal/types"

"golang.org/x/xerrors"
)

func init() {
analyzer.RegisterAnalyzer(&swiftLockAnalyzer{})
}

const (
version = 1
)

// swiftLockAnalyzer analyzes Package.resolved files
type swiftLockAnalyzer struct{}

func (a swiftLockAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
p := swift.NewParser()
res, err := language.Analyze(types.Swift, input.FilePath, input.Content, p)
if err != nil {
return nil, xerrors.Errorf("%s parse error: %w", input.FilePath, err)
}
return res, nil
}

func (a swiftLockAnalyzer) Required(_ string, fileInfo os.FileInfo) bool {
return fileInfo.Name() == types.SwiftResolved
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think other analyzers use filePath.

Suggested change
func (a swiftLockAnalyzer) Required(_ string, fileInfo os.FileInfo) bool {
return fileInfo.Name() == types.SwiftResolved
}
func (a swiftLockAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return path.Base(filePath) == types.SwiftResolved
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm.. i don't know why i used FileInfo...
Thanks! Changed in 9d1e8b6


func (a swiftLockAnalyzer) Type() analyzer.Type {
return analyzer.TypeSwift
}

func (a swiftLockAnalyzer) Version() int {
return version
}
85 changes: 85 additions & 0 deletions pkg/fanal/analyzer/language/swift/swift/swift_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package swift

import (
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)

func Test_swiftLockAnalyzer_Analyze(t *testing.T) {
tests := []struct {
name string
inputFile string
want *analyzer.AnalysisResult
}{
{
name: "happy path",
inputFile: "testdata/happy/Package.resolved",
want: &analyzer.AnalysisResult{
Applications: []types.Application{
{
Type: types.Swift,
FilePath: "testdata/happy/Package.resolved",
Libraries: types.Packages{

{
Name: "github.com/Quick/Nimble",
Version: "9.2.1",
Locations: []types.Location{
{
StartLine: 4,
EndLine: 12,
},
},
},
{
Name: "github.com/Quick/Quick",
Version: "7.0.0",
Locations: []types.Location{
{
StartLine: 13,
EndLine: 21,
},
},
},
{
Name: "github.com/ReactiveCocoa/ReactiveSwift",
Version: "7.1.1",
Locations: []types.Location{
{
StartLine: 22,
EndLine: 30,
},
},
},
},
},
},
},
},
{
name: "empty file",
inputFile: "testdata/empty/Package.resolved",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.inputFile)
require.NoError(t, err)
defer f.Close()

a := swiftLockAnalyzer{}
got, err := a.Analyze(nil, analyzer.AnalysisInput{
FilePath: tt.inputFile,
Content: f,
})

assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}