MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseForSecurityFromAst

Function parseForSecurityFromAst

source code/utils/bash/ast.ts:402–462  ·  view source on GitHub ↗
(
  cmd: string,
  root: Node | typeof PARSE_ABORTED,
)

Source from the content-addressed store, hash-verified

400 * successful parse doesn't.
401 */
402export function parseForSecurityFromAst(
403 cmd: string,
404 root: Node | typeof PARSE_ABORTED,
405): ParseForSecurityResult {
406 // Pre-checks: characters that cause tree-sitter and bash to disagree on
407 // word boundaries. These run before tree-sitter because they're the known
408 // tree-sitter/bash differentials. Everything after this point trusts
409 // tree-sitter's tokenization.
410 if (CONTROL_CHAR_RE.test(cmd)) {
411 return { kind: 'too-complex', reason: 'Contains control characters' }
412 }
413 if (UNICODE_WHITESPACE_RE.test(cmd)) {
414 return { kind: 'too-complex', reason: 'Contains Unicode whitespace' }
415 }
416 if (BACKSLASH_WHITESPACE_RE.test(cmd)) {
417 return {
418 kind: 'too-complex',
419 reason: 'Contains backslash-escaped whitespace',
420 }
421 }
422 if (ZSH_TILDE_BRACKET_RE.test(cmd)) {
423 return {
424 kind: 'too-complex',
425 reason: 'Contains zsh ~[ dynamic directory syntax',
426 }
427 }
428 if (ZSH_EQUALS_EXPANSION_RE.test(cmd)) {
429 return {
430 kind: 'too-complex',
431 reason: 'Contains zsh =cmd equals expansion',
432 }
433 }
434 if (BRACE_WITH_QUOTE_RE.test(maskBracesInQuotedContexts(cmd))) {
435 return {
436 kind: 'too-complex',
437 reason: 'Contains brace with quote character (expansion obfuscation)',
438 }
439 }
440
441 const trimmed = cmd.trim()
442 if (trimmed === '') {
443 return { kind: 'simple', commands: [] }
444 }
445
446 if (root === PARSE_ABORTED) {
447 // SECURITY: module loaded but parse aborted (timeout / node budget /
448 // panic). Adversarially triggerable — `(( a[0][0]... ))` with ~2800
449 // subscripts hits PARSE_TIMEOUT_MICROS under the 10K length limit.
450 // Previously indistinguishable from module-not-loaded → routed to
451 // legacy (parse-unavailable), which lacks EVAL_LIKE_BUILTINS — `trap`,
452 // `enable`, `hash` leaked with Bash(*). Fail closed: too-complex → ask.
453 return {
454 kind: 'too-complex',
455 reason:
456 'Parser aborted (timeout or resource limit) — possible adversarial input',
457 nodeType: 'PARSE_ABORT',
458 }
459 }

Callers 2

bashToolHasPermissionFunction · 0.85
parseForSecurityFunction · 0.85

Calls 2

walkProgramFunction · 0.85

Tested by

no test coverage detected