Skip to content

fix(android): verify the helper APK digest before running settings locale as shell - #5143

Merged
thymikee merged 5 commits into
mainfrom
t3/verify-locale-helper-digest
Nov 27, 2026
Merged

thymikee merged 5 commits into
mainfrom
t3/verify-locale-helper-digest

Conversation

@thymikee

@thymikee thymikee commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

settings locale on Android (#5050) runs SystemLocaleCommand from the installed helper APK as the shell user. The install step can trust a memo or a higher versionCode without reading the APK, so a foreign APK under the helper package name could run as shell.

Each locale command now hashes the base APK and runs app_process from that path only when the SHA-256 matches the trusted manifest.

  • Android before 8.0 can reuse install paths, so it refuses with locale-api-level-unsupported before helper preparation.
  • A missing base APK or a mismatch refuses with locale-helper-apk-unverified. The next locale command inspects the install; the shared snapshot memo stays, and an older overlapping call cannot clear a newer refusal.
  • pm path and pull failures keep their adb classification.
  • Failures before the locale write state dispatched: no and name a completed helper install.

Follow-ups: cmd locale set-device-locale on Android 16 QPR2+; reset semantics.

Before / after

settings locale de-DE with a newer foreign APK as the helper package:

  • Before: app_process runs it as shell.
  • After: locale-helper-apk-unverified, dispatched: no, no shell execution.

Validation

Source build of cda1497, Android 17 emulator, one daemon: settings locale de-DE twice (previousLocale en-US, then de-DE), then reset (en-US).

At 566e9c7, settings-locale.test.ts passes 17/17. Removing the digest check, the API gate, the locale recheck, or the refusal token fails the matching tests. The changes since cda1497 touch only refusal bookkeeping. CI pending. Android 8–16 and remote providers not run.

🤖 Generated with Claude Code

…cale as shell

settings locale loads SystemLocaleCommand from the installed helper's base
APK through app_process as the shell user. The install step may accept a
cached decision or a higher installed versionCode without reading the APK,
so an APK installed under the helper package name could run as shell.

Resolve the base APK path once, hash that file on the device, and run
app_process only from that path when its SHA-256 matches the trusted
manifest. A missing path, unreadable APK, or digest mismatch refuses with
locale-helper-apk-unverified and dispatched no, and drops the cached
install decision so the next command inspects the device again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.65 MB 6.65 MB +3.0 kB
Package (unpacked) 6.65 MB 6.65 MB +3.0 kB
Package (download) 2.03 MB 2.03 MB +1.1 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 24.1 ms 21.1 ms -3.0 ms
CLI --help 48.3 ms 47.5 ms -0.8 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread website/docs/docs/error-reasons.md Resolved
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on 7608df3. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

The review of 7608df3 found problems to resolve before merge in the digest check for Android settings locale.

settings-locale.ts:34 now runs the shell route only after a pull of the device's base.apk and an exact digest match. Only fake-adb tests cover this, and the PR body says no device run was done. If a real base.apk does not byte-match the bundled or provider artifact, or the pull fails on a real transport, every settings locale call refuses. Live evidence is required before merge. On an Android emulator with the bundled helper installed, run agent-device settings locale de-DE --json at this commit, or at the new head after fixes. Run it again in the same daemon, so the second call reuses the install decision. Both calls must succeed with locale de-DE and a previousLocale. Then settings locale reset --json must succeed. Please attach the outputs. The same run on one remote provider that supplies snapshotHelperArtifact is optional.

A second independent pass found a likely gap at settings-locale.ts:38. The Android locale route has no SDK gate, and the helper manifest admits API 25. Android 7.1 gives a new install the first vacant numeric directory (installer rename, path allocation). Apps cannot write there directly, but a verified path can be reused by another revision before app_process opens it. The shell then loads bytes the host did not hash. Can every release with reusable install paths verify and execute one pinned or privately staged APK, and otherwise refuse with a typed reason before app_process?

A second independent pass found that a newer official helper likely loses locale support at settings-locale.ts:130. The base code ran a newer helper that has a working SystemLocaleCommand. The installer treats a higher versionCode as current, but the digest check rejects it on every retry. Only an uninstall and a reinstall of the bundled helper clears this, so the documented locale usage fails on that setup. Can each higher version with a compatible official artifact be checked against the exact SHA-256 from a trusted manifest, while versions without a trusted digest stay refused?

A second independent pass found that settings-locale.ts:113 likely loses adb diagnostics. With a warm install memo, the device can go offline or revoke authorization before pm path. Local adb returns a nonzero result under allowFailure, and so does Limrun. The code treats it as a missing path, drops stderr, and tells the user to uninstall a valid helper. The base code returned the classified error with a reconnect or authorization hint. Can every nonzero or transport failure in this verification keep its classification and details (for example through androidAdbResultError), with the APK refusal reserved for evidence about the APK and locale execution still gated?

A second independent pass found that a pull timeout can likely block locale at settings-locale.ts:121 while the shell works. On a slow ADB link, short commands can succeed while the full APK pull exceeds the 10-second identity timeout. The caught timeout is then hidden behind uninstall advice. The base code needed only pm path and app_process. Limrun also transfers the full file. Can that route hash the installed APK through the device shell, so only the digest crosses the link, and refuse with the classified failure whenever no digest can be established?

A second independent pass found that device changes can likely precede the refusal at settings-locale.ts:103. Preparation can install a missing helper, uninstall an incompatible one, or force-stop a retiring one before the verification pull fails. app_process does not run, but dispatched: no can read as no device change. Can a no-change refusal follow only read-only checks? If provisioning stays in this command, the failure must report each completed change and limit the no-dispatch claim to the locale write.

A second independent pass found that the refusal likely adds work to a later snapshot (settings-locale.ts:94). After a transient pull failure, the next snapshot misses the install memo and hashes the local and installed APKs again. Can a failed locale check force a recheck for locale retries only, while the snapshot memo and the check before each locale execution stay in place?

Not blocking: settings-locale.ts:110 repeats the path-and-digest steps that readInstalledAndroidPackageSha256 owns, with different output parsing and timeout, so you can export that function with an {apkPath, sha256} result and keep the locale refusal mapping at the caller (then readAndroidDeviceFileSha256 needs no export), or leave it.

These high-priority threads still apply: reason documentation row and pm path errors. One lower-priority thread also still applies. The newer helper thread does not apply as a defect in the refusal itself: an exact digest cannot tell a newer genuine helper from a foreign one, and uninstall plus retry recovers. Signature-based trust would be separate follow-up work, so that thread can be resolved.

The only checks that did not pass are two cancelled "Body rules" runs. This is likely unrelated to the code: a later body edit probably superseded them, and the job does not exercise any file in this diff. All 22 other checks pass.

Not verified here: the tests were not run, and their regression value was judged only by reading them against the earlier path lookup. A byte match between a real device's base.apk and the bundled artifact was not confirmed, although the existing install inspection relies on it. The helper manifest was not checked for the absence of CHANGE_CONFIGURATION that the Javadoc claims. The cost of a full APK pull per locale call on remote providers was not measured.

Before merge, please resolve the points above, then attach the live Android settings locale run at the new head: first call, repeat call, and reset.

Copy the helper's base APK into the shell scratch directory, hash that
copy, and run app_process from it only on a match with the trusted
manifest. Apps cannot write there, so a reused install directory or a
package swap cannot change the bytes after the check.

Keep adb failures from pm path, the copy, and the pull classified instead
of reporting them as an unverified APK. Name a helper install that
preparation completed on every failure before the locale write. Drop a
refused install decision only on the next locale command, so snapshots
keep their memo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Response to the review of 7608df3. b2d37f9 addresses each point.

  • Reused install paths: the locale path now copies the base APK to /data/local/tmp/agent-device-locale-<random>.apk, hashes that copy, and runs app_process from it only on a match. Apps cannot write the shell scratch directory. A reused install directory or a package swap after the check therefore cannot change the executed bytes. This applies on every release, and the copy is removed afterwards.
  • Newer official helpers: these stay refused. The manifest carries one trusted digest, and the public signing key cannot tell a newer genuine build from a foreign APK. Uninstalling and retrying recovers.
  • adb diagnostics: a nonzero pm path, copy, or pull keeps its androidAdbResultError classification and details, and states dispatched: no. Only a missing base APK or a digest mismatch uses locale-helper-apk-unverified. The reason row is updated to match.
  • Device-side digest: this is not adopted. The helper APK is about 30 KB, so the pull is one short transfer within the existing identity timeout. A device-shell digest would add a second route that depends on toybox sha256sum on each supported release.
  • Completed changes: every failure before the locale write adds helperInstalled: true and helperInstallReason when preparation installed the helper. dispatched: no refers to the locale write.
  • Recheck scope: a refusal no longer drops the snapshot memo. Only the next locale command on that device forgets the cached install decision before it prepares the helper. Transport failures do not trigger the recheck.
  • Shared reader (not blocking): this is kept as is. The locale route hashes the staged copy, not the installed path, so a {apkPath, sha256} reader of the installed APK does not fit. readAndroidDeviceFileSha256 is the shared step.

Live run at b2d37f9 with the source build (pnpm build, pnpm build:android). One daemon with its own state directory, Android 17 emulator:

$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"en-US","message":"Locale set to de-DE"}}
$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"de-DE","message":"Locale set to de-DE"}}
$ agent-device settings locale reset --json
{"success":true,"data":{"setting":"locale","state":"reset","locale":"en-US","previousLocale":"de-DE","message":"Locale reset to the device default"}}

