Skip to content

Commit 607e6b8

Browse files
committed
chore(gates): refuse a simctl tool named after leading xcrun options
1 parent 574ecbc commit 607e6b8

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

‎scripts/layering/apple-simulator-scope-policy.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ test('a simctl argv is refused however it reaches an executor', () => {
4747
"runCmd('xcrun', [`simctl`, 'boot', udid]);\n",
4848
"host.commands.run({ executable: 'xcrun', args: ['simctl', 'spawn', udid, 'log', 'stream'] });\n",
4949
"const tools = ['simctl', 'devicectl'];\n",
50+
"runCmd('xcrun', ['--sdk', 'iphonesimulator', 'simctl', 'boot', udid]);\n",
51+
"const tool = 'simctl';\nconst argv = ['-v', tool, 'spawn', udid, bin];\nrunCmd('xcrun', argv);\n",
5052
]) {
5153
assertFlagged(`${APPLE_SRC}logs/start.ts`, source, SIMCTL_ARGV_MESSAGE);
5254
}
@@ -102,6 +104,7 @@ test('named tools, builder output, pass-through argv and simctl text elsewhere a
102104
[
103105
"runCmd('xcrun', ['devicectl', ...args]);",
104106
"runCmd('xcrun', ['--find', name]);",
107+
"runCmd('xcrun', ['--sdk', sdkName, '--show-sdk-version']);",
105108
"runCmd('xcrun', simctlCommand(args));",
106109
"runCmd('xcrun', [...argv]);",
107110
"runCmdBackground('xcrun', args);",

‎scripts/layering/apple-simulator-scope-policy.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Catches: the simctl scope bypasses `tsc` cannot reject. `runXcrun` accepts only a
22
// `ScopedSimctlCommand` or a named non-simctl tool, but the plain executors (`runCmd`,
33
// `runCmdBackground`, an `executable: 'xcrun'` spec) take any string argv, so every simctl argv
4-
// reaching them must be builder output. (1) An array whose first element names simctl (the
5-
// literal, a quasi-only template, or a same-file binding of either) outside the brand mints,
6-
// however it later travels: inline, held in a variable, aliased or spread. (2) At an xcrun
4+
// reaching them must be builder output. (1) An array whose tool names simctl (the literal, a
5+
// quasi-only template, or a same-file binding of either; the first element, or any element
6+
// after leading xcrun options such as `--sdk`) outside the brand mints, however it later
7+
// travels: inline, held in a variable, aliased or spread. (2) At an xcrun
78
// invocation, an inline argv whose tool is not a string literal other than `simctl`. (3) A cast
89
// to `ScopedSimctlArgs`, `ScopedSimctlCommand` or `SimulatorAddress` outside the brand mints.
910
// Every form runs against the default CoreSimulator set: `Invalid device` for a simulator in a
@@ -12,7 +13,7 @@
1213
// fold HID helper (`foldable/simulator-hid.ts`) built `['simctl', 'spawn', udid, ...]` from a
1314
// bare udid and lost the set; #2824 moved every call site onto `core/simctl.ts` and checked it
1415
// with a manual `git grep "'--set'"`.
15-
// Cost: 300 LOC (159 rule + 141 test).
16+
// Cost: 311 LOC (167 rule + 144 test).
1617
// Kill criterion: none enforced today; retire only by maintainer decision that scoped simulator
1718
// sets (`--ios-simulator-device-set`) are no longer supported, or when no production xcrun
1819
// executor takes a plain string argv.
@@ -61,12 +62,14 @@ export function appleSimulatorScopeViolations(
6162
const program = parseSync(file, source).program;
6263
const simctlBindings = simctlNameBindings(program);
6364
const isMint = BRAND_MINTS.has(file);
65+
const isSimctl = (element: AstNode | null) =>
66+
namesSimctl(element ?? undefined) ||
67+
(element?.type === 'Identifier' && simctlBindings.has(element.name));
6468
const buildsSimctlArgv = (array: AstNode) => {
65-
const tool = firstElement(array);
66-
return (
67-
!isMint &&
68-
(namesSimctl(tool) || (tool?.type === 'Identifier' && simctlBindings.has(tool.name)))
69-
);
69+
if (isMint) return false;
70+
const elements = array.elements as (AstNode | null)[];
71+
const [tool] = elements;
72+
return isSimctl(tool ?? null) || (isXcrunOption(tool) && elements.some(isSimctl));
7073
};
7174
const report = (node: AstNode, message: string) =>
7275
violations.push({ rule: RULE, file, line: lineAt(source, node.start), message });
@@ -109,6 +112,11 @@ function namesNonSimctlTool(argv: AstNode): boolean {
109112
return tool?.type === 'Literal' && typeof tool.value === 'string' && tool.value !== 'simctl';
110113
}
111114

115+
/** A leading `-` literal is an xcrun option (`--sdk`, `--find`), so the tool name comes later. */
116+
function isXcrunOption(node: AstNode | null | undefined): boolean {
117+
return node?.type === 'Literal' && typeof node.value === 'string' && node.value.startsWith('-');
118+
}
119+
112120
function firstElement(array: AstNode): AstNode | undefined {
113121
return (array.elements as (AstNode | null)[])[0] ?? undefined;
114122
}

0 commit comments

Comments
 (0)