-
Notifications
You must be signed in to change notification settings - Fork 12
/
privacy_manifest_analyser.sh
executable file
·1291 lines (1113 loc) · 43.3 KB
/
privacy_manifest_analyser.sh
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright (c) 2024, crasowas.
#
# Use of this source code is governed by a MIT-style license
# that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
# Keep comment during source code scanning when the `-c` option is enabled
keep_comment=false
# Analysis ignores dependencies when the `-i` option is enabled
ignore_dependencies=false
# Print verbose information when the `-v` option is enabled
verbose=false
# An array of directories for local dependencies
local_dependencies_dirs=()
# An array of directories excluded from analysis
target_excluded_dirs=()
# Parse command-line options
while getopts ":cd:e:iv" opt; do
case $opt in
c) keep_comment=true
;;
d) local_dependencies_dirs+=("$OPTARG")
;;
e) target_excluded_dirs+=("$OPTARG")
;;
i) ignore_dependencies=true
;;
v) verbose=true
;;
\?) echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Check if a directory path parameter is provided
if [ -z "$1" ]; then
echo "Usage: $0 <directory_path>"
exit 1
fi
target_dir=$1
pod_file="$target_dir/Podfile"
# Pods directory will be analyzed separately if it's a CocoaPods project
pods_dir="$target_dir/Pods"
pods_pbxproj_file="$pods_dir/Pods.xcodeproj/project.pbxproj"
# Exclude non-library directories within the Pods directory
pods_excluded_dirs=("$pods_dir/Pods.xcodeproj" "$pods_dir/Target Support Files" "$pods_dir/Local Podspecs" "$pods_dir/Headers")
# Carthage directory will be analyzed separately if it's a Carthage project
carthage_dir="$target_dir/Carthage"
# Flutter plugins directory will be analyzed separately if it's a Flutter project
flutter_plugins_dir="$target_dir/.symlinks/plugins"
# App Frameworks directory will be analyzed separately if the target directory is an application bundle
app_frameworks_dir="$target_dir/Frameworks"
# Exclude directories to be analyzed separately
target_excluded_dirs+=("$pods_dir" "$carthage_dir" "${local_dependencies_dirs[@]}" "$flutter_plugins_dir" "$app_frameworks_dir")
# Temporary file for keeping track of recursively traversed directories
visited_dirs_tempfile=$(mktemp)
trap "rm -f $visited_dirs_tempfile" EXIT
# Analysis indicators
found_count=0
warning_count=0
issue_count=0
completed_count=0
common_sdk_count=0
# Project Information
project_name=""
project_xcodeproj_file=""
# An array of dependencies information, including the name, product name, and Mach-O type
# For dependencies managed by Swift Package Manager, the paths to these dependencies are also included
dependencies=()
# An array variable for storing analysis results that affect the application's privacy manifest
app_privacy_manifest_effect_results=()
# File name of the privacy manifest
readonly PRIVACY_MANIFEST_FILE_NAME="PrivacyInfo.xcprivacy"
# Universal delimiter
readonly DELIMITER=":"
# Space escape symbol for handling space in path
readonly SPACE_ESCAPE="\u0020"
# Mach-O types
readonly MACH_O_TYPE_STATIC_LIB="staticlib"
readonly MACH_O_TYPE_DY_LIB="mh_dylib"
readonly MACH_O_TYPE_UNKNOWN="unknown"
# ANSI color codes
readonly BOLD_BLACK_COLOR="\033[1;30m"
readonly GREEN_COLOR="\033[0;32m"
readonly YELLOW_COLOR="\033[0;33m"
readonly RESET_COLOR="\033[0m"
# Text of the required reason APIs and their categories
# See also:
# * https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api
# * https://github.com/Wooder/ios_17_required_reason_api_scanner/blob/main/required_reason_api_text_scanner.sh
readonly API_TEXTS=(
# NSPrivacyAccessedAPICategoryFileTimestamp
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}creationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}modificationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fileModificationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}contentModificationDateKey"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}creationDateKey"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlist"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlistbulk"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fgetattrlist"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}stat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fstat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fstatat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}lstat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlistat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSFileCreationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSFileModificationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSURLContentModificationDateKey"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSURLCreationDateKey"
# NSPrivacyAccessedAPICategorySystemBootTime
"NSPrivacyAccessedAPICategorySystemBootTime${DELIMITER}systemUptime"
"NSPrivacyAccessedAPICategorySystemBootTime${DELIMITER}mach_absolute_time"
# NSPrivacyAccessedAPICategoryDiskSpace
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}volumeAvailableCapacityKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}volumeAvailableCapacityForImportantUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}volumeAvailableCapacityForOpportunisticUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}volumeTotalCapacityKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}systemFreeSize"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}systemSize"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}statfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}statvfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}fstatfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}fstatvfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}getattrlist"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}fgetattrlist"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}getattrlistat"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityForImportantUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityForOpportunisticUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeTotalCapacityKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSFileSystemFreeSize"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSFileSystemSize"
# NSPrivacyAccessedAPICategoryActiveKeyboards
"NSPrivacyAccessedAPICategoryActiveKeyboards${DELIMITER}activeInputModes"
# NSPrivacyAccessedAPICategoryUserDefaults
"NSPrivacyAccessedAPICategoryUserDefaults${DELIMITER}UserDefaults"
"NSPrivacyAccessedAPICategoryUserDefaults${DELIMITER}NSUserDefaults"
"NSPrivacyAccessedAPICategoryUserDefaults${DELIMITER}AppStorage"
)
# Symbol of the required reason APIs and their categories
# See also:
# * https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api
# * https://github.com/Wooder/ios_17_required_reason_api_scanner/blob/main/required_reason_api_binary_scanner.sh
readonly API_SYMBOLS=(
# NSPrivacyAccessedAPICategoryFileTimestamp
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlist"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlistbulk"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fgetattrlist"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}stat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fstat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}fstatat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}lstat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}getattrlistat"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSFileCreationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSFileModificationDate"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSURLContentModificationDateKey"
"NSPrivacyAccessedAPICategoryFileTimestamp${DELIMITER}NSURLCreationDateKey"
# NSPrivacyAccessedAPICategorySystemBootTime
"NSPrivacyAccessedAPICategorySystemBootTime${DELIMITER}systemUptime"
"NSPrivacyAccessedAPICategorySystemBootTime${DELIMITER}mach_absolute_time"
# NSPrivacyAccessedAPICategoryDiskSpace
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}statfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}statvfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}fstatfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}fstatvfs"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSFileSystemFreeSize"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSFileSystemSize"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityForImportantUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeAvailableCapacityForOpportunisticUsageKey"
"NSPrivacyAccessedAPICategoryDiskSpace${DELIMITER}NSURLVolumeTotalCapacityKey"
# NSPrivacyAccessedAPICategoryActiveKeyboards
"NSPrivacyAccessedAPICategoryActiveKeyboards${DELIMITER}activeInputModes"
# NSPrivacyAccessedAPICategoryUserDefaults
"NSPrivacyAccessedAPICategoryUserDefaults${DELIMITER}NSUserDefaults"
)
# List of commonly used SDKs
# See also:
# * https://developer.apple.com/support/third-party-SDK-requirements
readonly COMMON_SDKS=(
"Abseil"
"AFNetworking"
"Alamofire"
"AppAuth"
"BoringSSL / openssl_grpc"
"Capacitor"
"Charts"
"connectivity_plus"
"Cordova"
"device_info_plus"
"DKImagePickerController"
"DKPhotoGallery"
"FBAEMKit"
"FBLPromises"
"FBSDKCoreKit"
"FBSDKCoreKit_Basics"
"FBSDKLoginKit"
"FBSDKShareKit"
"file_picker"
"FirebaseABTesting"
"FirebaseAuth"
"FirebaseCore"
"FirebaseCoreDiagnostics"
"FirebaseCoreExtension"
"FirebaseCoreInternal"
"FirebaseCrashlytics"
"FirebaseDynamicLinks"
"FirebaseFirestore"
"FirebaseInstallations"
"FirebaseMessaging"
"FirebaseRemoteConfig"
"Flutter"
"flutter_inappwebview"
"flutter_local_notifications"
"fluttertoast"
"FMDB"
"geolocator_apple"
"GoogleDataTransport"
"GoogleSignIn"
"GoogleToolboxForMac"
"GoogleUtilities"
"grpcpp"
"GTMAppAuth"
"GTMSessionFetcher"
"hermes"
"image_picker_ios"
"IQKeyboardManager"
"IQKeyboardManagerSwift"
"Kingfisher"
"leveldb"
"Lottie"
"MBProgressHUD"
"nanopb"
"OneSignal"
"OneSignalCore"
"OneSignalExtension"
"OneSignalOutcomes"
"OpenSSL"
"OrderedSet"
"package_info"
"package_info_plus"
"path_provider"
"path_provider_ios"
"Promises"
"Protobuf"
"Reachability"
"RealmSwift"
"RxCocoa"
"RxRelay"
"RxSwift"
"SDWebImage"
"share_plus"
"shared_preferences_ios"
"SnapKit"
"sqflite"
"Starscream"
"SVProgressHUD"
"SwiftyGif"
"SwiftyJSON"
"Toast"
"UnityFramework"
"url_launcher"
"url_launcher_ios"
"video_player_avfoundation"
"wakelock"
"webview_flutter_wkwebview"
)
# Print a formatted title
print_title() {
local title="$1"
local title_length=${#title}
local border_width=$(( (80 - title_length) / 2 ))
local border_left=""
for ((i=0; i<border_width; i++)); do
border_left+="="
done
local border_right="$border_left"
if ((title_length % 2 == 1)); then
border_right+="="
fi
echo ""
print_text "$border_left $BOLD_BLACK_COLOR$title$RESET_COLOR $border_right"
echo ""
}
# Print a text with or without color
print_text() {
local text="$1"
if [ -t 1 ]; then
echo "$text"
else
echo "$text" | sed "s/\x1B\[[0-9;]*m//g"
fi
}
# Print the elements of an array along with their indices
print_array() {
local -a array=("$@")
for ((i=0; i<${#array[@]}; i++)); do
print_text "[$i] $(decode_path "${array[i]}")"
done
}
# Split a string into substrings using a specified delimiter
split_string_by_delimiter() {
local string="$1"
local -a substrings=()
IFS="$DELIMITER" read -ra substrings <<< "$string"
echo "${substrings[@]}"
}
# Encode a path string by replacing space with an escape character
encode_path() {
echo "$1" | sed "s/ /$SPACE_ESCAPE/g";
}
# Decode a path string by replacing encoded character with space
decode_path() {
echo "$1" | sed "s/$SPACE_ESCAPE/ /g";
}
# Filter out comments from the specified source file
filter_comment() {
if [ "$keep_comment" == false ]; then
sed -e "/\/\*/,/\*\//d" -e "s/\/\/.*//g" "$1"
else
cat "$file_path"
fi
}
# Check if the specified file is statically linked library
is_statically_linked_lib() {
local file_info=$(file "$1")
if [[ $file_info == *"current ar archive"* ]]; then
return 0
else
return 1
fi
}
# Check if the specified file is dynamically linked library
is_dynamically_linked_lib() {
local file_info=$(file "$1")
if [[ $file_info == *"dynamically linked"* ]]; then
return 0
else
return 1
fi
}
# Check if `use_frameworks!` is specified in the Podfile
check_use_frameworks() {
local file_path="$1"
if grep -qE "^[^#]*\buse_frameworks!\b" "$file_path"; then
return 0
else
return 1
fi
}
# Search for the names of dependencies managed by CocoaPods
search_names_in_cocoapods() {
local file_path="$1"
awk '
BEGIN {
in_targets = 0
}
/Begin PBXProject section/,/End PBXProject section/ {
if (/targets = \(/) {
in_targets = 1
next
}
if (in_targets && /\*.* \*\// && $3 !~ /^Pods-/) {
print $3
next
}
if (/;/) {
in_targets = 0
}
}
' "$file_path"
}
# Search for the products of dependencies managed by CocoaPods
search_products_in_cocoapods() {
local file_path="$1"
awk -v delimiter="$DELIMITER" '
BEGIN {
in_native_target = 0
}
/Begin PBXNativeTarget section/,/End PBXNativeTarget section/ {
if (/isa = PBXNativeTarget;/) {
in_native_target = 1
name = product_name = product_type = ""
next
}
if (in_native_target && /name|productName|productType/) {
sub(/^[[:space:]]+/, "")
gsub(/;/, "")
gsub(/"/, "", $3)
if ($3 !~ /^Pods-/) {
if (/name/) name = $3
else if (/productName/) product_name = $3
else if (/productType/) product_type = $3
}
next
}
if (in_native_target && /};/) {
in_native_target = 0
if (name != "") print name delimiter product_name delimiter product_type
}
}
' "$file_path"
}
# Search for the Mach-O types of dependencies managed by CocoaPods
# Note: If the dependency is a framework, it will not be included in the search results
search_mach_o_types_in_cocoapods() {
local file_path="$1"
awk -v use_frameworks="$2" -v static_lib="$MACH_O_TYPE_STATIC_LIB" -v dy_lib="$MACH_O_TYPE_DY_LIB" -v delimiter="$DELIMITER" '
BEGIN {
in_configuration = 0
}
/Begin XCBuildConfiguration section/,/End XCBuildConfiguration section/ {
if (/isa = XCBuildConfiguration;/) {
in_configuration = 1
product_name = mach_o_type = ""
next
}
if (in_configuration && /PRODUCT_NAME|MACH_O_TYPE/) {
sub(/^[[:space:]]+/, "")
gsub(/;/, "")
gsub(/"/, "", $3)
if (index($3, "$") == 0) {
if (/PRODUCT_NAME/) product_name = $3
else if (/MACH_O_TYPE/) mach_o_type = $3
}
next
}
if (in_configuration && /};/) {
in_configuration = 0
if (product_name != "" && !processed_products[product_name]) {
processed_products[product_name] = 1
# When the `MACH_O_TYPE` property is empty, the following scenarios are considered:
# 1. If `use_frameworks` is specified in the `Podfile`, the dependency is identified as a dynamic library
# 2. If `use_frameworks` is not specified in the `Podfile`, the dependency is identified as a static library
if (mach_o_type == "") mach_o_type = (use_frameworks == "true") ? dy_lib : static_lib
print product_name delimiter mach_o_type
}
}
}
' "$file_path"
}
# Search for dependencies managed by CocoaPods
search_dependencies_in_cocoapods() {
local file_path="$1"
local names=($(search_names_in_cocoapods "$file_path"))
local products=($(search_products_in_cocoapods "$file_path"))
local mach_o_types=($(search_mach_o_types_in_cocoapods "$file_path" "$2"))
for dep_name in "${names[@]}"; do
# Find the product name of the dependency
# The default product name is the same as the dependency name
dep_product_name="$dep_name"
for product in "${products[@]}"; do
product_substrings=($(split_string_by_delimiter "$product"))
product_type=${product_substrings[2]}
if [ "$dep_name" == "${product_substrings[0]}" ]; then
if ! [ "$product_type" == "com.apple.product-type.bundle" ]; then
dep_product_name=${product_substrings[1]}
else
dep_product_name=""
fi
break
fi
done
# Ignore if the product type is bundle
if [ -n "$dep_product_name" ]; then
# Find the Mach-O type of the dependency
dep_mach_o_type="$MACH_O_TYPE_UNKNOWN"
for mach_o_type in "${mach_o_types[@]}"; do
mach_o_type_substrings=($(split_string_by_delimiter "$mach_o_type"))
if [ "$dep_product_name" == "${mach_o_type_substrings[0]}" ]; then
dep_mach_o_type=${mach_o_type_substrings[1]}
break
fi
done
dependencies+=("$dep_name$DELIMITER$dep_product_name$DELIMITER$dep_mach_o_type")
fi
done
}
# Get the SourcePackages directory, which is used to store Swift Package Manager dependencies
get_source_packages_dir() {
local derived_data_dir="$HOME/Library/Developer/Xcode/DerivedData"
if ! [ -d "$derived_data_dir" ] || [ -z "$project_name" ]; then
echo ""
return
fi
# Find the DerivedData directory of the project. If there are multiple, by default return the latest one
# Perhaps it would be more accurate to let the user choose?
local project_derived_data_dir=$(find "$derived_data_dir" -maxdepth 1 -type d -name "$project_name-*" -exec stat -f "%m %N" {} + | sort -rn | head -n1 | cut -d ' ' -f2-)
echo "$project_derived_data_dir/SourcePackages"
}
# Get the Embed Frameworks from the specified `project.pbxproj` file
get_embed_frameworks() {
local file_path="$1"
awk '
BEGIN {
in_files = 0
}
/Begin PBXCopyFilesBuildPhase section/,/End PBXCopyFilesBuildPhase section/ {
if (/files = \(/) {
in_files = 1
next
}
if (in_files && /\*.* \*\//) {
print $3
next
}
if (in_files && /;/) {
in_files = 0
}
}
' "$file_path"
}
# Search for the packages of dependencies managed by Swift Package Manager
search_packages_in_swiftpm() {
local file_path="$1"
awk -v delimiter="$DELIMITER" '
BEGIN {
in_dependency = 0
}
/Begin XCSwiftPackageProductDependency section/,/End XCSwiftPackageProductDependency section/ {
if (/isa = XCSwiftPackageProductDependency;/) {
in_dependency = 1
package = product_name = ""
next
}
if (in_dependency && /package|productName/) {
sub(/^[[:space:]]+/, "")
gsub(/;/, "")
if (/package/) {
gsub(/"/, "", $6)
package = $6
} else if (/productName/) {
gsub(/"/, "", $3)
product_name = $3
}
next
}
if (in_dependency && /};/) {
in_dependency = 0
print package delimiter product_name
}
}
' "$file_path"
}
# Search for the artifacts of dependencies managed by Swift Package Manager
search_artifacts_in_swiftpm() {
local file_path="$1"
awk -v delimiter="$DELIMITER" '
BEGIN {
in_artifacts = 0
identity = targetName = path = ""
}
/"artifacts"/ {
in_artifacts = 1
next
}
in_artifacts && /"identity"/ {
sub(/^[[:space:]]+/, "")
gsub(/,/, "")
gsub(/"/, "", $3)
identity = $3
next
}
in_artifacts && /"path"/ {
sub(/^[[:space:]]+/, "")
gsub(/,/, "")
gsub(/"/, "", $3)
path = $3
next
}
in_artifacts && /"targetName"/ {
sub(/^[[:space:]]+/, "")
gsub(/,/, "")
gsub(/"/, "", $3)
targetName = $3
print identity delimiter targetName delimiter path
next
}
in_artifacts && /],/ {
in_artifacts = 0
}
' "$file_path"
}
# Search for dependencies managed by Swift Package Manager
search_dependencies_in_swiftpm() {
local file_path="$1"
local source_packages_dir="$2"
local checkouts_dir="$3"
local workspace_state_file="$source_packages_dir/workspace-state.json"
if ! [ -f "$workspace_state_file" ]; then
return
fi
local packages=($(search_packages_in_swiftpm "$file_path"))
local embed_frameworks=($(get_embed_frameworks "$file_path"))
local artifacts=($(search_artifacts_in_swiftpm "$workspace_state_file"))
for package in "${packages[@]}"; do
package_substrings=($(split_string_by_delimiter "$package"))
dep_name=${package_substrings[0]}
dep_product_name=${package_substrings[1]}
# By default, dependencies managed by Swift Package Manager are built as static libraries
dep_mach_o_type="$MACH_O_TYPE_STATIC_LIB"
dep_path=""
# Check if the dependency is a dynamic library
for embed_framework in "${embed_frameworks[@]}"; do
if [ "$dep_product_name" == "$embed_framework" ]; then
dep_mach_o_type="$MACH_O_TYPE_DY_LIB"
break
fi
done
# Check if the dependency is an unknown type of library
for artifact in "${artifacts[@]}"; do
artifact_substrings=($(split_string_by_delimiter "$artifact"))
# Prioritize matching the product name for greater accuracy in analysis
if [ "$dep_product_name" == "${artifact_substrings[1]}" ]; then
dep_mach_o_type="$MACH_O_TYPE_UNKNOWN"
dep_path="${artifact_substrings[2]}"
break
fi
if [ "$dep_name" == "${artifact_substrings[0]}" ]; then
dep_mach_o_type="$MACH_O_TYPE_UNKNOWN"
dep_path=$(echo ${artifact_substrings[2]} | sed "s/\(.*\/$dep_name\).*/\1/")
break
fi
done
if [ -z "$dep_path" ]; then
dep_path="$checkouts_dir/$dep_name"
fi
dependencies+=("$dep_name$DELIMITER$dep_product_name$DELIMITER$dep_mach_o_type$DELIMITER$dep_path")
done
}
get_dependency_name() {
local dep_path="$1"
local dir_name=$(basename "$dep_path")
# Remove version name for Flutter dependencies
local dep_name="${dir_name%-[0-9]*}"
# Remove .app, .framework, and .xcframework suffixes
dep_name="${dep_name%.*}"
echo "$dep_name"
}
get_mach_o_type() {
local dep_name="$1"
for dependency in "${dependencies[@]}"; do
dependency_substrings=($(split_string_by_delimiter "$dependency"))
if [ "$dep_name" == "${dependency_substrings[0]}" ]; then
echo "${dependency_substrings[2]}"
return
fi
done
echo "$MACH_O_TYPE_UNKNOWN"
}
is_excluded_dir() {
local dir_path="$1"
local excluded_dirs=("${@:2}")
for excluded_dir in "${excluded_dirs[@]}"; do
if [ "$dir_path" == "$excluded_dir" ]; then
return 0
fi
done
return 1
}
is_visited_dir() {
local dir_path="$1"
dir_path=$(readlink -f "$dir_path")
if grep -qFx "$dir_path" "$visited_dirs_tempfile"; then
return 0
else
echo "$dir_path" >> "$visited_dirs_tempfile"
return 1
fi
}
is_dependency() {
local dep_name="$1"
for dependency in "${dependencies[@]}"; do
dependency_substrings=($(split_string_by_delimiter "$dependency"))
if [ "$dep_name" == "${dependency_substrings[0]}" ]; then
return 0
fi
done
return 1
}
is_common_sdk() {
local dep_name="$1"
for common_sdk in "${COMMON_SDKS[@]}"; do
if [ "$dep_name" == "$common_sdk" ] ; then
return 0
fi
done
return 1
}
# Analyze the specified source file for API texts and their categories
analyze_source_file() {
local file_path="$1"
local -a results=()
for api_text in "${API_TEXTS[@]}"; do
substrings=($(split_string_by_delimiter "$api_text"))
category=${substrings[0]}
api=${substrings[1]}
# Check if the API text exists in the source code
if filter_comment "$file_path" | grep -qFw "$api"; then
index=-1
for ((i=0; i<${#results[@]}; i++)); do
result="${results[i]}"
result_substrings=($(split_string_by_delimiter "$result"))
# If the category matches an existing result, update it
if [ "$category" == "${result_substrings[0]}" ]; then
index=i
results[i]="${result_substrings[0]}$DELIMITER${result_substrings[1]},$api$DELIMITER${result_substrings[2]}"
break
fi
done
# If no matching category found, add a new result
if [[ $index -eq -1 ]]; then
results+=("$category$DELIMITER$api$DELIMITER$(encode_path "$file_path")")
fi
fi
done
echo "${results[@]}"
}
# Analyze the specified binary file for API symbols and their categories
analyze_binary_file() {
local file_path="$1"
local -a results=()
for api_symbol in "${API_SYMBOLS[@]}"; do
substrings=($(split_string_by_delimiter "$api_symbol"))
category=${substrings[0]}
api=${substrings[1]}
# Check if the API symbol exists in the binary file
if nm "$file_path" 2>/dev/null | xcrun swift-demangle | grep -E "$api$" >/dev/null; then
index=-1
for ((i=0; i<${#results[@]}; i++)); do
result="${results[i]}"
result_substrings=($(split_string_by_delimiter "$result"))
# If the category matches an existing result, update it
if [ "$category" == "${result_substrings[0]}" ]; then
index=i
results[i]="${result_substrings[0]}$DELIMITER${result_substrings[1]},$api$DELIMITER${result_substrings[2]}"
break
fi
done
# If no matching category found, add a new result
if [[ $index -eq -1 ]]; then
results+=("$category$DELIMITER$api$DELIMITER$(encode_path "$file_path")")
fi
fi
done
echo "${results[@]}"
}
# Recursively analyzes API usage in a directory and its subdirectories
analyze_api_usage() {
local dir_path="$1"
local excluded_dirs=("${@:2}")
local -a results=()
# Check if the directory is excluded from analysis
if is_excluded_dir "$dir_path" "${excluded_dirs[@]}"; then
return
fi
# Check if the directory has been visited during analysis
if is_visited_dir "$dir_path"; then
return
fi
local dir_name="$(basename "$dir_path")"
# If the directory is an application bundle (.app) or framework (.framework), analyze its binary file
if [[ "$dir_name" == *.app ]]; then
binary_name="${dir_name%.*}"
binary_file="$dir_path/$binary_name"
if [ -f "$binary_file" ]; then
results+=($(analyze_binary_file "$binary_file"))
fi
elif [[ "$dir_name" == *.framework ]]; then
binary_name="${dir_name%.*}"
binary_file="$dir_path/$binary_name"
if [ -f "$binary_file" ]; then
results+=($(analyze_binary_file "$binary_file"))
fi
else
for path in "$dir_path"/*; do
if [ -d "$path" ]; then
results+=($(analyze_api_usage "$path" "${excluded_dirs[@]}"))
elif [ -f "$path" ]; then
# Analyze source files (.swift, .h, .m, .mm, .c, .cc, .hpp, .cpp) and binary files (.a)
case "$path" in
*.swift | *.h | *.m | *.mm | *.c | *.cc | *.hpp | *.cpp)
results+=($(analyze_source_file "$path"))
;;
*.a)
results+=($(analyze_binary_file "$path"))
;;
esac
fi
done
fi
echo "${results[@]}"
}
# Search for privacy manifest files in a directory
search_privacy_manifest_files() {
local dir_path="$1"
local excluded_dirs=("${@:2}")
local -a privacy_manifest_files=()
# Create a temporary file to store search results
local tempfile=$(mktemp)
# Ensure the temporary file is deleted on script exit
trap "rm -f $tempfile" EXIT
# Find privacy manifest files within the specified directory and store the results in the temporary file
find "$dir_path" -type f -name "$PRIVACY_MANIFEST_FILE_NAME" -print0 2>/dev/null > "$tempfile"
# Exclude privacy manifest files within excluded directories
while IFS= read -r -d '' file_path; do
local skip_file=0
for excluded_dir in "${excluded_dirs[@]}"; do
if [[ "$file_path" == "$excluded_dir"* ]]; then
skip_file=1
break
fi
done
if [[ $skip_file -eq 0 ]]; then
privacy_manifest_files+=($(encode_path "$file_path"))
fi
done < "$tempfile"
echo "${privacy_manifest_files[@]}"
}
get_privacy_manifest_file() {
# If there are multiple privacy manifest files, return the one with the shortest path
local privacy_manifest_file=$(printf "%s\n" "$@" | awk '{print length, $0}' | sort -n | head -n1 | cut -d ' ' -f2-)
echo $(decode_path "$privacy_manifest_file")
}
check_privacy_manifest_file() {
local privacy_manifest_files=("$@")
if [[ ${#privacy_manifest_files[@]} -eq 0 ]]; then
((warning_count++))
echo "⚠️ Missing privacy manifest file!"
else
((found_count++))
echo "💡 Found privacy manifest file(s): ${#privacy_manifest_files[@]}"
fi
}
# Get unique categories from analysis results
get_categories() {
local results=("$@")
local -a categories=()
for result in "${results[@]}"; do
substrings=($(split_string_by_delimiter "$result"))
category=${substrings[0]}
if ! [[ "$(IFS=" "; echo "${categories[@]}")" =~ "$category" ]]; then
categories+=("$category")
fi
done
echo "${categories[@]}"
}
# Check if descriptions for required API reasons are missing
check_categories() {
local privacy_manifest_file="$1"
local categories=("${@:2}")
local -a miss_categories=()
if [ -f "$privacy_manifest_file" ]; then
for api_category in "${categories[@]}"; do
grep -q "$api_category" "$privacy_manifest_file"
if [ $? -ne 0 ]; then
miss_categories+=("$api_category")
fi
done
else
miss_categories=("${@:2}")
fi
if [[ ${#miss_categories[@]} -eq 0 ]]; then
if [ -f "$privacy_manifest_file" ]; then
((completed_count++))
echo "✅ All required API reasons have been described in the privacy manifest."
fi
else
((issue_count++))
for ((i=0; i<${#miss_categories[@]}; i++)); do
miss_categories[$i]="$YELLOW_COLOR${miss_categories[$i]}$RESET_COLOR"