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

Function walkString

source code/utils/bash/ast.ts:1510–1654  ·  view source on GitHub ↗

* Extract literal content from a double-quoted string node. A `string` node's * children are `"` delimiters, `string_content` literals, and possibly * expansion nodes. * * tree-sitter quirk: literal newlines inside double quotes are NOT included * in `string_content` node text. bash preserves t

(
  node: Node,
  innerCommands: SimpleCommand[],
  varScope: Map<string, string>,
)

Source from the content-addressed store, hash-verified

1508 * match what bash actually sees.
1509 */
1510function walkString(
1511 node: Node,
1512 innerCommands: SimpleCommand[],
1513 varScope: Map<string, string>,
1514): string | ParseForSecurityResult {
1515 let result = ''
1516 let cursor = -1
1517 // SECURITY: Track whether the string contains a runtime-unknown
1518 // placeholder ($() output or unknown-value tracked var) vs any literal
1519 // content. A string that is ONLY a placeholder (`"$(cmd)"`, `"$VAR"`
1520 // where VAR holds an unknown sentinel) produces an argv element that IS
1521 // the placeholder — which downstream path validation resolves as a
1522 // relative filename within cwd, bypassing the check. `cd "$(echo /etc)"`
1523 // would pass validation but runtime-cd into /etc. We reject
1524 // solo-placeholder strings; placeholders mixed with literal content
1525 // (`"prefix: $(cmd)"`) are safe — runtime value can't equal a bare path.
1526 let sawDynamicPlaceholder = false
1527 let sawLiteralContent = false
1528 for (const child of node.children) {
1529 if (!child) continue
1530 // Index gap between this child and the previous one = dropped newline(s).
1531 // Ignore the gap before the first non-delimiter child (cursor === -1).
1532 // Skip gap-fill for `"` delimiters: a gap before the closing `"` is the
1533 // tree-sitter whitespace-only-string quirk (space/tab, not newline) — let
1534 // the Fix C check below catch it as too-complex instead of mis-filling
1535 // with `\n` and diverging from bash.
1536 if (cursor !== -1 && child.startIndex > cursor && child.type !== '"') {
1537 result += '\n'.repeat(child.startIndex - cursor)
1538 sawLiteralContent = true
1539 }
1540 cursor = child.endIndex
1541 switch (child.type) {
1542 case '"':
1543 // Reset cursor after opening quote so the gap between `"` and the
1544 // first content child is captured.
1545 cursor = child.endIndex
1546 break
1547 case 'string_content':
1548 // Bash double-quote escape rules (NOT the generic /\\(.)/g used for
1549 // unquoted words in walkArgument): inside "...", a backslash only
1550 // escapes $ ` " \ — other sequences like \n stay literal. So
1551 // `"fix \"bug\""` → `fix "bug"`, but `"a\nb"` → `a\nb` (backslash
1552 // kept). tree-sitter preserves the raw escapes in .text; we resolve
1553 // them here so argv matches what bash actually passes.
1554 result += child.text.replace(/\\([$`"\\])/g, '$1')
1555 sawLiteralContent = true
1556 break
1557 case DOLLAR:
1558 // A bare dollar sign before closing quote or a non-name char is
1559 // literal in bash. tree-sitter emits it as a standalone node.
1560 result += DOLLAR
1561 sawLiteralContent = true
1562 break
1563 case 'command_substitution': {
1564 // Carve-out: `$(cat <<'EOF' ... EOF)` is safe. The quoted-delimiter
1565 // heredoc body is literal (no expansion), and `cat` just prints it.
1566 // The substitution result is therefore a known static string. This
1567 // pattern is the idiomatic way to pass multi-line content to tools

Callers 2

walkFileRedirectFunction · 0.85
walkArgumentFunction · 0.85

Calls 5

extractSafeCatHeredocFunction · 0.85
tooComplexFunction · 0.85
resolveSimpleExpansionFunction · 0.85
walkArithmeticFunction · 0.85

Tested by

no test coverage detected