Skip to content

Commit 80b0e78

Browse files
authored
fix(plugins): hash loader cache keys so plugin config never leaks into diagnostics (#117237)
1 parent ad409a8 commit 80b0e78

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

‎src/plugins/loader-load-context.ts‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,6 @@ function buildActivationMetadataHash(params: {
152152
.digest("hex");
153153
}
154154

155-
function redactPluginConfigForCacheKey(plugins: NormalizedPluginsConfig): NormalizedPluginsConfig {
156-
const entries = Object.fromEntries(
157-
Object.entries(plugins.entries).map(([pluginId, entry]) => [
158-
pluginId,
159-
"config" in entry ? { ...entry, config: "<plugin-config>" } : entry,
160-
]),
161-
);
162-
return { ...plugins, entries };
163-
}
164-
165155
function buildCacheKey(params: {
166156
workspaceDir?: string;
167157
plugins: NormalizedPluginsConfig;
@@ -227,15 +217,18 @@ function buildCacheKey(params: {
227217
const moduleLoadMode = params.loadModules === false ? "manifest-only" : "load-modules";
228218
const discoveryMode = params.toolDiscovery === true ? "tool-discovery" : "default-discovery";
229219
const activationMode = params.activate === false ? "snapshot" : "active";
230-
return `${roots.workspace ?? ""}::${roots.global ?? ""}::${roots.stock ?? ""}::${JSON.stringify({
231-
bundledPackage,
232-
devSourceRoot: params.devSourceRoot ?? "",
233-
discoveryFingerprint: fingerprintPluginDiscoveryContext(discoveryContext),
234-
...params.plugins,
235-
installs,
236-
loadPaths,
237-
activationMetadataKey: params.activationMetadataKey ?? "",
238-
})}::${serializePluginIdScope(params.onlyPluginIds)}::${setupOnlyKey}::${setupOnlyModeKey}::${setupOnlyRequirementKey}::${startupChannelMode}::${bundledArtifactMode}::${rawConfigEnvMode}::${moduleLoadMode}::${discoveryMode}::${params.runtimeSubagentMode ?? "default"}::${params.pluginSdkResolution ?? "auto"}::${JSON.stringify(params.coreGatewayMethodNames ?? [])}::${activationMode}`;
220+
const cacheIdentity = `${roots.workspace ?? ""}::${roots.global ?? ""}::${roots.stock ?? ""}::${JSON.stringify(
221+
{
222+
bundledPackage,
223+
devSourceRoot: params.devSourceRoot ?? "",
224+
discoveryFingerprint: fingerprintPluginDiscoveryContext(discoveryContext),
225+
...params.plugins,
226+
installs,
227+
loadPaths,
228+
activationMetadataKey: params.activationMetadataKey ?? "",
229+
},
230+
)}::${serializePluginIdScope(params.onlyPluginIds)}::${setupOnlyKey}::${setupOnlyModeKey}::${setupOnlyRequirementKey}::${startupChannelMode}::${bundledArtifactMode}::${rawConfigEnvMode}::${moduleLoadMode}::${discoveryMode}::${params.runtimeSubagentMode ?? "default"}::${params.pluginSdkResolution ?? "auto"}::${JSON.stringify(params.coreGatewayMethodNames ?? [])}::${activationMode}`;
231+
return createHash("sha256").update(cacheIdentity).digest("hex");
239232
}
240233

241234
export function resolveRuntimeSubagentMode(
@@ -384,9 +377,7 @@ export function resolvePluginLoadCacheContext(options: PluginLoadOptions = {}) {
384377
const devSourceRoot = resolveOpenClawDevSourceRoot(env);
385378
const cacheKey = buildCacheKey({
386379
workspaceDir: options.workspaceDir,
387-
plugins: shouldResolveRawConfigEnvVars
388-
? redactPluginConfigForCacheKey(trustNormalized)
389-
: trustNormalized,
380+
plugins: trustNormalized,
390381
activationMetadataKey: buildActivationMetadataHash({
391382
activationSource,
392383
autoEnabledReasons: options.autoEnabledReasons ?? {},

‎src/plugins/loader.base.test-utils.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
clearPluginInteractiveHandlers,
2424
resolvePluginInteractiveNamespaceMatch,
2525
} from "./interactive-registry.js";
26+
import { resolvePluginRegistryLoadCacheKey } from "./loader-cache.js";
2627
import { loadOpenClawPlugins, resolveRuntimePluginRegistry } from "./loader.js";
2728
import {
2829
EMPTY_PLUGIN_SCHEMA,
@@ -1384,9 +1385,10 @@ describe("loadOpenClawPlugins", () => {
13841385
},
13851386
},
13861387
{
1387-
label: "fails loudly when a plugin reenters the same snapshot load during register",
1388+
label: "does not expose plugin config in cache keys or reentry diagnostics",
13881389
run: () => {
13891390
useNoBundledPlugins();
1391+
const pluginConfigSentinel = "hunter2-sentinel";
13901392
const marker = "__openclaw_loader_reentry_error";
13911393
const reenterFnMarker = "__openclaw_loader_reentry_fn";
13921394
Reflect.deleteProperty(globalThis, marker);
@@ -1405,13 +1407,21 @@ describe("loadOpenClawPlugins", () => {
14051407
plugins: {
14061408
load: { paths: [pluginFile] },
14071409
allow: ["reentrant-snapshot"],
1410+
entries: {
1411+
"reentrant-snapshot": { config: { token: pluginConfigSentinel } },
1412+
},
14081413
},
14091414
},
14101415
} satisfies Parameters<typeof loadOpenClawPlugins>[0];
14111416
writePlugin({
14121417
id: "reentrant-snapshot",
14131418
dir: pluginDir,
14141419
filename: "reentrant-snapshot.cjs",
1420+
configSchema: {
1421+
type: "object",
1422+
additionalProperties: false,
1423+
properties: { token: { type: "string" } },
1424+
},
14151425
body: `module.exports = {
14161426
id: "reentrant-snapshot",
14171427
register() {
@@ -1428,17 +1438,24 @@ describe("loadOpenClawPlugins", () => {
14281438
};`,
14291439
});
14301440

1441+
const cacheKey = resolvePluginRegistryLoadCacheKey(nestedOptions);
1442+
expect(cacheKey).toMatch(/^[a-f0-9]{64}$/);
1443+
expect(cacheKey).not.toContain(pluginConfigSentinel);
14311444
const registry = loadOpenClawPlugins(nestedOptions);
14321445

14331446
try {
14341447
const reentryError = Reflect.get(globalThis, marker) as
14351448
| { name?: unknown; message?: unknown }
14361449
| undefined;
14371450
expect(reentryError?.name).toBe("PluginLoadReentryError");
1438-
expect(String(reentryError?.message)).toContain("plugin load reentry detected");
1451+
expect(reentryError?.message).toBe(
1452+
`plugin load reentry detected for cache key: ${cacheKey}`,
1453+
);
1454+
expect(String(reentryError?.message)).not.toContain(pluginConfigSentinel);
14391455
const record = registry.plugins.find((entry) => entry.id === "reentrant-snapshot");
14401456
expect(record?.status).toBe("error");
1441-
expect(record?.error).toContain("plugin load reentry detected");
1457+
expect(record?.error).toContain(cacheKey);
1458+
expect(record?.error).not.toContain(pluginConfigSentinel);
14421459
expect(record?.failurePhase).toBe("register");
14431460
} finally {
14441461
Reflect.deleteProperty(globalThis, marker);

0 commit comments

Comments
 (0)