-
Notifications
You must be signed in to change notification settings - Fork 258
feat: add integrity digest management commands and functionality #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nmggithub
wants to merge
3
commits into
electron:main
Choose a base branch
from
nmggithub:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| import path from 'node:path'; | ||
| import crypto from 'node:crypto'; | ||
| import plist from 'plist'; | ||
|
|
||
| import { wrappedFs as fs } from './wrapped-fs.js'; | ||
| import { FileRecord } from './disk.js'; | ||
|
|
||
| // Integrity digest type definitions | ||
|
|
||
| type IntegrityDigest<Version extends number, AdditionalParams> = | ||
| | { used: false } | ||
| | ({ used: true; version: Version } & AdditionalParams); | ||
|
|
||
| type IntegrityDigestV1 = IntegrityDigest<1, { sha256Digest: Buffer }>; | ||
|
|
||
| type AnyIntegrityDigest = IntegrityDigestV1; // Extend this union type as new versions are added | ||
|
|
||
| // Integrity digest calculation functions | ||
|
|
||
| type AsarIntegrity = Record<string, Pick<FileRecord['integrity'], 'algorithm' | 'hash'>>; | ||
|
|
||
| function calculateIntegrityDigestV1(asarIntegrity: AsarIntegrity): IntegrityDigestV1 { | ||
| const integrityHash = crypto.createHash('SHA256'); | ||
| for (const key of Object.keys(asarIntegrity).sort()) { | ||
| const { algorithm, hash } = asarIntegrity[key]; | ||
| integrityHash.update(key); | ||
| integrityHash.update(algorithm); | ||
| integrityHash.update(hash); | ||
| } | ||
| return { | ||
| used: true, | ||
| version: 1, | ||
| sha256Digest: integrityHash.digest(), | ||
| }; | ||
| } | ||
|
|
||
| function calculateIntegrityDigestV1ForApp(appPath: string): IntegrityDigestV1 { | ||
| const plistPath = path.join(appPath, 'Contents', 'Info.plist'); | ||
| const plistBuffer = fs.readFileSync(plistPath); | ||
| const plistData = plist.parse(plistBuffer.toString()) as Record<string, any>; | ||
| const asarIntegrity = plistData['ElectronAsarIntegrity'] as AsarIntegrity; | ||
| return calculateIntegrityDigestV1(asarIntegrity); | ||
| } | ||
|
|
||
| /// Integrity digest handling errors | ||
|
|
||
| const UnknownIntegrityDigestVersionError = class extends Error { | ||
| constructor(version: number) { | ||
| super(`Unknown integrity digest version: ${version}`); | ||
| this.name = 'UnknownIntegrityDigestVersionError'; | ||
| } | ||
| }; | ||
|
|
||
| // Integrity digest storage and retrieval functions | ||
|
|
||
| const INTEGRITY_DIGEST_SENTINEL = 'AGbevlPCksUGKNL8TSn7wGmJEuJsXb2A'; | ||
|
|
||
| function pathToIntegrityDigestFile(appPath: string) { | ||
| if (appPath.endsWith('.app')) { | ||
| return path.resolve( | ||
| appPath, | ||
| 'Contents', | ||
| 'Frameworks', | ||
| 'Electron Framework.framework', | ||
| 'Electron Framework', | ||
| ); | ||
| } | ||
| throw new Error('App path must be an .app bundle'); | ||
| } | ||
|
|
||
| function forEachSentinelInApp( | ||
| appPath: string, | ||
| callback: (sentinelIndex: number, integrityFile: Buffer) => void, | ||
| writeBack: boolean = false, | ||
| ) { | ||
| const integrityFilePath = pathToIntegrityDigestFile(appPath); | ||
| const integrityFile = fs.readFileSync(integrityFilePath); | ||
| let searchCursor = 0; | ||
| const sentinelAsBuffer = Buffer.from(INTEGRITY_DIGEST_SENTINEL); | ||
| do { | ||
| const sentinelIndex = integrityFile.indexOf(sentinelAsBuffer, searchCursor); | ||
| if (sentinelIndex === -1) break; | ||
| callback(sentinelIndex, integrityFile); | ||
| searchCursor = sentinelIndex + sentinelAsBuffer.length; | ||
| } while (true); | ||
| if (writeBack) { | ||
| fs.writeFileSync(integrityFilePath, integrityFile); | ||
| } | ||
| } | ||
|
|
||
| function doDigestsMatch(digestA: AnyIntegrityDigest, digestB: AnyIntegrityDigest): boolean { | ||
| if (digestA.used !== digestB.used) return false; | ||
| if (digestA.used && digestB.used) { | ||
| if (digestA.version !== digestB.version) return false; | ||
| switch (digestA.version) { | ||
| case 1: | ||
| return digestA.sha256Digest.equals(digestB.sha256Digest); | ||
| default: | ||
| throw new UnknownIntegrityDigestVersionError(digestA.version); | ||
| } | ||
| } else return true; | ||
| } | ||
|
|
||
| function sentinelIndexToDigest<T extends AnyIntegrityDigest>( | ||
| integrityFile: Buffer, | ||
| sentinelIndex: number, | ||
| ): T { | ||
| const used = integrityFile.readUInt8(sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length) === 1; | ||
| if (!used) { | ||
| return { used: false } as T; | ||
| } else { | ||
| const version = integrityFile.readUInt8(sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 1); | ||
| switch (version) { | ||
| case 1: { | ||
| const sha256Digest = integrityFile.subarray( | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 2, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 2 + 32, // SHA256 digest size | ||
| ); | ||
| return { | ||
| used: true, | ||
| version: 1, | ||
| sha256Digest, | ||
| } as T; | ||
| } | ||
| default: | ||
| throw new UnknownIntegrityDigestVersionError(version); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function getStoredIntegrityDigestForApp<T extends AnyIntegrityDigest>( | ||
| appPath: string, | ||
| ): Promise<T> { | ||
| let lastDigestFound: T | null = null; | ||
| forEachSentinelInApp(appPath, (sentinelIndex, integrityFile) => { | ||
| const currentDigest = sentinelIndexToDigest<T>(integrityFile, sentinelIndex); | ||
| if (lastDigestFound === null) { | ||
| lastDigestFound = currentDigest; | ||
| } else if (!doDigestsMatch(currentDigest, lastDigestFound)) { | ||
| throw new Error('Multiple differing integrity digests found in the binary'); | ||
| } | ||
| lastDigestFound = currentDigest; | ||
| }); | ||
| if (lastDigestFound === null) { | ||
| throw new Error('No integrity digest found in the binary'); | ||
| } | ||
| return lastDigestFound; | ||
| } | ||
|
|
||
| async function setStoredIntegrityDigestForApp<T extends AnyIntegrityDigest>( | ||
| appPath: string, | ||
| digest: T, | ||
| ): Promise<void> { | ||
| if (digest.used === true && digest.version !== 1) { | ||
| throw new UnknownIntegrityDigestVersionError(digest.version); | ||
| } | ||
| forEachSentinelInApp( | ||
| appPath, | ||
| (sentinelIndex, integrityFile) => { | ||
| integrityFile.writeUInt8( | ||
| digest.used ? 1 : 0, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length, | ||
| ); | ||
| const oldVersion = integrityFile.readUInt8( | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 1, | ||
| ); | ||
| switch (oldVersion) { | ||
| case 1: | ||
| integrityFile.fill( | ||
| 0, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 2, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 2 + 32, // SHA256 digest size | ||
| ); | ||
| break; | ||
| } | ||
| if (digest.used) { | ||
| integrityFile.writeUInt8( | ||
| digest.version, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 1, | ||
| ); | ||
| switch (digest.version) { | ||
| case 1: { | ||
| const v1Digest = digest as IntegrityDigestV1 & { used: true }; | ||
| v1Digest.sha256Digest.copy( | ||
| integrityFile, | ||
| sentinelIndex + INTEGRITY_DIGEST_SENTINEL.length + 2, | ||
| ); | ||
| break; | ||
| } | ||
| default: | ||
| throw new UnknownIntegrityDigestVersionError(digest.version); | ||
| } | ||
| } | ||
| }, | ||
| true, | ||
| ); | ||
| } | ||
|
|
||
| // High-level integrity digest management functions | ||
|
|
||
| function printDigest(digest: AnyIntegrityDigest, prefix: string = '') { | ||
| const digestLogger = prefix | ||
| ? (s: string, ...args: any[]) => console.log(prefix + s, ...args) | ||
| : console.log; | ||
| if (!digest.used) { | ||
| digestLogger('Integrity digest is OFF'); | ||
| return; | ||
| } | ||
| digestLogger('Integrity digest is ON (version: %d)', digest.version); | ||
| switch (digest.version) { | ||
| case 1: | ||
| digestLogger('\tDigest (SHA256): %s', digest.sha256Digest.toString('hex')); | ||
| break; | ||
| default: | ||
| digestLogger('\tUnknown metadata for digest version: %d', digest.version); | ||
| } | ||
| } | ||
|
|
||
| export async function enableIntegrityDigestForApp(appPath: string): Promise<void> { | ||
| try { | ||
| console.log('Calculating integrity digest...'); | ||
| const digest = calculateIntegrityDigestV1ForApp(appPath); | ||
| console.log('Turning integrity digest ON...'); | ||
| await setStoredIntegrityDigestForApp(appPath, digest); | ||
| console.log('Integrity digest turned ON'); | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| console.log('Failed to turn ON integrity digest: %s', errorMessage); | ||
| } | ||
| } | ||
|
|
||
| export async function disableIntegrityDigestForApp(appPath: string): Promise<void> { | ||
| try { | ||
| console.log('Turning integrity digest OFF...'); | ||
| await setStoredIntegrityDigestForApp(appPath, { used: false }); | ||
| console.log('Integrity digest turned OFF'); | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| console.log('Failed to turn OFF integrity digest: %s', errorMessage); | ||
| } | ||
| } | ||
|
|
||
| export async function printStoredIntegrityDigestForApp(appPath: string): Promise<void> { | ||
| try { | ||
| const storedDigest = await getStoredIntegrityDigestForApp(appPath); | ||
| printDigest(storedDigest); | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| console.log('Failed to read integrity digest: %s', errorMessage); | ||
| } | ||
| } | ||
|
|
||
| export async function verifyIntegrityDigestForApp(appPath: string): Promise<void> { | ||
| try { | ||
| const storedDigest = await getStoredIntegrityDigestForApp(appPath); | ||
| if (!storedDigest.used) { | ||
| console.log('Integrity digest is off, verification SKIPPED'); | ||
| return; | ||
| } | ||
| const calculatedDigest = calculateIntegrityDigestV1ForApp(appPath); | ||
| if (doDigestsMatch(storedDigest, calculatedDigest)) { | ||
| console.log('Integrity digest verification PASSED'); | ||
| } else { | ||
| console.log('Integrity digest verification FAILED'); | ||
| console.log('Expected digest:'); | ||
| printDigest(calculatedDigest, '\t'); | ||
| console.log('Actual digest:'); | ||
| printDigest(storedDigest, '\t'); | ||
| } | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| console.log('Failed to verify integrity digest: %s', errorMessage); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might want to change this message to be something more descriptive like "unexpected digest version", but that reads more like an error not an info. Perhaps the current message is fine.