Repository navigation
feat(ai): keep leak protection's inventory across proxy restarts - #331
Merged
Merged
Conversation
…rsioned entries The proxy knew the values it replaces only for the life of its process. After the hook-managed proxy's idle exit, an upgrade or a reboot, a resumed conversation whose history holds a value went out with only the pattern rules to catch it, and nothing said so. - The inventory key is kept in the system keystore beside the CA key, one per user, with an id, made on first use and never written to disk. The entries are kept in leak-inventory.bin in the Phase AI directory: owner-only, replaced whole through a temporary file, sync and rename, read only as a regular owner-only file of this user, and each record sealed under a key derived from the inventory key. Every proxy of the user loads it at start and adds to it under a lock file. With no keystore nothing is stored and nothing changes. - Two versions are stored. The store format is the file's layout: a newer file is left untouched and the proxy runs from memory. The matching scheme is each entry's own, and is what Entries already carried as their version: pkg/censor now says what a scheme covers. An index matches every scheme it supports side by side; entries of a newer scheme replace those an older one left for the same placeholder when the value is indexed again; entries of a scheme the build does not match stay in the file as they are and are counted. - Scheme 2: a form made of hex digits, and the base64 of a value made of hex digits, are stored in 24-character chunks, and a tail is hashed with the chunk before it. Whoever holds both the key and the file no longer has a 12-digit piece to try. A piece of such a form is caught for certain from 47 characters, where it was 35, or 23 for a value that is itself hex. Scheme 1 entries are matched as before. - An entry not registered again for 30 days is dropped. The file is bounded at 64 MiB and 65536 records. Entries under a replaced key are not matched, are counted and are dropped. phase ai disable removes the file and the key; logout --purge removes the key with the CA key. - /v1/status and phase ai status say whether the inventory is stored or in memory and why, how many values were loaded at start, and how many stored entries cannot be matched. Counts only. - A damaged, cut short, oversized or hostile file never stops the proxy: what authenticates and passes the validation a registration passes is loaded, and the rest is counted and dropped.
- Scheme 2 entries carry 24 bits of a chunk's rolling hash, not the whole: the whole, with the key, gave a 12-character chunk away in about 2^36 steps. Scheme 1 entries are matched from memory and never stored. - Entries never replace others by type and id: another value may share the id. Older-scheme records lapse at 30 days. - A client indexes in the scheme the proxy reads, when it knows it. - A key is made only when the keystore says it holds none, and a file that exists and cannot be read is never written over: memory only, with why. - A full index loads the newest entries and counts the rest. - The lock file is never removed, the lock has a two second deadline and is never held across a keystore call; purge deletes the key first; a replaced key shows in status within a second.
…rs it A value was stored in the partition of the proxy's login, whoever registered it: a command of organisation B that found organisation A's running proxy put B's values in A's store, and after a restart A's proxy wrote B's ids into A's model traffic and events. Who a registration belongs to: - One canonical identity, phaseproxy.NewInventoryIdentity, for the proxy and for every registering command: a tenant is a parsed Phase host and an organisation id, a login its kind and its account or Agent id. - The proxy's identity is what the Console says its session's token is (the Agent identity route, or the route a user's or service account's login is saved from), asked once per runtime and never read from the login file. A token from the environment has a tenant too. When the Console does not say, the session has none: memory only, no ids. - A runtime start takes one look at the login. - A registering command states its own tenant and login, on the key request and with its entries. Same tenant and login: stored, named. Same tenant, another login: stored under that login, id withheld. Anything else: kept for the life of the process under keys made at start, id withheld, never stored. - The key route hands a command of another tenant nothing derived for a stored one. A key id lets the proxy refuse entries made under keys it does not hand that command. - An engine refuses a session of another tenant or login. The rest of the review: - Store bounds are per login and per tenant as well as overall, and past one only the writing login loses records. Another tenant's are never dropped to make room. - A start loads a share of the index, the session's own login first, and one login registers up to a share of it, so a full inventory is one login's and does not outlive a restart. The messages no longer say a restart clears it. - Values are counted once per login that registered them, and the tag that linked records of one placeholder is gone: a record's head, with its tenant, login and time, is sealed under a key from the master. - Each withheld value found is reported once, by a number that tells it from the others of its type and is never written. - A withheld value is matched whole or not at all, so no command learns from the proxy that a guessed piece belongs to a value it cannot name. - phase users logout removes that login's stored values, and its tenant's when it was the tenant's last saved login.
…gin's stored keys to itself A proxy asked two token routes who it was, one request more for every runtime, and handed a command that stated another login of its tenant that login's stored confirming key. Whose proxy it is: - The session the Console opens says: its payload carries the organisation and the principal (an Agent, or a user's organisation member), and the proxy's tenant and login are those at its own host. The runtime asks no other route, and the client's token-identity request is gone. - A session that names neither, as an older Console's does not, or a principal of another kind than its token, has no identity: memory only, every placeholder ph:<type>, nothing stored. A runtime whose first session named nobody stays so for its life. - The kinds of login are named as a session's principal types (user, agent, service_account), so a session and a command write one login the same way. A command still tells its own from its saved login or the Console. The id a user's login is saved under is its organisation member's, which is its session's principal id. Whose keys a command is handed: - A command that states the proxy's own tenant and login is handed that login's stored keys, and its values are stored and named. - Every other command is a guest, one of another login of the same tenant included: keys of the process alone, ph:<type> with no id, never stored. A proxy hands out and writes one stored partition, its own session's. - A proxy still loads what the other logins of its tenant stored through their own proxies, and matches it withheld and whole. - A guest's confirming key is its stated identity's, and for a command that states none its one key request's, which its key id names. No key is shared by the commands that state nothing, and a registration that names no key request of the process is refused, as entries under any key the proxy does not hand that command are. The header comments of the store and of the proxy's inventory are a few lines, and the bound on principals made for stated logins went with the code that made them.
The tenant separation held. What did not: - Commands that state no identity were a new guest on every run, so one command's values filled the guests' share and the next guest got 507. They are one guest now, with one confirming key for the life of the proxy, and a full share drops its oldest entries: 507 is left for one registration that is larger than the share by itself. - A proxy the Console names nobody for took every command as a guest: whole values only, a quarter of the index. It is again what the proxy was before there was a store: one process key, ids written, fragments matched, the whole index, nothing stored. - The bound on how many chunks one lookup value leads to was shared, so forged forms under a guess's lookup value pushed out a chunk of another login's value only when the guess was right. The bound is each confirming key's own. - A withheld form is led to by one chunk, the first that is not structure, and what a scan hashes where that chunk's lookup bits turn up does not depend on what confirms. What timing still tells is in the package documentation and CONTROL_API.md. - What a guest's registration costs the guests' share no longer depends on whether its type and id are held. - A value known only from the store is reported and replaced and never stops a request, and a value that is a model host's name or path is not indexed: one such value stopped every request for 30 days. - Status counts the records a bound of the store dropped. - A store in use no longer makes a proxy memory-only for life: the next registration tries again, a process that waits for the lock is next to have it, and logout and disable wait longer and say what remains. - A client older than key ids registers scheme 1 entries again.
Status counted the stored records of other hosts and organisations, and the count moved whenever one of them registered a value: the one thing a session learned of another tenant's store. - The count is gone from the store's status, from the control API's `inventory` (`otherTenants`) and from `phase ai status`. Other tenants' records are still kept where they are, unopened. The size of the whole store is the file's size, for whoever may read the file. - `unmatchedKey` was a count of every record under a replaced key, whose tenant nothing can tell, so it summed every tenant's. It is now true or false: the store was under a replaced key and was dropped. - A test states the property: with one master and the same use of its own proxy, every answer, scan, event and status a tenant reads is the same to the byte whether another tenant stored nothing, the same value, other values or hundreds.
A tenant was a Console's scheme, host and port and an organisation id: the path a Console is served under was dropped. Two Consoles under different paths of one origin, with the same organisation and account ids as a copied database has, were one tenant and one login, and each loaded and named what the other stored. - A tenant is the Console's whole address: scheme, host, port and the path it is served under, in one form. A trailing slash, and the API's own path written after it, are still the same Console; another path is another. - The path is read once, as the client that opens a session reads a host (`agentapi.DeploymentPath`). `Client.Host` keeps it, so the proxy's identity, a command's and what a logout forgets are the same for one login and differ between two Consoles. - An address that can be read two ways names no tenant, and an identity's address is bounded. - The property test of what a tenant reads now runs for two Consoles under one origin with the same ids, at two paths and at the root and a path.
…re says A logout read the inventory key before it removed anything. When the keystore did not answer, as from a shell with no session bus, it reported success and removed nothing, and the login's records loaded again once the keystore was back. - The removal no longer depends on the key. With the key, and a file it opens, a logout still removes that login's records, or its tenant's, and no one else's. - The key is all that says whose a record is. When the keystore does not give it, holds none, or the file is one it does not open (another key's, a newer format's, not an owner-only file), the whole file is removed, every login's, and the logout says so on stderr. - What could not be removed is an error, with or without the key: a store in use, or one that cannot be written. Nothing left behind is reported gone.
A registration's answer waited for the write to the store: up to two seconds for its lock, and for the keystore when the file had gone. A client gives a registration one second, so a store in use turned a value the proxy already held into a failed registration. - The values are in the index before the store is touched, and the answer now waits a quarter of a second at most for the write. A write that takes longer finishes behind the answer, and status says how it went. - At most four writes wait on the store at once. A registration past that is not queued behind them: it is held in memory, and status says `store_busy` until a later registration is stored.
`phase ai disable` read the master before it deleted it and took any failed read for "there is no key". A keychain that refused the read, as after a denied prompt, kept the master, and the disable said nothing: a proxy enabled again handed out the same keys. - The master is deleted without being read. Where the keystore cannot delete, it is replaced, as before. - A keystore that will neither delete nor replace it is an error, and the disable asks to be run again. The stored entries are removed either way. - A machine with no keystore and nothing stored has no key to remove, and disabling there is still no error.
The Limits said the 24 lookup bits of a chunk list the guesses that fit and say nothing of which is right. That holds for one lookup value alone. A value has one for every chunk of every form, all under the tenant's lookup key, and a guess is tested against every one it would give: - a wrong guess of a whole value fits its n lookup values about once in 2^(24n), so a value that can be guessed whole is picked out with no confirming key; - the same 12 bytes have a lookup value in each form they stand in (as they are, and in the value's hex in both cases), so of the guesses of a chunk that fit one form's value the others leave the right one. The package documentation and CONTROL_API.md now say so, and what timing tells of the lookup values in use is worded to match.
…ouch The store's file was read by trusting each record's length, and the header's key id decided whose the file was. One flipped bit in that id, or one damaged length, made a starting proxy drop every record after it, of every tenant, and write the file back without them. - A record is found by its head, which is sealed under the key. Past a length or a head that is damaged the file is searched for the next head that opens, so the damage costs the record it is in and no other. - A file is another key's only when its header names another key and no head in it opens. A damaged id over heads that open is put right at the next write. - How many heads are tried in damaged stretches is bounded with the records read.
The Limits now say that the wrong guesses of a chunk that fit its lookup value in one form do not fit it in another. The test holds them to it, with a fixed key and value: of the wrong guesses of a value's first 12 bytes that fit its lookup value as they are, none fits its hex's too.
No change in behaviour: the comments added with the last fixes say why in a line or two, and the client's deployment path sits beside its base URL without moving the fields around it.
Status counts nothing of another tenant's records, but the bound on the whole file is shared: once other tenants have filled it, what a tenant registers is not stored, and its own `dropped` says so. The control API's description and the status type's comment claimed a little more than that.
… cap Three places written for one key, 12-byte chunks and values a client registered met an index that holds more than that: - The walk over bytes whose length must not change took a verified run's start from 12-byte chunks. A hex form is cut into 24-byte ones, so a value's hex read as one string across what parses as fields kept its first half. It takes the form's own chunk size. - It knew an id of one of its own placeholders only when the first principal added to the index had registered it. It knows the id whoever registered it, and never takes a withheld id for one it writes. - Find's cap kept the first indexed value. A caller stops a request for a value a client registered and not for one a store handed over, so the first of each is kept. Tests: a hex run of wide chunks, a withheld value and a guest's value in binary bodies (whole, by type alone, a second pass changing nothing), and a registered value behind a loaded one past the cap.
`phase users logout --purge` said that a Phase AI CA key was not removed when the system keystore did not answer, and said nothing of the leak protection inventory key that sits beside it: that removal still took an unreachable keystore for one with no key. The inventory key is reported as the CA key is, and the purge says once what it left: the CA key, the inventory key or both, each only where this machine made one. The line for a CA key alone is unchanged.
A value of text that is names and values is indexed value by value, as a JSON value is string by string, and a URL's token is indexed as its password is. The Limits and the control API say so where they list what a value of another login, or a guest's, is matched whole as.
A kubeconfig and a JSON value are indexed value by value. Registered by one login and read back from the store by three proxies, they are: - for that login, replaced value by value with its ids, pieces included; - for another login of the tenant, replaced only where a value stands whole, by its type alone, with no id in the request, events or status; - for another tenant, exactly what a proxy that was never told of them sends, to the byte.
…e was a store The scheme a store keeps cuts a form of hex digits into 24-byte chunks, and the key route handed that scheme to every command. So a piece of a hex secret that the proxy replaced before there was a store (32 hex digits less one, the first 23 of them, 30 of 40) went to the provider: from a proxy with no identity, which stores nothing, and for a login's own values. A value registered with a running proxy is matched as it was before: - A proxy with no identity answers the key route with scheme 1 for its one process key, and is the proxy from before a store. - A proxy with an identity hands a command of its own login a live key as well: a lookup key the process drew when it started, kept nowhere, in no answer to any other command. The command registers each value twice in one request: entries under the stored keys, which go to the store and are not indexed, and `liveEntries` in scheme 1 under the live key, which the index takes as that login's own (`Index.AddLive`). The key id names the live key too, so entries made for the last process are refused and the client fetches the key again. - A guest is as it was: handed no live key, matched whole or not at all. A value loaded from the store is known by its stored entries until a command registers it again. Then both are held: one value to every count, one placeholder where both find it, and no longer a store's alone. A value whose two sets of entries do not fit one registration is matched and not stored. Tests: the pieces through the CLI's own client, with no identity and for a login; the same pieces from the store alone and once registered again; live entries refused from every other command, in another scheme, of other names, and under the last process's key. Three tests compared whole key answers across processes or listed the answer's fields: they compare the stored keys, and check that the live key is each process's own and no guest's.
The check that a proxy with no identity protects as before compared it with an index made by the same build, so both sides moved together. This one compares with what the commit the store was built on did: a probe program that builds unchanged there registers fixed values and writes, for 1,724 probes, a hash of what each kind of scan made of the probe and of its report, then what the provider received for eleven requests, the events and the counts. Its output there is `testdata/leak_parity_base.txt`; the file holds hashes, counts and placeholders, and the probe program says how it was made. The probes are whole values and their encodings, cut copies, sliding pieces, lines, and runs of hex digits cut to every length from 23 to 48: 64-digit keys, lines of hex, and a 40-digit key id in a JSON document less a character. Held to it, line for line: a proxy with a store and no identity, registered with through the control route and through the CLI's own client; a login's own freshly registered values, both ways; and the same with the inventory kept in memory.
…r tenant anything While a login's proxy runs with values registered under its live key, and with a guest's value beside them: - the file holds that login's values in the stored scheme alone, nothing the live key confirms or opens, and nothing of the guest; - another login's proxy over the same store replaces the stored values whole, by their type, and no piece, and names no id; - the guest's own tenant, another login of it and the same organisation on another host send what a proxy that never saw the store sends, to the byte; - no proxy hands the live key to any command, and what a guest is handed confirms no hash of the entries made under it, in either scheme; - once the process ends the guest's value is known nowhere. The view a tenant is held to whatever another tenant does now holds pieces of each value as well.
…d again The limit on pieces of hex digits was written as the current scheme's, as if it held for every value. It holds for a value a proxy knows only from the store: one it started with and no command has registered with it since. `pkg/censor`'s Limits and the control API say so, and say what a running proxy matches by instead; the control API describes `liveKey`, `liveFormat` and `liveEntries`, and what a proxy with no identity answers.
A value held without its id is written `ph:<type>`. Directly before a colon and a token, as in `<value>:ghp_...` or `curl -u <value>:AKIA...`, the result read as `ph:<type>:<id>`, so the rules took the token for an id the matcher had written and left it in the request. With the value unknown, or held with its id, the token was replaced. The inventory pass tells the rule pass where it wrote a placeholder with no id, and such a placeholder ends there.
…events Another login's stored values are written `ph:<type>` with no id, and the request a proxy sends is the same however their owner filed them. The event was not: three values registered one by one were `placeholderCount` 3, the same three as one document 1, and severity and whether a later request raised a new event followed that number. A placeholder with no id is one placeholder to `placeholderCount`, `replacedCount` and the memory of what was reported, however many values stand behind it. Two tests held the old count: a guest's two values in one request are now one placeholder, and the test that each withheld value is reported once is now that the values of a type are reported as one.
… an encoded run A value known only from the store is reported and never stops a request; a value registered with the running proxy does. A decoded run took the first value found in it for the whole run, so a URL whose base64 held the hex of a stored value and then the hex of a registered one went through. A decoded run is a store's alone only when everything found in it is.
A value held without its id is matched whole or not at all, and the Limits said a copy cut short by one character is never caught. Inside base64 it can be: a base64 form holds only the characters made of the value's bits alone, so up to four bits of the first and last characters are in no form.
…hat holding a value both ways costs The control API said 23 characters of a hex value registered with the running proxy are enough to find it. They always hold one 12-character chunk, and such a chunk can be too plain to count, as the Limits say. It now gives what a piece needs under each scheme and points at the Limits for the odds, and says that a value held both ways costs a scan a pass per key.
Two of the new tests failed now and then on what they drew: - A random value that ends in a word the rules take for a name (`pwd`, `dsn`) makes everything after the colon that name's value, so the rule replaced the rest of the URL with the token. About one value in 330. - One 12-character chunk of hex digits in five is too plain to find a piece by, so a piece holding two chunks of a random hex key is missed about once in 24. Both are how the matcher has always worked. The values are fixed.
A value a proxy loaded from the store is reported where it cannot be replaced and never stops a request, until a command registers it with that proxy again. Since a registration's stored entries go to the file and are not indexed, the forms the index had loaded of the value kept their mark for the life of the process. The stored scheme finds some pieces the live one finds only inside a decoded run, the hex of 24 to 44 characters of a token among them, so such a piece in a URL was answered 200 with a report where a proxy that only had the value registered answers 403. When a command of the proxy's own login registers a value with live entries, the index is shown the stored entries too (`Index.Registered`): the forms it holds with those fingerprints are no longer a store's alone, and nothing is added. A fingerprint is of the principal's confirming key, the type, the id and the form, so only that login's own entries of that value match: another login's, a guest's, and a stored value nobody registered again stay as they were. Tests: the matcher's, for what changes the mark and what does not; and a proxy that loaded values and had them registered again against one that only had them registered, over each value whole, cut, encoded and as the hex of every piece of 24 characters and more, in the index and as real requests, with a stored value that was not registered again beside them.
Deploying phase-cli-install-script with
|
| Latest commit: |
8207506
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://2dc1e0fe.phase-cli-install-script.pages.dev |
| Branch Preview URL: | https://feat--leak-inventory-store.phase-cli-install-script.pages.dev |
…inventory # Conflicts: # src/cmd/ai_status.go # src/cmd/leak_protection.go # src/cmd/leak_protection_test.go # src/cmd/users_logout.go # src/internal/aiproxy/CONTROL_API.md # src/internal/aiproxy/control_inventory.go # src/internal/aiproxy/leak.go # src/internal/aiproxy/leak_policy_test.go # src/internal/leakguard/leakguard_test.go # src/pkg/censor/entries.go
…it was saved with
Five probes changed in the base itself: URL tokens and passwords now keep their first characters as structure, so pieces holding fewer than 12 secret characters are left alone.
…Windows reports as 0666
nimish-ks
marked this pull request as ready for review
November 22, 2026 12:57
|
[High impact] The changes reach every identified Phase AI proxy session that registers or scans secret values. Fix the compatibility and lifecycle issues before merging. Reviews (1) · Last reviewed commit: "test(config): compare the linked file's ..." · Reviewed by Greptile |
…1305, and finish its writes before a proxy exits - The store is now secret-inventory.bin (with .lock and .queue), its keystore entry phase-cli-ai-secret-inventory, and its derivation labels and file magic follow the name. - Records are sealed with XChaCha20-Poly1305 from golang.org/x/crypto, which the CLI already depends on, with 24-byte random nonces. - The key route takes POST only. Nothing sends GET to it. - Status waits at most 250 ms for the keystore's answer on whether the key is still this process's, so a locked keystore never makes a health check late. A slower answer still lands, and the next Status reports it. - A stopping proxy waits, at most 5 seconds, for the inventory writes that registrations it already acknowledged left running.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #322 (
feat--agents). Draft until it has run against a real OS keystore: every test here uses an in-memory keystore.What
Leak protection forgets every registered secret when the proxy restarts, so a resumed conversation is covered by pattern rules alone until a
phasecommand registers values again. This keeps a sealed, hashed inventory on disk so protection survives a restart.~/.phase/ai/secret-inventory.bin, records sealed with XChaCha20-Poly1305; the master key lives in the OS keystore. With no keystore nothing is stored and everything stays in memory, as today.ph:<type>with no id. Another organisation's values are unknown.phase users logoutandphase ai disableremove them and say what they could not remove.organisationandprincipalthe Console now sends with a session (feat: AI Agents console#1025, already on that branch).Limits
src/pkg/censor/doc.go.Checked
feat--agentson 1,724 probes and eleven real requests.Not checked: a real macOS keychain, Windows Credential Manager, Linux Secret Service.