mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:38:26 +00:00
refactor: tighten safe-bin policy model and docs parity
This commit is contained in:
@@ -26,15 +26,15 @@ function hasGlobToken(value: string): boolean {
|
||||
export type SafeBinProfile = {
|
||||
minPositional?: number;
|
||||
maxPositional?: number;
|
||||
valueFlags?: ReadonlySet<string>;
|
||||
blockedFlags?: ReadonlySet<string>;
|
||||
allowedValueFlags?: ReadonlySet<string>;
|
||||
deniedFlags?: ReadonlySet<string>;
|
||||
};
|
||||
|
||||
export type SafeBinProfileFixture = {
|
||||
minPositional?: number;
|
||||
maxPositional?: number;
|
||||
valueFlags?: readonly string[];
|
||||
blockedFlags?: readonly string[];
|
||||
allowedValueFlags?: readonly string[];
|
||||
deniedFlags?: readonly string[];
|
||||
};
|
||||
|
||||
const NO_FLAGS: ReadonlySet<string> = new Set();
|
||||
@@ -50,8 +50,8 @@ function compileSafeBinProfile(fixture: SafeBinProfileFixture): SafeBinProfile {
|
||||
return {
|
||||
minPositional: fixture.minPositional,
|
||||
maxPositional: fixture.maxPositional,
|
||||
valueFlags: toFlagSet(fixture.valueFlags),
|
||||
blockedFlags: toFlagSet(fixture.blockedFlags),
|
||||
allowedValueFlags: toFlagSet(fixture.allowedValueFlags),
|
||||
deniedFlags: toFlagSet(fixture.deniedFlags),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -68,19 +68,8 @@ export const SAFE_BIN_GENERIC_PROFILE_FIXTURE: SafeBinProfileFixture = {};
|
||||
export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> = {
|
||||
jq: {
|
||||
maxPositional: 1,
|
||||
valueFlags: [
|
||||
"--arg",
|
||||
"--argjson",
|
||||
"--argstr",
|
||||
"--argfile",
|
||||
"--rawfile",
|
||||
"--slurpfile",
|
||||
"--from-file",
|
||||
"--library-path",
|
||||
"-L",
|
||||
"-f",
|
||||
],
|
||||
blockedFlags: [
|
||||
allowedValueFlags: ["--arg", "--argjson", "--argstr"],
|
||||
deniedFlags: [
|
||||
"--argfile",
|
||||
"--rawfile",
|
||||
"--slurpfile",
|
||||
@@ -95,30 +84,25 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
|
||||
// Allowing one positional is ambiguous because -e consumes the pattern and
|
||||
// frees the positional slot for a filename.
|
||||
maxPositional: 0,
|
||||
valueFlags: [
|
||||
allowedValueFlags: [
|
||||
"--regexp",
|
||||
"--file",
|
||||
"--max-count",
|
||||
"--after-context",
|
||||
"--before-context",
|
||||
"--context",
|
||||
"--devices",
|
||||
"--directories",
|
||||
"--binary-files",
|
||||
"--exclude",
|
||||
"--exclude-from",
|
||||
"--include",
|
||||
"--label",
|
||||
"-e",
|
||||
"-f",
|
||||
"-m",
|
||||
"-A",
|
||||
"-B",
|
||||
"-C",
|
||||
"-D",
|
||||
"-d",
|
||||
],
|
||||
blockedFlags: [
|
||||
deniedFlags: [
|
||||
"--file",
|
||||
"--exclude-from",
|
||||
"--dereference-recursive",
|
||||
@@ -132,7 +116,7 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
|
||||
},
|
||||
cut: {
|
||||
maxPositional: 0,
|
||||
valueFlags: [
|
||||
allowedValueFlags: [
|
||||
"--bytes",
|
||||
"--characters",
|
||||
"--fields",
|
||||
@@ -146,7 +130,7 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
|
||||
},
|
||||
sort: {
|
||||
maxPositional: 0,
|
||||
valueFlags: [
|
||||
allowedValueFlags: [
|
||||
"--key",
|
||||
"--field-separator",
|
||||
"--buffer-size",
|
||||
@@ -154,28 +138,33 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
|
||||
"--parallel",
|
||||
"--batch-size",
|
||||
"--random-source",
|
||||
"--files0-from",
|
||||
"--output",
|
||||
"-k",
|
||||
"-t",
|
||||
"-S",
|
||||
"-T",
|
||||
"-o",
|
||||
],
|
||||
// --compress-program can invoke an external executable and breaks stdin-only guarantees.
|
||||
blockedFlags: ["--compress-program", "--files0-from", "--output", "-o"],
|
||||
deniedFlags: ["--compress-program", "--files0-from", "--output", "-o"],
|
||||
},
|
||||
uniq: {
|
||||
maxPositional: 0,
|
||||
valueFlags: ["--skip-fields", "--skip-chars", "--check-chars", "--group", "-f", "-s", "-w"],
|
||||
allowedValueFlags: [
|
||||
"--skip-fields",
|
||||
"--skip-chars",
|
||||
"--check-chars",
|
||||
"--group",
|
||||
"-f",
|
||||
"-s",
|
||||
"-w",
|
||||
],
|
||||
},
|
||||
head: {
|
||||
maxPositional: 0,
|
||||
valueFlags: ["--lines", "--bytes", "-n", "-c"],
|
||||
allowedValueFlags: ["--lines", "--bytes", "-n", "-c"],
|
||||
},
|
||||
tail: {
|
||||
maxPositional: 0,
|
||||
valueFlags: [
|
||||
allowedValueFlags: [
|
||||
"--lines",
|
||||
"--bytes",
|
||||
"--sleep-interval",
|
||||
@@ -191,8 +180,7 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
|
||||
},
|
||||
wc: {
|
||||
maxPositional: 0,
|
||||
valueFlags: ["--files0-from"],
|
||||
blockedFlags: ["--files0-from"],
|
||||
deniedFlags: ["--files0-from"],
|
||||
},
|
||||
};
|
||||
|
||||
@@ -201,6 +189,29 @@ export const SAFE_BIN_GENERIC_PROFILE = compileSafeBinProfile(SAFE_BIN_GENERIC_P
|
||||
export const SAFE_BIN_PROFILES: Record<string, SafeBinProfile> =
|
||||
compileSafeBinProfiles(SAFE_BIN_PROFILE_FIXTURES);
|
||||
|
||||
export function resolveSafeBinDeniedFlags(
|
||||
fixtures: Readonly<Record<string, SafeBinProfileFixture>> = SAFE_BIN_PROFILE_FIXTURES,
|
||||
): Record<string, string[]> {
|
||||
const out: Record<string, string[]> = {};
|
||||
for (const [name, fixture] of Object.entries(fixtures)) {
|
||||
const denied = Array.from(new Set(fixture.deniedFlags ?? [])).toSorted();
|
||||
if (denied.length > 0) {
|
||||
out[name] = denied;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function renderSafeBinDeniedFlagsDocBullets(
|
||||
fixtures: Readonly<Record<string, SafeBinProfileFixture>> = SAFE_BIN_PROFILE_FIXTURES,
|
||||
): string {
|
||||
const deniedByBin = resolveSafeBinDeniedFlags(fixtures);
|
||||
const bins = Object.keys(deniedByBin).toSorted();
|
||||
return bins
|
||||
.map((bin) => `- \`${bin}\`: ${deniedByBin[bin].map((flag) => `\`${flag}\``).join(", ")}`)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function isSafeLiteralToken(value: string): boolean {
|
||||
if (!value || value === "-") {
|
||||
return true;
|
||||
@@ -217,16 +228,16 @@ function consumeLongOptionToken(
|
||||
index: number,
|
||||
flag: string,
|
||||
inlineValue: string | undefined,
|
||||
valueFlags: ReadonlySet<string>,
|
||||
blockedFlags: ReadonlySet<string>,
|
||||
allowedValueFlags: ReadonlySet<string>,
|
||||
deniedFlags: ReadonlySet<string>,
|
||||
): number {
|
||||
if (blockedFlags.has(flag)) {
|
||||
if (deniedFlags.has(flag)) {
|
||||
return -1;
|
||||
}
|
||||
if (inlineValue !== undefined) {
|
||||
return isSafeLiteralToken(inlineValue) ? index + 1 : -1;
|
||||
}
|
||||
if (!valueFlags.has(flag)) {
|
||||
if (!allowedValueFlags.has(flag)) {
|
||||
return index + 1;
|
||||
}
|
||||
return isInvalidValueToken(args[index + 1]) ? -1 : index + 2;
|
||||
@@ -238,15 +249,15 @@ function consumeShortOptionClusterToken(
|
||||
raw: string,
|
||||
cluster: string,
|
||||
flags: string[],
|
||||
valueFlags: ReadonlySet<string>,
|
||||
blockedFlags: ReadonlySet<string>,
|
||||
allowedValueFlags: ReadonlySet<string>,
|
||||
deniedFlags: ReadonlySet<string>,
|
||||
): number {
|
||||
for (let j = 0; j < flags.length; j += 1) {
|
||||
const flag = flags[j];
|
||||
if (blockedFlags.has(flag)) {
|
||||
if (deniedFlags.has(flag)) {
|
||||
return -1;
|
||||
}
|
||||
if (!valueFlags.has(flag)) {
|
||||
if (!allowedValueFlags.has(flag)) {
|
||||
continue;
|
||||
}
|
||||
const inlineValue = cluster.slice(j + 1);
|
||||
@@ -275,8 +286,8 @@ function validatePositionalCount(positional: string[], profile: SafeBinProfile):
|
||||
}
|
||||
|
||||
export function validateSafeBinArgv(args: string[], profile: SafeBinProfile): boolean {
|
||||
const valueFlags = profile.valueFlags ?? NO_FLAGS;
|
||||
const blockedFlags = profile.blockedFlags ?? NO_FLAGS;
|
||||
const allowedValueFlags = profile.allowedValueFlags ?? NO_FLAGS;
|
||||
const deniedFlags = profile.deniedFlags ?? NO_FLAGS;
|
||||
const positional: string[] = [];
|
||||
let i = 0;
|
||||
while (i < args.length) {
|
||||
@@ -315,8 +326,8 @@ export function validateSafeBinArgv(args: string[], profile: SafeBinProfile): bo
|
||||
i,
|
||||
token.flag,
|
||||
token.inlineValue,
|
||||
valueFlags,
|
||||
blockedFlags,
|
||||
allowedValueFlags,
|
||||
deniedFlags,
|
||||
);
|
||||
if (nextIndex < 0) {
|
||||
return false;
|
||||
@@ -331,8 +342,8 @@ export function validateSafeBinArgv(args: string[], profile: SafeBinProfile): bo
|
||||
token.raw,
|
||||
token.cluster,
|
||||
token.flags,
|
||||
valueFlags,
|
||||
blockedFlags,
|
||||
allowedValueFlags,
|
||||
deniedFlags,
|
||||
);
|
||||
if (nextIndex < 0) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user