Afterwards, /data/local/tmp held no agent-device-locale-* file. No run was done on API 24–34 or on a remote provider.

…d install path

Replace the staged copy with a smaller rule. Android 8.0 gives each
install a random directory, so a verified base APK path names one APK
and app_process can run from it. Older releases can reuse an install
path for another APK, so settings locale refuses them with
locale-api-level-unsupported before it prepares the helper.

A missing base APK or a digest mismatch drops the cached install
decision it disproves. Transport failures keep their adb classification
and leave the decision in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Update at 5ff4a0c. The staged-copy mechanism from b2d37f9 is removed in favor of a smaller rule.

  • Reused install paths: Android 8.0 gives each install a random directory, so a verified base APK path names one APK, and app_process runs from that path after its digest matches. Before 8.0, an install can take a vacant numeric directory, so a verified path can be reused for another APK. Making those releases safe would need a private staged copy and its cleanup, which is disproportionate for API 24–25. settings locale therefore refuses them with locale-api-level-unsupported and dispatched: no. The refusal comes after one getprop and before any helper preparation. Help text and the command docs state the floor.
  • Recheck scope: the per-device recheck map is removed. Only a missing base APK or a digest mismatch drops the cached install decision, because those prove the decision wrong. Transport failures leave it in place, so a later snapshot keeps its memo.
  • adb classification, the helperInstalled report, newer-helper refusal, and the pull-based digest are unchanged from the previous comment.

