Skip to content

Commit

Permalink
feat: asar integrity: base64, externalAllowed
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jun 3, 2017
1 parent b57dc8a commit 9a7ac65
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions .idea/electron-builder.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions packages/asar-integrity/src/asarIntegrity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AsarIntegrity> {
// 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 <AsarIntegrity>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<string>((resolve, reject) => {
const hash = createHash(algorithm)
hash
.on("error", reject)
.setEncoding("hex")
.setEncoding(encoding)

createReadStream(file)
.on("error", reject)
Expand Down
7 changes: 4 additions & 3 deletions packages/electron-builder/src/packager/mac.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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<any>, appOutDir: string, checksums: { [key: string]: string; }) {
export async function createApp(packager: PlatformPackager<any>, appOutDir: string, asarIntegrity: AsarIntegrity) {
const appInfo = packager.appInfo
const appFilename = appInfo.productFilename

Expand Down Expand Up @@ -135,8 +136,8 @@ export async function createApp(packager: PlatformPackager<any>, appOutDir: stri
use(packager.platformSpecificBuildOptions.category || (<any>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<Promise<any | n>> = [
Expand Down
2 changes: 1 addition & 1 deletion test/out/mac/__snapshots__/macPackagerTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
7 changes: 4 additions & 3 deletions test/src/helpers/packTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9a7ac65

Please sign in to comment.