Skip to content

fix(ios): a device or simulator id no longer shapes a file path - #780

Merged
pbertsch merged 3 commits into
mainfrom
fix/devicectl-temp-file
Nov 17, 2026
Merged

pbertsch merged 3 commits into
mainfrom
fix/devicectl-temp-file

Conversation

@pbertsch

Copy link
Copy Markdown
Member

Summary

A device or simulator id no longer shapes a file path.

On main:

  • internal/ios/devicectl.go:68 FindPID (a physical iPhone's process list) wrote devicectl's output to filepath.Join(os.TempDir(), fmt.Sprintf("devicectl-procs-%s.json", udid[:8])) and then ran defer os.Remove(tmpFile). This had three problems:
    • Path traversal and deletion: an id such as ../../x names a file outside the temporary folder, and the deferred os.Remove deletes it.
    • A crash: udid[:8] panics for an id shorter than 8 characters.
    • A link in a shared folder: the name is predictable in the shared temporary folder, so it could be replaced by a symlink before devicectl writes.
  • internal/ios/simctl.go:426 simDataPath built ~/Library/Developer/CoreSimulator/Devices/<udid>/data. ReadToken globbed and read token files under it. With an id holding .., a separator or a glob character, the read would be shaped by the id.
  • internal/runner/device_context.go:923 iosTokenPath built the same path from the serial, and the reconnect removes that file.

The change

  • ios.ValidDeviceUDID and ios.ValidSimulatorUDID (internal/ios/udid.go) accept exact forms only: hex digits and dashes.
    • A device UDID is 8 hex, a dash and 16 hex (iPhone XS and later), or 40 hex (earlier devices). Sources: Reincubate, Corellium, TechTarget. These are third-party pages: Apple publishes no format.
    • A UUID is accepted too, for devicectl's CoreDevice identifier.
    • A simulator UDID is a UUID.
  • FindPID refuses an id that is not a device id before anything runs. Its output goes to processes.json in a new private folder (os.MkdirTemp: mode 0700, name chosen by the system, under the temporary folder), and the folder is removed after the call. devicectl creates the file itself, so a 0700 folder that only this process can write into rules out a planted link.
  • simDataPath is "" for anything but a simulator UDID. ReadToken then skips the glob and the legacy fallback: built on "", they would be relative to the working directory, or the shared /tmp/probe/token.
  • iosTokenPath is "" for anything but a simulator UDID, so the reconnect removes nothing.

Other paths built from device or app strings (checked, listed)

Site Built from State
internal/ios/devicectl.go:68 device UDID fixed here
internal/ios/simctl.go:271, 280 (token glob and fallback via simDataPath) simulator UDID fixed here
internal/runner/device_context.go:923 (token path, then os.Remove) serial fixed here
internal/cli/record.go:329 deviceSlug(device) device name safe: every run of characters outside [a-z0-9] becomes -, so no separator or .. survives
internal/record/bundle.go:174 e.ID+".json" exchange id safe: bundleID.MatchString(e.ID) is checked first
internal/runner/runner.go:736 recipe file names safe: read through the confined opener
simctl.AppDataPath / appContainerDataPath the path simctl get_app_container prints not built from the id; it is the local tool's own answer

Ids passed as command arguments (xcrun devicectl … --device <udid>, adb -s <serial>) go through exec without a shell, as separate arguments, so they cannot become a path or a command there.

Tests

  • TestUDIDForms: the three valid forms pass. These do not: ../../x, an absolute path, .., a UDID with /../../x behind it or a path in front of a UUID, a NUL, * and ?, 4096 characters, a non-ASCII digit (U+0660), the empty string, a short one.
  • TestFindPIDRefusesAHostileUDID: each hostile id is refused, and the fake xcrun on PATH never runs.
  • TestFindPIDWritesToAPrivateFolder: the --json-output file is processes.json in a probe-devicectl- folder under the temporary folder, with nothing of the id in the path, and the folder is gone after the call.
  • TestReadTokenIgnoresPathsAHostileUDIDWouldShape: a token planted where the old relative glob would look (<cwd>/Containers/Data/Application/x/tmp/probe/token) is not read for ../../x.
  • TestIOSTokenPathOnlyForASimulatorUDID: no path for hostile serials or a device UDID; a simulator UUID gives the usual path.

Hostile Unicode is built from code points in the test source, so the source stays ASCII (staticcheck ST1018).

Mutation check

Each mutant was applied, the tests were run, and the file was restored byte for byte. All 8 were killed. "The simulator id is not anchored" survived at first, because no hostile id held a valid UUID with more around it. Two such ids were added, and it is now killed.

Mutant Result
the device id regex is not anchored killed
the simulator id regex is not anchored killed
FindPID does not check the id killed
the output file is named after the id again killed
the private folder is not removed killed
simDataPath does not check the id killed
the token glob runs without a data path killed
iosTokenPath does not check the serial killed

Test plan

  • go test ./internal/ios/ ./internal/runner/ ./internal/device/ passes.
  • go vet and staticcheck pass on ./internal/ios/ and ./internal/runner/.
  • No raw format characters in the changed files.
  • Mutation check (table above).

@pbertsch
pbertsch requested a review from a team as a code owner November 17, 2026 06:53
@pbertsch
pbertsch merged commit 9b465bc into main Nov 17, 2026
10 of 11 checks passed
pbertsch added a commit that referenced this pull request Nov 17, 2026
…ig load, token read, kill (#784)

## Summary

Follow-ups from the review of #780: a device id that is not one is now refused where it would act, not later.

1. **`device.ios_device_id` must be a UDID.** It becomes the iOS device serial (`internal/cli/test.go`), and a simulator id shapes host paths. Until now it was not validated: `config.ValidateDeviceSerial` allows `/` and `.`, and does not see this key.
   - `config.LoadFile` now refuses anything but a simulator UDID or a device UDID (`ios.ValidSimulatorUDID` / `ios.ValidDeviceUDID`), with a message that names the key and says where to find the id.
   - Breaking for a `probe.yaml` that put a device name there. As the `--device` serial, a name with a space already failed.
2. **`DeviceCtl.Terminate` no longer hides a failed process lookup.** It returned nil, meaning "not running", whenever `FindPID` failed, including for an id #780 refuses. So on a physical iPhone a kill could pass silently and a relaunch could start beside a running app. It now returns the lookup error, and `KillApp` prints `the app was not stopped: …` in one line. The error quotes the id with `%q`.
3. **`SimCtl.ReadToken` fails fast** for an id that is not a simulator UDID (a physical device's, or not an id), instead of polling for the whole timeout.
4. **The token paths are built in one place**, `SimCtl.tokenFileCandidates`: the app's container, every data container, then the legacy device-level file. With an id that is not a simulator UDID it returns no path at all, the legacy file included. It is testable on its own; that mutant survived in #780.
5. **CHANGELOG wording of #780, corrected:**
   - the device UDID is 8 hex digits, a dash and **exactly 16** hex digits (not "8-16"), or 40;
   - only the id's first 8 characters were used, so the reach was a `.json` file **one folder above** the temporary folder.

Docs: `advanced/configuration.md` (the `ios_device_id` row) and `platform/ios.md` say the value must be a UDID. Their example is a valid UUID.

## Tests

- `TestTokenFileCandidatesNeedASimulatorUDID`: no paths for a device UDID or any of #780's hostile ids. A simulator's list ends with the legacy file.
- `TestReadTokenFailsFastForAnIDThatIsNotASimulator`: a hostile id, a device UDID and an empty id are each refused in under a second, with a 5 s timeout.
- `TestTerminateReportsALookupFailure`: a refused id gives the lookup error, and the fake `xcrun` never runs.
- `TestKillOnAPhysicalIPhoneWarnsWhenTheLookupFails`: the warning line is printed.
- `TestIOSDeviceIDMustBeAUDID`: empty, a UUID, a 25-character and a 40-character device UDID load. `../../x`, `/etc`, `iPhone 16`, a UUID with `/../x` and `*` are refused with the key named.

## Mutation check

Each mutant was applied, the tests were run, and the file was restored byte for byte. All 5 were killed.

| Mutant | Result |
|---|---|
| `ReadToken` does not fail fast | killed |
| the legacy fallback is built without a data path | killed |
| `Terminate` hides the lookup failure | killed |
| `KillApp` drops the warning | killed |
| the config accepts any `ios_device_id` | killed |

## Test plan

- [x] `go test ./internal/ios/ ./internal/config/ ./internal/runner/ ./internal/cli/` passes.
- [x] `go vet` and `staticcheck` pass on the changed packages.
- [x] No raw format characters in the changed files.
- [x] Mutation check (table above).
jay23606 added a commit to jay23606/flutter-probe that referenced this pull request Nov 17, 2026
…aWaveSystems#780)

## Summary

A device or simulator id no longer shapes a file path.

**On main:**
- **`internal/ios/devicectl.go:68` `FindPID`** (a physical iPhone's process list) wrote devicectl's output to `filepath.Join(os.TempDir(), fmt.Sprintf("devicectl-procs-%s.json", udid[:8]))` and then ran `defer os.Remove(tmpFile)`. This had three problems:
  - **Path traversal and deletion:** an id such as `../../x` names a file outside the temporary folder, and the deferred `os.Remove` deletes it.
  - **A crash:** `udid[:8]` panics for an id shorter than 8 characters.
  - **A link in a shared folder:** the name is predictable in the shared temporary folder, so it could be replaced by a symlink before devicectl writes.
- **`internal/ios/simctl.go:426` `simDataPath`** built `~/Library/Developer/CoreSimulator/Devices/<udid>/data`. `ReadToken` globbed and read token files under it. With an id holding `..`, a separator or a glob character, the read would be shaped by the id.
- **`internal/runner/device_context.go:923` `iosTokenPath`** built the same path from the serial, and the reconnect **removes** that file.

## The change

- **`ios.ValidDeviceUDID` and `ios.ValidSimulatorUDID`** (`internal/ios/udid.go`) accept exact forms only: hex digits and dashes.
  - A device UDID is 8 hex, a dash and 16 hex (iPhone XS and later), or 40 hex (earlier devices). Sources: [Reincubate](https://docs.reincubate.com/deviceidentifier/apple), [Corellium](https://support.corellium.com/device-settings/customizing-device-ids), [TechTarget](https://www.techtarget.com/whatis/definition/unique-device-identifier-UDID). These are third-party pages: Apple publishes no format.
  - A UUID is accepted too, for devicectl's CoreDevice identifier.
  - A simulator UDID is a UUID.
- **`FindPID`** refuses an id that is not a device id before anything runs. Its output goes to `processes.json` in a new private folder (`os.MkdirTemp`: mode 0700, name chosen by the system, under the temporary folder), and the folder is removed after the call. devicectl creates the file itself, so a 0700 folder that only this process can write into rules out a planted link.
- **`simDataPath`** is `""` for anything but a simulator UDID. `ReadToken` then skips the glob and the legacy fallback: built on `""`, they would be relative to the working directory, or the shared `/tmp/probe/token`.
- **`iosTokenPath`** is `""` for anything but a simulator UDID, so the reconnect removes nothing.

## Other paths built from device or app strings (checked, listed)

| Site | Built from | State |
|---|---|---|
| `internal/ios/devicectl.go:68` | device UDID | **fixed here** |
| `internal/ios/simctl.go:271, 280` (token glob and fallback via `simDataPath`) | simulator UDID | **fixed here** |
| `internal/runner/device_context.go:923` (token path, then `os.Remove`) | serial | **fixed here** |
| `internal/cli/record.go:329` `deviceSlug(device)` | device name | safe: every run of characters outside `[a-z0-9]` becomes `-`, so no separator or `..` survives |
| `internal/record/bundle.go:174` `e.ID+".json"` | exchange id | safe: `bundleID.MatchString(e.ID)` is checked first |
| `internal/runner/runner.go:736` | recipe file names | safe: read through the confined opener |
| `simctl.AppDataPath` / `appContainerDataPath` | the path `simctl get_app_container` prints | not built from the id; it is the local tool's own answer |

Ids passed as **command arguments** (`xcrun devicectl … --device <udid>`, `adb -s <serial>`) go through `exec` without a shell, as separate arguments, so they cannot become a path or a command there.

## Tests

- `TestUDIDForms`: the three valid forms pass. These do not: `../../x`, an absolute path, `..`, a UDID with `/../../x` behind it or a path in front of a UUID, a NUL, `*` and `?`, 4096 characters, a non-ASCII digit (U+0660), the empty string, a short one.
- `TestFindPIDRefusesAHostileUDID`: each hostile id is refused, and the fake `xcrun` on PATH never runs.
- `TestFindPIDWritesToAPrivateFolder`: the `--json-output` file is `processes.json` in a `probe-devicectl-` folder under the temporary folder, with nothing of the id in the path, and the folder is gone after the call.
- `TestReadTokenIgnoresPathsAHostileUDIDWouldShape`: a token planted where the old relative glob would look (`<cwd>/Containers/Data/Application/x/tmp/probe/token`) is not read for `../../x`.
- `TestIOSTokenPathOnlyForASimulatorUDID`: no path for hostile serials or a device UDID; a simulator UUID gives the usual path.

Hostile Unicode is built from code points in the test source, so the source stays ASCII (staticcheck ST1018).

## Mutation check

Each mutant was applied, the tests were run, and the file was restored byte for byte. All 8 were killed. "The simulator id is not anchored" survived at first, because no hostile id held a valid UUID with more around it. Two such ids were added, and it is now killed.

| Mutant | Result |
|---|---|
| the device id regex is not anchored | killed |
| the simulator id regex is not anchored | killed |
| `FindPID` does not check the id | killed |
| the output file is named after the id again | killed |
| the private folder is not removed | killed |
| `simDataPath` does not check the id | killed |
| the token glob runs without a data path | killed |
| `iosTokenPath` does not check the serial | killed |

## Test plan

- [x] `go test ./internal/ios/ ./internal/runner/ ./internal/device/` passes.
- [x] `go vet` and `staticcheck` pass on `./internal/ios/` and `./internal/runner/`.
- [x] No raw format characters in the changed files.
- [x] Mutation check (table above).
jay23606 added a commit to jay23606/flutter-probe that referenced this pull request Nov 17, 2026
…ig load, token read, kill (AlphaWaveSystems#784)

## Summary

Follow-ups from the review of AlphaWaveSystems#780: a device id that is not one is now refused where it would act, not later.

1. **`device.ios_device_id` must be a UDID.** It becomes the iOS device serial (`internal/cli/test.go`), and a simulator id shapes host paths. Until now it was not validated: `config.ValidateDeviceSerial` allows `/` and `.`, and does not see this key.
   - `config.LoadFile` now refuses anything but a simulator UDID or a device UDID (`ios.ValidSimulatorUDID` / `ios.ValidDeviceUDID`), with a message that names the key and says where to find the id.
   - Breaking for a `probe.yaml` that put a device name there. As the `--device` serial, a name with a space already failed.
2. **`DeviceCtl.Terminate` no longer hides a failed process lookup.** It returned nil, meaning "not running", whenever `FindPID` failed, including for an id AlphaWaveSystems#780 refuses. So on a physical iPhone a kill could pass silently and a relaunch could start beside a running app. It now returns the lookup error, and `KillApp` prints `the app was not stopped: …` in one line. The error quotes the id with `%q`.
3. **`SimCtl.ReadToken` fails fast** for an id that is not a simulator UDID (a physical device's, or not an id), instead of polling for the whole timeout.
4. **The token paths are built in one place**, `SimCtl.tokenFileCandidates`: the app's container, every data container, then the legacy device-level file. With an id that is not a simulator UDID it returns no path at all, the legacy file included. It is testable on its own; that mutant survived in AlphaWaveSystems#780.
5. **CHANGELOG wording of AlphaWaveSystems#780, corrected:**
   - the device UDID is 8 hex digits, a dash and **exactly 16** hex digits (not "8-16"), or 40;
   - only the id's first 8 characters were used, so the reach was a `.json` file **one folder above** the temporary folder.

Docs: `advanced/configuration.md` (the `ios_device_id` row) and `platform/ios.md` say the value must be a UDID. Their example is a valid UUID.

## Tests

- `TestTokenFileCandidatesNeedASimulatorUDID`: no paths for a device UDID or any of AlphaWaveSystems#780's hostile ids. A simulator's list ends with the legacy file.
- `TestReadTokenFailsFastForAnIDThatIsNotASimulator`: a hostile id, a device UDID and an empty id are each refused in under a second, with a 5 s timeout.
- `TestTerminateReportsALookupFailure`: a refused id gives the lookup error, and the fake `xcrun` never runs.
- `TestKillOnAPhysicalIPhoneWarnsWhenTheLookupFails`: the warning line is printed.
- `TestIOSDeviceIDMustBeAUDID`: empty, a UUID, a 25-character and a 40-character device UDID load. `../../x`, `/etc`, `iPhone 16`, a UUID with `/../x` and `*` are refused with the key named.

## Mutation check

Each mutant was applied, the tests were run, and the file was restored byte for byte. All 5 were killed.

| Mutant | Result |
|---|---|
| `ReadToken` does not fail fast | killed |
| the legacy fallback is built without a data path | killed |
| `Terminate` hides the lookup failure | killed |
| `KillApp` drops the warning | killed |
| the config accepts any `ios_device_id` | killed |

## Test plan

- [x] `go test ./internal/ios/ ./internal/config/ ./internal/runner/ ./internal/cli/` passes.
- [x] `go vet` and `staticcheck` pass on the changed packages.
- [x] No raw format characters in the changed files.
- [x] Mutation check (table above).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant