forked from SugarCoatJS/sugarcoat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·318 lines (284 loc) · 9.04 KB
/
cli.js
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
// vim: set tw=99 ts=2 sw=2 et:
'use strict';
const crypto = require('crypto');
const path = require('path');
const am = require('am');
const { program } = require('commander');
const glob = require('fast-glob');
const fs = require('fs-extra');
const graphml = require('graphology-graphml');
const { iterate, zip } = require('iterare');
const { MultiDirectedGraph } = require('graphology');
const sanitizeFileName = require('sanitize-filename');
const { VError } = require('verror');
const { parseConfigFile } = require('./config');
program
.version(require('./package.json').version)
.requiredOption('-c, --config <path>', 'config JSON file path')
.option('-i, --ingest', 'ingest Page Graph data and produce a trace of API accesses')
.option('-R, --report', 'generated report of recorded API access sites')
.option('-r, --rewrite', 'produce rewritten script files')
.option('-b, --bundle', 'bundle rewrites into adblock data')
.parse(require('process').argv);
const {
config: configFilePath,
ingest: shouldIngest,
report: shouldReport,
rewrite: shouldRewrite,
bundle: shouldBundle,
} = program;
const requiredConfigKeys = new Set();
if (shouldIngest) {
requiredConfigKeys.add('graphs');
requiredConfigKeys.add('code');
requiredConfigKeys.add('trace');
}
if (shouldReport) {
requiredConfigKeys.add('trace');
requiredConfigKeys.add('report');
}
if (shouldRewrite) {
requiredConfigKeys.add('code');
requiredConfigKeys.add('trace');
}
if (shouldBundle) {
requiredConfigKeys.add('code');
requiredConfigKeys.add('bundle');
}
const compilePolicy = policy => {
if (policy) {
if (policy.include) {
const includeSet = new Set(policy.include);
if (policy.exclude) {
for (const binding of policy.exclude) {
includeSet.delete(binding);
}
}
return binding => includeSet.has(binding);
}
if (policy.exclude) {
const excludeSet = new Set(policy.exclude);
return binding => excludeSet.has(binding);
}
}
return null;
};
am(async () => {
let config;
try {
config = await parseConfigFile(configFilePath);
} catch (err) {
throw new VError(err, `Failed to load config JSON file ${JSON.stringify(configFilePath)}`);
}
const missingConfigKeys = iterate(requiredConfigKeys)
.filter(key => !config[key])
.toArray();
if (missingConfigKeys.length > 0) {
throw new Error(
`Can't perform the requested operation(s) without the following config key(s): ${missingConfigKeys.join(
', '
)}`
);
}
const {
graphs: graphFileGlobs,
code: codeDirPath,
trace: traceFilePath,
report: reportFilePath,
bundle: bundleConfig,
} = config;
const targets = config.targets
? Object.entries(config.targets).map(([name, data]) => ({ name, ...data }))
: [];
if (shouldIngest || shouldRewrite) {
await Promise.all(
targets.map(async target => {
const origSrcFilePath = path.join(codeDirPath, `${target.name}.js`);
let origSrc;
try {
origSrc = await fs.readFile(origSrcFilePath, { encoding: 'utf8' });
} catch (err) {
throw new VError(
err,
`Failed to read target source file ${JSON.stringify(origSrcFilePath)}`
);
}
target.src = origSrc;
target.srcHash = crypto.createHash('sha256').update(origSrc).digest('hex');
})
);
}
if (shouldIngest) {
const graphFilePaths = await glob(graphFileGlobs);
const graphs = await Promise.all(
graphFilePaths.map(async graphFilePath => {
try {
const graphSrc = await fs.readFile(graphFilePath, { encoding: 'utf8' });
return graphml.parse(MultiDirectedGraph, graphSrc, { addMissingNodes: true });
} catch (err) {
throw new VError(err, `Failed to load Page Graph file ${JSON.stringify(graphFilePath)}`);
}
})
);
const tracesObj = Object.fromEntries(
zip(targets, require('./ingest')(graphs, targets)).map(([target, trace]) => {
target.trace = trace;
return [target.name, { srcHash: target.srcHash, trace }];
})
);
try {
await fs.writeFile(traceFilePath, JSON.stringify(tracesObj, null, 2), { encoding: 'utf8' });
} catch (err) {
throw new VError(err, `Failed to write trace file ${JSON.stringify(traceFilePath)}`);
}
} else if (shouldReport || shouldRewrite) {
let tracesObj;
try {
tracesObj = JSON.parse(await fs.readFile(traceFilePath, { encoding: 'utf8' }));
} catch (err) {
throw new VError(err, `Failed to load trace JSON file ${JSON.stringify(traceFilePath)}`);
}
for (const target of targets) {
if (!Object.prototype.hasOwnProperty.call(tracesObj, target.name)) {
throw new Error(
`Trace file is missing an entry for target ${JSON.stringify(target.name)}`
);
}
const { srcHash, trace } = tracesObj[target.name];
if (target.srcHash !== srcHash) {
throw new Error(
`SHA-256 source hash for target ${JSON.stringify(target.name)} (${
target.srcHash
}) doesn't match hash recorded in trace file (${srcHash})`
);
}
target.trace = trace;
}
}
if (shouldReport) {
const report = require('./report');
const reportTemplate = await report.loadReportTemplate();
const reportSrc = report(reportTemplate, targets);
try {
await fs.writeFile(reportFilePath, reportSrc, { encoding: 'utf8' });
} catch (err) {
throw new VError(err, `Failed to write report file ${JSON.stringify(reportFilePath)}`);
}
}
let resources;
if (shouldBundle) {
if (shouldRewrite) {
resources = [];
} else {
}
}
if (shouldRewrite) {
const rewrite = require('./rewrite');
let mocksMap;
try {
mocksMap = await rewrite.loadMocks();
} catch (err) {
throw new VError(err, 'Failed to load mocks library');
}
let recipesMap;
try {
recipesMap = await rewrite.loadRecipes(mocksMap);
} catch (err) {
throw new VError(err, 'Failed to load recipes library');
}
await Promise.all(
targets.flatMap(target => {
const rewriteData = rewrite(
mocksMap,
recipesMap,
target.name,
target.src,
target.trace,
compilePolicy(target.policy)
);
if (!rewriteData) {
return [];
}
target.rewriteSrc = rewriteData.src;
const rewriteSrcFilePath = path.join(codeDirPath, `sugarcoat-${target.name}.js`);
const rewriteSrcMapFilePath = `${rewriteSrcFilePath}.map`;
target.sugarcoated = true;
return [
(async () => {
try {
await fs.writeFile(rewriteSrcFilePath, rewriteData.src, { encoding: 'utf8' });
} catch (err) {
throw new VError(
err,
`Failed to write rewrite source file ${JSON.stringify(rewriteSrcFilePath)}`
);
}
})(),
(async () => {
try {
await fs.writeFile(rewriteSrcMapFilePath, rewriteData.srcMap, { encoding: 'utf8' });
} catch (err) {
throw new VError(
err,
`Failed to write rewrite source map file ${JSON.stringify(rewriteSrcMapFilePath)}`
);
}
})(),
];
})
);
} else if (shouldBundle) {
await Promise.all(
targets.map(async target => {
const rewriteSrcFilePath = path.join(codeDirPath, `sugarcoat-${target.name}.js`);
if (!(await fs.pathExists(rewriteSrcFilePath))) {
return;
}
let rewriteSrc;
try {
rewriteSrc = await fs.readFile(rewritesSrcFilePath, { encoding: 'utf8' });
} catch (err) {
throw new VError(
err,
`Failed to read rewrite source file ${JSON.stringify(rewriteSrcFilePath)}`
);
}
target.rewriteSrc = rewriteSrc;
target.sugarcoated = true;
})
);
}
if (shouldBundle) {
const { rules: rulesFilePath, resources: resourcesFilePath } = bundleConfig;
const bundle = await require('./bundle')(
targets
.filter(target => target.sugarcoated) // only want to print rules for sugarcoated targets
.map(target => ({
name: `sugarcoat-${target.name}`,
src: target.src,
patterns: target.patterns,
}))
);
await Promise.all([
(async () => {
try {
await fs.writeFile(rulesFilePath, bundle.rules.join('\n'), { encoding: 'utf8' });
} catch (err) {
throw new VError(err, `Failed to write rules file ${JSON.stringify(rulesFilePath)}`);
}
})(),
(async () => {
try {
await fs.writeFile(resourcesFilePath, JSON.stringify(bundle.resources, null, 2), {
encoding: 'utf8',
});
} catch (err) {
throw new VError(
err,
`Failed to write resources file ${JSON.stringify(resourcesFilePath)}`
);
}
})(),
]);
}
});