MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / maskBracesInQuotedContexts

Function maskBracesInQuotedContexts

src/utils/bash/ast.ts:332–372  ·  view source on GitHub ↗

* Mask `{` characters that appear inside single- or double-quoted contexts. * Uses a single-pass bash-aware quote-state scanner instead of a regex. * * A naive regex (`/'[^']*'/g`) mis-detects spans when a `'` appears inside * a double-quoted string: for `echo "it's" {a'}',b}`, it matches from t

(cmd: string)

Source from the content-addressed store, hash-verified

330 * either is safe. Secondary defense: BRACE_EXPANSION_RE in walkArgument.
331 */
332function maskBracesInQuotedContexts(cmd: string): string {
333 // Fast path: no `{` → nothing to mask. Skips the char-by-char scan for
334 // the >90% of commands with no braces (`ls -la`, `git status`, etc).
335 if (!cmd.includes('{')) return cmd
336 const out: string[] = []
337 let inSingle = false
338 let inDouble = false
339 let i = 0
340 while (i < cmd.length) {
341 const c = cmd[i]!
342 if (inSingle) {
343 // Bash single quotes: no escapes, `'` always terminates.
344 if (c === "'") inSingle = false
345 out.push(c === '{' ? ' ' : c)
346 i++
347 } else if (inDouble) {
348 // Bash double quotes: `\` escapes `"` and `\` (also `$`, backtick,
349 // newline — but those don't affect quote state so we let them pass).
350 if (c === '\\' && (cmd[i + 1] === '"' || cmd[i + 1] === '\\')) {
351 out.push(c, cmd[i + 1]!)
352 i += 2
353 } else {
354 if (c === '"') inDouble = false
355 out.push(c === '{' ? ' ' : c)
356 i++
357 }
358 } else {
359 // Unquoted: `\` escapes any next char.
360 if (c === '\\' && i + 1 < cmd.length) {
361 out.push(c, cmd[i + 1]!)
362 i += 2
363 } else {
364 if (c === "'") inSingle = true
365 else if (c === '"') inDouble = true
366 out.push(c)
367 i++
368 }
369 }
370 }
371 return out.join('')
372}
373
374const DOLLAR = String.fromCharCode(0x24)
375

Callers 1

parseForSecurityFromAstFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected