Skip to content

Commit ce26013

Browse files
committed
fix(android): validate native integer launch controls
1 parent 94b90a8 commit ce26013

3 files changed

Lines changed: 88 additions & 14 deletions

File tree

‎packages/platform-android/src/intent-arguments.test.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fc from 'fast-check';
33
import { buildAndroidIntentArgs } from './intent-arguments.ts';
44

55
const ACTION_ARGS = ['-a', 'android.settings.SETTINGS'];
6+
const PROPERTY_RUNS = 100;
67

78
test.each([
89
{
@@ -15,6 +16,11 @@ test.each([
1516
args: ['-S', '--selector', '-a', 'com.example.VIEW', '--display', '0', '-p', 'com.example'],
1617
intentArgs: ['--selector', '-a', 'com.example.VIEW', '-p', 'com.example'],
1718
},
19+
{
20+
name: 'MIME type retained by AOSP across a selector',
21+
args: ['-t', 'text/plain', '--selector', '-d', 'content://example/item', '-S'],
22+
intentArgs: ['-t', 'text/plain', '--selector', '-d', 'content://example/item'],
23+
},
1824
{
1925
name: 'positional component and unparsed trailing tokens',
2026
args: ['-S', 'com.example/.Main', '--display', '0'],
@@ -38,6 +44,9 @@ test.each([
3844
['-R'],
3945
['--es', 'key'],
4046
['-Sattached'],
47+
['--display', 'not-a-number'],
48+
['-R--display'],
49+
['-d', 'content://example/item', '--selector', '-t', 'text/plain'],
4150
['intent:#Intent;scheme=content;end'],
4251
])('unprojectable launch arguments retain unknown ownership: %j', (...args) => {
4352
expect(buildAndroidIntentArgs('android.settings.SETTINGS', args)).toEqual({
@@ -52,6 +61,17 @@ const flagLikeValueArb = fc.oneof(
5261
fc.string({ maxLength: 40 }),
5362
fc.constantFrom('-S', '-R', '--display', '--user', '--selector', '--', ''),
5463
);
64+
const integerLaunchFlagArb = fc.constantFrom(
65+
'--sampling',
66+
'--profiler-output-version',
67+
'-R',
68+
'--display',
69+
'--task-display-area-feature-id',
70+
'--windowingMode',
71+
'--activityType',
72+
'--task',
73+
'--allow-fgs-start-reason',
74+
);
5575
const argumentGroupArb: fc.Arbitrary<ArgumentGroup> = fc.oneof(
5676
fc
5777
.tuple(fc.constantFrom('-a', '-d', '-t', '-i', '-c', '-p'), flagLikeValueArb)
@@ -67,8 +87,18 @@ const argumentGroupArb: fc.Arbitrary<ArgumentGroup> = fc.oneof(
6787
.tuple(fc.constantFrom('-P', '--attach-agent'), flagLikeValueArb)
6888
.map<ArgumentGroup>((tokens) => ({ role: 'launch', tokens })),
6989
fc
70-
.tuple(fc.constantFrom('-R', '--display'), fc.nat({ max: 20 }))
71-
.map<ArgumentGroup>(([flag, value]) => ({ role: 'launch', tokens: [flag, String(value)] })),
90+
.tuple(
91+
integerLaunchFlagArb,
92+
fc.oneof(
93+
fc.integer({ min: -2147483648, max: 2147483647 }).map(String),
94+
fc.constantFrom('+0', '-0', '0001', '2147483647', '-2147483648', '٠١', '02'),
95+
),
96+
fc.boolean(),
97+
)
98+
.map<ArgumentGroup>(([flag, value, attached]) => ({
99+
role: 'launch',
100+
tokens: flag === '-R' && attached ? [flag + value] : [flag, value],
101+
})),
72102
);
73103

74104
test('intent projection preserves whole argument groups while launch preserves the original stream', () => {
@@ -85,5 +115,24 @@ test('intent projection preserves whole argument groups while launch preserves t
85115
resolutionArgs: [...ACTION_ARGS, ...intentArgs],
86116
});
87117
}),
118+
{ numRuns: PROPERTY_RUNS },
119+
);
120+
});
121+
122+
test('invalid native integer launch controls keep ownership unknown, including in selectors', () => {
123+
fc.assert(
124+
fc.property(
125+
integerLaunchFlagArb,
126+
fc.constantFrom('', '+', '--display', '1.0', '0x10', ' 1', '2147483648', '-2147483649', '𝟙'),
127+
fc.array(argumentGroupArb, { maxLength: 10 }),
128+
(flag, value, groups) => {
129+
const args = [...groups.flatMap((group) => group.tokens), '--selector', flag, value];
130+
expect(buildAndroidIntentArgs('android.settings.SETTINGS', args)).toEqual({
131+
launchArgs: [...ACTION_ARGS, ...args],
132+
resolutionArgs: undefined,
133+
});
134+
},
135+
),
136+
{ numRuns: PROPERTY_RUNS },
88137
);
89138
});

‎packages/platform-android/src/intent-arguments.ts‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
type AndroidIntentOption = readonly [audience: 'intent' | 'launch', valueCount: 0 | 1 | 2];
1+
type AndroidIntentOption = readonly [
2+
audience: 'intent' | 'launch',
3+
valueCount: 0 | 1 | 2,
4+
valueType?: 'java-int',
5+
];
26

37
/** AOSP Intent.parseCommandArgs and ActivityManagerShellCommand.makeIntent option arities. */
48
const ANDROID_INTENT_OPTIONS: Readonly<Record<string, AndroidIntentOption>> = {
@@ -66,29 +70,29 @@ const ANDROID_INTENT_OPTIONS: Readonly<Record<string, AndroidIntentOption>> = {
6670
'-W': ['launch', 0],
6771
'-P': ['launch', 1],
6872
'--start-profiler': ['launch', 1],
69-
'--sampling': ['launch', 1],
73+
'--sampling': ['launch', 1, 'java-int'],
7074
'--clock-type': ['launch', 1],
71-
'--profiler-output-version': ['launch', 1],
75+
'--profiler-output-version': ['launch', 1, 'java-int'],
7276
'--streaming': ['launch', 0],
7377
'--attach-agent': ['launch', 1],
7478
'--attach-agent-bind': ['launch', 1],
75-
'-R': ['launch', 1],
79+
'-R': ['launch', 1, 'java-int'],
7680
'-S': ['launch', 0],
7781
'--track-allocation': ['launch', 0],
7882
'--receiver-permission': ['launch', 1],
79-
'--display': ['launch', 1],
80-
'--task-display-area-feature-id': ['launch', 1],
81-
'--windowingMode': ['launch', 1],
82-
'--activityType': ['launch', 1],
83-
'--task': ['launch', 1],
83+
'--display': ['launch', 1, 'java-int'],
84+
'--task-display-area-feature-id': ['launch', 1, 'java-int'],
85+
'--windowingMode': ['launch', 1, 'java-int'],
86+
'--activityType': ['launch', 1, 'java-int'],
87+
'--task': ['launch', 1, 'java-int'],
8488
'--task-overlay': ['launch', 0],
8589
'--lock-task': ['launch', 0],
8690
'--allow-background-activity-starts': ['launch', 0],
8791
'--async': ['launch', 0],
8892
'--splashscreen-show-icon': ['launch', 0],
8993
'--dismiss-keyguard-if-insecure': ['launch', 0],
9094
'--dismiss-keyguard': ['launch', 0],
91-
'--allow-fgs-start-reason': ['launch', 1],
95+
'--allow-fgs-start-reason': ['launch', 1, 'java-int'],
9296
};
9397

9498
type ParsedAndroidIntentOption = {
@@ -98,6 +102,23 @@ type ParsedAndroidIntentOption = {
98102
value: string | undefined;
99103
};
100104

105+
/** Integer.parseInt accepts ASCII signs and BMP decimal digits, within signed 32-bit bounds. */
106+
function isAndroidIntentJavaInteger(value: string): boolean {
107+
if (!/^[+-]?\p{Decimal_Number}+$/u.test(value)) return false;
108+
const negative = value[0] === '-';
109+
const digits = value[0] === '-' || value[0] === '+' ? value.slice(1) : value;
110+
let result = 0;
111+
for (const digit of digits) {
112+
if (digit.length !== 1) return false;
113+
const code = digit.charCodeAt(0);
114+
let zero = code;
115+
while (/\p{Decimal_Number}/u.test(String.fromCharCode(zero - 1))) zero -= 1;
116+
result = result * 10 + code - zero;
117+
if (result > (negative ? 2147483648 : 2147483647)) return false;
118+
}
119+
return true;
120+
}
121+
101122
function readAndroidIntentOption(
102123
args: readonly string[],
103124
index: number,
@@ -109,11 +130,13 @@ function readAndroidIntentOption(
109130
if (!option || (attachedValue && option[1] === 0)) return undefined;
110131
const end = index + 1 + option[1] - Number(attachedValue);
111132
if (end > args.length) return undefined;
133+
const value = attachedValue ? token.slice(2) : args[index + 1];
134+
if (option[2] === 'java-int' && !isAndroidIntentJavaInteger(value ?? '')) return undefined;
112135
return {
113136
name,
114137
audience: option[0],
115138
end,
116-
value: attachedValue ? token.slice(2) : args[index + 1],
139+
value,
117140
};
118141
}
119142

‎packages/platform-android/src/open-target-resolution.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ test('intent selectors resolve only handlers eligible for activity launch', asyn
207207
});
208208

209209
test.each([
210+
['--display', 'not-a-number'],
211+
['-R--display'],
210212
['-a', 'android.intent.action.VIEW', '-d', 'content://example/image/1'],
211213
['-dcontent://example/image/1'],
212214
['--selector', '-d', 'content://example/image/1'],
213215
['content://example/image/1'],
214-
])('implicit content MIME resolution retains unknown ownership: %j', async (...launchArgs) => {
216+
])('unprojectable intent resolution retains unknown ownership: %j', async (...launchArgs) => {
215217
resolveAndroidApp.mockResolvedValue({ type: 'intent', value: 'android.settings.SETTINGS' });
216218
runAndroidShell.mockResolvedValue({
217219
stdout: 'com.example.generic/.Main',

0 commit comments

Comments
 (0)