-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcode.ts
executable file
·289 lines (266 loc) · 9.48 KB
/
xcode.ts
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
import { join } from 'path';
import { Project } from './project';
import { project } from 'xcode';
import * as plist from 'simple-plist';
import { existsSync, writeFileSync } from 'fs';
import { QueueFunction, Tip, TipType } from './tip';
import { ExtensionContext, window } from 'vscode';
import { writeError, writeIonic } from './logging';
import { exists } from './analyzer';
import { openUri, replaceAll } from './utilities';
import { privacyManifestRules } from './privacy-manifest';
import { LastManifestCheck } from './context-variables';
import { clearRefreshCache } from './process-packages';
interface XCProject {
p: any;
projectFolder: string;
projectFilePath: string;
}
interface APIUsage {
api: string;
plugin: string;
reasons: string[];
reasonUrl: string;
}
const oneHour = 60 * 1000 * 60;
export async function checkPrivacyManifest(project: Project, context: ExtensionContext): Promise<void> {
const lastManifestCheck = getLastManifestCheck(context);
if (lastManifestCheck < oneHour) {
return;
}
const apisUsed: APIUsage[] = [];
for (const api of Object.keys(privacyManifestRules)) {
for (const plugin of privacyManifestRules[api]) {
if (exists(plugin)) {
apisUsed.push({ api, plugin, reasons: getReasons(api), reasonUrl: getReasonUrl(api) });
}
}
}
if (apisUsed.length == 0) {
return; // Manifest file is not required
}
try {
const xc = await getXCProject(project);
if (!xc) {
if (!existsSync(iosFolder(project))) {
return; // They have @capacitor/ios but haven't added an iOS project yet
}
writeError(`XCode project file is missing: ${xCodeProjectFile(project)}.`);
return;
}
const pFiles = xc.p.pbxFileReferenceSection();
const files = Object.keys(pFiles);
const found = files.find((f) => pFiles[f].path?.includes('.xcprivacy'));
if (found) {
// Has a .xcprivacy file
investigatePrivacyManifest(project, replaceAll(pFiles[found].path, '"', ''), context, apisUsed);
setLastManifestCheck(context);
return;
}
const title = 'Add Privacy Manifest';
project.add(
new Tip(title, '', TipType.Warning)
.setQueuedAction(createPrivacyManifest, project, context)
.setTooltip('A Privacy Manifest file is required by Apple when submitting your app to the App Store.')
.canRefreshAfter()
.canIgnore(),
);
setLastManifestCheck(context);
return undefined;
} catch (err) {
writeError(`Unable to read privacy manifest of XCode project: ${err}`);
}
}
function setLastManifestCheck(context: ExtensionContext) {
context.workspaceState.update(LastManifestCheck, new Date().getTime());
}
function getLastManifestCheck(context: ExtensionContext): number {
const v = parseInt(context.workspaceState.get(LastManifestCheck));
return new Date().getTime() - (isNaN(v) ? 0 : v);
}
async function investigatePrivacyManifest(
project: Project,
filename: string,
context: ExtensionContext,
apisUsages: APIUsage[],
) {
const path = join(iosFolder(project), filename);
if (!existsSync(path)) {
writeError(`Unable to find privacy manifest file from XCode project: ${path}`);
return;
}
try {
const data: any = plist.readFileSync(path);
for (const apiUsage of apisUsages) {
const found = data.NSPrivacyAccessedAPITypes
? data.NSPrivacyAccessedAPITypes.find((a: any) => a.NSPrivacyAccessedAPIType == apiUsage.api)
: undefined;
if (!found) {
project.add(
new Tip(`Missing Privacy Manifest Category`, '', TipType.Error)
.setQueuedAction(
setPrivacyCategory,
context,
path,
apiUsage.plugin,
apiUsage.api,
apiUsage.reasons,
apiUsage.reasonUrl,
)
.setTooltip(`${apiUsage.plugin} requires that the privacy manifest specifies ${apiUsage.api}.`)
.canRefreshAfter()
.canIgnore(),
);
}
}
} catch (e) {
writeError(`Unable to parse plist file: ${path}: ${e}`);
}
}
async function setPrivacyCategory(
queueFunction: QueueFunction,
context: ExtensionContext,
privacyFilename: string,
plugin: string,
category: string,
reasons: string[],
reasonUrl: string,
): Promise<void> {
const result = await window.showInformationMessage(
`The Privacy Manifest file in your XCode project requires ${category} and a reason code for using it because ${plugin} uses this feature.`,
'Docs',
...reasons,
'Exit',
);
if (result === 'Docs') {
openUri(reasonUrl);
return;
}
if (result === 'Exit') {
return;
}
queueFunction();
const data: any = plist.readFileSync(privacyFilename);
if (!data.NSPrivacyAccessedAPITypes) {
data.NSPrivacyAccessedAPITypes = [];
}
const found = data.NSPrivacyAccessedAPITypes.find((t: any) => t.NSPrivacyAccessedAPIType == category);
if (found) {
if (!found.NSPrivacyAccessedAPITypeReasons) {
found.NSPrivacyAccessedAPITypeReasons = [];
}
found.NSPrivacyAccessedAPITypeReasons.push(result);
} else {
data.NSPrivacyAccessedAPITypes.push({
NSPrivacyAccessedAPIType: category,
NSPrivacyAccessedAPITypeReasons: [result],
});
}
plist.writeFileSync(privacyFilename, data);
clearRefreshCache(context);
}
function XCodeProjFolder(project: Project): string {
return join(iosFolder(project), 'App.xcodeproj');
}
export function iosFolder(project: Project): string {
return join(project.projectFolder(), 'ios', 'App');
}
function xCodeProjectFile(project: Project): string {
const projectFolder = XCodeProjFolder(project);
return join(projectFolder, 'project.pbxproj');
}
async function getXCProject(project: Project): Promise<XCProject> {
const projectFolder = XCodeProjFolder(project);
const path = join(projectFolder, 'project.pbxproj');
if (!existsSync(path)) {
// iOS project not found
return;
}
const p = await parse(path);
return { projectFilePath: path, projectFolder, p };
}
async function createPrivacyManifest(queueFunction: QueueFunction, project: Project, context: ExtensionContext) {
const result = await window.showInformationMessage(
`Your app requires a Privacy Manifest file as it uses particular plugins. Would you like to create one?`,
'Yes',
'More Information',
'Exit',
);
if (result == 'More Information') {
openUri('https://developer.apple.com/support/third-party-SDK-requirements/');
return;
}
if (result !== 'Yes') {
return;
}
queueFunction();
try {
const xc = await getXCProject(project);
const filename = 'PrivacyInfo.xcprivacy';
const path = writeManifestFile(iosFolder(project), filename);
const res = xc.p.addPbxGroup([], 'Resources', undefined, undefined);
const r3 = xc.p.getPBXGroupByKey('504EC2FB1FED79650016851F', 'PBXGroup');
const r2 = xc.p.addResourceFile(filename, {}, res.uuid);
r3.children.push({ value: r2.fileRef, comment: 'Resources' });
writeFileSync(xc.projectFilePath, xc.p.writeSync());
writeIonic('A privacy manifest file was added to your project.');
} catch (e) {
writeError(`Unable to create privacy manifest file: ${e}`);
}
clearRefreshCache(context);
}
function writeManifestFile(iosFolder: string, filename: string): string {
const content = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyTracking</key>
<false/>
<key>NSPrivacyAccessedAPITypes</key>
<array/>
<key>NSPrivacyCollectedDataTypes</key>
<array/>
</dict>
</plist>`;
const f = join(iosFolder, filename);
writeFileSync(f, content);
return f;
}
async function parse(path: string): Promise<any> {
return new Promise((resolve) => {
const p = project(path);
p.parse((err: any) => {
resolve(p);
});
});
}
function getReasons(api: string): string[] {
switch (api) {
case 'NSPrivacyAccessedAPICategoryUserDefaults':
return ['CA92.1', '1C8F.1'];
case 'NSPrivacyAccessedAPICategoryFileTimestamp':
return ['C617.1', 'DDA9.1', '3B52.1']; // FYI: 0A2A.1 is not applicable
case 'NSPrivacyAccessedAPICategoryDiskSpace':
return ['85F4.1', 'E174.1', '7D9E.1', 'B728.1'];
case 'NSPrivacyAccessedAPICategorySystemBootTime':
return ['35F9.1', '8FFB.1', '3D61.1'];
case 'NSPrivacyAccessedAPICategoryActiveKeyboards':
return ['3EC4.1', '54BD.1'];
default:
writeError(`Unknown api ${api} in getReasons`);
}
}
function getReasonUrl(api: string): string {
switch (api) {
case 'NSPrivacyAccessedAPICategoryUserDefaults':
return 'https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278401';
case 'NSPrivacyAccessedAPICategoryFileTimestamp':
return 'https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278393';
case 'NSPrivacyAccessedAPICategoryDiskSpace':
return 'https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278397';
case 'NSPrivacyAccessedAPICategoryActiveKeyboards':
return 'https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278400';
case 'NSPrivacyAccessedAPICategorySystemBootTime':
return 'https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278394';
}
}