Skip to content

Commit e787e1a

Browse files
fix(swift): try to use branch to resolve version (aquasecurity#6168)
Signed-off-by: knqyf263 <[email protected]> Co-authored-by: knqyf263 <[email protected]>
1 parent 327cf88 commit e787e1a

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

pkg/dependency/parser/swift/swift/parse.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"strings"
77

88
"github.com/liamg/jfather"
9+
"github.com/samber/lo"
910
"golang.org/x/xerrors"
1011

1112
dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
13+
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
1214
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
1315
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
1416
)
@@ -37,10 +39,21 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
3739
}
3840
for _, pin := range pins {
3941
name := libraryName(pin, lockFile.Version)
42+
43+
// Skip packages for which we cannot resolve the version
44+
if pin.State.Version == "" && pin.State.Branch == "" {
45+
log.Logger.Warnf("Unable to resolve %q. Both the version and branch fields are empty.", name)
46+
continue
47+
}
48+
49+
// A Pin can be resolved using `branch` without `version`.
50+
// e.g. https://github.com/element-hq/element-ios/blob/6a9bcc88ea37147efba8f0a7bcf3ec187f4a4011/Riot.xcworkspace/xcshareddata/swiftpm/Package.resolved#L84-L92
51+
version := lo.Ternary(pin.State.Version != "", pin.State.Version, pin.State.Branch)
52+
4053
libs = append(libs, types.Library{
41-
ID: utils.PackageID(name, pin.State.Version),
54+
ID: utils.PackageID(name, version),
4255
Name: name,
43-
Version: pin.State.Version,
56+
Version: version,
4457
Locations: []types.Location{
4558
{
4659
StartLine: pin.StartLine,

pkg/dependency/parser/swift/swift/parse_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ func TestParser_Parse(t *testing.T) {
3737
},
3838
},
3939
},
40-
// docker run -it --rm swift@sha256:45e5e44ed4873063795f150182437f4dbe7d5527ba5655979d7d11e0829179a7
41-
// mkdir app && cd app
42-
// swift package init
43-
// ## add new deps: ##
44-
// sed -i 's/],/],\ndependencies: [\n.package(url: "https:\/\/github.com\/ReactiveCocoa\/ReactiveSwift", from: "7.0.0"),\n.package(url: "https:\/\/github.com\/Quick\/Quick.git", from: "7.0.0"),\n.package(url: "https:\/\/github.com\/Quick\/Nimble.git", .exact("9.2.1")),\n],/' Package.swift
45-
// swift package update
4640
{
4741
name: "happy path v2",
4842
inputFile: "testdata/happy-v2-Package.resolved",
@@ -65,6 +59,12 @@ func TestParser_Parse(t *testing.T) {
6559
Version: "7.1.1",
6660
Locations: []types.Location{{StartLine: 39, EndLine: 47}},
6761
},
62+
{
63+
ID: "github.com/element-hq/[email protected]",
64+
Name: "github.com/element-hq/swift-ogg",
65+
Version: "0.0.1",
66+
Locations: []types.Location{{StartLine: 48, EndLine: 56}},
67+
},
6868
{
6969
ID: "github.com/mattgallagher/[email protected]",
7070
Name: "github.com/mattgallagher/CwlCatchException",

pkg/dependency/parser/swift/swift/testdata/happy-v2-Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/dependency/parser/swift/swift/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Pin struct {
2020
}
2121

2222
type State struct {
23-
Branch any `json:"branch"`
23+
Branch string `json:"branch"`
2424
Revision string `json:"revision"`
2525
Version string `json:"version"`
2626
}

0 commit comments

Comments
 (0)