-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
knqyf263
merged 9 commits into
aquasecurity:main
from
DmitriyLewen:feat/swift-package-resolved
Aug 23, 2023
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eac07cd
add Package.resolved files analyzer
DmitriyLewen ea3c338
add Swift detector and integration test
DmitriyLewen af012f4
refactor after go-dep-parser changes
DmitriyLewen 4bc2b77
Merge branch 'main' into 'feat/swift-package-resolved'
DmitriyLewen a961e8a
Merge branch 'main' into 'feat/swift-package-resolved'
DmitriyLewen 77367ed
bump go-dep-parser
DmitriyLewen a660186
remove replaces
DmitriyLewen 9d1e8b6
use filePath for Required func
DmitriyLewen 3479840
add ID field
DmitriyLewen File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ jobs: | |
c\+\+ | ||
elixir | ||
dart | ||
swift | ||
|
||
os | ||
lang | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
func (a swiftLockAnalyzer) Type() analyzer.Type { | ||
return analyzer.TypeSwift | ||
} | ||
|
||
func (a swiftLockAnalyzer) Version() int { | ||
return version | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/fanal/analyzer/language/swift/swift/testdata/empty/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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