惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
WordPress大学
WordPress大学
U
Unit 42
I
InfoQ
A
About on SuperTechFans
宝玉的分享
宝玉的分享
J
Java Code Geeks
博客园 - 司徒正美
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
腾讯CDC
Recent Announcements
Recent Announcements

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
feat(gateway): emit security events for device pairing · ...
vincentkoc · 2026-06-17 · via Recent Commits to openclaw:main
11

// Device method tests cover pairing approval/rejection, paired-device lookup,

22

// token rotation/revocation, and operator scope enforcement.

33

import { beforeEach, describe, expect, it, vi } from "vitest";

4+

import {

5+

onInternalDiagnosticEvent,

6+

resetDiagnosticEventsForTest,

7+

type DiagnosticSecurityEvent,

8+

} from "../../infra/diagnostic-events.js";

49

import { deviceHandlers } from "./devices.js";

510

import type { GatewayRequestHandlerOptions } from "./types.js";

611

@@ -122,8 +127,22 @@ function expectRespondedErrorMessage(opts: GatewayRequestHandlerOptions, message

122127

expect(call[2]?.message).toBe(message);

123128

}

124129130+

function captureSecurityEvents(): {

131+

events: DiagnosticSecurityEvent[];

132+

stop: () => void;

133+

} {

134+

const events: DiagnosticSecurityEvent[] = [];

135+

const stop = onInternalDiagnosticEvent((event, metadata) => {

136+

if (metadata.trusted && event.type === "security.event") {

137+

events.push(event);

138+

}

139+

});

140+

return { events, stop };

141+

}

142+125143

