Skip to content

feat(gateway): seal registered credentials and unseal client tool calls - #7340

Closed
t0saki wants to merge 13 commits into
volcengine:mainfrom
t0saki:feat/gateway-secret-seal
Closed

t0saki wants to merge 13 commits into
volcengine:mainfrom
t0saki:feat/gateway-secret-seal

Conversation

@t0saki

@t0saki t0saki commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Description

Adds credential sealing to the OpenViking Gateway, as designed in the RFC at #7335 (sections 5 and 6).

Today a credential that reaches the conversation is resent to the model provider on every later turn. It gets there through privacy_resolve, a pasted key, tool output such as cat .env, or recalled content. With seal_secrets on, the gateway replaces every registered credential value with its existing privacy reference before anything is sent upstream, so the provider only ever sees references. With unseal_tool_calls also on, references the model writes into client tool-call arguments are replaced with the real values in the copy sent to the client, which makes references usable without a harness plugin.

Both switches are off by default, including for new profiles. With them off, request bytes and behaviour are unchanged.

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

Related Issue

RFC: #7335. Related: #7213.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • openviking_gateway/secret_guard.py: the per-user catalog (value ↔ reference), the protocol-agnostic seal function and the unseal function.
    • The catalog is read with the caller's own OpenViking key from the existing Privacy API, refreshed at most every 30 seconds, and only re-fetches targets whose version changed. It holds credential values only: current version, top-level string fields, at least 6 characters.
    • Sealing walks the string values of the request JSON with longest-first replacement, and also matches the common JSON-escaped forms of each value. Known references are registered as themselves so a value that happens to appear inside a reference cannot corrupt it.
    • Signed thinking / redacted_thinking / reasoning blocks, signature and encrypted_content fields, and data: URIs are left alone. Tool-call arguments and tool results are never exempt.
  • Request path: one inbound seal after routing and before the kernel, plus a final sweep at each of the three places that send to the model (main request, compaction summary, hidden continuation). The kernel takes an optional seal function so recall text and gateway tool results are sealed before they are written to local records. Sweep hits are logged as secrets_sealed_egress.
  • Reply path: with unseal_tool_calls on, requests that carry tool definitions go through the tool loop, and references in client tool-call arguments are unsealed in the client copy only. Anchors and captured content stay in reference form. Only exact references to the caller's own credential values are unsealed; assistant text and thinking never are.
  • Failure handling is fail-closed: 503 when the catalog cannot be read and no copy younger than 600 seconds is held (this includes OpenViking Server older than 0.5.2, which lacks the credential listing), 400 for a JSON body the gateway cannot parse safely, 415 for a compressed body. A user with no registered credentials passes through untouched.
  • Policy: two new Context Profile fields, seal_secrets and unseal_tool_calls. They cannot be overridden by X-OpenViking-Context and are read from the live profile rather than frozen at session creation.
  • Other: privacy_resolve is dropped from the gateway tools while seal_secrets is on; the session preamble explains reference usage when unseal_tool_calls is on; request logs gain seal / unseal counters and never record values or references.
  • Studio: the Profile editor gets both switches. Without this, saving a profile in Studio would silently reset them.
  • Docs: gateway and API docs in English and Chinese, and the RFC document with its figures under bot/docs/zh/design/.