Live run at 5ff4a0c with the source build (pnpm build, pnpm build:android), one daemon with its own state directory, Android 17 emulator:

$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"en-US","message":"Locale set to de-DE"}}
$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"de-DE","message":"Locale set to de-DE"}}
$ agent-device settings locale reset --json
{"success":true,"data":{"setting":"locale","state":"reset","locale":"en-US","previousLocale":"de-DE","message":"Locale reset to the device default"}}

The emulator was booted and shown through the host device binding. The commands ran from the source build, because the host binding's CLI is a client of the released daemon and does not contain this change.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 5ff4a0c. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

At 5ff4a0c, one problem from the earlier review remains: a definitive locale refusal still evicts the shared install memo. pm path and pull failures now keep their adb error, and the error reason docs now cover both refusal cases.

A second independent pass found that this likely still slows the next snapshot (settings-locale.ts). On API 26+, snapshot preparation accepts and memoizes a newer official helper (helper-package-install.ts:145, snapshot-helper-install.ts:140). settings locale with the older manifest then refuses the digest and deletes that shared memo, and the latest changes brought this immediate eviction back. The next helper-backed snapshot takes the cold path (snapshot.ts:444 and :595), rehashes the local artifact and queries PackageManager (snapshot-helper-install.ts:103–137). A same-version replacement also pulls the installed APK and can trigger a reinstall from the snapshot side. A successful pm path response with no base entry evicts the memo in the same way. The rule: a locale verification refusal must not make the next snapshot repeat install checks. Could the refusal instead record a recheck for the affected locale install identity, so the next locale preparation skips the memo fast path and the snapshot entry stays? Please keep the digest check on every invocation. A test should show that, after a refusal, the next snapshot repeats no install checks and the next locale call checks the install again.

