mirror of
https://github.com/bitbaum/openclaw.git
synced 2026-12-04 19:48:07 +01:00
fix(skills): guard workshop rollback recovery
This commit is contained in:
5 files changed
+120
-1
No files matched your search
@@ -4,6 +4,7 @@ import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createTrackedTempDirs } from "../../test-utils/tracked-temp-dirs.js";
|
||||
import {
|
||||
applyWorkspaceSkillMutation,
|
||||
isWorkspaceSkillMutationApplied,
|
||||
prepareWorkspaceSkillMutation,
|
||||
prepareWorkspaceSkillRestoration,
|
||||
restoreWorkspaceSkillMutation,
|
||||
@@ -109,4 +110,23 @@ describe("workspace skill mutations", () => {
|
||||
await expect(fs.readFile(skillFile, "utf8")).resolves.toBe("# Before\n");
|
||||
await expect(fs.readFile(supportFile, "utf8")).resolves.toBe("before support\n");
|
||||
});
|
||||
|
||||
it("detects external edits before restoring a completed mutation", async () => {
|
||||
const workspaceDir = await tempDirs.make("openclaw-workspace-skill-external-edit-");
|
||||
const skillDir = path.join(workspaceDir, "skills", "external-edit");
|
||||
const skillFile = path.join(skillDir, "SKILL.md");
|
||||
const mutation = await prepareWorkspaceSkillMutation({
|
||||
workspaceDir,
|
||||
skillDir,
|
||||
skillFile,
|
||||
content: "# Proposed\n",
|
||||
mode: "create",
|
||||
symlinkPolicy,
|
||||
});
|
||||
|
||||
await applyWorkspaceSkillMutation(mutation);
|
||||
await expect(isWorkspaceSkillMutationApplied(mutation)).resolves.toBe(true);
|
||||
await fs.writeFile(skillFile, "# External edit\n", "utf8");
|
||||
await expect(isWorkspaceSkillMutationApplied(mutation)).resolves.toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -267,6 +267,22 @@ export async function restoreWorkspaceSkillMutation(
|
||||
await restorePreparedWorkspaceFiles(files);
|
||||
}
|
||||
|
||||
export async function isWorkspaceSkillMutationApplied(
|
||||
mutation: PreparedWorkspaceSkillMutation,
|
||||
): Promise<boolean> {
|
||||
const skillContent = await readPreparedWorkspaceFile(mutation.skillFile, 1024 * 1024);
|
||||
if (skillContent !== mutation.skillFile.content) {
|
||||
return false;
|
||||
}
|
||||
for (const file of mutation.supportFiles) {
|
||||
const content = await readPreparedWorkspaceFile(file, MAX_WORKSPACE_SKILL_SUPPORT_FILE_BYTES);
|
||||
if (content !== file.content) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function normalizeSupportFiles(
|
||||
supportFiles: readonly WorkspaceSkillSupportFileWrite[],
|
||||
): WorkspaceSkillSupportFileWrite[] {
|
||||
@@ -319,6 +335,22 @@ async function restorePreparedWorkspaceFiles(
|
||||
}
|
||||
}
|
||||
|
||||
async function readPreparedWorkspaceFile(
|
||||
file: PreparedWorkspaceSkillFileMutation,
|
||||
maxBytes: number,
|
||||
): Promise<string | null> {
|
||||
if (!(await pathExists(path.join(file.rootDir, file.relativePath)))) {
|
||||
return null;
|
||||
}
|
||||
const targetRoot = await root(file.rootDir);
|
||||
const read = await targetRoot.read(file.relativePath, {
|
||||
hardlinks: "reject",
|
||||
maxBytes,
|
||||
symlinks: "reject",
|
||||
});
|
||||
return read.buffer.toString("utf8");
|
||||
}
|
||||
|
||||
async function resolveWorkspaceSkillWriteTarget(
|
||||
params: WorkspaceSkillWriteTargetParams,
|
||||
): Promise<{ rootDir: string; relativePath: string }> {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import {
|
||||
applyWorkspaceSkillMutation,
|
||||
assertInsideWorkspace,
|
||||
isWorkspaceSkillMutationApplied,
|
||||
prepareWorkspaceSkillMutation,
|
||||
readWorkspaceSkillFile,
|
||||
restoreWorkspaceSkillMutation,
|
||||
@@ -592,6 +593,10 @@ async function recoverAfterApplyCommitFailure(params: {
|
||||
return true;
|
||||
}
|
||||
requiredApplyStatus("apply_failed");
|
||||
const stillApplied = await isWorkspaceSkillMutationApplied(params.mutation).catch(() => false);
|
||||
if (!stillApplied) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await restoreWorkspaceSkillMutation(params.mutation);
|
||||
} catch (restoreError) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import path from "node:path";
|
||||
import { loadConfig } from "../../config/config.js";
|
||||
import {
|
||||
normalizeWorkspaceSkillSupportPath,
|
||||
prepareWorkspaceSkillRestoration,
|
||||
@@ -6,7 +7,9 @@ import {
|
||||
readWorkspaceSupportFile,
|
||||
restoreWorkspaceSkillMutation,
|
||||
} from "../lifecycle/workspace-skill-write.js";
|
||||
import { resolveAllowedSkillSymlinkTargetRealPaths } from "../loading/symlink-targets.js";
|
||||
import { bumpSkillsSnapshotVersion } from "../runtime/refresh-state.js";
|
||||
import { resolveSkillWorkshopConfig } from "./config.js";
|
||||
import { stripProposalFrontmatterForSkill } from "./frontmatter.js";
|
||||
import { createSkillProposalEvent } from "./plugin-hooks.js";
|
||||
import { hashSkillProposalContent } from "./proposal-hash.js";
|
||||
@@ -87,6 +90,8 @@ export async function reconcileInterruptedSkillProposalApply(params: {
|
||||
return true;
|
||||
}
|
||||
if (recovery.state === "partial") {
|
||||
const config = loadConfig({ skipPluginValidation: true });
|
||||
const workshopConfig = resolveSkillWorkshopConfig(config);
|
||||
const restoration = await prepareWorkspaceSkillRestoration({
|
||||
workspaceDir: params.workspaceDir,
|
||||
skillDir: stored.record.target.skillDir,
|
||||
@@ -94,7 +99,12 @@ export async function reconcileInterruptedSkillProposalApply(params: {
|
||||
previousContent: rollback.previousContent ?? null,
|
||||
supportFiles: recovery.supportFiles,
|
||||
mode: stored.record.kind,
|
||||
symlinkPolicy: { allowWrites: false, allowedTargetRealPaths: [] },
|
||||
symlinkPolicy: {
|
||||
allowWrites: workshopConfig.allowSymlinkTargetWrites,
|
||||
allowedTargetRealPaths: workshopConfig.allowSymlinkTargetWrites
|
||||
? resolveAllowedSkillSymlinkTargetRealPaths(config)
|
||||
: [],
|
||||
},
|
||||
});
|
||||
try {
|
||||
await restoreWorkspaceSkillMutation(restoration);
|
||||
|
||||
@@ -990,6 +990,58 @@ describe("skill workshop proposals", () => {
|
||||
await expect(fs.readFile(supportFile, "utf8")).resolves.toBe("Partial support.\n");
|
||||
});
|
||||
|
||||
it.runIf(process.platform !== "win32")(
|
||||
"restores a partial create through an allowed skill symlink",
|
||||
async () => {
|
||||
const workspaceDir = await makeWorkspace();
|
||||
const targetSkillsDir = await tempDirs.make("openclaw-workshop-recovery-symlink-");
|
||||
await fs.symlink(targetSkillsDir, path.join(workspaceDir, "skills"), "dir");
|
||||
const config = {
|
||||
skills: {
|
||||
load: { allowSymlinkTargets: [targetSkillsDir] },
|
||||
workshop: { allowSymlinkTargetWrites: true },
|
||||
},
|
||||
};
|
||||
await testState.writeConfig(config);
|
||||
const proposal = await proposeCreateSkill({
|
||||
workspaceDir,
|
||||
config,
|
||||
name: "Partial Symlink",
|
||||
description: "Recover an allowed symlink target",
|
||||
content: "# Partial Symlink\n\nRetry after recovery.\n",
|
||||
supportFiles: [{ path: "references/proof.md", content: "Symlink support.\n" }],
|
||||
});
|
||||
const targetSupportFile = path.join(
|
||||
targetSkillsDir,
|
||||
"partial-symlink",
|
||||
"references",
|
||||
"proof.md",
|
||||
);
|
||||
await writeSkillProposalRollback({
|
||||
proposalId: proposal.record.id,
|
||||
rollback: createSkillProposalRollback({
|
||||
proposalId: proposal.record.id,
|
||||
targetSkillFile: proposal.record.target.skillFile,
|
||||
action: "create",
|
||||
supportFiles: [{ path: "references/proof.md", existed: false }],
|
||||
}),
|
||||
});
|
||||
await fs.mkdir(path.dirname(targetSupportFile), { recursive: true });
|
||||
await fs.writeFile(targetSupportFile, "Symlink support.\n", "utf8");
|
||||
|
||||
closeOpenClawStateDatabaseForTest();
|
||||
await expect(listSkillProposals({ workspaceDir })).resolves.toMatchObject({
|
||||
proposals: [expect.objectContaining({ id: proposal.record.id, status: "pending" })],
|
||||
});
|
||||
await expect(fs.access(targetSupportFile)).rejects.toThrow();
|
||||
|
||||
await expect(
|
||||
applySkillProposal({ workspaceDir, config, proposalId: proposal.record.id }),
|
||||
).resolves.toMatchObject({ record: { status: "applied" } });
|
||||
await expect(fs.readFile(targetSupportFile, "utf8")).resolves.toBe("Symlink support.\n");
|
||||
},
|
||||
);
|
||||
|
||||
it("does not reconcile an interrupted apply from a tampered proposal draft", async () => {
|
||||
const workspaceDir = await makeWorkspace();
|
||||
const proposal = await proposeCreateSkill({
|
||||
|
||||
Reference in new issue
Block a user