WrenAI icon indicating copy to clipboard operation
WrenAI copied to clipboard

fix(profile): mask secrets nested under kwargs and settings in profile debug

Open AnnaSuSu opened this issue 4 months ago • 1 comments

Summary

  • wren profile debug masks only top-level keys. Secrets nested under kwargs / settings — a shape this module explicitly supports — are printed in plaintext, while the top-level password right next to them shows ***.
  • Fix: walk nested dicts and lists when masking, mirroring _expand_obj, which already walks them.

Motivation

expand_profile_secrets documents nested secrets as a supported shape:

"Only string values are substituted; integers, booleans, lists, and nested dicts are preserved (lists and dicts are walked recursively so kwargs: {password: ${PG_PW}} works)."

Expansion honours that — _expand_obj recurses through dicts and lists. Masking does not:

for k, v in profile.items():        # top level only
    masked[k] = "***" if sensitive(k) else v   # nested dict passes through whole

So the two halves of the same feature disagree: expansion recurses, masking doesn't.

The declared contract says these must be masked:

  • _registry_sensitive_keys docstring — "over-masking a benign key in a diagnostic dump is harmless, under-masking a secret is not."
  • CLI help (wren profile debug) — "Show resolved profile config (sensitive fields masked)."

Reachability: debug is what the CLI points users to after a connection failure, and its output is what gets pasted into bug reports. With kwargs: {password: ...} — the exact form the docstring advertises — that paste contains the password:

"password": "***"                                  ← top-level, masked
"kwargs":   { "password": "NESTED_SECRET" }        ← nested, plaintext
"settings": { "token": "NESTED_TOKEN" }            ← nested, plaintext

This is the sibling site my own #2492 missed: that PR fixed which keys count as sensitive and I scoped it to the predicate, never asking whether the guard's shape matched the function's semantics. The predicate was right; the traversal depth was not.

Fix

Add _mask_obj, mirroring the existing _expand_obj shape (dict / list / scalar) rather than inventing a new structure. The sensitivity predicate itself is unchanged — only traversal depth is. Nothing that was masked before becomes unmasked.

Scope: I grepped every surface that prints profile fields and drove each one. wren profile list prints only name + datasource (no leak). debug is the only leaking exit. (wren profile show, listed in .claude/CLAUDE.md, does not actually exist as a command — out of scope here.)

Verification

  • New regression tests in tests/unit/test_profile_env_expansion.py (covered by the CI unit job):
    • test_debug_masks_nested_sensitive_fields — nested password, registry-sensitive connection_url, arbitrary depth (settings.deep.token), and dicts inside lists.
      • Without the change: assert 'NESTED' == '***' — RED. With it: PASS.
    • test_debug_leaves_nested_benign_fields_intact — reverse anchor: kwargs.sslmode / connect_timeout must survive.
      • Verified load-bearing: replacing the fix with a blanket k: "***" reds it.
  • Existing tests not hollowed out: neutralising the sensitivity predicate reds test_debug_profile_returns_literal_placeholder, test_debug_masks_registry_sensitive_fields and tests/test_profile.py::test_debug_masks_sensitive — they still guard top-level masking.
  • Full unit suite vs clean main, verbatim: 967 → 969 passed, 2 skipped (+2 = exactly the new tests; 0 new failures). Lint clean (just lint).
  • End-to-end via the real CLI: wren profile debug pg masks kwargs.password and settings.token to *** while kwargs.sslmode stays require; plaintext-secret occurrences in the output: 0. The same profile on unpatched main prints the secret.

License

core/** is Apache-2.0 per the repo LICENSE path map — this contribution is within an Apache-2.0 path.

Summary by CodeRabbit

  • Bug Fixes

    • Improved profile diagnostics to recursively mask sensitive values across nested configuration, including deeply nested kwargs/settings and values inside lists.
    • Updated masking logic to correctly identify sensitive fields while leaving non-sensitive details unchanged.
  • Tests

    • Expanded coverage to verify recursive masking at multiple depths, ensure safe fields aren’t over-masked, and confirm robustness when profile/YAML keys aren’t strings.

AnnaSuSu avatar Jul 17 '26 06:07 AnnaSuSu

Review Change Stack

Walkthrough

debug_profile now recursively masks sensitive values in nested dictionaries and lists, using registry-based and heuristic key detection. Tests cover deeply nested secrets, benign values, and non-string keys.

Changes

Profile masking

Layer / File(s) Summary
Recursive masking implementation
core/wren/src/wren/profile.py
Adds recursive masking for nested profile structures and combines registry-based and heuristic sensitivity detection.
Nested masking validation
core/wren/tests/unit/test_profile_env_expansion.py
Tests nested secret masking, preservation of benign values, and resilience to non-string keys.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant debug_profile
  participant ConnectionFieldRegistry
  participant _mask_obj
  debug_profile->>ConnectionFieldRegistry: Read sensitive connection keys
  debug_profile->>_mask_obj: Mask the complete profile
  _mask_obj-->>debug_profile: Return recursively masked profile

Possibly related PRs

Poem

I’m a rabbit guarding keys in the night,
Nesting through settings, hiding secrets from sight.
Deep in lists, the stars now gleam,
While harmless fields stay clear as a dream.
Hop, hop—safe profiles beam!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: recursively masking nested secrets in profile debug output.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 17 '26 06:07 coderabbitai[bot]