mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 23:14:31 +00:00
fix(exec): escape regex literals in allowlist path matching
This commit is contained in:
@@ -82,13 +82,25 @@ describe("exec approvals allowlist matching", () => {
|
|||||||
expect(match?.pattern).toBe("*");
|
expect(match?.pattern).toBe("*");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("requires a resolved path", () => {
|
it("matches absolute paths containing regex metacharacters", () => {
|
||||||
const match = matchAllowlist([{ pattern: "bin/rg" }], {
|
const plusPathCases = ["/usr/bin/g++", "/usr/bin/clang++"];
|
||||||
rawExecutable: "bin/rg",
|
for (const candidatePath of plusPathCases) {
|
||||||
resolvedPath: undefined,
|
const match = matchAllowlist([{ pattern: candidatePath }], {
|
||||||
executableName: "rg",
|
rawExecutable: candidatePath,
|
||||||
|
resolvedPath: candidatePath,
|
||||||
|
executableName: candidatePath.split("/").at(-1) ?? candidatePath,
|
||||||
|
});
|
||||||
|
expect(match?.pattern).toBe(candidatePath);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not throw when wildcard globs are mixed with + in path", () => {
|
||||||
|
const match = matchAllowlist([{ pattern: "/usr/bin/*++" }], {
|
||||||
|
rawExecutable: "/usr/bin/g++",
|
||||||
|
resolvedPath: "/usr/bin/g++",
|
||||||
|
executableName: "g++",
|
||||||
});
|
});
|
||||||
expect(match).toBeNull();
|
expect(match?.pattern).toBe("/usr/bin/*++");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -111,6 +111,10 @@ function tryRealpath(value: string): string | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeRegExpLiteral(input: string): string {
|
||||||
|
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
function globToRegExp(pattern: string): RegExp {
|
function globToRegExp(pattern: string): RegExp {
|
||||||
let regex = "^";
|
let regex = "^";
|
||||||
let i = 0;
|
let i = 0;
|
||||||
@@ -132,7 +136,7 @@ function globToRegExp(pattern: string): RegExp {
|
|||||||
i += 1;
|
i += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
regex += ch.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\$&");
|
regex += escapeRegExpLiteral(ch);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
regex += "$";
|
regex += "$";
|
||||||
|
|||||||
Reference in New Issue
Block a user