One lower-priority thread still applies as a non-blocking cost note. These threads no longer apply and can be resolved: this one, because the docs row now names both refusal cases and an unreadable APK no longer maps to that reason; this one, because a nonzero pm path now keeps its error classification and hint, with a test; this one, because refusing a newer helper is intended without a trusted digest and the hint names uninstalling as recovery; and this one, because pm path and pull failures no longer map to locale-helper-apk-unverified.

Not verified here: tests were not run locally, so CI covers them, and their regression value was judged by reading them against the production route. Not verified here: the live run covers only Android 17, and, as the PR body states, a base.apk byte match was not observed on Android 8–16 or on remote providers that supply snapshotHelperArtifact. Not verified here: the random install directory on Android 8.0+ was not checked in AOSP source, and the comment-only SystemLocaleCommand Javadoc claim about CHANGE_CONFIGURATION was not checked against the helper manifest.

The Body rules check fails because the PR body has 270 words outside code blocks, and the limit is under 250. This failure comes from the body text, not the code, and only a body edit fixes it. A second Body rules run was cancelled and superseded. All other checks pass.

Before merge, fix the memo eviction above and trim the PR body below 250 words outside code blocks.

…apshot memo

A locale refusal no longer deletes the shared install memo. It records
the device, and the next locale command inspects the install instead of
trusting the memo, while snapshots keep answering from it. The digest
check still runs on every locale command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Update at cda1497 for the review of 5ff4a0c.

  • Memo eviction: a locale refusal no longer deletes the shared install memo. It records the device instead. The next locale command passes inspectInstalled to the snapshot install step, which inspects the device instead of trusting the memo, while snapshots keep answering from it. The digest check still runs on every locale command.
  • Test: after a refusal, the snapshot install step returns current with no adb calls, and the next locale call queries the package manager. The tests fail when the memo bypass is disabled or forced.
  • PR body: trimmed below 250 words.

Live run at cda1497, source build, one daemon, Android 17 emulator:

$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"en-US","message":"Locale set to de-DE"}}
$ agent-device settings locale de-DE --json
{"success":true,"data":{"setting":"locale","state":"de-DE","locale":"de-DE","previousLocale":"de-DE","message":"Locale set to de-DE"}}
$ agent-device settings locale reset --json
{"success":true,"data":{"setting":"locale","state":"reset","locale":"en-US","previousLocale":"de-DE","message":"Locale reset to the device default"}}

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At cda1497, the problem from the earlier review is fixed: a locale refusal now records a recheck and no longer evicts the shared install memo. Two problems remain, one in the new test and one in how that recheck is cleared.

A second independent pass found that the new snapshot probe in settings-locale.test.ts pushes its adb arguments into a string[][]. AndroidAdbExecutor passes readonly string[], so the new test fails type checking with TS4104. Please declare snapshotCalls as (readonly string[])[], so its entries match the executor type.

A second independent pass also found that an overlapping locale call can likely erase a newer refusal without inspecting the install (settings-locale.ts). This needs a warm memo, a same-version helper APK with a different digest, and two sessionless settings requests from different workspaces on one running target. These requests take separate session locks, and the device is bound later. Request A reads no refusal at line 51 and waits for preparation. Request B then refuses and sets the marker. A takes the still-warm memo and deletes that marker at line 53. If pm path then fails transiently in A, no marker is restored, and the next locale call trusts the memo and refuses the same APK without an install inspection that could repair it. Could each refusal store a fresh token, so that a request clears only the token it saw when it asked for inspection, and only if that token is still current? A request that saw no token would leave later refusals in place. A test should reach this order and show that the next locale call inspects the install.

