Skip to content

feat(insights): redact secrets in digests and saved reports - #1339

Merged
edenreich merged 1 commit into
mainfrom
fix/issue-1338
Sep 26, 2026
Merged

edenreich merged 1 commit into
mainfrom
fix/issue-1338

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1338

Summary

infer insights sent verbatim session content to the analysis model and saved it into the report under ~/.infer/insights/ - a failed tool call whose error echoes a credential, or a log line printing one, ended up in the provider request and the saved markdown. The digest and the report now pass through a local redactor before either leaves the process, reusing the patterns infer-action already ships in src/redact.ts.

Changes

  • New internal/insights/redact.go: redact() masks credential shapes and secret env values locally, before any network call. Patterns kept in step with infer-action's redact.ts:
    • Always-on PEM private-key block pattern (-----BEGIN [A-Z ]*PRIVATE KEY( BLOCK)?-----...), covering RSA, DSA, EC, OpenSSH, PKCS#8 and PGP keys.
    • GitHub token shapes: ghp_, gho_, ghu_, ghs_, ghr_ (36+ chars) and github_pat_ (82+ chars).
    • Values of the provider-secret env vars (SECRET_ENV_NAMES: GITHUB_TOKEN, the generated provider API-key list, OTEL_EXPORTER_OTLP_HEADERS, MEMORY_TOKEN, MEMORY_DEPLOY_KEY) are redacted when set in the environment, both plain and JSON-escaped (values shorter than 8 chars are skipped, matching infer-action's default).
  • buildDigest wraps its return in redact(...) - one choke point covering the session intents, the memory index, and the verbatim failure and log samples before analyze makes the model call. Counts and normalized error keys (which are never rendered) are unaffected.
  • renderReport wraps its return in redact(...) - the saved report under ~/.infer/insights/ is redacted the same way, including failure and log samples.
  • Placeholder is the fixed [redacted] (markdown-safe; matches the browser tool's existing redaction convention).
  • Tests in internal/insights/redact_test.go: the required buildDigest test drives the full collect path with a GitHub token echoed in a tool error sample and asserts it is masked (and counts unchanged); a report-rendering test pins failure/log samples; table-driven tests cover the credential shapes and env-value redaction (plain, JSON-escaped, short-value skip).

Docs follow-up: inference-gateway/docs#830 (insights redaction is user-facing; the affected pages are listed there).

Downstream impact: none beyond docs - no schema, config, or env-var changes; only the docs repo needs a follow-up (inference-gateway/docs#830).

Verification

  • go test ./internal/insights/ and full task test pass.
  • task precommit:run passes (mod:tidy, mocks, fmt, golangci-lint 0 issues, markdownlint).

Session content reaching the analysis model and the report under ~/.infer/insights/ now passes a local redactor first: PEM private-key blocks, GitHub token shapes (ghp_, gho_, ghu_, ghs_, ghr_, github_pat_), and the values of provider-secret env vars set in the environment, plain and JSON-escaped. Patterns mirror infer-action's src/redact.ts.
@edenreich

Copy link
Copy Markdown
Contributor

@infer Can you review this? are we making sure the LLM never sees leaked secrets in the flow?

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Model: ollama_cloud/glm-5.3-flash · View Job

Todos

  • Read PR diff and changed files (insights.go, redact.go, redact_test.go)
  • Trace the full insights LLM flow for other secret paths (analyze, memory, title, logs)
  • Compare redaction patterns with infer-action src/redact.ts
  • Verify tests pass and report review findings

Pull Request

#1339

✅ Infer Result: Success

Review: are we making sure the LLM never sees leaked secrets in the flow?

Short answer: yes for the mainline flow - and I verified it end to end - but three residual holes keep it short of "never". Two are one-line fixes.

What I verified as covered

  • The insights flow makes exactly one model call: Generate -> analyze -> llm.Call, which sends a single user message of insightsPrompt + digest (no history, no system content). buildDigest wraps its return in redact(...), so everything the model sees - session intents/titles, the memory index, verbatim failure samples, log samples - is masked locally before the request leaves the process (internal/insights/insights.go:132 and :388).
  • The saved report under ~/.infer/insights/, the stdout print from infer insights, and the TUI shortcut all reuse the same renderReport output, which is also wrapped in redact(...) (insights.go:533). No other writer of session/log content exists in this flow; progress lines carry counts only.
  • Counts (totalErrors, countMemoryFacts) are computed from raw data before redaction, so masking cannot corrupt the numbers - the load-bearing test pins this.
  • Parity with infer-action's src/redact.ts (I fetched and decoded it): SECRET_ENV_NAMES is identical (GITHUB_TOKEN + the 13 generated provider keys + OTEL_EXPORTER_OTLP_HEADERS + MEMORY_TOKEN + MEMORY_DEPLOY_KEY), the PEM always-on pattern is byte-identical, and minLength 8 (trimmed), dedupe, and longest-first ordering all match. The PR is actually stricter than infer-action in two good ways: GitHub token shapes are always-on here (infer-action gates them behind heuristics: true), and the JSON-escaped env-value pass is an addition. Literal strings.ReplaceAll instead of regex-escaping env values is also the safer implementation.
  • go test ./internal/insights/ passes locally.

Findings

  1. Truncation runs before redaction - redact(truncate(b.String(), maxDigestChars)). A secret straddling the 12000-char cut keeps its head, and the head no longer matches the shape (a PEM without its END marker, a token without its full char run), so the partial secret reaches the model. With a busy history (50 sessions + failures + 25 log groups + memory index) the cap is reachable. Swapping the order masks every complete secret before any cutting.
  2. Unterminated PEM heads survive the 200-char sample cut. foldFailures and collectLogs truncate every sample via oneLine(..., 200) before buildDigest/renderReport see them. A PEM echoed in a tool error or log line loses its -----END ... ----- marker at that cut, so the always-on regex cannot match and roughly 165 chars of base64 key material survive redaction into both the digest (to the model) and the report (to disk). Adding an unterminated-block alternative after the full-block arm closes this.
  3. Shape coverage is below the "never" bar. infer-action's heuristic patterns (JWT eyJ..., Google AIza..., Slack xox[boas]-, generic sk-) are not adopted, and env-value masking only helps when the leaked secret is one of the listed vars set in this process. A 401 from a third-party API echoing Bearer eyJ... or a sk- key not in the env list passes through. Since insights input is exactly the freeform failure text those patterns were written for, consider promoting them to always-on here; otherwise the docs follow-up ([DOCS] Document secret redaction in infer insights reports docs#830) should state the coverage so "never" is calibrated.

Minor notes (no action required)

  • jsonEscape uses strconv.Quote, which does not reproduce encoding/json's HTML escaping of &, <, > (\u0026 etc.), so a JSON-encoded log line containing those chars inside a secret will not match. Fine for typical alnum API keys.
  • AWS access keys (AKIA...) are not covered in either repo - parity, not a regression.
  • Scope: this PR covers the insights flow only. The main chat loop necessarily sends raw tool output to the model to function; extending env-value masking to outbound tool results there would be a separate, riskier change and deserves its own discussion if you want it.

Model: ollama_cloud/glm-5.3-flash · Exit Code: 0 · Duration: 2m 49s

Tokens: 352,631 in · 21,715 out · 374,346 total (8 requests)

Tool calls: 14 total · 100% success rate

Traces
Session: 1790439335-385316b0

session (headless, success)                                                                        2m41s
├── chat ollama_cloud/glm-5.3-flash                                                                 3.6s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       3.5s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     3.5s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             3.5s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 3.5s
├── execute_tool TodoWrite call_xp60j76w                                                            65µs
├── execute_tool Bash call_nsw68ne8                                                                  3ms
├── chat ollama_cloud/glm-5.3-flash                                                                 1.3s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.3s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.3s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             1.3s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.3s
├── execute_tool Bash call_ybums8qa                                                                  3ms
├── execute_tool Read call_7pjnvhai                                                                114µs
├── chat ollama_cloud/glm-5.3-flash                                                                 1.7s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       1.7s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     1.7s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             1.7s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 1.7s
├── execute_tool Read call_5fykr2wp                                                                225µs
├── execute_tool Read call_fw7h1q65                                                                172µs
├── chat ollama_cloud/glm-5.3-flash                                                                27.3s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      27.3s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    27.3s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            27.3s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                27.3s
├── execute_tool Read call_l3345vap                                                                310µs
├── execute_tool Read call_l6x8co8r                                                                235µs
├── execute_tool Bash call_gqk7ig0e                                                                366ms
├── chat ollama_cloud/glm-5.3-flash                                                                37.9s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      37.9s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    37.9s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            37.9s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                37.9s
├── execute_tool TodoWrite call_98y0tkp8                                                            55µs
├── execute_tool Bash call_0pjngu1f                                                                41.8s
├── execute_tool Grep call_6uoz0jam                                                                684µs
├── chat ollama_cloud/glm-5.3-flash                                                                23.5s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      23.5s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    23.5s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            23.5s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                23.5s
├── execute_tool Memory call_ov3prma0                                                               1.3s
├── chat ollama_cloud/glm-5.3-flash                                                                 3.7s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       3.7s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     3.7s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             3.7s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 3.7s
├── execute_tool TodoWrite call_xgrlgm9f                                                            56µs
╰── chat ollama_cloud/glm-5.3-flash                                                                16.7s
    ╰── POST /v1/chat/completions [inference-gateway, server]                                      16.7s
        ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    16.7s
            ╰── POST /proxy/:provider/*path [inference-gateway, server]                            16.7s
                ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                16.7s
Stats
Tool Calls

╭───────────┬───────┬───────┬─────────╮
│ Tool      │ Calls │ Fail% │ Avg     │
├───────────┼───────┼───────┼─────────┤
│ Read      │ 5     │ 0%    │ 217µs   │
│ Bash      │ 4     │ 0%    │ 10548ms │
│ TodoWrite │ 3     │ 0%    │ 54µs    │
│ Grep      │ 1     │ 0%    │ 700µs   │
│ Memory    │ 1     │ 0%    │ 1270ms  │
╰───────────┴───────┴───────┴─────────╯

Token Usage

╭────────────────────────────┬────────┬────────┬────────────┬────────┬──────╮
│ Model                      │ Prompt │ Cached │ Completion │ Total  │ Cost │
├────────────────────────────┼────────┼────────┼────────────┼────────┼──────┤
│ ollama_cloud/glm-5.3-flash │ 352631 │ 210560 │ 21715      │ 374346 │ -    │
╰────────────────────────────┴────────┴────────┴────────────┴────────┴──────╯

Sessions

╭───────────┬──────────┬──────────╮
│ Execution │ Mode     │ Sessions │
├───────────┼──────────┼──────────┤
│ headless  │ headless │ 1        │
╰───────────┴──────────┴──────────╯

Triggered by edenreich · Infer Action · OSS

@edenreich
edenreich merged commit 642cba1 into main Sep 26, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-1338 branch September 26, 2026 16:20
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.210.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Redact secrets in infer insights

1 participant