-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathios.go
319 lines (256 loc) · 8.54 KB
/
ios.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package insider
import (
"context"
"errors"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/insidersec/insider/engine"
"github.com/insidersec/insider/report"
)
var (
extractLibraryFromPodfile = regexp.MustCompile(`pod\s'(?P<name>[[:alnum:]]+)'(?:,\s'(?:~>|>=|<=|)(?P<version>\d\.\d\.\d|\s\d\.\d\.\d|\s\d\.\d)|)`)
extractBundleID = regexp.MustCompile(`<key>BUNDLE_ID</key>(?:\n*.*)<string>(.*)</string>`)
plistFilesFilter = regexp.MustCompile(`.plist`)
informativeFilesFilter = regexp.MustCompile(`.xcodeproj|.plist`)
extractLibraryFromCartfile = regexp.MustCompile(`(git|github|binary)\s['"](?P<name>[a-zA-Z0-9\/\:\.\-]*)['"]\s(?:(?:~>\s|==\s|>=\s|<=\s|")(?P<version>\d+\.\d+\.\d+|\d+\.\d+)|\"(?P<branch>[a-zA-Z]+)\"|)`)
cartfileFilter = regexp.MustCompile(`(?i)cartfile`)
cartfileResolverFilter = regexp.MustCompile(`Cartfile.resolved`)
podfileFilter = regexp.MustCompile(`(?i)(?:\.Podfile|Podfile)`)
)
// PlistPermission holds data about how the app will use certain permissions
type PlistPermission struct {
Name string `json:"name"`
Usage string `json:"usage"`
}
// ATSDomain is a domain put in the exceptions settings for App Transport Security
type ATSDomain struct {
Name string `json:"name"`
RequiresFowardSecrecy bool `json:"requiresFowardSecrecy"` // NSExceptionRequiresForwardSecrecy
IncludesSubdomains bool `json:"includesSubdomains"` // NSIncludesSubdomains
AllowsInsecureHTTPLoads bool `json:"allowInsecureHTTPLoads"` // NSTemporaryExceptionAllowsInsecureHTTPLoads
}
// ATS holds data about rules in the ATS section
type ATS struct {
AllowArbitraryLoads bool `json:"arbitraryLoads"` // NSAllowsArbitraryLoads
ExceptionDomains []ATSDomain `json:"exceptionDomains"` // NSExceptionDomains
}
// Plist structure holds data in the Property List
type Plist struct {
Compiler string `json:"compiler"` // DTCompiler
PlatformName string `json:"platformName"` // DTPlatformName
PlatformBuild string `json:"platformBuild"` // DTPlatformBuild
PlatformVersion string `json:"platformVersion"` // DTPlatformVersion
XCodeVersion string `json:"xcodeVersion"` // DTXcode
XCodeBuild string `json:"xcodeBuildNumber"` // DTXcodeBuild
SDKName string `json:"sdkName"` // DTSDKName
SDKBuild string `json:"DTSDKBuild"` // DTSDKBuild
BundleName string `json:"bundleName"` // CFBundleName
BundleVersion string `json:"bundleVersion"` // CFBundleVersion
ExecutableName string `json:"executableName"` // CFBundleExecutable
DisplayName string `json:"displayName"` // CFBundleDisplayName
AppIdentifier string `json:"appIdentifier"` // CFBundleIdentifier
PackageType string `json:"packageType"` // CFBundlePackageType
MinimumOSVersion string `json:"minOSVersion"` // MinimumOSVersion
Permissions []PlistPermission `json:"permissions"` // NS*UsageDescription section
ATS ATS `json:"ats"` // NSAppTransportSecurity section
SupportedPlatforms []string `json:"supportedPlatforms"` // CFBundleSupportedPlatforms section
}
type IOSAnalyzer struct {
logger *log.Logger
}
func NewIosAnalyzer(logger *log.Logger) IOSAnalyzer {
return IOSAnalyzer{
logger: logger,
}
}
func (a IOSAnalyzer) Analyze(ctx context.Context, dir string) (report.Reporter, error) {
var r report.IOSReporter
if err := a.analyzeSource(ctx, &r, dir); err != nil {
return nil, err
}
return r, nil
}
func (a IOSAnalyzer) analyzeSource(ctx context.Context, r *report.IOSReporter, dir string) error {
a.logger.Println("Analysing IOS libraries")
libraries, err := a.extracLibraries(dir)
if err != nil {
return err
}
r.Libraries = libraries
a.logger.Println("Analysing IOS plist files")
if err := a.analyzePlist(dir, r); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
a.logger.Printf("Not found plist files at %s\n", dir)
}
return nil
}
func (a IOSAnalyzer) analyzePlist(dir string, rep *report.IOSReporter) error {
files, err := engine.FindInputFiles(dir, true, findInfoFiles)
if err != nil {
return err
}
if len(files) == 0 {
return fmt.Errorf("info %w", os.ErrNotExist)
}
var actualAppDir engine.InputFile
for _, file := range files {
if file.IsDir {
if strings.Contains(file.Name, ".xcodeproj") {
if actualAppDir.PhysicalPath == "" {
actualAppDir = file
continue
} else if len(actualAppDir.PhysicalPath) > len(file.PhysicalPath) {
actualAppDir = file
}
}
}
}
if len(actualAppDir.PhysicalPath) == 0 {
return fmt.Errorf("info %w", os.ErrNotExist)
}
rep.IOSInfo.AppName = strings.Split(actualAppDir.Name, ".xcodeproj")[0]
mainAppDir := strings.Split(actualAppDir.PhysicalPath, ".xcodeproj")[0]
mainAppFiles, err := engine.FindInputFiles(mainAppDir, false, findPListFiles)
if err != nil {
return err
}
for _, mainAppFile := range mainAppFiles {
results := extractBundleID.FindStringSubmatch(mainAppFile.Content)
if results != nil {
rep.IOSInfo.BinaryID = results[1]
}
}
return nil
}
func (a IOSAnalyzer) extracLibraries(dir string) ([]report.Library, error) {
libraries := make([]report.Library, 0)
podfileLibraries, err := a.extractLibsFromPodfiles(dir)
if err != nil {
return nil, err
}
libraries = append(libraries, podfileLibraries...)
cartfileLibraries, err := a.extractLibsFromCartfiles(dir)
if err != nil {
return nil, err
}
libraries = append(libraries, cartfileLibraries...)
return libraries, nil
}
func (a IOSAnalyzer) extractLibsFromCartfiles(dir string) ([]report.Library, error) {
libraries := make([]report.Library, 0)
files, err := engine.FindInputFiles(dir, false, isCartfileResolved)
if err != nil {
return nil, err
}
if len(files) <= 0 {
// If we do not find a Cartfile.resolved, use the Cartfile instead
files, err = engine.FindInputFiles(dir, false, isCartfile)
if err != nil {
return nil, err
}
}
for _, file := range files {
libs, err := a.extractLibsFromCartfile(file)
if err != nil {
return nil, err
}
libraries = append(libraries, libs...)
}
return libraries, nil
}
func (a IOSAnalyzer) extractLibsFromPodfiles(dir string) ([]report.Library, error) {
libraries := make([]report.Library, 0)
files, err := engine.FindInputFiles(dir, false, isPodfile)
if err != nil {
return nil, err
}
for _, file := range files {
libs, err := extractLibsFromPodfile(file)
if err != nil {
return nil, err
}
libraries = append(libraries, libs...)
}
return libraries, nil
}
func (a IOSAnalyzer) extractLibsFromCartfile(file engine.InputFile) (libraries []report.Library, err error) {
libs := extractLibsFromFile(file.Content, extractLibraryFromCartfile)
for _, lib := range libs {
library := report.Library{
Name: lib[2],
Source: lib[1],
}
if lib[3] != "" {
library.Version = lib[3]
} else if lib[4] != "" {
library.Version = lib[4]
} else {
library.Version = "latest"
}
libraries = append(libraries, library)
}
return
}
func extractLibsFromFile(content string, extractor *regexp.Regexp) [][]string {
return extractor.FindAllStringSubmatch(content, -1)
}
func extractLibsFromPodfile(file engine.InputFile) ([]report.Library, error) {
libraries := make([]report.Library, 0)
libs := extractLibsFromFile(file.Content, extractLibraryFromPodfile)
for _, lib := range libs {
library := report.Library{
Name: lib[1],
Source: "CocoaPod",
}
if lib[2] != "" {
library.Version = strings.TrimSpace(lib[2])
} else {
library.Version = "latest"
}
libraries = append(libraries, library)
}
return libraries, nil
}
func isMacosx(path string) bool {
return strings.Contains(path, "__MACOSX")
}
func isPodfile(path string) bool {
// Ignore __MACOSX files since it's generally metadata
if isMacosx(path) {
return false
}
return podfileFilter.MatchString(path)
}
func isCartfileResolved(path string) bool {
// Ignore __MACOSX files since it's generally metadata
if isMacosx(path) {
return false
}
return cartfileResolverFilter.MatchString(path)
}
func isCartfile(path string) bool {
// Ignore __MACOSX files since it's generally metadata
if isMacosx(path) {
return false
}
return cartfileFilter.MatchString(path)
}
func findInfoFiles(path string) bool {
// Ignore __MACOSX files since it's generally metadata
if isMacosx(path) {
return false
}
return informativeFilesFilter.MatchString(path)
}
func findPListFiles(path string) bool {
// Ignore __MACOSX files since it's generally metadata
if isMacosx(path) {
return false
}
return plistFilesFilter.MatchString(path)
}