Skip to content

Commit 637da87

Browse files
authored
fix(gateway): tools.invoke must carry the caller's host-minted role authority (#129725)
* fix(gateway): tools.invoke must carry the caller's host-minted role authority The connect handshake resolves each connection's authority once and stores it server-side (shared-secret operator owners mint system authority there). tools.invoke discarded that fact and re-derived ownership from scopes, so a shared-secret caller with no durable profile resolved to the deny-by-default role and was refused dispatch on its own agents — while the same connection could still mutate sessions directly. Carry client.internal.operatorRoleActor into the synthetic dispatch client and keep the scope-derived fallback for callers that have no connection actor (HTTP). Regression test fails pre-fix with the FORBIDDEN agent-allowlist error. * test(opencode): close the fake CLI before exec to stop ETXTBSY flakes The catalog suite wrote the fake opencode executable and spawned it immediately. Under parallel CI shards the write handle could still be open at exec time, so the launch failed with ETXTBSY and failed the shard. Write through an explicit file handle with an fsync before close so the binary is fully durable before the first spawn. * fix(ci): repair red main type and lint gates Two gates were failing on main independently of this branch: - extensions/qa-lab cleanup tests still built OpenClawCrablineChannelDriverSelection with the retired smokeArtifactPath and a stale capabilityMatrixPath, so check:test-types failed after the readiness-artifact change (#124189). Align both fixtures with the current type and its pinned constants. - scripts/github/release-validation-campaign.d.mts declared the Actions Octokit client as any (#129726), tripping no-explicit-any. Declare the structural subset the publisher actually calls instead of suppressing the rule. Verified failing on clean origin/main before the fix.
1 parent c841a99 commit 637da87

7 files changed

Lines changed: 89 additions & 9 deletions

File tree

‎extensions/opencode/session-catalog.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,15 @@ if (args[0] === "--pure" && args[1] === "db" && args.includes("--format") && arg
362362
process.exitCode = 2;
363363
}
364364
`;
365-
await fs.writeFile(executable, script);
365+
// Flush and close the executable before exec: a still-open write handle makes
366+
// the immediately following spawn fail with ETXTBSY under parallel CI shards.
367+
const executableHandle = await fs.open(executable, "w");
368+
try {
369+
await executableHandle.writeFile(script);
370+
await executableHandle.sync();
371+
} finally {
372+
await executableHandle.close();
373+
}
366374
if (process.platform === "win32") {
367375
await fs.writeFile(path.join(directory, "opencode.js"), script);
368376
// This exact direct-forwarder shape is parsed into a Node entrypoint;

‎extensions/qa-lab/src/suite-run-isolated.cleanup.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ describe("isolated QA suite transport cleanup", () => {
242242
it("keeps Crabline workers concurrent while publishing readiness only from the final aggregate", async () => {
243243
const lab = createCleanupTestLab();
244244
const selection = {
245-
capabilityMatrixPath: "crabline-fake-provider-capabilities.json",
245+
capabilityMatrixPath: "crabline-channel-driver-capabilities.json",
246246
channel: "telegram",
247247
channelDriver: "crabline",
248-
smokeArtifactPath: "crabline-fake-provider-smoke.json",
248+
providerReadinessArtifactPath: "crabline-provider-readiness.json",
249249
} as const;
250250
let activeWorkers = 0;
251251
let maxActiveWorkers = 0;

‎extensions/qa-lab/src/suite-runtime-parity-runner.cleanup.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ describe("runtime parity suite transport cleanup", () => {
284284
mocks.writeQaSuiteArtifacts.mockClear();
285285
const scenario = makeQaSuiteTestScenario("runtime-cleanup");
286286
const selection = {
287-
capabilityMatrixPath: "crabline-fake-provider-capabilities.json",
287+
capabilityMatrixPath: "crabline-channel-driver-capabilities.json",
288288
channel: "telegram",
289289
channelDriver: "crabline",
290-
smokeArtifactPath: "crabline-fake-provider-smoke.json",
290+
providerReadinessArtifactPath: "crabline-provider-readiness.json",
291291
} as const;
292292
const parentLab = createCleanupTestLab();
293293
const openClawLab = createCleanupTestLab();

‎scripts/github/release-validation-campaign.d.mts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,27 @@ export function validateReleaseValidationCampaignArtifact(
2727
},
2828
): ReleaseValidationCampaignArtifact;
2929

30+
/**
31+
* Structural subset of the Actions-provided Octokit client this publisher uses.
32+
* Declared locally so the script keeps a real contract without depending on
33+
* Octokit's generated types from a plain-Node script surface.
34+
*/
35+
export type ReleaseValidationCampaignGitHubClient = {
36+
rest: {
37+
issues: {
38+
get(params: Record<string, unknown>): Promise<{ data: unknown }>;
39+
getLabel(params: Record<string, unknown>): Promise<unknown>;
40+
createLabel(params: Record<string, unknown>): Promise<unknown>;
41+
createComment(params: Record<string, unknown>): Promise<unknown>;
42+
update(params: Record<string, unknown>): Promise<{ data: unknown }>;
43+
listForRepo: unknown;
44+
};
45+
};
46+
paginate(route: unknown, params: Record<string, unknown>): Promise<unknown[]>;
47+
};
48+
3049
export function runReleaseValidationCampaignPublish(params: {
31-
github: any;
50+
github: ReleaseValidationCampaignGitHubClient;
3251
context: { repo: { owner: string; repo: string } };
3352
core: { info(message: string): void; setOutput?(name: string, value: string): void };
3453
artifact: unknown;

‎src/gateway/server-methods/tools-invoke.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const toolsInvokeHandlers: GatewayRequestHandlers = {
5555
cfg: context.getRuntimeConfig(),
5656
input: params,
5757
authenticatedUserProfile: client?.authenticatedUserProfile,
58+
operatorRoleActor: client?.internal?.operatorRoleActor,
5859
operatorScopes: client?.connect.scopes,
5960
senderIsOwner: client?.connect?.scopes?.includes("operator.admin"),
6061
clientCaps: client?.connect?.caps,

‎src/gateway/tools-invoke-http.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ const invokeToolsRpc = async (
473473
hasAvatar: boolean;
474474
updatedAt: number;
475475
},
476+
internal?: { operatorRoleActor?: { kind: "system" } | { kind: "operator"; profileId: string } },
476477
) => {
477478
const respond = vi.fn();
478479
await expectDefined(
@@ -484,6 +485,7 @@ const invokeToolsRpc = async (
484485
context: { getRuntimeConfig: () => cfg } as never,
485486
client: {
486487
...(authenticatedUserProfile ? { authenticatedUserProfile } : {}),
488+
...(internal ? { internal } : {}),
487489
connect: {
488490
role: "operator",
489491
scopes,
@@ -572,6 +574,50 @@ describe("POST /tools/invoke", () => {
572574
});
573575
});
574576

577+
it("preserves host-minted system authority for a shared-secret caller under roles", async () => {
578+
await withOpenClawTestState({ label: "tools-invoke-system-authority" }, async () => {
579+
const owner = ensureProfileForEmail("[email protected]");
580+
const sessionKey = "agent:main:sysauth-primary";
581+
const entry = {
582+
sessionId: "sysauth-primary-session",
583+
updatedAt: 1,
584+
visibility: "shared" as const,
585+
createdActor: { type: "human" as const, id: owner.id },
586+
};
587+
await upsertSessionEntryCore({ agentId: "main", sessionKey }, entry);
588+
sessionEntries.set(sessionKey, entry);
589+
cfg = {
590+
agents: { list: [{ id: "main", default: true, tools: { allow: ["agents_list"] } }] },
591+
gateway: {
592+
roles: {
593+
default: "guest",
594+
definitions: {
595+
guest: {
596+
sessions: { others: "view" },
597+
agents: ["guest-agent"],
598+
scopes: ["operator.write"],
599+
},
600+
},
601+
},
602+
},
603+
};
604+
605+
// Shared-secret operator owners have no durable profile; connect mints system
606+
// authority on the connection. Dispatch must carry that fact forward instead of
607+
// re-deriving ownership from scopes, or the caller is denied its own agent.
608+
const call = await invokeToolsRpc(
609+
{ name: "agents_list", args: {}, sessionKey },
610+
["operator.write"],
611+
undefined,
612+
undefined,
613+
undefined,
614+
{ operatorRoleActor: { kind: "system" } },
615+
);
616+
617+
expect(call?.[1]).toMatchObject({ ok: true, toolName: "agents_list" });
618+
});
619+
});
620+
575621
it("rejects a nested sessions_send target that the operator cannot mutate", async () => {
576622
await withOpenClawTestState({ label: "tools-invoke-foreign-session" }, async () => {
577623
const owner = ensureProfileForEmail("[email protected]");

‎src/gateway/tools-invoke-shared.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ type InvokeGatewayToolParams = {
177177
agentTo?: string;
178178
agentThreadId?: string;
179179
authenticatedUserProfile?: GatewayClient["authenticatedUserProfile"];
180+
/** Host-minted authority from the calling connection; never derived from wire params. */
181+
operatorRoleActor?: NonNullable<GatewayClient["internal"]>["operatorRoleActor"];
180182
operatorScopes?: readonly string[];
181183
senderIsOwner?: boolean;
182184
clientCaps?: string[];
@@ -248,11 +250,15 @@ async function invokeGatewayToolWithSignal(
248250
const authenticatedUserProfile = params.cfg.gateway?.roles
249251
? params.authenticatedUserProfile
250252
: undefined;
253+
// The calling connection already resolved its authority at connect (shared-secret
254+
// owners mint system authority there). Carry that exact fact forward instead of
255+
// re-deriving it from scopes, or role boundaries deny the caller's own dispatch.
256+
const operatorRoleActor =
257+
params.operatorRoleActor ??
258+
(params.senderIsOwner && !authenticatedUserProfile ? { kind: "system" as const } : undefined);
251259
const client = createSyntheticPluginRuntimeClient({
252260
...(authenticatedUserProfile ? { authenticatedUserProfile } : {}),
253-
...(params.senderIsOwner && !authenticatedUserProfile
254-
? { operatorRoleActor: { kind: "system" as const } }
255-
: {}),
261+
...(operatorRoleActor ? { operatorRoleActor } : {}),
256262
scopes: params.senderIsOwner ? [ADMIN_SCOPE] : [...(params.operatorScopes ?? [])],
257263
});
258264
const primarySessionAuthorizationError = authorizeResolvedSessionMutation({

0 commit comments

Comments
 (0)