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

Function extractSafeCatHeredoc

source code/utils/bash/ast.ts:1723–1777  ·  view source on GitHub ↗

* Check if a command_substitution node is exactly `$(cat <<'DELIM'...DELIM)` * and return the heredoc body if so. Any deviation (extra args to cat, * unquoted delimiter, additional commands) returns null. * * tree-sitter structure: * command_substitution * $( * redirected_statement

(subNode: Node)

Source from the content-addressed store, hash-verified

1721 * )
1722 */
1723function extractSafeCatHeredoc(subNode: Node): string | 'DANGEROUS' | null {
1724 // Expect exactly: $( + one redirected_statement + )
1725 let stmt: Node | null = null
1726 for (const child of subNode.children) {
1727 if (!child) continue
1728 if (child.type === '$(' || child.type === ')') continue
1729 if (child.type === 'redirected_statement' && stmt === null) {
1730 stmt = child
1731 } else {
1732 return null
1733 }
1734 }
1735 if (!stmt) return null
1736
1737 // redirected_statement must be: command(cat) + heredoc_redirect (quoted)
1738 let sawCat = false
1739 let body: string | null = null
1740 for (const child of stmt.children) {
1741 if (!child) continue
1742 if (child.type === 'command') {
1743 // Must be bare `cat` — no args, no env vars
1744 const cmdChildren = child.children.filter(c => c)
1745 if (cmdChildren.length !== 1) return null
1746 const nameNode = cmdChildren[0]
1747 if (nameNode?.type !== 'command_name' || nameNode.text !== 'cat') {
1748 return null
1749 }
1750 sawCat = true
1751 } else if (child.type === 'heredoc_redirect') {
1752 // Reuse the existing validator: quoted delimiter, body is pure text.
1753 // walkHeredocRedirect returns null on success, non-null on rejection.
1754 if (walkHeredocRedirect(child) !== null) return null
1755 for (const hc of child.children) {
1756 if (hc?.type === 'heredoc_body') body = hc.text
1757 }
1758 } else {
1759 return null
1760 }
1761 }
1762
1763 if (!sawCat || body === null) return null
1764 // SECURITY: the heredoc body becomes the outer command's argv value via
1765 // substitution, so a body like `/proc/self/environ` is semantically
1766 // `cat /proc/self/environ`. checkSemantics never sees the body (we drop it
1767 // at the walkString call site to avoid newline+# FPs). Returning `null`
1768 // here would fall through to collectCommandSubstitution in walkString,
1769 // which would extract the inner `cat` via walkHeredocRedirect (body text
1770 // not inspected there) — effectively bypassing this check. Return a
1771 // distinct sentinel so the caller can reject instead of falling through.
1772 if (PROC_ENVIRON_RE.test(body)) return 'DANGEROUS'
1773 // Same for jq system(): checkSemantics checks argv but never sees the
1774 // heredoc body. Check unconditionally (we don't know the outer command).
1775 if (/\bsystem\s*\(/.test(body)) return 'DANGEROUS'
1776 return body
1777}
1778
1779function walkVariableAssignment(
1780 node: Node,

Callers 1

walkStringFunction · 0.85

Calls 1

walkHeredocRedirectFunction · 0.85

Tested by

no test coverage detected