/
asar.js
713 lines (656 loc) · 19.3 KB
/
asar.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
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
(function () {
const asar = process.binding('atom_common_asar')
const {Buffer} = require('buffer')
const childProcess = require('child_process')
const path = require('path')
const util = require('util')
const hasProp = {}.hasOwnProperty
// Cache asar archive objects.
const cachedArchives = {}
const envNoAsar = process.env.ELECTRON_NO_ASAR && process.type !== 'browser' && process.type !== 'renderer'
const isAsarDisabled = function () {
return process.noAsar || envNoAsar
}
const getOrCreateArchive = function (p) {
let archive = cachedArchives[p]
if (archive != null) {
return archive
}
archive = asar.createArchive(p)
if (!archive) {
return false
}
cachedArchives[p] = archive
return archive
}
// Clean cache on quit.
process.on('exit', function () {
for (let p in cachedArchives) {
if (!hasProp.call(cachedArchives, p)) continue
cachedArchives[p].destroy()
}
})
// Separate asar package's path from full path.
const splitPath = function (p) {
// shortcut to disable asar.
if (isAsarDisabled()) {
return [false]
}
if (Buffer.isBuffer(p)) {
p = p.toString()
}
if (typeof p !== 'string') {
return [false]
}
if (p.substr(-5) === '.asar') {
return [true, p, '']
}
p = path.normalize(p)
const index = p.lastIndexOf('.asar' + path.sep)
if (index === -1) {
return [false]
}
return [true, p.substr(0, index + 5), p.substr(index + 6)]
}
// Convert asar archive's Stats object to fs's Stats object.
let nextInode = 0
const uid = process.getuid != null ? process.getuid() : 0
const gid = process.getgid != null ? process.getgid() : 0
const fakeTime = new Date()
const asarStatsToFsStats = function (stats) {
return {
dev: 1,
ino: ++nextInode,
mode: 33188,
nlink: 1,
uid: uid,
gid: gid,
rdev: 0,
atime: stats.atime || fakeTime,
birthtime: stats.birthtime || fakeTime,
mtime: stats.mtime || fakeTime,
ctime: stats.ctime || fakeTime,
size: stats.size,
isFile: function () {
return stats.isFile
},
isDirectory: function () {
return stats.isDirectory
},
isSymbolicLink: function () {
return stats.isLink
},
isBlockDevice: function () {
return false
},
isCharacterDevice: function () {
return false
},
isFIFO: function () {
return false
},
isSocket: function () {
return false
}
}
}
// Create a ENOENT error.
const notFoundError = function (asarPath, filePath, callback) {
const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
error.code = 'ENOENT'
error.errno = -2
if (typeof callback !== 'function') {
throw error
}
process.nextTick(function () {
callback(error)
})
}
// Create a ENOTDIR error.
const notDirError = function (callback) {
const error = new Error('ENOTDIR, not a directory')
error.code = 'ENOTDIR'
error.errno = -20
if (typeof callback !== 'function') {
throw error
}
process.nextTick(function () {
callback(error)
})
}
// Create a EACCES error.
const accessError = function (asarPath, filePath, callback) {
const error = new Error(`EACCES: permission denied, access '${filePath}'`)
error.code = 'EACCES'
error.errno = -13
if (typeof callback !== 'function') {
throw error
}
process.nextTick(function () {
callback(error)
})
}
// Create invalid archive error.
const invalidArchiveError = function (asarPath, callback) {
const error = new Error(`Invalid package ${asarPath}`)
if (typeof callback !== 'function') {
throw error
}
process.nextTick(function () {
callback(error)
})
}
// Override APIs that rely on passing file path instead of content to C++.
const overrideAPISync = function (module, name, arg) {
if (arg == null) {
arg = 0
}
const old = module[name]
module[name] = function () {
const p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return old.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const newPath = archive.copyFileOut(filePath)
if (!newPath) {
notFoundError(asarPath, filePath)
}
arguments[arg] = newPath
return old.apply(this, arguments)
}
}
const overrideAPI = function (module, name, arg) {
if (arg == null) {
arg = 0
}
const old = module[name]
module[name] = function () {
const p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return old.apply(this, arguments)
}
const callback = arguments[arguments.length - 1]
if (typeof callback !== 'function') {
return overrideAPISync(module, name, arg)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const newPath = archive.copyFileOut(filePath)
if (!newPath) {
return notFoundError(asarPath, filePath, callback)
}
arguments[arg] = newPath
return old.apply(this, arguments)
}
}
// Override fs APIs.
exports.wrapFsWithAsar = function (fs) {
const logFDs = {}
const logASARAccess = function (asarPath, filePath, offset) {
if (!process.env.ELECTRON_LOG_ASAR_READS) {
return
}
if (!logFDs[asarPath]) {
const path = require('path')
const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
const logPath = path.join(require('os').tmpdir(), logFilename)
logFDs[asarPath] = fs.openSync(logPath, 'a')
console.log('Logging ' + asarPath + ' access to ' + logPath)
}
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
}
const {lstatSync} = fs
fs.lstatSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return lstatSync(p)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const stats = archive.stat(filePath)
if (!stats) {
notFoundError(asarPath, filePath)
}
return asarStatsToFsStats(stats)
}
const {lstat} = fs
fs.lstat = function (p, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return lstat(p, callback)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) {
return notFoundError(asarPath, filePath, callback)
}
process.nextTick(function () {
callback(null, asarStatsToFsStats(stats))
})
}
const {statSync} = fs
fs.statSync = function (p) {
const [isAsar] = splitPath(p)
if (!isAsar) {
return statSync(p)
}
// Do not distinguish links for now.
return fs.lstatSync(p)
}
const {stat} = fs
fs.stat = function (p, callback) {
const [isAsar] = splitPath(p)
if (!isAsar) {
return stat(p, callback)
}
// Do not distinguish links for now.
process.nextTick(function () {
fs.lstat(p, callback)
})
}
const {statSyncNoException} = fs
fs.statSyncNoException = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return statSyncNoException(p)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return false
}
const stats = archive.stat(filePath)
if (!stats) {
return false
}
return asarStatsToFsStats(stats)
}
const {realpathSync} = fs
fs.realpathSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return realpathSync.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const real = archive.realpath(filePath)
if (real === false) {
notFoundError(asarPath, filePath)
}
return path.join(realpathSync(asarPath), real)
}
const {realpath} = fs
fs.realpath = function (p, cache, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return realpath.apply(this, arguments)
}
if (typeof cache === 'function') {
callback = cache
cache = void 0
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const real = archive.realpath(filePath)
if (real === false) {
return notFoundError(asarPath, filePath, callback)
}
return realpath(asarPath, function (err, p) {
if (err) {
return callback(err)
}
return callback(null, path.join(p, real))
})
}
const {exists} = fs
fs.exists = function (p, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return exists(p, callback)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
process.nextTick(function () {
// Disabled due to false positive in StandardJS
// eslint-disable-next-line standard/no-callback-literal
callback(archive.stat(filePath) !== false)
})
}
const {existsSync} = fs
fs.existsSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return existsSync(p)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return false
}
return archive.stat(filePath) !== false
}
const {access} = fs
fs.access = function (p, mode, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return access.apply(this, arguments)
}
if (typeof mode === 'function') {
callback = mode
mode = fs.constants.F_OK
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const info = archive.getFileInfo(filePath)
if (!info) {
return notFoundError(asarPath, filePath, callback)
}
if (info.unpacked) {
const realPath = archive.copyFileOut(filePath)
return fs.access(realPath, mode, callback)
}
const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) {
return notFoundError(asarPath, filePath, callback)
}
if (mode & fs.constants.W_OK) {
return accessError(asarPath, filePath, callback)
}
process.nextTick(function () {
callback()
})
}
const {accessSync} = fs
fs.accessSync = function (p, mode) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return accessSync.apply(this, arguments)
}
if (mode == null) {
mode = fs.constants.F_OK
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const info = archive.getFileInfo(filePath)
if (!info) {
notFoundError(asarPath, filePath)
}
if (info.unpacked) {
const realPath = archive.copyFileOut(filePath)
return fs.accessSync(realPath, mode)
}
const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) {
notFoundError(asarPath, filePath)
}
if (mode & fs.constants.W_OK) {
accessError(asarPath, filePath)
}
}
const {readFile} = fs
fs.readFile = function (p, options, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return readFile.apply(this, arguments)
}
if (typeof options === 'function') {
callback = options
options = {
encoding: null
}
} else if (typeof options === 'string') {
options = {
encoding: options
}
} else if (options === null || options === undefined) {
options = {
encoding: null
}
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments')
}
const {encoding} = options
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const info = archive.getFileInfo(filePath)
if (!info) {
return notFoundError(asarPath, filePath, callback)
}
if (info.size === 0) {
return process.nextTick(function () {
callback(null, encoding ? '' : Buffer.alloc(0))
})
}
if (info.unpacked) {
const realPath = archive.copyFileOut(filePath)
return fs.readFile(realPath, options, callback)
}
const buffer = Buffer.alloc(info.size)
const fd = archive.getFd()
if (!(fd >= 0)) {
return notFoundError(asarPath, filePath, callback)
}
logASARAccess(asarPath, filePath, info.offset)
fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
callback(error, encoding ? buffer.toString(encoding) : buffer)
})
}
const {readFileSync} = fs
fs.readFileSync = function (p, options) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return readFileSync.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const info = archive.getFileInfo(filePath)
if (!info) {
notFoundError(asarPath, filePath)
}
if (info.size === 0) {
if (options) {
return ''
} else {
return Buffer.alloc(0)
}
}
if (info.unpacked) {
const realPath = archive.copyFileOut(filePath)
return fs.readFileSync(realPath, options)
}
if (!options) {
options = {
encoding: null
}
} else if (typeof options === 'string') {
options = {
encoding: options
}
} else if (typeof options !== 'object') {
throw new TypeError('Bad arguments')
}
const {encoding} = options
const buffer = Buffer.alloc(info.size)
const fd = archive.getFd()
if (!(fd >= 0)) {
notFoundError(asarPath, filePath)
}
logASARAccess(asarPath, filePath, info.offset)
fs.readSync(fd, buffer, 0, info.size, info.offset)
if (encoding) {
return buffer.toString(encoding)
} else {
return buffer
}
}
const {readdir} = fs
fs.readdir = function (p, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return readdir.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return invalidArchiveError(asarPath, callback)
}
const files = archive.readdir(filePath)
if (!files) {
return notFoundError(asarPath, filePath, callback)
}
process.nextTick(function () {
callback(null, files)
})
}
const {readdirSync} = fs
fs.readdirSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return readdirSync.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
invalidArchiveError(asarPath)
}
const files = archive.readdir(filePath)
if (!files) {
notFoundError(asarPath, filePath)
}
return files
}
const {internalModuleReadJSON} = process.binding('fs')
process.binding('fs').internalModuleReadJSON = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return internalModuleReadJSON(p)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return
}
const info = archive.getFileInfo(filePath)
if (!info) {
return
}
if (info.size === 0) {
return ''
}
if (info.unpacked) {
const realPath = archive.copyFileOut(filePath)
return fs.readFileSync(realPath, {
encoding: 'utf8'
})
}
const buffer = Buffer.alloc(info.size)
const fd = archive.getFd()
if (!(fd >= 0)) {
return
}
logASARAccess(asarPath, filePath, info.offset)
fs.readSync(fd, buffer, 0, info.size, info.offset)
return buffer.toString('utf8')
}
const {internalModuleStat} = process.binding('fs')
process.binding('fs').internalModuleStat = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) {
return internalModuleStat(p)
}
const archive = getOrCreateArchive(asarPath)
// -ENOENT
if (!archive) {
return -34
}
const stats = archive.stat(filePath)
// -ENOENT
if (!stats) {
return -34
}
if (stats.isDirectory) {
return 1
} else {
return 0
}
}
// Calling mkdir for directory inside asar archive should throw ENOTDIR
// error, but on Windows it throws ENOENT.
// This is to work around the recursive looping bug of mkdirp since it is
// widely used.
if (process.platform === 'win32') {
const {mkdir} = fs
fs.mkdir = function (p, mode, callback) {
if (typeof mode === 'function') {
callback = mode
}
const [isAsar, , filePath] = splitPath(p)
if (isAsar && filePath.length) {
return notDirError(callback)
}
mkdir(p, mode, callback)
}
const {mkdirSync} = fs
fs.mkdirSync = function (p, mode) {
const [isAsar, , filePath] = splitPath(p)
if (isAsar && filePath.length) {
notDirError()
}
return mkdirSync(p, mode)
}
}
// Executing a command string containing a path to an asar
// archive confuses `childProcess.execFile`, which is internally
// called by `childProcess.{exec,execSync}`, causing
// Electron to consider the full command as a single path
// to an archive.
for (const functionName of ['exec', 'execSync']) {
childProcess[functionName] = wrap(childProcess[functionName], function (func) {
return function () {
const processNoAsarOriginalValue = process.noAsar
process.noAsar = true
try {
return func.apply(this, arguments)
} finally {
process.noAsar = processNoAsarOriginalValue
}
}
})
}
function wrap (func, wrapper) {
const wrapped = wrapper(func)
if (func[util.promisify.custom]) {
wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
}
return wrapped
}
overrideAPI(fs, 'open')
overrideAPI(childProcess, 'execFile')
overrideAPISync(process, 'dlopen', 1)
overrideAPISync(require('module')._extensions, '.node', 1)
overrideAPISync(fs, 'openSync')
overrideAPISync(childProcess, 'execFileSync')
}
})()