From 9a7ac6531b852549416ed740f1eb6f8a46653810 Mon Sep 17 00:00:00 2001 From: develar Date: Sat, 3 Jun 2017 12:44:53 +0200 Subject: [PATCH] feat: asar integrity: base64, externalAllowed --- .idea/electron-builder.iml | 1 + packages/asar-integrity/src/asarIntegrity.ts | 23 +++++++++++++++---- packages/electron-builder/src/packager/mac.ts | 7 +++--- .../mac/__snapshots__/macPackagerTest.js.snap | 2 +- test/src/helpers/packTester.ts | 7 +++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.idea/electron-builder.iml b/.idea/electron-builder.iml index daaab92c34c..383bc5b8228 100644 --- a/.idea/electron-builder.iml +++ b/.idea/electron-builder.iml @@ -5,6 +5,7 @@ + diff --git a/packages/asar-integrity/src/asarIntegrity.ts b/packages/asar-integrity/src/asarIntegrity.ts index ebf647da10e..068d95e61bf 100644 --- a/packages/asar-integrity/src/asarIntegrity.ts +++ b/packages/asar-integrity/src/asarIntegrity.ts @@ -4,24 +4,37 @@ import { createReadStream } from "fs" import { readdir } from "fs-extra-p" import * as path from "path" -export async function computeData(resourcesPath: string): Promise<{ [key: string]: string; }> { +export interface AsarIntegrityOptions { + /** + * Allows external asar files. + * + * @default false + */ + readonly externalAllowed: Boolean +} + +export interface AsarIntegrity extends AsarIntegrityOptions { + checksums: { [key: string]: string; } +} + +export async function computeData(resourcesPath: string, options?: AsarIntegrityOptions): Promise { // sort to produce constant result const names = (await readdir(resourcesPath)).filter(it => it.endsWith(".asar")).sort() - const checksums = await BluebirdPromise.map(names, it => hashFile(path.join(resourcesPath, it), "sha512")) + const checksums = await BluebirdPromise.map(names, it => hashFile(path.join(resourcesPath, it), "sha512", "base64")) const result: { [key: string]: string; } = {} for (let i = 0; i < names.length; i++) { result[names[i]] = checksums[i] } - return result + return Object.assign({checksums: result}, options) } -export function hashFile(file: string, algorithm: string) { +export function hashFile(file: string, algorithm: string, encoding: "hex" | "base64" | "latin1" = "hex") { return new BluebirdPromise((resolve, reject) => { const hash = createHash(algorithm) hash .on("error", reject) - .setEncoding("hex") + .setEncoding(encoding) createReadStream(file) .on("error", reject) diff --git a/packages/electron-builder/src/packager/mac.ts b/packages/electron-builder/src/packager/mac.ts index 5706e7e58c6..fadaf738cfd 100644 --- a/packages/electron-builder/src/packager/mac.ts +++ b/packages/electron-builder/src/packager/mac.ts @@ -1,3 +1,4 @@ +import { AsarIntegrity } from "asar-integrity" import BluebirdPromise from "bluebird-lst" import { asArray, getPlatformIconFileName, use } from "electron-builder-util" import { copyFile, unlinkIfExists } from "electron-builder-util/out/fs" @@ -25,7 +26,7 @@ export function filterCFBundleIdentifier(identifier: string) { return identifier.replace(/ /g, "-").replace(/[^a-zA-Z0-9.-]/g, "") } -export async function createApp(packager: PlatformPackager, appOutDir: string, checksums: { [key: string]: string; }) { +export async function createApp(packager: PlatformPackager, appOutDir: string, asarIntegrity: AsarIntegrity) { const appInfo = packager.appInfo const appFilename = appInfo.productFilename @@ -135,8 +136,8 @@ export async function createApp(packager: PlatformPackager, appOutDir: stri use(packager.platformSpecificBuildOptions.category || (buildMetadata).category, it => appPlist.LSApplicationCategoryType = it) appPlist.NSHumanReadableCopyright = appInfo.copyright - if (checksums != null) { - appPlist.AsarChecksums = JSON.stringify(checksums) + if (asarIntegrity != null) { + appPlist.AsarIntegrity = JSON.stringify(asarIntegrity) } const promises: Array> = [ diff --git a/test/out/mac/__snapshots__/macPackagerTest.js.snap b/test/out/mac/__snapshots__/macPackagerTest.js.snap index 217dc0b1764..e3e5dfafd62 100644 --- a/test/out/mac/__snapshots__/macPackagerTest.js.snap +++ b/test/out/mac/__snapshots__/macPackagerTest.js.snap @@ -29,7 +29,7 @@ Object { exports[`one-package 2`] = ` Object { - "AsarChecksums": "{\\"app.asar\\":\\"hash\\",\\"electron.asar\\":\\"hash\\"}", + "AsarIntegrity": "{\\"checksums\\":{\\"app.asar\\":\\"hash\\",\\"electron.asar\\":\\"hash\\"}}", "BuildMachineOSBuild": "16E195", "CFBundleDisplayName": "Test App ßW", "CFBundleDocumentTypes": Array [ diff --git a/test/src/helpers/packTester.ts b/test/src/helpers/packTester.ts index 72e1ebddcd0..67b3e3005be 100644 --- a/test/src/helpers/packTester.ts +++ b/test/src/helpers/packTester.ts @@ -266,13 +266,14 @@ async function checkMacResult(packager: Packager, packagerOptions: PackagerOptio delete info.CFBundleVersion delete info.NSHumanReadableCopyright - const checksumData = info.AsarChecksums + const checksumData = info.AsarIntegrity if (checksumData != null) { - const checksums = JSON.parse(checksumData) + const data = JSON.parse(checksumData) + const checksums = data.checksums for (const name of Object.keys(checksums)) { checksums[name] = "hash" } - info.AsarChecksums = JSON.stringify(checksums) + info.AsarIntegrity = JSON.stringify(data) } if (checkOptions.checkMacApp != null) {