Skip to content

Commit 6dac1af

Browse files
committed
fix(plugins): reject external public-artifact hardlinks
1 parent e99f6dc commit 6dac1af

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎src/plugins/provider-public-artifacts.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,46 @@ describe("provider public artifacts", () => {
232232
}
233233
});
234234

235+
it.runIf(process.platform !== "win32")(
236+
"rejects trusted official provider policy artifacts hardlinked outside the installed root",
237+
() => {
238+
const tempRoot = fs.realpathSync(
239+
fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-provider-policy-hardlink-")),
240+
);
241+
const pluginRoot = path.join(tempRoot, "installed-provider");
242+
const outsidePath = path.join(tempRoot, "outside-policy.js");
243+
fs.mkdirSync(pluginRoot, { recursive: true });
244+
fs.writeFileSync(
245+
outsidePath,
246+
'export function resolveThinkingProfile() { return { defaultLevel: "escaped" }; }\n',
247+
"utf8",
248+
);
249+
fs.linkSync(outsidePath, path.join(pluginRoot, "provider-policy-api.js"));
250+
251+
try {
252+
const pluginId = "hardlinked-provider";
253+
expect(() =>
254+
resolveProviderPolicySurface(pluginId, {
255+
manifestRegistry: {
256+
plugins: [
257+
{
258+
id: pluginId,
259+
origin: "global",
260+
trustedOfficialInstall: true,
261+
rootDir: pluginRoot,
262+
providers: [pluginId],
263+
cliBackends: [],
264+
} as never,
265+
],
266+
},
267+
}),
268+
).toThrow("Unable to open plugin public surface provider-policy-api.js");
269+
} finally {
270+
fs.rmSync(tempRoot, { recursive: true, force: true });
271+
}
272+
},
273+
);
274+
235275
it("resolves namespaced provider policies from their trusted external plugin root", () => {
236276
const bundledPluginsDir = fs.mkdtempSync(
237277
path.join(os.tmpdir(), "openclaw-empty-bundled-plugins-"),

‎src/plugins/public-surface-loader.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,35 @@ describe("bundled plugin public surface loader", () => {
329329
},
330330
);
331331

332+
it
333+
.runIf(process.platform !== "win32")
334+
.each(["provider-policy-api.js", "api.js", "runtime-api.js"])(
335+
"rejects installed plugin public artifact %s hardlinked outside its root",
336+
async (artifact) => {
337+
const publicSurfaceLoader = await importFreshModule<
338+
typeof import("./public-surface-loader.js")
339+
>(
340+
import.meta.url,
341+
`./public-surface-loader.js?scope=installed-hardlink-${artifact.replace(".js", "")}`,
342+
);
343+
const tempRoot = fs.realpathSync(createTempDir());
344+
const pluginRoot = path.join(tempRoot, "installed-plugin");
345+
const outsidePath = path.join(tempRoot, "outside.js");
346+
const artifactPath = path.join(pluginRoot, artifact);
347+
fs.mkdirSync(pluginRoot, { recursive: true });
348+
fs.writeFileSync(outsidePath, 'export const marker = "outside-plugin-root";\n', "utf8");
349+
fs.linkSync(outsidePath, artifactPath);
350+
351+
expect(fs.statSync(artifactPath).nlink).toBeGreaterThan(1);
352+
expect(() =>
353+
publicSurfaceLoader.loadPluginPublicArtifactModuleSync({
354+
pluginRoot,
355+
artifactBasename: artifact,
356+
}),
357+
).toThrow(`Unable to open plugin public surface ${artifact}`);
358+
},
359+
);
360+
332361
it("does not cache missing public artifact locations", async () => {
333362
vi.doMock("./native-module-require.js", () => ({
334363
tryNativeRequireJavaScriptModule: (modulePath: string) => ({

‎src/plugins/public-surface-loader.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
66
import { openRootFileSync } from "../infra/boundary-file-read.js";
77
import { sameFileIdentity } from "../infra/fs-safe-advanced.js";
88
import { resolveBundledPluginsDir } from "./bundled-dir.js";
9+
import { shouldRejectHardlinkedPluginFiles } from "./hardlink-policy.js";
910
import { registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle.js";
1011
import {
1112
createPluginModuleLoaderCache,
@@ -127,17 +128,23 @@ function loadValidatedPublicSurfaceModule(params: {
127128
boundaryRoot: string;
128129
boundaryLabel: string;
129130
surfaceLabel: string;
131+
origin: "bundled" | "global";
130132
}): object {
131133
const cached = publicSurfaceModuleCache.get(params.modulePath);
132134
if (cached) {
133135
return cached as object;
134136
}
135137

138+
// Official install trust does not make mutable plugin roots immutable;
139+
// the shared policy preserves bundled and immutable Nix-store exceptions.
136140
const opened = openRootFileSync({
137141
absolutePath: params.modulePath,
138142
rootPath: params.boundaryRoot,
139143
boundaryLabel: params.boundaryLabel,
140-
rejectHardlinks: false,
144+
rejectHardlinks: shouldRejectHardlinkedPluginFiles({
145+
origin: params.origin,
146+
rootDir: params.boundaryRoot,
147+
}),
141148
});
142149
if (!opened.ok) {
143150
throw new Error(`Unable to open ${params.surfaceLabel}`, { cause: opened.error });
@@ -182,6 +189,7 @@ export function loadBundledPluginPublicArtifactModuleSync<T extends object>(para
182189
boundaryLabel:
183190
location.boundaryRoot === OPENCLAW_PACKAGE_ROOT ? "OpenClaw package root" : "plugin root",
184191
surfaceLabel: `bundled plugin public surface ${params.dirName}/${params.artifactBasename}`,
192+
origin: "bundled",
185193
}) as T;
186194
}
187195

@@ -201,6 +209,7 @@ export function loadPluginPublicArtifactModuleSync<T extends object>(params: {
201209
boundaryRoot: path.resolve(params.pluginRoot),
202210
boundaryLabel: "plugin root",
203211
surfaceLabel: `plugin public surface ${params.artifactBasename}`,
212+
origin: "global",
204213
}) as T;
205214
}
206215

0 commit comments

Comments
 (0)