@@ -1545,39 +1545,83 @@ def run_codex_exec(
15451545)
15461546_COPILOT_SECRET_KEY_EXACT = {"pwd" , "sig" , "authorization" , "bearer" }
15471547
1548- # Matching is two-level: the regex only ENGAGES quoted keys containing a
1549- # secret seed (so it never swallows a non-secret object like ``"a": {...}``),
1550- # and the callback decides with the project's endswith-based rule so
1551- # token_count / token_budget / secret_version diagnostics are NOT scrubbed.
1552- # ponytail: the object branch is single-level only; deep-nested values under a
1553- # secret key in non-JSON text are not stripped (valid-JSON lines already go
1554- # through the structural walker). Add an unbounded nest parser if that ever
1555- # appears in real stderr.
1556- _COPILOT_QUOTED_JSON_KEY = re .compile (
1557- r'(?i)"([^"\\]*(?:token|apikey|api[_-]?key|secret|password|authorization|'
1558- r'bearer|cookie|setcookie)[^"\\]*)"\s*:\s*(?P<val>'
1559- r'"(?:\\.|[^"\\])*"' # JSON string value
1560- r'|\[[^\]]*\]' # JSON array literal
1561- r'|\{[^{}]*\}' # JSON object literal (single level)
1562- r'|[^\s,]+' # bare token / number / bool
1563- r')'
1564- )
1548+ def _is_copilot_secret_key (field : str ) -> bool :
1549+ """The single mapping-aware secret-key policy for the Copilot path.
15651550
1551+ Uses endswith on the compacted key (mirroring ``_is_secret_mapping_key``), so
1552+ ``token`` / ``api_key`` / ``refreshToken`` / ``bearer`` / ``cookie`` are
1553+ redacted, but ``token_count`` / ``token_budget`` / ``secret_version``
1554+ diagnostics are preserved.
1555+ """
1556+ compact = re .sub (r"[^a-z0-9]" , "" , (field or "" ).casefold ())
1557+ return compact in _COPILOT_SECRET_KEY_EXACT or compact .endswith (_COPILOT_SECRET_KEY_SUFFIXES )
15661558
1567- def _redact_quoted_json_pair (match : re .Match ) -> str :
1568- key = match .group (1 )
1569- compact = re .sub (r"[^a-z0-9]" , "" , key .strip ().casefold ())
1570- if not (
1571- compact in _COPILOT_SECRET_KEY_EXACT
1572- or compact .endswith (_COPILOT_SECRET_KEY_SUFFIXES )
1573- ):
1574- return match .group (0 )
1575- return f'{ match .group (1 )} : "[REDACTED]"'
1559+
1560+ def _find_json_end (text : str , start : int ) -> int | None :
1561+ """Bracket-match a JSON object/array starting at ``start`` (unbounded nesting).
1562+
1563+ String-aware (handles quotes and escapes), so a ``{`` inside a string value
1564+ does not confuse the matching.
1565+ """
1566+ open_ch = text [start ]
1567+ close_ch = "}" if open_ch == "{" else "]"
1568+ depth = 0
1569+ in_str = False
1570+ escaped = False
1571+ for k in range (start , len (text )):
1572+ ch = text [k ]
1573+ if in_str :
1574+ if escaped :
1575+ escaped = False
1576+ elif ch == "\\ " :
1577+ escaped = True
1578+ elif ch == '"' :
1579+ in_str = False
1580+ else :
1581+ if ch == '"' :
1582+ in_str = True
1583+ elif ch == open_ch :
1584+ depth += 1
1585+ elif ch == close_ch :
1586+ depth -= 1
1587+ if depth == 0 :
1588+ return k
1589+ return None
1590+
1591+
1592+ def _redact_embedded_json (text : str ) -> str :
1593+ """Structurally redact JSON objects/arrays embedded in plain text.
1594+
1595+ Finds balanced JSON fragments (unbounded nesting, string-aware) and walks
1596+ each with the mapping-aware redactor, so deeply nested or pretty-printed
1597+ JSON embedded in a non-JSON line no longer leaks. Non-JSON text is preserved.
1598+ """
1599+ out : list [str ] = []
1600+ i = 0
1601+ n = len (text )
1602+ while i < n :
1603+ ch = text [i ]
1604+ if ch in "{[" :
1605+ end = _find_json_end (text , i )
1606+ if end is not None :
1607+ frag = text [i :end + 1 ]
1608+ try :
1609+ obj = json .loads (frag )
1610+ except (ValueError , TypeError ):
1611+ out .append (ch )
1612+ i += 1
1613+ continue
1614+ out .append (json .dumps (_redact_copilot_json (obj ), ensure_ascii = False ))
1615+ i = end + 1
1616+ continue
1617+ out .append (ch )
1618+ i += 1
1619+ return "" .join (out )
15761620
15771621
15781622def _redact_cursor_error (value : str ) -> str :
15791623 text = value or ""
1580- text = _COPILOT_QUOTED_JSON_KEY . sub ( _redact_quoted_json_pair , text )
1624+ text = _redact_embedded_json ( text )
15811625 text = _CURSOR_SECRET_ASSIGNMENT .sub (r"\1\2[REDACTED]" , text )
15821626 return _CURSOR_SECRET_TOKEN .sub ("[REDACTED]" , text )
15831627
@@ -1609,12 +1653,10 @@ def _redact_copilot_json(value: Any, *, field: str = "") -> Any:
16091653 Unlike the cursor trace sanitizer, this does NOT omit ``content``/``prompt``
16101654 (those are the CLI output we want to keep debuggable); it redacts by secret
16111655 field name and applies the string-level redactor to remaining string leaves.
1656+ Uses the SAME key policy as the embedded-JSON fallback so valid JSON and
1657+ non-JSON fragments agree on what a secret field is.
16121658 """
1613- normalized_field = re .sub (r"[^a-z0-9]" , "" , field .lower ())
1614- if (
1615- normalized_field in _CURSOR_SECRET_TRACE_FIELDS
1616- or normalized_field .endswith ("apikey" )
1617- ):
1659+ if _is_copilot_secret_key (field ):
16181660 return "[REDACTED]"
16191661 if isinstance (value , dict ):
16201662 return {
@@ -1629,28 +1671,16 @@ def _redact_copilot_json(value: Any, *, field: str = "") -> Any:
16291671
16301672
16311673def _redact_copilot_trace (raw : str | bytes ) -> str :
1632- """Structurally sanitize Copilot JSONL output (mapping-key aware).
1674+ """Sanitize Copilot JSONL output (mapping-key aware, unbounded nesting ).
16331675
1634- ``_redact_cursor_error`` catches unquoted ``key=value`` forms, but the
1635- Copilot JSONL stream carries ``"token": "..."`` objects. Parse each line
1636- and walk it with the field-name-aware sanitizer; non-JSON lines still fall
1637- through the string-level redactor.
1676+ The whole text is scanned for JSON objects/arrays (single-line, multiple
1677+ fragments, or pretty-printed / deeply nested) and each is walked with the
1678+ mapping-aware redactor; remaining non-JSON text gets string-level redaction
1679+ for ``key=value`` and token patterns. This replaces the old line-by-line
1680+ regex, which only handled single-level object values.
16381681 """
16391682 text = _cursor_process_text (raw )
1640- lines_out : list [str ] = []
1641- for line in text .splitlines ():
1642- stripped = line .strip ()
1643- if not stripped :
1644- lines_out .append (line )
1645- continue
1646- try :
1647- obj = json .loads (stripped )
1648- except (ValueError , TypeError ):
1649- lines_out .append (_redact_cursor_error (line ))
1650- continue
1651- obj = _redact_copilot_json (obj )
1652- lines_out .append (json .dumps (obj , ensure_ascii = False ))
1653- return "\n " .join (lines_out )
1683+ return _redact_cursor_error (text )
16541684
16551685
16561686def _cursor_process_text (value : str | bytes ) -> str :
0 commit comments