Skip to content

Commit f175afa

Browse files
rustyconoverclaude
andcommitted
Secret ATTACH options reach functions encrypted inside attach_opaque_data
The worker now carries the ATTACH options in the framework part of attach_opaque_data (uuid || options section || catalog bytes) and hands them to every function and catalog hook through one public accessor: params.attach_options (BindParams, InitParams, ProcessParams, scalar BindParameters) and vgi.catalog.attach_options(attach_opaque_data) for hooks such as catalog_transaction_commit. Catalog code keeps seeing exactly its own bytes. Options declared secret=True are encrypted (SPEC-secret-options, sealing rule 5): XChaCha20-Poly1305 under K = HKDF-SHA256(root, info "vgi.attach_secret_options.v1"), where root is an explicitly configured key (set_attach_secret_options_key), else the signing key when the worker seals (HTTP), else the host-local key file shared by all SDKs on the host ($VGI_ATTACH_SECRET_KEY_FILE, $XDG_RUNTIME_DIR/vgi/..., or <tmp>/vgi-<user>/attach-secret-options.key; 32 random bytes, 0700/0600, created atomically via hard link, read only when a secret has a value). AAD binds the blob to the attach uuid; a moved or tampered blob fails with the uniform "attach_opaque_data not recognized". The options never travel with copies of the catalog bytes (the bytes object pickles as plain bytes). pack_attach_options no longer keeps secrets in shared function storage: it drops them (they arrive through attach_options); unpack_attach_options merges the framework options. The txfix fixture reads token only via attach_options and reports whoami().token_in_attach_data. Docs: sealing rule 5 and the catalog guide now describe SDK-encrypted secret options as the standard way, with re-derive / digest as alternatives. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c349d85 commit f175afa

15 files changed

Lines changed: 897 additions & 163 deletions

