mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:04:33 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -15,9 +15,15 @@ type CompiledPattern =
|
||||
|
||||
function compilePattern(pattern: string): CompiledPattern {
|
||||
const normalized = normalizeToolName(pattern);
|
||||
if (!normalized) return { kind: "exact", value: "" };
|
||||
if (normalized === "*") return { kind: "all" };
|
||||
if (!normalized.includes("*")) return { kind: "exact", value: normalized };
|
||||
if (!normalized) {
|
||||
return { kind: "exact", value: "" };
|
||||
}
|
||||
if (normalized === "*") {
|
||||
return { kind: "all" };
|
||||
}
|
||||
if (!normalized.includes("*")) {
|
||||
return { kind: "exact", value: normalized };
|
||||
}
|
||||
const escaped = normalized.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
return {
|
||||
kind: "regex",
|
||||
@@ -26,7 +32,9 @@ function compilePattern(pattern: string): CompiledPattern {
|
||||
}
|
||||
|
||||
function compilePatterns(patterns?: string[]): CompiledPattern[] {
|
||||
if (!Array.isArray(patterns)) return [];
|
||||
if (!Array.isArray(patterns)) {
|
||||
return [];
|
||||
}
|
||||
return expandToolGroups(patterns)
|
||||
.map(compilePattern)
|
||||
.filter((pattern) => pattern.kind !== "exact" || pattern.value);
|
||||
@@ -34,9 +42,15 @@ function compilePatterns(patterns?: string[]): CompiledPattern[] {
|
||||
|
||||
function matchesAny(name: string, patterns: CompiledPattern[]): boolean {
|
||||
for (const pattern of patterns) {
|
||||
if (pattern.kind === "all") return true;
|
||||
if (pattern.kind === "exact" && name === pattern.value) return true;
|
||||
if (pattern.kind === "regex" && pattern.value.test(name)) return true;
|
||||
if (pattern.kind === "all") {
|
||||
return true;
|
||||
}
|
||||
if (pattern.kind === "exact" && name === pattern.value) {
|
||||
return true;
|
||||
}
|
||||
if (pattern.kind === "regex" && pattern.value.test(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -46,10 +60,18 @@ function makeToolPolicyMatcher(policy: SandboxToolPolicy) {
|
||||
const allow = compilePatterns(policy.allow);
|
||||
return (name: string) => {
|
||||
const normalized = normalizeToolName(name);
|
||||
if (matchesAny(normalized, deny)) return false;
|
||||
if (allow.length === 0) return true;
|
||||
if (matchesAny(normalized, allow)) return true;
|
||||
if (normalized === "apply_patch" && matchesAny("exec", allow)) return true;
|
||||
if (matchesAny(normalized, deny)) {
|
||||
return false;
|
||||
}
|
||||
if (allow.length === 0) {
|
||||
return true;
|
||||
}
|
||||
if (matchesAny(normalized, allow)) {
|
||||
return true;
|
||||
}
|
||||
if (normalized === "apply_patch" && matchesAny("exec", allow)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
@@ -84,12 +106,16 @@ export function resolveSubagentToolPolicy(cfg?: OpenClawConfig): SandboxToolPoli
|
||||
}
|
||||
|
||||
export function isToolAllowedByPolicyName(name: string, policy?: SandboxToolPolicy): boolean {
|
||||
if (!policy) return true;
|
||||
if (!policy) {
|
||||
return true;
|
||||
}
|
||||
return makeToolPolicyMatcher(policy)(name);
|
||||
}
|
||||
|
||||
export function filterToolsByPolicy(tools: AnyAgentTool[], policy?: SandboxToolPolicy) {
|
||||
if (!policy) return tools;
|
||||
if (!policy) {
|
||||
return tools;
|
||||
}
|
||||
const matcher = makeToolPolicyMatcher(policy);
|
||||
return tools.filter((tool) => matcher(tool.name));
|
||||
}
|
||||
@@ -102,7 +128,9 @@ type ToolPolicyConfig = {
|
||||
};
|
||||
|
||||
function unionAllow(base?: string[], extra?: string[]) {
|
||||
if (!Array.isArray(extra) || extra.length === 0) return base;
|
||||
if (!Array.isArray(extra) || extra.length === 0) {
|
||||
return base;
|
||||
}
|
||||
// If the user is using alsoAllow without an allowlist, treat it as additive on top of
|
||||
// an implicit allow-all policy.
|
||||
if (!Array.isArray(base) || base.length === 0) {
|
||||
@@ -112,14 +140,18 @@ function unionAllow(base?: string[], extra?: string[]) {
|
||||
}
|
||||
|
||||
function pickToolPolicy(config?: ToolPolicyConfig): SandboxToolPolicy | undefined {
|
||||
if (!config) return undefined;
|
||||
if (!config) {
|
||||
return undefined;
|
||||
}
|
||||
const allow = Array.isArray(config.allow)
|
||||
? unionAllow(config.allow, config.alsoAllow)
|
||||
: Array.isArray(config.alsoAllow) && config.alsoAllow.length > 0
|
||||
? unionAllow(undefined, config.alsoAllow)
|
||||
: undefined;
|
||||
const deny = Array.isArray(config.deny) ? config.deny : undefined;
|
||||
if (!allow && !deny) return undefined;
|
||||
if (!allow && !deny) {
|
||||
return undefined;
|
||||
}
|
||||
return { allow, deny };
|
||||
}
|
||||
|
||||
@@ -132,18 +164,26 @@ function resolveGroupContextFromSessionKey(sessionKey?: string | null): {
|
||||
groupId?: string;
|
||||
} {
|
||||
const raw = (sessionKey ?? "").trim();
|
||||
if (!raw) return {};
|
||||
if (!raw) {
|
||||
return {};
|
||||
}
|
||||
const base = resolveThreadParentSessionKey(raw) ?? raw;
|
||||
const parts = base.split(":").filter(Boolean);
|
||||
let body = parts[0] === "agent" ? parts.slice(2) : parts;
|
||||
if (body[0] === "subagent") {
|
||||
body = body.slice(1);
|
||||
}
|
||||
if (body.length < 3) return {};
|
||||
if (body.length < 3) {
|
||||
return {};
|
||||
}
|
||||
const [channel, kind, ...rest] = body;
|
||||
if (kind !== "group" && kind !== "channel") return {};
|
||||
if (kind !== "group" && kind !== "channel") {
|
||||
return {};
|
||||
}
|
||||
const groupId = rest.join(":").trim();
|
||||
if (!groupId) return {};
|
||||
if (!groupId) {
|
||||
return {};
|
||||
}
|
||||
return { channel: channel.trim().toLowerCase(), groupId };
|
||||
}
|
||||
|
||||
@@ -153,15 +193,21 @@ function resolveProviderToolPolicy(params: {
|
||||
modelId?: string;
|
||||
}): ToolPolicyConfig | undefined {
|
||||
const provider = params.modelProvider?.trim();
|
||||
if (!provider || !params.byProvider) return undefined;
|
||||
if (!provider || !params.byProvider) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const entries = Object.entries(params.byProvider);
|
||||
if (entries.length === 0) return undefined;
|
||||
if (entries.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const lookup = new Map<string, ToolPolicyConfig>();
|
||||
for (const [key, value] of entries) {
|
||||
const normalized = normalizeProviderKey(key);
|
||||
if (!normalized) continue;
|
||||
if (!normalized) {
|
||||
continue;
|
||||
}
|
||||
lookup.set(normalized, value);
|
||||
}
|
||||
|
||||
@@ -174,7 +220,9 @@ function resolveProviderToolPolicy(params: {
|
||||
|
||||
for (const key of candidates) {
|
||||
const match = lookup.get(key);
|
||||
if (match) return match;
|
||||
if (match) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -238,14 +286,20 @@ export function resolveGroupToolPolicy(params: {
|
||||
senderUsername?: string | null;
|
||||
senderE164?: string | null;
|
||||
}): SandboxToolPolicy | undefined {
|
||||
if (!params.config) return undefined;
|
||||
if (!params.config) {
|
||||
return undefined;
|
||||
}
|
||||
const sessionContext = resolveGroupContextFromSessionKey(params.sessionKey);
|
||||
const spawnedContext = resolveGroupContextFromSessionKey(params.spawnedBy);
|
||||
const groupId = params.groupId ?? sessionContext.groupId ?? spawnedContext.groupId;
|
||||
if (!groupId) return undefined;
|
||||
if (!groupId) {
|
||||
return undefined;
|
||||
}
|
||||
const channelRaw = params.messageProvider ?? sessionContext.channel ?? spawnedContext.channel;
|
||||
const channel = normalizeMessageChannel(channelRaw);
|
||||
if (!channel) return undefined;
|
||||
if (!channel) {
|
||||
return undefined;
|
||||
}
|
||||
let dock;
|
||||
try {
|
||||
dock = getChannelDock(channel);
|
||||
|
||||
Reference in New Issue
Block a user