Skip to content

Commit 8c56a03

Browse files
committed
cocoapods-audit
1 parent 775f8e0 commit 8c56a03

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

commands/audit/sca/cocoapods/cocoapods.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"strings"
1919
)
2020

21+
// VersionForMainModule - We don't have information in cocoapods on the current package, or main module, we only have information on its
22+
// dependencies.
2123
const (
2224
VersionForMainModule = "0.0.0"
2325
)
@@ -45,39 +47,46 @@ func GetTechDependencyLocation(directDependencyName, directDependencyVersion str
4547
foundDependency := false
4648
var tempIndex int
4749
for i, line := range lines {
48-
if strings.Contains(line, directDependencyName) {
49-
startLine = i
50-
startCol = strings.Index(line, directDependencyName)
51-
foundDependency = true
52-
tempIndex = i
53-
}
54-
// This means we are in a new dependency (we cannot find dependency name and version together)
55-
if i > tempIndex && foundDependency && strings.Contains(line, "pod") {
56-
foundDependency = false
57-
} else if foundDependency && strings.Contains(line, directDependencyVersion) {
58-
endLine = i
59-
endCol = len(line)
60-
var snippet string
61-
if endLine == startLine {
62-
snippet = lines[startLine][startCol:endCol]
63-
} else {
64-
for snippetLine := 1; snippetLine < endLine-startLine+1; snippetLine++ {
65-
switch snippetLine {
66-
case 0:
67-
snippet += "\n" + lines[snippetLine][startLine:]
68-
case endLine - startLine:
69-
snippet += "\n" + lines[snippetLine][:endCol]
70-
default:
71-
snippet += "\n" + lines[snippetLine]
72-
}
73-
}
50+
foundDependency, tempIndex, startLine, startCol = parsePodLine(line, directDependencyName, directDependencyVersion, descriptorPath, i, tempIndex, startLine, startCol, endLine, endCol, lines, foundDependency, &podPositions)
51+
}
52+
}
53+
return podPositions, nil
54+
}
55+
56+
func parsePodLine(line, directDependencyName, directDependencyVersion, descriptorPath string, i, tempIndex, startLine, startCol, endLine, endCol int, lines []string, foundDependency bool, podPositions *[]*sarif.Location) (bool, int, int, int) {
57+
if strings.Contains(line, directDependencyName) {
58+
startLine = i
59+
startCol = strings.Index(line, directDependencyName)
60+
foundDependency = true
61+
tempIndex = i
62+
}
63+
// This means we are in a new dependency (we cannot find dependency name and version together)
64+
if i > tempIndex && foundDependency && strings.Contains(line, "pod") {
65+
foundDependency = false
66+
} else if foundDependency && strings.Contains(line, directDependencyVersion) {
67+
endLine = i
68+
endCol = len(line)
69+
var snippet string
70+
// if the tech dependency is a one-liner
71+
if endLine == startLine {
72+
snippet = lines[startLine][startCol:endCol]
73+
// else it is more than one line, so we need to parse all lines
74+
} else {
75+
for snippetLine := 0; snippetLine < endLine-startLine+1; snippetLine++ {
76+
switch snippetLine {
77+
case 0:
78+
snippet += "\n" + lines[snippetLine][startLine:]
79+
case endLine - startLine:
80+
snippet += "\n" + lines[snippetLine][:endCol]
81+
default:
82+
snippet += "\n" + lines[snippetLine]
7483
}
75-
podPositions = append(podPositions, sarifutils.CreateLocation(descriptorPath, startLine, endLine, startCol, endCol, snippet))
76-
foundDependency = false
7784
}
7885
}
86+
*podPositions = append(*podPositions, sarifutils.CreateLocation(descriptorPath, startLine, endLine, startCol, endCol, snippet))
87+
foundDependency = false
7988
}
80-
return podPositions, nil
89+
return foundDependency, tempIndex, startLine, startCol
8190
}
8291

8392
func FixTechDependency(dependencyName, dependencyVersion, fixVersion string, descriptorPaths ...string) error {

commands/audit/sca/cocoapods/cocoapods_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ func TestGetTechDependencyLocation(t *testing.T) {
6565
locations, err := GetTechDependencyLocation("GoogleSignIn", "6.2.4", filepath.Join(currentDir, "Podfile"))
6666
assert.NoError(t, err)
6767
assert.Len(t, locations, 1)
68+
assert.Equal(t, *locations[0].PhysicalLocation.Region.StartLine, 4)
69+
assert.Equal(t, *locations[0].PhysicalLocation.Region.StartColumn, 4)
70+
assert.Equal(t, *locations[0].PhysicalLocation.Region.EndLine, 5)
71+
assert.Equal(t, *locations[0].PhysicalLocation.Region.EndColumn, 30)
6872
assert.Equal(t, *locations[0].PhysicalLocation.Region.Snippet.Text, "GoogleSignIn', '~> 6.2.4'")
6973
}
7074

71-
func TestFixTechDependency(t *testing.T) {
75+
func TestFixTechDependencySingleLocation(t *testing.T) {
7276
_, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods"))
7377
defer cleanUp()
7478
currentDir, err := coreutils.GetWorkingDirectory()
@@ -80,3 +84,29 @@ func TestFixTechDependency(t *testing.T) {
8084
lines := strings.Split(string(file), "\n")
8185
assert.Contains(t, lines, "pod 'GoogleSignIn', '~> 6.2.5'")
8286
}
87+
88+
func TestFixTechDependencyMultipleLocations(t *testing.T) {
89+
_, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods"))
90+
defer cleanUp()
91+
currentDir, err := coreutils.GetWorkingDirectory()
92+
assert.NoError(t, err)
93+
err = FixTechDependency("AppAuth", "1.7.5", "1.7.6", filepath.Join(currentDir, "Podfile"))
94+
assert.NoError(t, err)
95+
file, err := os.ReadFile(filepath.Join(currentDir, "Podfile"))
96+
assert.NoError(t, err)
97+
numAppearances := strings.Count(string(file), "pod 'AppAuth', '~> 1.7.6'")
98+
assert.Equal(t, numAppearances, 2)
99+
}
100+
101+
func TestFixTechDependencyNoLocations(t *testing.T) {
102+
_, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods"))
103+
defer cleanUp()
104+
currentDir, err := coreutils.GetWorkingDirectory()
105+
assert.NoError(t, err)
106+
err = FixTechDependency("GoogleSignIn", "1.8.2", "1.8.3", filepath.Join(currentDir, "Podfile"))
107+
assert.NoError(t, err)
108+
file, err := os.ReadFile(filepath.Join(currentDir, "Podfile"))
109+
assert.NoError(t, err)
110+
lines := strings.Split(string(file), "\n")
111+
assert.Contains(t, lines, "pod 'GoogleSignIn', '~> 6.2.4'")
112+
}

tests/testdata/projects/package-managers/cocoapods/Podfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ platform :ios, '9.0'
33
target 'Test' do
44
use_frameworks!
55
pod 'GoogleSignIn', '~> 6.2.4'
6+
pod 'AppAuth', '~> 1.7.5'
7+
pod 'AppAuth', '~> 1.7.5'
68

79
end

0 commit comments

Comments
 (0)