‎docs/catalog-interface.md‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,28 @@ ATTACH 'mydb' (TYPE vgi, LOCATION 'https://worker.example.com', api_key getenv('
387387

388388
On the wire, `required` and `secret` are nullable boolean columns appended after the four shared spec columns (`name`, `description`, `type`, `default_value`), in that order. Readers look columns up by name, so a spec from a peer that predates either column reads it as `false`, and older peers ignore columns they don't know.
389389

390-
**Never put a secret option's value in `attach_opaque_data`.** On HTTP the worker seals the value, but over stdio and a unix socket it reaches the client, and the DuckDB extension, unsealed. Keep the credential server-side, re-derive it per call, or store a digest of it, as the `ticket_probe` fixture does (see [Opaque values: sealing](protocol/vgi-opaque-data-sealing.md), rule 5).
390+
**Read options, secret ones included, with the framework accessor:** `params.attach_options` in any
391+
function (bind, init and process, every function kind) or `vgi.catalog.attach_options(attach_opaque_data)`
392+
in a catalog hook such as `catalog_transaction_commit`. The worker carries the ATTACH options inside
393+
`attach_opaque_data` itself, so they reach every worker process on every transport, and encrypts the
394+
`secret=True` ones with a key the client never has (an HKDF subkey of the signing key on HTTP, or of a
395+
per-user host-local key file on stdio, unix sockets and launcher pools). **Never put a secret option's
396+
value in your catalog's own attach bytes:** over stdio and unix sockets those reach the client, and the
397+
DuckDB extension, unsealed. Catalogs that need only a fingerprint can still store a digest instead, as
398+
the `ticket_probe` fixture does (see [Opaque values: sealing](protocol/vgi-opaque-data-sealing.md),
399+
rule 5).
400+
401+
```python test="skip"
402+
class Lookup(TableFunctionGenerator[None, None]):
403+
@classmethod
404+
def process(cls, params, state, out):
405+
api_key = params.attach_options.get("api_key") # decrypted by the framework
406+
...
407+
```
408+
409+
On a host whose temp directory is shared or unsuitable, point `VGI_ATTACH_SECRET_KEY_FILE` at a
410+
private file (all worker processes of the deployment must use the same one), or call
411+
`vgi.attach_secret_options.set_attach_secret_options_key(key)` at startup.
391412

392413
`secret` does not change what the worker itself logs: attach options are never logged unless the catalog opts in through `loggable_attach_options()` (below), which must never return a secret option.
393414

@@ -445,30 +466,27 @@ class MyFunctionCatalog(ReadOnlyCatalogInterface):
445466
| `attach_supports_transactions(name=, options=, attach_opaque_data=)` | the `supports_transactions` class attribute | `CatalogAttachResult.supports_transactions`, decided per attach. |
446467
| `catalog_transaction_begin` / `_commit` / `_rollback` | raise `CatalogReadOnlyError` | Mint the transaction id; apply or discard the transaction's work. |
447468

448-
`pack_attach_options(options, specs)` turns the ATTACH options into attach bytes and
449-
`unpack_attach_options(params.attach_opaque_data)` reads them back in any function or hook, in any
450-
worker process. Options declared `secret=True` never enter the bytes: they are kept in the shared
451-
function storage (`VGI_WORKER_SHARED_STORAGE`) under a random handle, so pass the specs.
469+
Functions and hooks read the ATTACH options with `params.attach_options` /
470+
`attach_options(attach_opaque_data)` (secret options included, see
471+
[Declaring Attach Options](#declaring-attach-options)), so most catalogs need not override
472+
`attach_opaque_data_for` at all. (`pack_attach_options(options, specs)` remains for a catalog whose
473+
own bytes should depend on the non-secret options; it drops secret ones.)
452474

453475
```python test="skip"
454476
import os
455-
from vgi.catalog import (
456-
ReadOnlyCatalogInterface, TransactionOpaqueData, pack_attach_options, unpack_attach_options,
457-
)
477+
from vgi.catalog import ReadOnlyCatalogInterface, TransactionOpaqueData, attach_options
458478

459479
class MyCatalog(ReadOnlyCatalogInterface):
460480
catalog = CATALOG
461481

462-
def attach_opaque_data_for(self, *, name, options):
463-
return pack_attach_options(options, self.attach_option_specs)
464-
465482
def attach_supports_transactions(self, *, name, options, attach_opaque_data):
466-
return unpack_attach_options(attach_opaque_data).get("transactional") is True
483+
return options.get("transactional") is True
467484

468485
def catalog_transaction_begin(self, *, attach_opaque_data):
469486
return TransactionOpaqueData(os.urandom(16))
470487

471488
def catalog_transaction_commit(self, *, attach_opaque_data, transaction_opaque_data):
489+
token = attach_options(attach_opaque_data).get("token") # a secret option, decrypted
472490
... # apply the work staged under transaction_opaque_data
473491

474492
def catalog_transaction_rollback(self, *, attach_opaque_data, transaction_opaque_data):

‎docs/protocol/vgi-opaque-data-sealing.md‎

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,46 @@ the rule every SDK follows.
9292
- There is **no plaintext fallback**: no prefix, flag or missing call context
9393
may skip the open.
9494
5. **No secrets in plaintext.** Attach options declared `secret=True` MUST NOT
95-
appear in plaintext in any opaque value.
96-
- On an authenticating transport the seal satisfies this.
97-
- On an OS-owned transport that skips sealing, leave secret options out of
98-
the value. Re-derive them or keep them server-side.
99-
- Reference: the framework never copies attach options into the value. It
100-
wraps only the catalog's own bytes, so this rule binds the catalog
101-
implementation. A catalog that keeps options in its attach bytes MUST drop
102-
the secret ones, or keep only a digest. The `attach_options` fixture
103-
catalog echoes only its declared non-secret options. The `ticket_probe`
104-
fixture keeps `region` and `sha256(api_key)[:12]`, never the key.
95+
appear in plaintext in any opaque value, on any transport: not on the wire,
96+
and not in the opened (inner) bytes of a sealed value either.
97+
- **Standard mechanism: the SDK encrypts secret options inside
98+
`attach_opaque_data`.** The framework carries the ATTACH options in its
99+
own part of the attach value and hands them to every function (bind,
100+
init, process, every function kind) and to the transaction hooks through
101+
its public attach-options accessor. Secret values are encrypted there with
102+
a key the client never has, so they reach any worker process of the
103+
deployment on every transport. Catalog authors never handle the
104+
encryption and never put a secret into their own attach bytes.
105+
- Key: `K = HKDF-SHA256(ikm = root, salt = empty,
106+
info = "vgi.attach_secret_options.v1", L = 32)`. `root` is, in order: an
107+
explicitly configured secret-options key; else the signing key, when the
108+
worker seals (HTTP); else the host-local key. The root is never the AEAD
109+
key itself.
110+
- Host-local key (identical across SDKs, so workers of different SDKs on
111+
one host share it): `$VGI_ATTACH_SECRET_KEY_FILE` if set and non-empty;
112+
else `$XDG_RUNTIME_DIR/vgi/attach-secret-options.key` if `XDG_RUNTIME_DIR`
113+
is a directory; else `<per-user temp dir>/vgi-<username>/attach-secret-options.key`.
114+
Exactly 32 raw CSPRNG bytes, directory 0700 and file 0600, owned by the
115+
user and not group/other accessible (refused otherwise). Created
116+
atomically (a private temp file hard-linked into place; an existing key is
117+
used, never overwritten) and read only when a secret option has a value.
118+
- AEAD: XChaCha20-Poly1305 or ChaCha20-Poly1305 (AES-256-GCM where ChaCha
119+
is unavailable), random nonce per encryption, envelope
120+
`version || nonce || ciphertext || tag` (the version byte identifies the
121+
algorithm). AAD `"vgi.attach_secret_options.v1" 0x00 || attach_uuid`,
122+
binding the blob to its attach (rule 6): moved into another attach value it
123+
fails to open, with the rule 4 error. A secret with a value but no
124+
obtainable key fails the ATTACH with a clear message.
125+
- Never log the values, the key or the blob (rule 7), and never put them in
126+
table-function state, bind opaque data or any other client-visible value.
127+
- Alternatives for catalogs that do not need the value itself: re-derive it,
128+
or keep only a digest. The `ticket_probe` fixture keeps `region` and
129+
`sha256(api_key)[:12]`. A catalog that keeps options in its *own* attach
130+
bytes MUST drop the secret ones.
131+
- Reference: vgi-python `vgi.catalog.attach_options()` /
132+
`params.attach_options` (implementation `vgi/attach_secret_options.py`).
133+
The `txfix` fixture's `whoami().token_in_attach_data` checks the secret
134+
never appears in the attach bytes a function receives.
105135
6. **Unpredictable identifiers.** Any id inside an opaque value (session id,
106136
attach uuid) MUST come from a cryptographically secure random source, never
107137
a seeded PRNG such as `mt19937`. The reference uses `uuid.uuid4()`.

‎tests/catalog/test_read_only_attach_hooks.py‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class TestPerAttach:
103103
def test_options_reach_the_attach_bytes(self) -> None:
104104
"""Options reach the attach bytes."""
105105
result = _attach(_PerAttach(), token="tok-1", TRANSACTIONAL=True)
106-
assert unpack_attach_options(result.attach_opaque_data) == {"transactional": True, "token": "tok-1"}
106+
# The secret token is never packed into the catalog's own bytes.
107+
assert unpack_attach_options(result.attach_opaque_data) == {"transactional": True}
108+
assert b"tok-1" not in result.attach_opaque_data
107109
assert result.supports_transactions is True
108110
# Custom bytes carry state: the client must keep them.
109111
assert result.attach_opaque_data_required is True
@@ -112,7 +114,7 @@ def test_transactions_only_when_asked(self) -> None:
112114
"""Transactions only when asked."""
113115
result = _attach(_PerAttach(), token="tok-1")
114116
assert result.supports_transactions is False
115-
assert unpack_attach_options(result.attach_opaque_data) == {"transactional": False, "token": "tok-1"}
117+
assert unpack_attach_options(result.attach_opaque_data) == {"transactional": False}
116118

117119
def test_supports_transactions_class_attribute_still_works(self) -> None:
118120
"""Supports transactions class attribute still works."""
@@ -135,32 +137,18 @@ def test_untyped_round_trip(self) -> None:
135137
assert unpack_attach_options(packed) == {"a": 1, "b": "x", "c": None, "d": [1, 2]}
136138

137139
def test_specs_type_and_default(self) -> None:
138-
"""Specs type and default."""
140+
"""Specs type the options and fill defaults; secret options are dropped."""
139141
packed = pack_attach_options({"token": "t"}, _SPECS)
140-
assert unpack_attach_options(packed) == {"transactional": False, "token": "t"}
141-
# A default-less option the caller omitted is simply absent.
142-
assert "token" not in unpack_attach_options(pack_attach_options({}, _SPECS))
142+
assert unpack_attach_options(packed) == {"transactional": False}
143143

144-
def test_secret_options_stay_out_of_the_bytes(self) -> None:
145-
"""A secret option is kept in shared storage, never in the attach bytes."""
144+
def test_secret_options_never_packed(self) -> None:
145+
"""A secret option never enters the catalog's own bytes."""
146146
packed = pack_attach_options({"token": "s3cr3t-value", "transactional": True}, _SPECS)
147147
assert b"s3cr3t-value" not in packed
148-
assert unpack_attach_options(packed) == {"transactional": True, "token": "s3cr3t-value"}
149-
# Two attaches with the same secret get distinct, unpredictable handles.
150-
assert pack_attach_options({"token": "s3cr3t-value"}, _SPECS) != pack_attach_options(
151-
{"token": "s3cr3t-value"}, _SPECS
152-
)
148+
assert unpack_attach_options(packed) == {"transactional": True}
153149
# Without specs nothing is known to be secret.
154150
assert b"plain-token" in pack_attach_options({"token": "plain-token"})
155151

156-
def test_missing_server_side_secret_is_an_error(self) -> None:
157-
"""Bytes whose secrets the shared storage lost fail loudly, not silently."""
158-
packed = bytearray(pack_attach_options({"token": "t"}, _SPECS))
159-
offset = len(b"vgi.attach-options.v1\x00")
160-
packed[offset : offset + 16] = b"\x01" * 16
161-
with pytest.raises(LookupError, match="re-ATTACH"):
162-
unpack_attach_options(bytes(packed))
163-
164152
def test_empty_and_foreign_bytes(self) -> None:
165153
"""Empty and foreign bytes."""
166154
assert unpack_attach_options(pack_attach_options({})) == {}

‎tests/conformance/test_writable_tx.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ def test_attach_options_and_per_attach_transactions(connect: Any, token: str) ->
117117
assert plain.supports_transactions is False
118118
assert plain.attach_opaque_data_required is True
119119
with client:
120-
assert _table(client, "whoami") == [{"token": token, "transactional": False}]
120+
assert _table(client, "whoami") == [{"token": token, "transactional": False, "token_in_attach_data": False}]
121121
assert _table(client, "reset") == [{"ok": True}]
122122
tx_result, tx_client = _attach(connect, token=token, transactional=True)
123123
assert tx_result.supports_transactions is True
124124
with tx_client:
125-
assert _table(tx_client, "whoami") == [{"token": token, "transactional": True}]
125+
assert _table(tx_client, "whoami") == [{"token": token, "transactional": True, "token_in_attach_data": False}]
126126
anonymous, anon_client = _attach(connect)
127127
with anon_client:
128-
assert _table(anon_client, "whoami") == [{"token": None, "transactional": False}]
128+
assert _table(anon_client, "whoami") == [{"token": None, "transactional": False, "token_in_attach_data": False}]
129129

130130

131131
def test_update_modes_on_a_plain_attach(connect: Any, token: str) -> None:

0 commit comments

Comments
 (0)