These threads no longer apply and can be resolved: this one, because the docs row now names both refusal cases and an unreadable APK keeps its adb error; this one, because a nonzero pm path keeps its adb error classification; this one, because refusing a newer helper without a trusted digest is intended and the hint names uninstalling as recovery; this one, because pm path and pull failures no longer carry locale-helper-apk-unverified; and this one, because the second pull is intentional, as it hashes the exact path that app_process loads, and its small cost does not hold up the merge.

Not verified here: tests were not run locally, and the regression value of the new test was judged by reading it against 5ff4a0c and the production install route. Not verified here: the live run in the PR body covers the Android 17 success path at cda1497, not a live refusal followed by a snapshot and a second locale call; only the fake-adb test shows that interaction.

The Lint, Typecheck & Package check fails because of the TS4104 error above, which this change introduces. The Body rules run was cancelled and superseded; the PR body now has about 240 words outside code blocks, under the 250 limit, so a rerun should pass. All other checks pass, including the unit jobs.

Before merge, fix the test type, protect newer refusals from being cleared, and rerun Body rules.

…ts recheck

Each locale refusal records a fresh token. A locale call clears the
recheck only when it asked for the inspection and the token it saw is
still current, so a refusal recorded while it prepared the helper keeps
the next locale call inspecting the install. Type the snapshot probe
calls as readonly argument lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Update at 566e9c7 for the review of cda1497.

  • Overlapping refusal: each locale refusal now stores a fresh token per device. A locale call captures the token before preparation, asks for inspection only when one exists, and clears it only if the same token is still current. A call that saw no token leaves later refusals in place. A new test reaches the reported order. Call A reads no refusal and pauses inside helper preparation. Call B refuses the APK. Call A resumes and fails on pm path with device_offline. The next locale call still queries the package manager and reinstalls the helper. The test fails when the clear is unconditional.
  • TS4104: snapshotCalls is now (readonly string[])[]. tsc -b on the Android package passes.
  • Body rules: the PR body is under the limit (about 241 counted words), and the edit retriggers the check.

The changes since cda1497 touch only refusal bookkeeping. The live success path recorded at cda1497 is unchanged.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 566e9c7

Since the review of cda1497: 0 new · 0 still open · 0 no longer raised

No open findings.

2 findings below minor hidden (user_settings.min_severity).

1 new finding away from what changed since cda1497 not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (7d392e62143f).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

At 566e9c7, both problems from the earlier review are fixed, and no code problem remains. The snapshot probe now uses the executor's argument type, and a locale call now clears only the refusal it inspected, so a newer refusal still forces an install check on the next call. A new test covers that overlap.

These threads no longer apply and can be resolved: this one, because the docs now name both refusal cases and an unreadable APK keeps its adb error; this one, because a nonzero pm path keeps its adb error classification; this one, because refusing a newer helper without a trusted digest is intended and the hint names uninstalling as recovery; this one, because pm path and pull failures no longer carry locale-helper-apk-unverified; and this one, because the second pull intentionally hashes the exact path that app_process loads, at a small cost.

Not verified here: tests were not run locally. The regression value of the overlap test was judged by reading it against the earlier implementation, and its pass relies on CI. Not verified here: the live run in the PR body covers the Android 17 success path at cda1497, and no live run covers a refusal followed by a second locale call. The changes since that run only affect in-process refusal tracking and tests, so no new device run is requested.

The only check not passing is Body rules, which was cancelled and did not fail; it checks the PR body, which this diff does not touch. All other checks pass, including Typecheck.

Before merge, rerun Body rules. The body is about 235 words, under the 250 limit, so it should pass.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 27, 2026
@thymikee
thymikee merged commit bdb132d into main Nov 27, 2026
21 of 22 checks passed
@thymikee
thymikee deleted the t3/verify-locale-helper-digest branch November 27, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant