Skip to content

Commit cd60f29

Browse files
committed
feat: check the SHA-256 of a stored build before the install
1 parent 4ae39cd commit cd60f29

7 files changed

Lines changed: 108 additions & 5 deletions

File tree

‎docs/docs/commands/install.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ the fix, for these builds:
7676
`devhub list --json` gives this reason in `installProblem`, before you
7777
try the install.
7878

79+
## The hash check
80+
81+
After it selects the device, DevHub reads the SHA-256 of the stored file
82+
and compares it with the record in the [store](../guides/store.md). The
83+
sync and the local store write the hash of the file they store. When the
84+
two differ, the file changed after that, and DevHub does not install it.
85+
The exit code is `1`, and the message names both hashes and the fix:
86+
delete the build folder, and run `devhub sync` or `npx expo run:<platform>`
87+
again. The install button of `devhub ui` makes the same check.
88+
7989
## When adb install fails
8090

8191
DevHub reads the adb output, and gives the cause and the fix:

‎packages/devhub/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ notes name each one.
2424
`primaryAbi`, and `ANDROID_ABIS`.
2525
- The public docs site at https://rootnative.github.io/devhub/. The README
2626
files link to it, and `homepage` in the package points at it.
27+
- `devhub install` and the install button of `devhub ui` compare the
28+
SHA-256 of the stored file with the record before the install, and refuse
29+
a file that differs. The message names both hashes and the fix.
2730

2831
- `devhub match` calculates the native fingerprint of the project in the
2932
current folder, with the project's own `expo/fingerprint` and the options

‎packages/devhub/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ the fix, for these builds:
127127
| iOS device build (`.ipa`) | A simulator cannot run it. | Set `"ios": { "simulator": true }` in the build profile in `eas.json`, and make a new build. |
128128
| Any iOS build on Windows or Linux | iOS simulators run only on macOS. | Install the build on a Mac. |
129129

130+
After it selects the device, DevHub reads the SHA-256 of the stored file
131+
and compares it with the record. The sync and the local store write the
132+
hash of the file they store. When the two differ, the file changed after
133+
that, and DevHub does not install it. The exit code is `1`, and the message
134+
gives the fix: delete the build folder, and run `devhub sync` or
135+
`npx expo run:<platform>` again.
136+
130137
When `adb install` fails, DevHub reads the adb output and gives the cause
131138
and the fix:
132139

‎packages/devhub/llms.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ Usage: devhub install <id> [options]
176176
Install a stored build on a device that is ready: an Android build on an
177177
emulator or a device, or an iOS simulator build on a booted simulator (macOS
178178
only). <id> is the EAS build ID, or the start of it. "devhub list" shows the
179-
builds, and "devhub devices" shows the devices. DevHub does not start a
179+
builds, and "devhub devices" shows the devices. DevHub compares the SHA-256
180+
of the file with the record before the install. DevHub does not start a
180181
device, and does not open the app.
181182

182183
Options:

‎packages/devhub/src/__tests__/flow.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { mkdir } from 'node:fs/promises'
1+
import { createHash } from 'node:crypto'
2+
import { mkdir, writeFile } from 'node:fs/promises'
23
import os from 'node:os'
4+
import path from 'node:path'
35
import { describe, expect, it } from 'vitest'
46
import type { CommandRunner } from '../command/run-command'
57
import { deviceReport } from '../flow/devices'
@@ -14,10 +16,16 @@ import {
1416
fakeDeviceTools,
1517
fakeDownload,
1618
fakeEas,
19+
localBuild,
1720
useTempDir,
21+
writeLocalBuild,
1822
writeStoredBuild,
1923
} from './helpers'
2024

25+
function sha256(text: string): string {
26+
return createHash('sha256').update(text).digest('hex')
27+
}
28+
2129
const ID_A = 'aaaa1111-0000-4000-8000-000000000001'
2230
const ID_B = 'aaaa2222-0000-4000-8000-000000000002'
2331
const ID_C = 'cccc3333-0000-4000-8000-000000000003'
@@ -206,6 +214,41 @@ describe('installStoredBuild', () => {
206214
expect(tools.calls).toEqual([])
207215
})
208216

