Skip to content

Commit 09db282

Browse files
fix: guard sqlite shell reopen commands
1 parent df71c37 commit 09db282

2 files changed

Lines changed: 99 additions & 18 deletions

File tree

‎src/agents/bash-tools.exec-live-state-sqlite-guard.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ describeNonWin("exec live OpenClaw state SQLite guard", () => {
5353
context,
5454
),
5555
).resolves.toBe("live-state-sqlite");
56+
await expect(
57+
detectUnsafeExecControlShellCommand(
58+
`sqlite3 :memory: ${quote(`.open ${quote(databasePath)}`)}`,
59+
context,
60+
),
61+
).resolves.toBe("live-state-sqlite");
62+
await expect(
63+
detectUnsafeExecControlShellCommand(
64+
`sqlite3 :memory: ${quote(`.op ${quote(databasePath)}`)}`,
65+
context,
66+
),
67+
).resolves.toBe("live-state-sqlite");
68+
await expect(
69+
detectUnsafeExecControlShellCommand(
70+
`sqlite3 -cmd ${quote(`.open --readonly ${quote(databasePath)}`)} :memory:`,
71+
context,
72+
),
73+
).resolves.toBe("live-state-sqlite");
5674
});
5775
});
5876

@@ -91,6 +109,18 @@ describeNonWin("exec live OpenClaw state SQLite guard", () => {
91109
workdir: root,
92110
}),
93111
).resolves.toBeNull();
112+
await expect(
113+
detectUnsafeExecControlShellCommand(
114+
`sqlite3 :memory: ${quote(`.open ${quote(copiedDatabasePath)}`)}`,
115+
{ stateDir, workdir: root },
116+
),
117+
).resolves.toBeNull();
118+
await expect(
119+
detectUnsafeExecControlShellCommand(
120+
`sqlite3 -cmd ${quote(`.open --readonly ${quote(copiedDatabasePath)}`)} :memory:`,
121+
{ stateDir, workdir: root },
122+
),
123+
).resolves.toBeNull();
94124
await expect(
95125
detectUnsafeExecControlShellCommand(`cat ${quote(path.join(stateDir, "state.sqlite"))}`, {
96126
stateDir,
@@ -127,7 +157,7 @@ describeNonWin("exec live OpenClaw state SQLite guard", () => {
127157
});
128158
await expect(
129159
tool.execute("call-live-sqlite", {
130-
command: `${quote(sqlitePath)} ${quote(databasePath)}`,
160+
command: `${quote(sqlitePath)} :memory: ${quote(`.open ${quote(databasePath)}`)}`,
131161
workdir: root,
132162
}),
133163
).rejects.toThrow(

‎src/infra/exec-control-command-guard.ts‎

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const SQLITE_OPTIONS_WITH_VALUES = new Map<string, number>([
4141
["vfs", 1],
4242
]);
4343

44+
const SQLITE_OPEN_OPTIONS_WITH_VALUES = new Map<string, number>([
45+
["hexkey", 1],
46+
["key", 1],
47+
["maxsize", 1],
48+
["textkey", 1],
49+
]);
50+
4451
function parseExecApprovalShellCommand(raw: string): ParsedExecApprovalCommand | null {
4552
const normalized = raw.trimStart();
4653
const match = normalized.match(
@@ -120,12 +127,13 @@ function parseOpenClawChannelsLoginShellCommand(raw: string): boolean {
120127
);
121128
}
122129

123-
function parseSqliteDatabaseToken(argv: string[]): string | null {
124-
if (normalizeCommandBaseName(argv[0]) !== "sqlite3") {
130+
function parseSqliteOpenCommandDatabaseToken(command: string): string | null {
131+
const argv = splitShellArgs(command);
132+
if (!argv || !/^\.op(?:e(?:n)?)?$/u.test(argv[0] ?? "")) {
125133
return null;
126134
}
127135
for (let index = 1; index < argv.length; index += 1) {
128-
const token = expectDefined(argv[index], "sqlite3 argv entry");
136+
const token = expectDefined(argv[index], "sqlite3 .open argv entry");
129137
if (token === "--") {
130138
return argv[index + 1] ?? null;
131139
}
@@ -134,12 +142,55 @@ function parseSqliteDatabaseToken(argv: string[]): string | null {
134142
}
135143
const optionName = token.replace(/^-+/u, "").split("=", 1)[0]?.toLowerCase() ?? "";
136144
if (!token.includes("=")) {
137-
index += SQLITE_OPTIONS_WITH_VALUES.get(optionName) ?? 0;
145+
index += SQLITE_OPEN_OPTIONS_WITH_VALUES.get(optionName) ?? 0;
138146
}
139147
}
140148
return null;
141149
}
142150

151+
function parseSqliteDatabaseTokens(argv: string[]): string[] {
152+
if (normalizeCommandBaseName(argv[0]) !== "sqlite3") {
153+
return [];
154+
}
155+
const databaseTokens: string[] = [];
156+
const commandTokens: string[] = [];
157+
for (let index = 1; index < argv.length; index += 1) {
158+
const token = expectDefined(argv[index], "sqlite3 argv entry");
159+
if (token === "--") {
160+
const databaseToken = argv[index + 1];
161+
if (databaseToken) {
162+
databaseTokens.push(databaseToken);
163+
}
164+
commandTokens.push(...argv.slice(index + 2));
165+
break;
166+
}
167+
if (token === "-" || !token.startsWith("-")) {
168+
databaseTokens.push(token);
169+
commandTokens.push(...argv.slice(index + 1));
170+
break;
171+
}
172+
const optionName = token.replace(/^-+/u, "").split("=", 1)[0]?.toLowerCase() ?? "";
173+
if (optionName === "cmd") {
174+
const commandToken = token.includes("=")
175+
? token.slice(token.indexOf("=") + 1)
176+
: argv[index + 1];
177+
if (commandToken) {
178+
commandTokens.push(commandToken);
179+
}
180+
}
181+
if (!token.includes("=")) {
182+
index += SQLITE_OPTIONS_WITH_VALUES.get(optionName) ?? 0;
183+
}
184+
}
185+
for (const commandToken of commandTokens) {
186+
const databaseToken = parseSqliteOpenCommandDatabaseToken(commandToken);
187+
if (databaseToken) {
188+
databaseTokens.push(databaseToken);
189+
}
190+
}
191+
return databaseTokens;
192+
}
193+
143194
function expandSqliteDatabaseToken(token: string, stateDir: string): string | null {
144195
let expanded = token.trim();
145196
if (!expanded || expanded === ":memory:") {
@@ -184,22 +235,22 @@ function targetsLiveStateSqliteDatabase(
184235
if (!stateDir) {
185236
return false;
186237
}
187-
const databaseToken = parseSqliteDatabaseToken(argv);
188-
if (!databaseToken) {
189-
return false;
190-
}
191-
const expandedTarget = expandSqliteDatabaseToken(databaseToken, stateDir);
192-
if (!expandedTarget) {
193-
return false;
194-
}
195-
const targetPath = path.isAbsolute(expandedTarget)
196-
? expandedTarget
197-
: path.resolve(context.workdir ?? process.cwd(), expandedTarget);
198238
// External SQLite clients bypass OpenClaw's runtime/version guard and can join the live WAL.
199239
// Resolve existing ancestors so an alias outside the state root cannot hide that ownership.
200240
const canonicalStateDir = resolvePathViaExistingAncestorSync(stateDir);
201-
const canonicalTarget = resolvePathViaExistingAncestorSync(targetPath);
202-
return canonicalTarget === canonicalStateDir || isPathInside(canonicalStateDir, canonicalTarget);
241+
return parseSqliteDatabaseTokens(argv).some((databaseToken) => {
242+
const expandedTarget = expandSqliteDatabaseToken(databaseToken, stateDir);
243+
if (!expandedTarget) {
244+
return false;
245+
}
246+
const targetPath = path.isAbsolute(expandedTarget)
247+
? expandedTarget
248+
: path.resolve(context.workdir ?? process.cwd(), expandedTarget);
249+
const canonicalTarget = resolvePathViaExistingAncestorSync(targetPath);
250+
return (
251+
canonicalTarget === canonicalStateDir || isPathInside(canonicalStateDir, canonicalTarget)
252+
);
253+
});
203254
}
204255

205256
export async function detectUnsafeExecControlShellCommand(

0 commit comments

Comments
 (0)