Testing

  • I have added tests that prove my fix is effective or that my feature works

  • New and existing unit tests pass locally with my changes

  • I have tested this on the following platforms:

    • Linux
    • macOS
    • Windows
  • pytest tests/gateway: 1176 passed, 89 skipped, 0 failed. The skips are tests that need a Redis server or the mcp package, neither of which is on this machine. The new tests/gateway/test_secret_guard.py accounts for 39 of the passes.

  • ruff format --check and ruff check on openviking_gateway and tests/gateway: clean.

  • Studio vitest run: 1920 passed, 2 failed. Both failures are in files this PR does not touch: script/architecture.test.ts reports routes/arranger/*, and server-configuration/lib/config-file-api.test.ts expects an English string on a machine with a Chinese locale.

  • Unsealing has only been exercised against a simulated upstream. It has not been run end to end with a real client.

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

None.

Additional Notes

  • This branch carries the RFC document commits as its base, so the diff includes bot/docs/zh/design/rfc-agent-secret-use.md and five figures. The plugin PR carries the same commits.
  • Known limits are listed in the RFC (section 8). The main ones: only literal values and their JSON escapes are matched, so Base64 or sliced values pass through; values rotated out are no longer matched; the skill category is not covered.
  • This targets accidental disclosure. A malicious model or prompt injection that deliberately exfiltrates a credential is out of scope.

t0saki and others added 11 commits November 10, 2026 04:24
Draft RFC for keeping registered credentials out of model upstreams while
agents can still use them: seal values into privacy references on the wire
(gateway), unseal only at the tool execution boundary (harness runner).
The hook gives no permission decision unless secretExecApproval is ask, and
never returns allow.
Lead with a one-minute summary and a running example, explain terms on
first use, and replace the mermaid sketch with five SVG figures. Move the
RFC next to the existing one under bot/docs/zh/design.
…ew findings

Wrapping a command in the runner hides it from Claude Code's permission
rules, so the hook now prepends never-executed guard lines that expose
each original segment to rule matching (verified on Claude Code 2.1.321).
Also corrects overstated claims found by cold readers, defines gateway
terms on first use, and redraws the figures accordingly.
…ttled

Gateway: its own additions (recall, tool results) are sealed before they
are stored, with the egress sweep as the second line; unsealing needs a
tools-bearing request, the canonical spelling and a value of 6+
characters; exemptions and JSON spellings as implemented; server 0.5.2.

Plugin: the hook also asks when a command names the runner itself, the
runner line is written so a rule for it is easy, and the guard lines were
verified end to end on Claude Code 2.1.321 (scenario table in section 11).
Lead with goals, non-goals, the three switches and the behaviour changes they bring. Gateway and plugin internals follow as detailed design, and the Claude Code guard line, the harness hook comparison and the acceptance notes move to appendices.

Settle the open questions: privacy_resolve stays as is on the server with a recommendation to stop exposing it, new profiles keep seal_secrets off, and the skill category stays out of sealing with the consequence listed under known limitations. The two laws become invariants I1 and I2, in the text and in the architecture figure.
Credentials registered in OpenViking Privacy reached model providers as
soon as they entered a conversation: pasted by the user, printed by a
tool, recalled from memory, or returned by the default privacy_resolve
gateway tool.

With the new seal_secrets profile switch the gateway replaces every
registered value with its owner-bound reference before the kernel reads
the request, and again right before each upstream send (main request,
compaction summary, hidden tool continuation). Anchors, replay records
and capture therefore stay in reference form and the upstream never
sees a registered value. unseal_tool_calls also puts the real values
back into the tool calls the client runs, on the client's copy only,
by routing the reply through the hidden tool loop.

secret_guard.py holds the logic: the reference grammar, a per-user
catalog read with the caller's own OpenViking key (30 s refresh, only
changed targets, 600 s stale fallback, 503 without one), the
position-aware seal walk and the client-call unseal. Both switches are
read from the live profile and cannot be set by X-OpenViking-Context;
privacy_resolve is not offered while sealing. Studio gets a "Seal
credentials" section so saves keep the fields, and the gateway docs
describe the switches and what providers see.

Implements docs/repository/rfcs/agent-secret-use.md sections 5 and 6.
- Exempt only a string that is entirely a data: URI, and nothing a tool read
  or wrote (tool_use, tool_result, role=tool, function_call_output,
  custom_tool_call_output). Text that merely began with a data URI, tool
  results included, went upstream unsealed.
- Seal recall before the kernel records it and gateway tool results before
  the loop keeps them, so replay records hold references only. Continuations
  start from the body sealed before the first send, so secrets_sealed_egress
  no longer counts the same recall once per hidden round.
- Refuse with 400 JSON that only json.loads can read on every path, not only
  on protocol paths, and read no credentials for a request without a body.
- Seal the ASCII-only, \/ and Go HTML-safe JSON spellings of each value.
- Unseal only known references spelled exactly as rendered, so a resent call
  seals back to the bytes the gateway recorded; Chat calls without a type
  unseal too.
- Log why a credential read failed: servers before 0.5.2 have no tree.
- Docs: v-pre around {{...}} for the VitePress build, the 0.5.2 requirement,
  privacy_resolve in earlier conversations, the 6-character unseal minimum,
  and what local storage holds.
Tool results are sealed as they arrive, so a continuation normally holds
no value by the time it is sent. The send is still the last place one can
be caught, and the RFC's claim is that every body that goes upstream is
sealed on the way out. Sealing is idempotent, so the extra pass counts
nothing unless a path did skip it.
@t0saki
t0saki marked this pull request as ready for review November 11, 2026 11:33
Copilot AI balanced review requested due to automatic review settings November 11, 2026 11:33
# Conflicts:
#	docs/en/gateway/08-context-profiles.md
#	docs/zh/gateway/08-context-profiles.md
#	openviking_gateway/kernel.py
#	openviking_gateway/proxy.py
#	web-studio/src/features/admin/gateway/lib/profile-schema.test.ts

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical gaps in secret refresh, custom-tool argument sealing, and compaction/window-reset handling can expose credentials upstream or persist plaintext secrets.

3 open issues
What changed in this PR

Adds opt-in credential sealing to the Gateway’s Privacy integration, replacing registered values with references before model requests and optionally restoring values in client tool calls.

Changes:

  • Adds per-user credential catalogs, request sealing, client-call unsealing, failure handling, and counters.
  • Exposes live profile switches through the API and Studio, both disabled by default.
  • Adds gateway and Studio tests, bilingual documentation, and the supporting RFC diagrams.
File Description
web-studio/​src/​i18n/​locales/​zh-CN/​gateway/​profiles.ts Adds Chinese credential-setting labels.
web-studio/​src/​i18n/​locales/​en/​gateway/​profiles.ts Adds English credential-setting labels.
web-studio/​src/​features/​admin/​gateway/​lib/​profile-schema.ts Defaults both switches to off.
web-studio/​src/​features/​admin/​gateway/​lib/​profile-schema.test.ts Tests preservation of credential settings.
web-studio/​src/​features/​admin/​gateway/​lib/​client-guides.ts Links to credential-sealing documentation.
web-studio/​src/​features/​admin/​gateway/​lib/​api.ts Adds credential switches to profile types.
web-studio/​src/​features/​admin/​gateway/​components/​profiles-settings.tsx Adds sealing and unsealing controls.
web-studio/​src/​features/​admin/​gateway/​components/​profiles-pages.test.tsx Tests controls, summaries, and saving.
web-studio/​src/​features/​admin/​gateway/​components/​profiles-page.tsx Summarizes credential settings on profile cards.
tests/​gateway/​test_secret_guard.py Tests matching, catalogs, protocol flows, and failures.
openviking_gateway/​tool_loop.py Seals results and unseals client reply copies.
openviking_gateway/​tool_catalog.py Excludes privacy_resolve when sealing is enabled.
openviking_gateway/​secret_guard.py Implements credential catalogs and transformations.
openviking_gateway/​proxy.py Integrates sealing, unsealing, and enforcement.
openviking_gateway/​models.py Defines and validates credential switches.
openviking_gateway/​kernel.py Seals recalled text before recording it.
openviking_gateway/​blocks.py Adds instructions for using credential references.
openviking_gateway/​app.py Initializes the credential catalog service.
docs/​zh/​gateway/​13-configuration.md Documents credential settings in Chinese.
docs/​zh/​gateway/​11-security-and-data.md Explains credential exposure and storage boundaries.
docs/​zh/​gateway/​08-context-profiles.md Documents behavior, limits, and failures.
docs/​zh/​gateway/​06-manage-in-studio.md Explains immediate application of credential switches.
docs/​zh/​api/​25-gateway.md Documents sealing and unsealing counters.
docs/​en/​gateway/​13-configuration.md Documents credential settings in English.
docs/​en/​gateway/​11-security-and-data.md Explains credential exposure and storage boundaries.
docs/​en/​gateway/​08-context-profiles.md Documents behavior, limits, and failures.
docs/​en/​gateway/​06-manage-in-studio.md Explains immediate application of credential switches.
docs/​en/​api/​25-gateway.md Documents sealing and unsealing counters.
bot/​docs/​zh/​design/​rfc-agent-secret-use.md Adds the credential-use design and acceptance criteria.
bot/​docs/​zh/​design/​images/​secret-use-plugin.zh.svg Illustrates plugin execution and output sealing.
bot/​docs/​zh/​design/​images/​secret-use-gateway.zh.svg Illustrates Gateway request and reply processing.
bot/​docs/​zh/​design/​images/​secret-use-deployments.zh.svg Compares deployment modes and plaintext exposure.
bot/​docs/​zh/​design/​images/​secret-use-before-after.zh.svg Compares plaintext and reference-based histories.
bot/​docs/​zh/​design/​images/​secret-use-architecture.zh.svg Illustrates Gateway, plugin, and Privacy boundaries.

Review effort: Balanced

Comment thread openviking_gateway/kernel.py Resolved
Comment thread openviking_gateway/secret_guard.py Resolved
Comment thread openviking_gateway/secret_guard.py Resolved
…ed credentials

- Seal compaction and new-window replacement text before it is stored: the opening block it copies may predate sealing or a registered value.
- Never exempt client tool calls (custom_tool_call, Chat custom, function_call) from sealing, so a data URI argument holding a credential is sealed.
- Re-read every credential target on refresh instead of skipping by version: a target deleted and created again restarts at version 1.
@t0saki

t0saki commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #8384. That PR rewrites the gateway part on current main (this branch was about 350 commits behind) and drops the client-side catalog: the gateway now seals through a server endpoint (POST /api/v1/privacy-configs/seal), so it no longer fetches plain values. Unsealing is part of seal_secrets there, and new tokens the user types are enrolled automatically. The design changes are written up in the RFC supplement: #7335 (comment)

@t0saki t0saki closed this Nov 24, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants