Skip to content

Commit 9a7ac65

Browse files
committed
feat: asar integrity: base64, externalAllowed
1 parent b57dc8a commit 9a7ac65

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

.idea/electron-builder.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/asar-integrity/src/asarIntegrity.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@ import { createReadStream } from "fs"
44
import { readdir } from "fs-extra-p"
55
import * as path from "path"
66

7-
export async function computeData(resourcesPath: string): Promise<{ [key: string]: string; }> {
7+
export interface AsarIntegrityOptions {
8+
/**
9+
* Allows external asar files.
10+
*
11+
* @default false
12+
*/
13+
readonly externalAllowed: Boolean
14+
}
15+
16+
export interface AsarIntegrity extends AsarIntegrityOptions {
17+
checksums: { [key: string]: string; }
18+
}
19+
20+
export async function computeData(resourcesPath: string, options?: AsarIntegrityOptions): Promise<AsarIntegrity> {
821
// sort to produce constant result
922
const names = (await readdir(resourcesPath)).filter(it => it.endsWith(".asar")).sort()
10-
const checksums = await BluebirdPromise.map(names, it => hashFile(path.join(resourcesPath, it), "sha512"))
23+
const checksums = await BluebirdPromise.map(names, it => hashFile(path.join(resourcesPath, it), "sha512", "base64"))
1124

1225
const result: { [key: string]: string; } = {}
1326
for (let i = 0; i < names.length; i++) {
1427
result[names[i]] = checksums[i]
1528
}
16-
return result
29+
return <AsarIntegrity>Object.assign({checksums: result}, options)
1730
}
1831

19-
export function hashFile(file: string, algorithm: string) {
32+
export function hashFile(file: string, algorithm: string, encoding: "hex" | "base64" | "latin1" = "hex") {
2033
return new BluebirdPromise<string>((resolve, reject) => {
2134
const hash = createHash(algorithm)
2235
hash
2336
.on("error", reject)
24-
.setEncoding("hex")
37+
.setEncoding(encoding)
2538

2639
createReadStream(file)
2740
.on("error", reject)

packages/electron-builder/src/packager/mac.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsarIntegrity } from "asar-integrity"
12
import BluebirdPromise from "bluebird-lst"
23
import { asArray, getPlatformIconFileName, use } from "electron-builder-util"
34
import { copyFile, unlinkIfExists } from "electron-builder-util/out/fs"
@@ -25,7 +26,7 @@ export function filterCFBundleIdentifier(identifier: string) {
2526
return identifier.replace(/ /g, "-").replace(/[^a-zA-Z0-9.-]/g, "")
2627
}
2728

28-
export async function createApp(packager: PlatformPackager<any>, appOutDir: string, checksums: { [key: string]: string; }) {
29+
export async function createApp(packager: PlatformPackager<any>, appOutDir: string, asarIntegrity: AsarIntegrity) {
2930
const appInfo = packager.appInfo
3031
const appFilename = appInfo.productFilename
3132

@@ -135,8 +136,8 @@ export async function createApp(packager: PlatformPackager<any>, appOutDir: stri
135136
use(packager.platformSpecificBuildOptions.category || (<any>buildMetadata).category, it => appPlist.LSApplicationCategoryType = it)
136137
appPlist.NSHumanReadableCopyright = appInfo.copyright
137138

138-
if (checksums != null) {
139-
appPlist.AsarChecksums = JSON.stringify(checksums)
139+
if (asarIntegrity != null) {
140+
appPlist.AsarIntegrity = JSON.stringify(asarIntegrity)
140141
}
141142

142143
const promises: Array<Promise<any | n>> = [

test/out/mac/__snapshots__/macPackagerTest.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Object {
2929
3030
exports[`one-package 2`] = `
3131
Object {
32-
"AsarChecksums": "{\\"app.asar\\":\\"hash\\",\\"electron.asar\\":\\"hash\\"}",
32+
"AsarIntegrity": "{\\"checksums\\":{\\"app.asar\\":\\"hash\\",\\"electron.asar\\":\\"hash\\"}}",
3333
"BuildMachineOSBuild": "16E195",
3434
"CFBundleDisplayName": "Test App ßW",
3535
"CFBundleDocumentTypes": Array [

test/src/helpers/packTester.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,14 @@ async function checkMacResult(packager: Packager, packagerOptions: PackagerOptio
266266
delete info.CFBundleVersion
267267
delete info.NSHumanReadableCopyright
268268

269-
const checksumData = info.AsarChecksums
269+
const checksumData = info.AsarIntegrity
270270
if (checksumData != null) {
271-
const checksums = JSON.parse(checksumData)
271+
const data = JSON.parse(checksumData)
272+
const checksums = data.checksums
272273
for (const name of Object.keys(checksums)) {
273274
checksums[name] = "hash"
274275
}
275-
info.AsarChecksums = JSON.stringify(checksums)
276+
info.AsarIntegrity = JSON.stringify(data)
276277
}
277278

278279
if (checkOptions.checkMacApp != null) {

0 commit comments

Comments
 (0)