[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
163
infrastructure/ai/cattyAgent/safety.ts
Normal file
163
infrastructure/ai/cattyAgent/safety.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import commandBlocklistTable from '../../../lib/commandBlocklist.json';
|
||||
import { DEFAULT_COMMAND_BLOCKLIST } from '../types';
|
||||
|
||||
/**
|
||||
* Check if a regex pattern is safe from ReDoS attacks.
|
||||
*
|
||||
* Rejects patterns with nested quantifiers like `(a+)+`, `(a*)*`, `(a+)*`
|
||||
* which can cause catastrophic backtracking / CPU exhaustion.
|
||||
*/
|
||||
function isSafeRegex(pattern: string): boolean {
|
||||
// Detect nested quantifiers: a group containing a quantifier, followed by another quantifier.
|
||||
// Matches patterns like (x+)+, (x*)+, (x+)*, (x{2,})+ etc.
|
||||
const nestedQuantifier = /\([^)]*[+*}]\)[+*?{]/;
|
||||
if (nestedQuantifier.test(pattern)) {
|
||||
return false;
|
||||
}
|
||||
// Also catch overlapping alternations with quantifiers inside quantified groups
|
||||
// e.g. (a|a)+ — not always dangerous but a common ReDoS vector
|
||||
const overlappingAlt = /\([^)]*\|[^)]*\)[+*]{/;
|
||||
if (overlappingAlt.test(pattern)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-compiled RegExp cache for default blocklist patterns, grouped by the
|
||||
* shell family the pattern targets.
|
||||
*
|
||||
* The blocklist is a best-effort defense-in-depth measure. It is NOT a
|
||||
* security boundary — determined users or sophisticated prompt injection
|
||||
* can bypass regex-based filtering. The primary security boundary is the
|
||||
* permission / confirmation system and OS-level sandboxing.
|
||||
*/
|
||||
interface CompiledPattern { pattern: string; regex: RegExp }
|
||||
|
||||
const compileGroup = (patterns: string[]): CompiledPattern[] =>
|
||||
patterns.flatMap((pattern) => {
|
||||
try {
|
||||
if (!isSafeRegex(pattern)) {
|
||||
console.warn(`[Safety] Skipping default blocklist pattern with nested quantifiers (ReDoS risk): ${pattern}`);
|
||||
return [];
|
||||
}
|
||||
return [{ pattern, regex: new RegExp(pattern, 'i') }];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const compiledCommonGroup = compileGroup(commandBlocklistTable.common);
|
||||
const compiledPosixNativeGroup = compileGroup(commandBlocklistTable.posixNative);
|
||||
const compiledPosixGroup = compileGroup(commandBlocklistTable.posix);
|
||||
const compiledPowershellGroup = compileGroup(commandBlocklistTable.powershell);
|
||||
const compiledGroups = {
|
||||
common: compiledCommonGroup,
|
||||
posixNative: compiledPosixNativeGroup,
|
||||
posix: compiledPosixGroup,
|
||||
powershell: compiledPowershellGroup,
|
||||
};
|
||||
const compiledAllGroups = [
|
||||
compiledCommonGroup,
|
||||
compiledPosixNativeGroup,
|
||||
compiledPosixGroup,
|
||||
compiledPowershellGroup,
|
||||
];
|
||||
const DEFAULT_PATTERN_SET = new Set(DEFAULT_COMMAND_BLOCKLIST);
|
||||
|
||||
/**
|
||||
* Default-blocklist groups that apply for a shell kind, from common
|
||||
* (shell-independent) patterns to per-family ones. Unknown / empty kinds
|
||||
* intentionally fall back to every group so callers that cannot classify a
|
||||
* session keep the strict behavior.
|
||||
*/
|
||||
function selectDefaultGroups(shellKind?: string): CompiledPattern[][] {
|
||||
const groupNames = commandBlocklistTable.shellGroups[
|
||||
String(shellKind ?? '').toLowerCase() as keyof typeof commandBlocklistTable.shellGroups
|
||||
];
|
||||
if (!groupNames) return compiledAllGroups;
|
||||
|
||||
return groupNames.map((name) => compiledGroups[name as keyof typeof compiledGroups]);
|
||||
}
|
||||
|
||||
function checkCommandAgainstGroups(
|
||||
command: string,
|
||||
blocklist: string[],
|
||||
groups: CompiledPattern[][],
|
||||
): { blocked: boolean; matchedPattern?: string } {
|
||||
const enabledPatterns = new Set(blocklist);
|
||||
|
||||
// Settings entries that are not built-in defaults are user patterns and
|
||||
// remain shell-independent.
|
||||
for (const pattern of blocklist) {
|
||||
if (DEFAULT_PATTERN_SET.has(pattern)) continue;
|
||||
const regex = getCompiledPattern(pattern);
|
||||
if (regex && regex.test(command)) {
|
||||
return { blocked: true, matchedPattern: pattern };
|
||||
}
|
||||
}
|
||||
|
||||
// Shell selection narrows the built-in entries that are enabled in the
|
||||
// configured list. It must not restore a default the user removed/edited.
|
||||
for (const group of groups) {
|
||||
for (const { pattern, regex } of group) {
|
||||
if (enabledPatterns.has(pattern) && regex.test(command)) {
|
||||
return { blocked: true, matchedPattern: pattern };
|
||||
}
|
||||
}
|
||||
}
|
||||
return { blocked: false };
|
||||
}
|
||||
|
||||
/** Cache for user-provided (non-default) blocklist patterns. */
|
||||
const userPatternCache = new Map<string, RegExp | null>();
|
||||
|
||||
function getCompiledPattern(pattern: string): RegExp | null {
|
||||
if (userPatternCache.has(pattern)) {
|
||||
return userPatternCache.get(pattern)!;
|
||||
}
|
||||
if (!isSafeRegex(pattern)) {
|
||||
console.warn(`[Safety] Skipping user blocklist pattern with nested quantifiers (ReDoS risk): ${pattern}`);
|
||||
userPatternCache.set(pattern, null);
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const regex = new RegExp(pattern, 'i');
|
||||
userPatternCache.set(pattern, regex);
|
||||
return regex;
|
||||
} catch {
|
||||
userPatternCache.set(pattern, null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a command matches any pattern in the blocklist.
|
||||
* Returns the matching pattern if blocked, null if safe.
|
||||
*
|
||||
* The caller's list remains authoritative. User patterns apply on every shell,
|
||||
* while enabled default patterns are narrowed by shell kind. Unknown shell
|
||||
* kinds fall back to every enabled default group.
|
||||
*
|
||||
* Default blocklist patterns are pre-compiled at module load time.
|
||||
* User-provided patterns are compiled once and cached.
|
||||
*/
|
||||
export function checkCommandSafety(
|
||||
command: string,
|
||||
blocklist: string[] = DEFAULT_COMMAND_BLOCKLIST,
|
||||
shellKind?: string,
|
||||
): { blocked: boolean; matchedPattern?: string } {
|
||||
return checkCommandAgainstGroups(command, blocklist, selectDefaultGroups(shellKind));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply user patterns and enabled shell-independent defaults only. This is the
|
||||
* safe pre-filter for renderer metadata that does not yet know the remote shell;
|
||||
* the live bridge performs the final shell-selected check after probing.
|
||||
*/
|
||||
export function checkCommandSafetyCommonOnly(
|
||||
command: string,
|
||||
blocklist: string[] = DEFAULT_COMMAND_BLOCKLIST,
|
||||
): { blocked: boolean; matchedPattern?: string } {
|
||||
return checkCommandAgainstGroups(command, blocklist, [compiledCommonGroup]);
|
||||
}
|
||||
Reference in New Issue
Block a user