Skip to content

Commit 62e814c

Browse files
amankansal-ltclaude
andcommitted
fix(connect): refuse a WebDriver plugin's refused fields before resolve
Builtin connect routes run rejectRefusedProviderProfileFields against their declaration; the plugin route left it to the plugin. A { webDriver } plugin already hands core a total profileFields declaration, so the connect route now applies it before the plugin's resolve. Raw runtime plugins carry no declaration and still validate their own flags. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 244c131 commit 62e814c

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { test } from 'vitest';
5+
import { AppError } from '@agent-device/kernel/errors';
6+
import { resolveConnectProviderProfile } from './connect-provider-adapters.ts';
7+
import { pluginHome, selectPlugin, webDriverPluginSource } from '../../plugins/plugin.fixtures.ts';
8+
9+
test('connect refuses a field a WebDriver plugin declares refused before its resolve runs', async () => {
10+
const { home, env } = pluginHome();
11+
const marker = path.join(home, 'resolved');
12+
const connection = `{
13+
resolve: () => {
14+
fs.writeFileSync(${JSON.stringify(marker)}, 'yes');
15+
return { profile: { leaseProvider: 'example', platform: 'android' } };
16+
},
17+
verify: async () => ({}),
18+
}`;
19+
selectPlugin(
20+
home,
21+
'example',
22+
'example',
23+
`import fs from 'node:fs';\n${webDriverPluginSource('example', ['awsProjectArn'], connection)}`,
24+
);
25+
26+
await assert.rejects(
27+
resolveConnectProviderProfile({
28+
provider: 'example',
29+
flags: {
30+
json: false,
31+
help: false,
32+
version: false,
33+
platform: 'android',
34+
awsProjectArn: 'arn:project',
35+
},
36+
stateDir: path.join(home, 'state'),
37+
cwd: home,
38+
env,
39+
}),
40+
(error: unknown) => {
41+
assert.ok(error instanceof AppError);
42+
assert.equal(error.code, 'INVALID_ARGS');
43+
assert.equal(error.details?.provider, 'example');
44+
assert.deepEqual(error.details?.flags, ['--aws-project-arn']);
45+
return true;
46+
},
47+
);
48+
assert.ok(!fs.existsSync(marker));
49+
});

‎src/cli/connection/connect-provider-adapters.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CliFlags } from '@agent-device/contracts/command';
22
import type { ProviderConnectionVerification } from '@agent-device/contracts/remote';
33
import { verifyLimrunConnection } from '@agent-device/provider-limrun';
44
import { AppError } from '@agent-device/kernel/errors';
5+
import { rejectRefusedProviderProfileFields } from '@agent-device/contracts/provider-profile-fields';
56
import { providerWebDriver } from '../../provider-webdriver.ts';
67
import { resolveRemoteConfigProfile } from '../../remote/remote-config.ts';
78
import { readVersion } from '@agent-device/host-kit/version';
@@ -123,7 +124,8 @@ export async function resolveConnectProviderProfile(options: {
123124
];
124125
const profile = adapter
125126
? await adapter.resolve(context)
126-
: await withPluginConnection(provider, env, async (connection) => {
127+
: await withPluginConnection(provider, env, async (connection, profileFields) => {
128+
if (profileFields) rejectRefusedProviderProfileFields(context.flags, profileFields);
127129
const resolved = await connection.resolve(context);
128130
if (resolved.profile.leaseProvider !== provider)
129131
throw new AppError(

‎src/plugins/load.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import {
1212
import { createPluginHost } from './host.ts';
1313
import type { PluginConnection } from './connection.ts';
1414
import type { WebDriverPluginOptions } from '../sdk/plugin-webdriver.ts';
15+
import type { ProviderProfileFieldDeclaration } from '@agent-device/contracts/provider-profile-fields';
1516

1617
type ProviderPluginRegistration = Readonly<{
1718
runtime: ProviderDeviceRuntime;
1819
platformModule: PlatformRuntimeProviderModule;
1920
connection?: PluginConnection;
21+
/** Known only for `{ webDriver }` plugins; a raw runtime plugin validates its own fields. */
22+
profileFields?: ProviderProfileFieldDeclaration;
2023
}>;
2124

2225
export async function loadProviderPlugins(
@@ -45,16 +48,17 @@ export async function loadProviderPlugins(
4548
export async function withPluginConnection<T>(
4649
provider: string,
4750
env: NodeJS.ProcessEnv,
48-
runConnection: (connection: PluginConnection) => Promise<T>,
51+
runConnection: (
52+
connection: PluginConnection,
53+
profileFields: ProviderProfileFieldDeclaration | undefined,
54+
) => Promise<T>,
4955
): Promise<T> {
5056
const registrations = await loadProviderPlugins(env, RESERVED_PLUGIN_PROVIDERS, provider);
5157
try {
52-
const connection = registrations.find(
53-
(entry) => entry.runtime.provider === provider,
54-
)?.connection;
55-
if (!connection)
58+
const registration = registrations.find((entry) => entry.runtime.provider === provider);
59+
if (!registration?.connection)
5660
throw new AppError('INVALID_ARGS', `Plugin does not register connect: ${provider}`);
57-
return await runConnection(connection);
61+
return await runConnection(registration.connection, registration.profileFields);
5862
} finally {
5963
await Promise.allSettled(registrations.map(async ({ runtime }) => await runtime.shutdown()));
6064
}
@@ -98,6 +102,7 @@ async function instantiateProviderPlugin(
98102
runtime,
99103
platformModule: runtime.platformRuntimeModule,
100104
connection: result.connection,
105+
profileFields: result.webDriver.profileFields,
101106
};
102107
} else registration = result;
103108
if (!registration?.runtime || typeof registration.runtime.shutdown !== 'function') {

‎website/docs/docs/plugins.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ To support `agent-device connect example`, declare `agentDevicePlugin.connection
4949
}
5050
```
5151

52-
Return `connection` alongside the runtime or WebDriver options. Its `resolve({ flags, env, cwd, stateDir })` callback validates provider flags and returns `{ profile, extraFlags? }`; `profile.leaseProvider` must match the manifest. Core supplies connection identity, session defaults, Metro settings, and persists the profile. Its async `verify({ flags, env })` callback returns the provider verification result. These callbacks run without allocating a device and their temporary runtimes are shut down afterwards.
52+
Return `connection` alongside the runtime or WebDriver options. Its `resolve({ flags, env, cwd, stateDir })` callback validates provider flags and returns `{ profile, extraFlags? }`; for a WebDriver plugin, core first refuses the flags its `profileFields` marks `refused`; `profile.leaseProvider` must match the manifest. Core supplies connection identity, session defaults, Metro settings, and persists the profile. Its async `verify({ flags, env })` callback returns the provider verification result. These callbacks run without allocating a device and their temporary runtimes are shut down afterwards.
5353

5454
Bundle the plugin implementation and ship ready-to-run ESM: installation disables lifecycle scripts. Bundle shared implementation helpers with the plugin; `AppError` carries a shared brand so core preserves its code and details across package copies. Import types with `import type` to keep them out of the runtime dependency graph.

0 commit comments

Comments
 (0)