describe("deviceHandlers", () => {

126144

beforeEach(() => {

145+

resetDiagnosticEventsForTest();

127146

vi.clearAllMocks();

128147

});

129148

@@ -243,15 +262,20 @@ describe("deviceHandlers", () => {

243262

it("disconnects active clients after revoking a device token", async () => {

244263

revokeDeviceTokenMock.mockResolvedValue({

245264

ok: true,

246-

entry: { role: "operator", revokedAtMs: 456 },

265+

entry: { token: "raw-revoked-token", role: "operator", scopes: [], revokedAtMs: 456 },

247266

});

248267

const opts = createOptions("device.token.revoke", {

249268

deviceId: " device-1 ",

250269

role: " operator ",

251270

});

271+

const captured = captureSecurityEvents();

252272253-

await deviceHandlers["device.token.revoke"](opts);

254-

await Promise.resolve();

273+

try {

274+

await deviceHandlers["device.token.revoke"](opts);

275+

await Promise.resolve();

276+

} finally {

277+

captured.stop();

278+

}

255279256280

expect(revokeDeviceTokenMock).toHaveBeenCalledWith({

257281

deviceId: " device-1 ",

@@ -266,6 +290,21 @@ describe("deviceHandlers", () => {

266290

{ deviceId: "device-1", role: "operator", revokedAtMs: 456 },

267291

undefined,

268292

);

293+

expect(captured.events).toHaveLength(1);

294+

expect(captured.events[0]).toMatchObject({

295+

type: "security.event",

296+

category: "auth",

297+

action: "device.token.revoked",

298+

outcome: "success",

299+

severity: "high",

300+

target: { kind: "device", idHash: expect.stringMatching(/^sha256:[a-f0-9]{12}$/u) },

301+

policy: { id: "gateway.device-token", decision: "allow" },

302+

control: { id: "device.token.revoke", family: "auth" },

303+

attributes: { role: "operator" },

304+

});

305+

const serialized = JSON.stringify(captured.events);

306+

expect(serialized).not.toContain("device-1");

307+

expect(serialized).not.toContain("raw-revoked-token");

269308

});

270309271310

it("allows admin-scoped callers to revoke another device's token", async () => {

@@ -299,12 +338,37 @@ describe("deviceHandlers", () => {

299338

{ deviceId: "device-1", role: "node" },

300339

{ client: createClient(["operator.pairing"], "device-1", { isDeviceTokenAuth: true }) },

301340

);

341+

const captured = captureSecurityEvents();

302342303-

await deviceHandlers["device.token.revoke"](opts);

343+

try {

344+

await deviceHandlers["device.token.revoke"](opts);

345+

} finally {

346+

captured.stop();

347+

}

304348305349

expect(revokeDeviceTokenMock).not.toHaveBeenCalled();

306350

expect(opts.context.disconnectClientsForDevice).not.toHaveBeenCalled();

307351

expectRespondedErrorMessage(opts, "device token revocation denied");

352+

expect(captured.events).toHaveLength(1);

353+

expect(captured.events[0]).toMatchObject({

354+

action: "device.token.revocation_denied",

355+

outcome: "denied",

356+

reason: "role-management-requires-admin",

357+

actor: {

358+

kind: "operator",

359+

deviceIdHash: expect.stringMatching(/^sha256:[a-f0-9]{12}$/u),

360+

role: "operator",

361+

},

362+

target: { kind: "device", idHash: expect.stringMatching(/^sha256:[a-f0-9]{12}$/u) },

363+

policy: {

364+

id: "gateway.device-token",

365+

decision: "deny",

366+

reason: "role-management-requires-admin",

367+

},

368+

control: { id: "device.token.revoke", family: "auth" },

369+

attributes: { role: "node" },

370+

});

371+

expect(JSON.stringify(captured.events)).not.toContain("device-1");

308372

});

309373310374

it("treats normalized device ids as self-owned for token revocation", async () => {

@@ -823,8 +887,13 @@ describe("deviceHandlers", () => {

823887

}),

824888

},

825889

);

890+

const captured = captureSecurityEvents();

826891827-

await deviceHandlers["device.pair.approve"](opts);

892+

try {

893+

await deviceHandlers["device.pair.approve"](opts);

894+

} finally {

895+

captured.stop();

896+

}

828897829898

expect(getPendingDevicePairingMock).not.toHaveBeenCalled();

830899

expect(approveDevicePairingMock).toHaveBeenCalledWith("req-2", {

@@ -844,6 +913,25 @@ describe("deviceHandlers", () => {

844913

},

845914

undefined,

846915

);

916+

expect(captured.events).toHaveLength(1);

917+

expect(captured.events[0]).toMatchObject({

918+

action: "device.pairing.approved",

919+

outcome: "success",

920+

severity: "low",

921+

actor: {

922+

kind: "operator",

923+

deviceIdHash: expect.stringMatching(/^sha256:[a-f0-9]{12}$/u),

924+

role: "admin",

925+

},

926+

target: { kind: "device", idHash: expect.stringMatching(/^sha256:[a-f0-9]{12}$/u) },

927+

policy: { id: "gateway.device-pairing", decision: "allow" },

928+

control: { id: "device.pair.approve", family: "auth" },

929+

attributes: { role_count: 0, scope_count: 0 },

930+

});

931+

const serialized = JSON.stringify(captured.events);

932+

expect(serialized).not.toContain("device-1");

933+

expect(serialized).not.toContain("device-2");

934+

expect(serialized).not.toContain("pk-2");

847935

});

848936849937

it("allows approving the caller device from a non-admin device session", async () => {

@@ -957,11 +1045,29 @@ describe("deviceHandlers", () => {

9571045

{ requestId: "req-1" },

9581046

{ client: createClient(["operator.pairing"], "device-1", { isDeviceTokenAuth: true }) },

9591047

);

1048+

const captured = captureSecurityEvents();

9601049961-

await deviceHandlers["device.pair.approve"](opts);

1050+

try {

1051+

await deviceHandlers["device.pair.approve"](opts);

1052+

} finally {

1053+

captured.stop();

1054+

}

96210559631056

expect(approveDevicePairingMock).not.toHaveBeenCalled();

9641057

expectRespondedErrorMessage(opts, "device pairing approval denied");

1058+

expect(captured.events).toHaveLength(1);

1059+

expect(captured.events[0]).toMatchObject({

1060+

action: "device.pairing.denied",

1061+

outcome: "denied",

1062+

reason: "role-management-requires-admin",

1063+

policy: {

1064+

id: "gateway.device-pairing",

1065+

decision: "deny",

1066+

reason: "role-management-requires-admin",

1067+

},

1068+

control: { id: "device.pair.approve", family: "auth" },

1069+

});

1070+

expect(JSON.stringify(captured.events)).not.toContain("device-1");

9651071

});

96610729671073

it("rejects approving node roles from non-admin shared-auth sessions", async () => {