Skip to content

Commit fe6b8bf

Browse files
authored
fix: prevent $-pattern injection in tilde expansion across extensions (#123004)
* fix: prevent dollar-pattern injection in tilde expansion across extensions String.replace interprets dollar-amp/dollar-1/dollar-backtick in the replacement string. Eight call sites across extensions and memory-host-sdk expanded ~ using .replace(/^~/, homeVariable), which corrupts the path when the home directory contains these sequences. Switch each to a function replacement (() => homeVariable) so the home path is treated literally. Same fix as the core home-dir resolver (PR #122991) and terminal-core/display-string (#111398). Affected: voice-call utils + doctor, browser config, migrate-claude, migrate-hermes, memory-lancedb doctor, memory-host-sdk config-utils. * fix: cover iMessage and 1Password tilde expansion owners Address review: iMessage CLI path expansion and both 1Password state-path expansions (OPENCLAW_HOME and OPENCLAW_STATE_DIR) also passed resolved home as a string replacement. Apply the callback form so a literal-dollar home path is preserved. * test(voice-call): add literal-dollar tilde expansion regression Address review rank-up: mock os.homedir to a literal-dollar path and assert the resolved path preserves dollar-amp verbatim (fails on the string replacement form, passes with the callback). * test(extensions): cover literal-dollar tilde expansion at every changed owner * test(onepassword): cover tilde OPENCLAW_HOME literal-dollar expansion
1 parent f09ec76 commit fe6b8bf

18 files changed

Lines changed: 238 additions & 14 deletions

File tree

‎extensions/browser/src/browser/config.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
55
import { createOpenClawTestState, type OpenClawTestState } from "openclaw/plugin-sdk/test-state";
6-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
6+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import type { BrowserConfig, BrowserProfileConfig } from "../config/config.js";
88
import { resolveUserPath } from "../utils.js";
99
import {
@@ -158,6 +158,17 @@ describe("browser config", () => {
158158
expect(resolveProfile(resolved, "work")?.cdpPort).toBe(20123);
159159
});
160160

161+
it("keeps literal $ patterns in home when expanding a tilde executable path", () => {
162+
const spy = vi.spyOn(os, "homedir").mockReturnValue("/home/$&user");
163+
try {
164+
expect(resolveBrowserConfig({ executablePath: "~/chrome-bin" }).executablePath).toBe(
165+
path.resolve("/home/$&user/chrome-bin"),
166+
);
167+
} finally {
168+
spy.mockRestore();
169+
}
170+
});
171+
161172
it("does not assign an implicit extension relay an explicitly pinned extension port", () => {
162173
const resolved = resolveBrowserConfig({
163174
profiles: {

‎extensions/browser/src/browser/config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function normalizeExecutablePath(raw: string | undefined): string | undefined {
183183
if (!/^~(?=$|[\\/])/.test(value)) {
184184
return value;
185185
}
186-
return path.resolve(value.replace(/^~(?=$|[\\/])/, os.homedir()));
186+
return path.resolve(value.replace(/^~(?=$|[\\/])/, () => os.homedir()));
187187
}
188188

189189
function normalizeExistingSessionCdpUrl(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Imessage plugin tests cover CLI path tilde expansion.
2+
import { describe, expect, it } from "vitest";
3+
import { expandIMessageUserPath } from "./cli-path.js";
4+
5+
describe("expandIMessageUserPath", () => {
6+
it("does not interpret $ patterns in home when expanding tildes", () => {
7+
const previous = process.env.HOME;
8+
process.env.HOME = "/home/user$&d";
9+
try {
10+
expect(expandIMessageUserPath("~/Library/Messages/chat.db")).toBe(
11+
"/home/user$&d/Library/Messages/chat.db",
12+
);
13+
} finally {
14+
if (previous === undefined) {
15+
delete process.env.HOME;
16+
} else {
17+
process.env.HOME = previous;
18+
}
19+
}
20+
});
21+
});

‎extensions/imessage/src/cli-path.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function expandIMessageUserPath(value: string): string {
3535
return value;
3636
}
3737
const home = resolveIMessageHomeDir();
38-
return home ? value.replace(/^~(?=$|[\\/])/, home) : value;
38+
return home ? value.replace(/^~(?=$|[\\/])/, () => home) : value;
3939
}
4040

4141
function resolveIMessageExecutable(cliPath: string): string | undefined {

‎extensions/memory-lancedb/doctor-contract-api.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,47 @@ describe("memory-lancedb doctor migration", () => {
121121
migratedConnection.close();
122122
});
123123

124+
test("keeps literal $ patterns in home when expanding a tilde dbPath", async () => {
125+
const home = path.join(getTmpDir(), "home$&d");
126+
const dollarDbPath = path.join(home, "lancedb-dollar");
127+
const connection = await lancedb.connect(dollarDbPath);
128+
const table = await connection.createTable("memories", [
129+
{
130+
id: "22222222-2222-4222-8222-222222222222",
131+
text: "legacy shared memory",
132+
vector: [1, 0],
133+
importance: 0.7,
134+
category: "fact",
135+
createdAt: 1,
136+
},
137+
]);
138+
table.close();
139+
connection.close();
140+
141+
const config = {
142+
agents: { list: [{ id: "main", default: true }] },
143+
plugins: {
144+
entries: {
145+
"memory-lancedb": {
146+
config: { dbPath: "~/lancedb-dollar" },
147+
},
148+
},
149+
},
150+
};
151+
const params = {
152+
config,
153+
env: { ...process.env, HOME: home },
154+
stateDir: getTmpDir(),
155+
oauthDir: path.join(getTmpDir(), "oauth"),
156+
context: unusedDoctorContext,
157+
};
158+
const migration = expectDefined(stateMigrations[0], "memory-lancedb state migration");
159+
160+
await expect(migration.detectLegacyState(params)).resolves.toMatchObject({
161+
preview: [expect.stringContaining(dollarDbPath)],
162+
});
163+
});
164+
124165
test("deletes only structurally complete legacy envelope rows", async () => {
125166
const benignRows = [
126167
{

‎extensions/memory-lancedb/doctor-contract-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function resolveConfiguredDbPath(
115115
return configured;
116116
}
117117
if (configured.startsWith("~")) {
118-
return path.resolve(configured.replace(/^~(?=$|[\\/])/, resolveHome(env)));
118+
return path.resolve(configured.replace(/^~(?=$|[\\/])/, () => resolveHome(env)));
119119
}
120120
// Plugin runtime api.resolvePath() anchors relative paths at this same root.
121121
return path.resolve(pluginRoot, configured);

‎extensions/migrate-claude/helpers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function resolveHomePath(input: string): string {
1515
if (!trimmed) {
1616
return trimmed;
1717
}
18-
return path.resolve(trimmed.replace(/^~(?=$|[\\/])/u, os.homedir()));
18+
return path.resolve(trimmed.replace(/^~(?=$|[\\/])/u, () => os.homedir()));
1919
}
2020

2121
export async function exists(filePath: string): Promise<boolean> {

‎extensions/migrate-claude/provider.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ describe("Claude migration provider", () => {
6161
}
6262
});
6363

64+
it("keeps literal $ patterns in home when expanding tildes", () => {
65+
const spy = vi.spyOn(os, "homedir").mockReturnValue("/home/$&user");
66+
try {
67+
expect(resolveHomePath("~/.claude")).toBe(path.resolve("/home/$&user/.claude"));
68+
} finally {
69+
spy.mockRestore();
70+
}
71+
});
72+
6473
it("rejects missing Claude sources before planning", async () => {
6574
const root = testWorkspace.dir;
6675
const source = path.join(root, "missing");

‎extensions/migrate-hermes/helpers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const EDGE_DASHES_RE = /^-+|-+$/g;
1919

2020
export function resolveHomePath(input: string): string {
2121
const value = input.trim();
22-
return value ? path.resolve(value.replace(HOME_SHORTHAND_RE, os.homedir())) : value;
22+
return value ? path.resolve(value.replace(HOME_SHORTHAND_RE, () => os.homedir())) : value;
2323
}
2424

2525
export async function exists(filePath: string): Promise<boolean> {

‎extensions/migrate-hermes/provider.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
tempWorkspace,
99
type TempWorkspace,
1010
} from "openclaw/plugin-sdk/temp-path";
11-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
11+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1212
import { resolveHomePath } from "./helpers.js";
1313
import pluginEntry from "./index.js";
1414
import { HERMES_REASON_INCLUDE_SECRETS } from "./items.js";
@@ -56,6 +56,15 @@ describe("Hermes migration provider", () => {
5656
}
5757
});
5858

59+
it("keeps literal $ patterns in home when expanding tildes", () => {
60+
const spy = vi.spyOn(os, "homedir").mockReturnValue("/home/$&user");
61+
try {
62+
expect(resolveHomePath("~/.hermes")).toBe(path.resolve("/home/$&user/.hermes"));
63+
} finally {
64+
spy.mockRestore();
65+
}
66+
});
67+
5968
it("detects Hermes sources supported by planning", async () => {
6069
const root = testWorkspace.dir;
6170
const source = path.join(root, "hermes");

0 commit comments

Comments
 (0)