Skip to content

Commit d86e765

Browse files
committed
fix(web): require executable containment for browser home roots
1 parent 069bb4a commit d86e765

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎packages/platform-web/src/agent-browser-lifecycle.test.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ test('records verified current and retained native roots and stable descendants'
642642
}
643643
});
644644

645-
test('retained Chrome home and marker roots require a Chrome executable and this state directory', async () => {
645+
test('browser home roots require executable containment and marker roots require Chrome', async () => {
646646
const stateDir = mkdtempForTestSync('agent-device-web-life-');
647647
const otherStateDir = mkdtempForTestSync('agent-device-web-other-');
648648
try {
@@ -662,6 +662,19 @@ test('retained Chrome home and marker roots require a Chrome executable and this
662662
'chrome-150',
663663
'chrome',
664664
);
665+
const browserRoot = path.dirname(path.dirname(home));
666+
const helper = path.join(path.dirname(home), 'Google Chrome Helper');
667+
const crashpad = path.join(path.dirname(home), 'chrome_crashpad_handler');
668+
const outside = path.join(stateDir, 'system-browser', 'Google Chrome');
669+
const sibling = path.join(`${browserRoot}-other`, 'chrome');
670+
for (const executable of [home, helper, crashpad, outside, sibling]) {
671+
fs.mkdirSync(path.dirname(executable), { recursive: true });
672+
fs.writeFileSync(executable, 'fixture');
673+
}
674+
const escaped = path.join(browserRoot, 'escaped-chrome');
675+
const alias = path.join(stateDir, 'browser-alias');
676+
fs.symlinkSync(outside, escaped);
677+
fs.symlinkSync(home, alias);
665678
const marker = agentBrowserChromeLaunchMarker(previous);
666679
const rows: ProcessFixture[] = [
667680
{ pid: 201, command: `/opt/Chromium ${marker}`, executable: '/opt/Chromium' },
@@ -674,14 +687,28 @@ test('retained Chrome home and marker roots require a Chrome executable and this
674687
command: `/opt/Chromium ${agentBrowserChromeLaunchMarker(other)}`,
675688
executable: '/opt/Chromium',
676689
},
690+
{ pid: 207, command: `${outside} ${home}`, executable: outside },
691+
{ pid: 208, ppid: 207, command: 'system browser helper' },
692+
{ pid: 209, command: `${helper} --type=renderer`, executable: helper },
693+
{ pid: 210, command: crashpad, executable: crashpad },
694+
{ pid: 211, command: `${escaped} ${home}`, executable: escaped },
695+
{ pid: 212, command: `${sibling} ${home}`, executable: sibling },
696+
{ pid: 213, command: alias, executable: alias },
697+
{ pid: 214, command: home, executable: path.join(browserRoot, 'missing-chrome') },
677698
];
678699
observeProcesses(rows);
679700
const store = { replace: vi.fn(), clear: vi.fn(), read: vi.fn(() => []) };
680701
const summary = await recordManagedAgentBrowserProcesses(status, store, testHost);
681-
expect(summary.pids).toEqual([201, 202]);
702+
expect(summary.pids).toEqual([201, 202, 209, 210, 213]);
682703
expect(summary.processes.map(({ reason }) => reason)).toEqual([
683704
'launch-marker',
684705
'managed-browser-home',
706+
'managed-browser-home',
707+
'managed-browser-home',
708+
'managed-browser-home',
709+
]);
710+
expect(store.replace.mock.calls[0]?.[1].map(({ pid }: { pid: number }) => pid)).toEqual([
711+
201, 202, 209, 210, 213,
685712
]);
686713
} finally {
687714
fs.rmSync(stateDir, { recursive: true, force: true });

‎packages/platform-web/src/agent-browser-process-record.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,40 @@ function matchManagedAgentBrowserProcess(
7575
ownership: ManagedFleetOwnership,
7676
executablePath: string | undefined,
7777
): AgentBrowserProcessMatch | undefined {
78-
if (!executablePath) return undefined;
78+
if (!executablePath || processInfo.pid === hostProcessId()) return undefined;
7979
const executable = normalizePathSeparators(executablePath);
80-
if (isChromeExecutable(executable)) {
81-
const chrome = matchChromeProcess(processInfo, ownership);
82-
if (chrome) return chrome;
83-
}
8480
if (
85-
processInfo.pid !== hostProcessId() &&
86-
matchesManagedDaemonExecutable(executable, ownership.daemonEntries)
81+
isChromeExecutable(executable) &&
82+
ownership.launchMarkers.some((marker) => processInfo.command.includes(marker))
8783
) {
84+
return { process: processInfo, reason: 'launch-marker' };
85+
}
86+
if (isManagedBrowserExecutable(executable, ownership.browserHomeRoots)) {
87+
return { process: processInfo, reason: 'managed-browser-home' };
88+
}
89+
if (matchesManagedDaemonExecutable(executable, ownership.daemonEntries)) {
8890
return { process: processInfo, reason: 'agent-browser-daemon' };
8991
}
9092
return undefined;
9193
}
9294

95+
function isManagedBrowserExecutable(executable: string, roots: readonly string[]): boolean {
96+
let resolvedExecutable: string;
97+
try {
98+
resolvedExecutable = normalizePathSeparators(realHostPathSync(executable));
99+
} catch {
100+
return false;
101+
}
102+
return roots.some((root) => {
103+
try {
104+
const resolvedRoot = normalizePathSeparators(realHostPathSync(root)).replace(/\/+$/, '');
105+
return resolvedExecutable.startsWith(`${resolvedRoot}/`);
106+
} catch {
107+
return false;
108+
}
109+
});
110+
}
111+
93112
function matchesManagedDaemonExecutable(executable: string, entries: readonly string[]): boolean {
94113
if (entries.includes(executable)) return true;
95114
if (hostPlatform() !== 'win32') return false;

0 commit comments

Comments
 (0)