mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 01:41:22 +00:00
fix: prevent config clobbering
This commit is contained in:
@@ -6,7 +6,12 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import { createClawdbotTools } from "./clawdbot-tools.js";
|
||||
|
||||
vi.mock("./tools/gateway.js", () => ({
|
||||
callGatewayTool: vi.fn(async () => ({ ok: true })),
|
||||
callGatewayTool: vi.fn(async (method: string) => {
|
||||
if (method === "config.get") {
|
||||
return { hash: "hash-1" };
|
||||
}
|
||||
return { ok: true };
|
||||
}),
|
||||
}));
|
||||
|
||||
describe("gateway tool", () => {
|
||||
@@ -71,11 +76,13 @@ describe("gateway tool", () => {
|
||||
raw,
|
||||
});
|
||||
|
||||
expect(callGatewayTool).toHaveBeenCalledWith("config.get", expect.any(Object), {});
|
||||
expect(callGatewayTool).toHaveBeenCalledWith(
|
||||
"config.apply",
|
||||
expect.any(Object),
|
||||
expect.objectContaining({
|
||||
raw: raw.trim(),
|
||||
baseHash: "hash-1",
|
||||
sessionKey: "agent:main:whatsapp:dm:+15555550123",
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
import { Type } from "@sinclair/typebox";
|
||||
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
@@ -33,6 +35,7 @@ const GatewayToolSchema = Type.Object({
|
||||
timeoutMs: Type.Optional(Type.Number()),
|
||||
// config.apply
|
||||
raw: Type.Optional(Type.String()),
|
||||
baseHash: Type.Optional(Type.String()),
|
||||
// config.apply, update.run
|
||||
sessionKey: Type.Optional(Type.String()),
|
||||
note: Type.Optional(Type.String()),
|
||||
@@ -125,6 +128,24 @@ export function createGatewayTool(opts?: {
|
||||
}
|
||||
if (action === "config.apply") {
|
||||
const raw = readStringParam(params, "raw", { required: true });
|
||||
let baseHash = readStringParam(params, "baseHash");
|
||||
if (!baseHash) {
|
||||
const snapshot = await callGatewayTool("config.get", gatewayOpts, {});
|
||||
if (snapshot && typeof snapshot === "object") {
|
||||
const hash = (snapshot as { hash?: unknown }).hash;
|
||||
if (typeof hash === "string" && hash.trim()) {
|
||||
baseHash = hash.trim();
|
||||
} else {
|
||||
const rawSnapshot = (snapshot as { raw?: unknown }).raw;
|
||||
if (typeof rawSnapshot === "string") {
|
||||
baseHash = crypto
|
||||
.createHash("sha256")
|
||||
.update(rawSnapshot)
|
||||
.digest("hex");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const sessionKey =
|
||||
typeof params.sessionKey === "string" && params.sessionKey.trim()
|
||||
? params.sessionKey.trim()
|
||||
@@ -137,6 +158,7 @@ export function createGatewayTool(opts?: {
|
||||
: undefined;
|
||||
const result = await callGatewayTool("config.apply", gatewayOpts, {
|
||||
raw,
|
||||
baseHash,
|
||||
sessionKey,
|
||||
note,
|
||||
restartDelayMs,
|
||||
|
||||
Reference in New Issue
Block a user