217+
it('refuses a build whose file does not have the SHA-256 of its record', async () => {
218+
const home = await tempDir()
219+
const file = await writeStoredBuild(home, easBuild({ id: ID_A }))
220+
await writeFile(file, 'changed')
221+
const tools = fakeDeviceTools({ devices: adbList(EMULATOR_LINE) })
222+
const { deps, progress } = installDeps(home, tools.run)
223+
224+
const result = await installStoredBuild({ id: ID_A }, deps)
225+
226+
expect(result).toMatchObject({ kind: 'error', usage: false })
227+
const message = result.kind === 'error' ? result.message : ''
228+
expect(message).toContain(
229+
`The file ${file} of the build ${ID_A} has the SHA-256 ${sha256('changed')}, but its record has ${sha256('binary')}.`,
230+
)
231+
expect(message).toContain('run devhub sync in the project')
232+
expect(progress).toEqual([])
233+
expect(installCalls(tools.calls)).toEqual([])
234+
})
235+
236+
it('names npx expo run for a local build whose file changed', async () => {
237+
const home = await tempDir()
238+
const file = await writeLocalBuild(home, ID_C, localBuild())
239+
await writeFile(file, 'changed')
240+
const tools = fakeDeviceTools({ devices: adbList(EMULATOR_LINE) })
241+
const { deps } = installDeps(home, tools.run)
242+
243+
const result = await installStoredBuild({ id: ID_C }, deps)
244+
245+
expect(result).toMatchObject({ kind: 'error', usage: false })
246+
expect(result.kind === 'error' && result.message).toContain(
247+
`Delete the folder ${path.dirname(file)}, and then run npx expo run:android in the project to store the build again.`,
248+
)
249+
expect(installCalls(tools.calls)).toEqual([])
250+
})
251+
209252
it('returns a failed adb install as an error, and does not throw', async () => {
210253
const home = await tempDir()
211254
await writeStoredBuild(home, easBuild({ id: ID_A }))

‎packages/devhub/src/cli/install.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const INSTALL_HELP = `Usage: devhub install <id> [options]
1212
Install a stored build on a device that is ready: an Android build on an
1313
emulator or a device, or an iOS simulator build on a booted simulator (macOS
1414
only). <id> is the EAS build ID, or the start of it. "devhub list" shows the
15-
builds, and "devhub devices" shows the devices. DevHub does not start a
15+
builds, and "devhub devices" shows the devices. DevHub compares the SHA-256
16+
of the file with the record before the install. DevHub does not start a
1617
device, and does not open the app.
1718
1819
Options:

‎packages/devhub/src/flow/install.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path'
12
import { listAndroidDevices, resolveAdb } from '../devices/android'
23
import {
34
DeviceError,
@@ -12,9 +13,15 @@ import {
1213
installPlatform,
1314
installProblem,
1415
} from '../install/install'
16+
import {
17+
downloadAgainFix,
18+
isLocalRecord,
19+
type StoredBuild,
20+
} from '../store/builds'
21+
import { hashFile } from '../store/files'
1522
import { resolveHome } from '../store/home'
1623
import { findStoredBuild } from './find'
17-
import { failure, type FlowResult } from './result'
24+
import { errorMessage, failure, type FlowResult } from './result'
1825

1926
export type InstallRequest = {
2027
/** The EAS build ID, or a unique prefix of it. */
@@ -41,7 +48,8 @@ export type InstalledBuild = {
4148

4249
/**
4350
* Finds a stored build, checks it, selects the device, and installs the
44-
* build. It checks the build before it runs a device tool.
51+
* build. It checks the build before it runs a device tool, and it compares
52+
* the SHA-256 of the file with the record before the install.
4553
*/
4654
export async function installStoredBuild(
4755
request: InstallRequest,
@@ -69,6 +77,10 @@ export async function installStoredBuild(
6977
return choice
7078
}
7179
const { device } = choice
80+
const hashProblem = await hashProblemOf(build)
81+
if (hashProblem !== undefined) {
82+
return failure(hashProblem)
83+
}
7284
deps.progress(
7385
`Installing the build ${build.id} on ${device.name} (${device.id}).`,
7486
)
@@ -125,3 +137,29 @@ function platformDevices(
125137
? listIosSimulators(deps.run, deps.cwd)
126138
: listAndroidDevices(deps.run, resolveAdb(deps), deps.cwd)
127139
}
140+
141+
/**
142+
* The file must have the SHA-256 that the record stores. The sync and the
143+
* local store write the hash of the file they store, so a different hash
144+
* means the file changed after that.
145+
*/
146+
async function hashProblemOf(build: StoredBuild): Promise<string | undefined> {
147+
let sha256: string
148+
try {
149+
sha256 = (await hashFile(build.path)).sha256
150+
} catch (error) {
151+
return `DevHub cannot read the file ${build.path} of the build ${build.id}: ${errorMessage(error)}`
152+
}
153+
if (sha256 === build.record.sha256) {
154+
return undefined
155+
}
156+
return `The file ${build.path} of the build ${build.id} has the SHA-256 ${sha256}, but its record has ${build.record.sha256}. The file changed after DevHub stored it, so DevHub does not install it. ${storeAgainFix(build)}`
157+
}
158+
159+
function storeAgainFix(build: StoredBuild): string {
160+
if (!isLocalRecord(build.record)) {
161+
return downloadAgainFix(build.path)
162+
}
163+
const platform = build.record.local.platform.toLowerCase()
164+
return `Delete the folder ${path.dirname(build.path)}, and then run npx expo run:${platform} in the project to store the build again.`
165+
}

0 commit comments

Comments
 (0)