Skip to content

Commit c6cb2ca

Browse files
shengyfuCopilot
andcommitted
Authenticate managed daemon RPCs
Require per-daemon credentials before v2 or compatibility dispatch. Publish and validate native owner-only credential files, bind discovery to the private registration, rotate capabilities at restart, and preserve genuine v1 clients with a separate private instance capability. Co-authored-by: Copilot App <[email protected]>
1 parent bbf070c commit c6cb2ca

11 files changed

Lines changed: 900 additions & 36 deletions

File tree

‎AGENTS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ versioned view. `shared owner-hold --claim <issued-claim.json>` retains ownershi
110110
until stdin closes; supervise that process too. Do not replace its guard with a
111111
PID check or inactivity timeout. Unknown ownership proof protects the lease.
112112
Normal queries discover a ready managed attachment without new search flags.
113+
Managed RPC also requires a per-daemon private credential; the CLI loads it
114+
automatically from an owner-only file. Public namespace/instance IDs are not
115+
credentials. Do not copy the private file into logs or bypass authentication.
113116

114117
Managed advancement is version-checked and may be adaptive under explicit
115118
policy. Recover the authoritative current exact commit/version after a lost

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,11 @@ mode supports v1 clients but **never collects legacy generations**. Storage mode
809809
is immutable; omitting `--shared-policy` still selects the existing v1 daemon.
810810
Ordinary indexing, non-Git directories and unborn repositories are unchanged.
811811

812+
A managed daemon authenticates every RPC with a rotating per-daemon credential
813+
stored in an owner-only file; loopback and public namespace IDs alone do not
814+
authorize access. The CLI handles this automatically. Direct clients follow
815+
the [per-user authentication contract](SHARED_WORKTREE_INDEXES.md#per-user-rpc-authentication).
816+
812817
A generic managed client persists its operation and lease tokens, prepares an
813818
instance-bound owner, holds the issued guard with `tgrep shared owner-hold`,
814819
registers that claim, then calls `views.attach` through `shared manage`. Operations

‎SHARED_WORKTREE_INDEXES.md‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,9 @@ ordinary indexes and default v1 retain-all namespaces keep their existing format
790790

791791
Each publication has a fresh physical incarnation independent of its logical
792792
repository/tree/profile key. A delayed deletion of an old incarnation cannot
793-
delete a replacement for the same logical key. IDs are opaque 32-character
794-
lowercase hexadecimal identifiers, not paths or authorization credentials.
793+
delete a replacement for the same logical key. Catalog, namespace and instance
794+
IDs are opaque 32-character lowercase hexadecimal identifiers, not paths or
795+
authorization credentials.
795796

796797
`managed::open_generation` and the managed generation/checkpoint adapters acquire
797798
OS-backed protection before opening data. Protection follows escaped
@@ -980,7 +981,8 @@ the daemon and emits one `{"ok":true,"result":...}` result or
980981
`{"ok":false,"error":...}` failure; failures also have a nonzero exit status.
981982
For direct loopback RPC, send a newline-terminated JSON-RPC 2.0 request containing
982983
`protocol:2`, the discovered `namespace`, `instance`, `repository`, `method`,
983-
`params` and `id`. Successful RPC results repeat those identities and place
984+
`params`, `id` and the private `authorization` credential described below.
985+
Successful RPC results repeat those identities and place
984986
method data in `result.data`. Never reuse a stale daemon registration solely
985987
because its PID or port still exists. Request/response limits are negotiated
986988
by `hello` (currently 1 MiB / 64 MiB).
@@ -990,6 +992,47 @@ semantics, indexing profile, effective versioned policy/allocation and
990992
directory-sync capability. Copy its `profile` into view requests; a profile
991993
mismatch is an error, not a silently different corpus.
992994

995+
### Per-user RPC authentication
996+
997+
Loopback alone is not a per-user boundary. Every request to a managed daemon,
998+
including `hello`, queries and compatibility-v1 requests, must authenticate
999+
before dispatch. Each daemon creates a fresh 128-bit OS-random bearer credential
1000+
and atomically publishes `tgrep-daemon-v2.auth.json` beside its public
1001+
`tgrep-daemon-v2.json` registration in the Git common directory. The private file
1002+
contains `{"registration":<complete-public-registration>,"token":"<credential>",
1003+
"legacy_token":"<compatibility-credential>"}`.
1004+
The v2 request's top-level `authorization` field carries that token. Namespace,
1005+
instance, owner, lease and operation IDs are not substitutes for it.
1006+
1007+
`shared manage` and normal managed queries load credentials automatically.
1008+
A direct client must verify native owner-only access, read the bounded private
1009+
file, and compare its **complete registration**, including endpoint and instance,
1010+
with discovery before connecting. `tgrep_core::managed::read_private_control_file`
1011+
performs the native file checks. The daemon advertises
1012+
`private-rpc-authentication` in `hello`; clients must not fall back to
1013+
unauthenticated RPC when a credential is missing, stale or inaccessible.
1014+
Invalid credentials return `permission` / `rpc-authentication-failed`,
1015+
non-retryable and `not-committed`. A changed public registration is rejected
1016+
locally as `stale-identity` / `rpc-registration-mismatch`.
1017+
1018+
Private files are created with owner-only access **before writing any secret**:
1019+
Unix owner UID and mode `0600`, or a protected Windows DACL granting only the
1020+
daemon's user SID. Readers reject symlinks, external hard links, foreign owners
1021+
and broader permissions. Darwin extended ACLs are conservatively rejected,
1022+
including inherited ACLs, rather than assuming mode bits exclude other users.
1023+
Do not copy these files into logs or shared configuration. Credentials rotate
1024+
at daemon restart and are removed by identity during normal shutdown.
1025+
1026+
In `compatibility-retain-all`, genuine v1 clients continue to use their existing
1027+
wire format. Their owner-only v1 daemon/view markers carry a separate private capability
1028+
in the opaque v1 `instance` field; that value is **not** the public v2 instance ID.
1029+
The adapter validates it before entering any legacy handler; it cannot authorize
1030+
v2 requests. V1 responses echo this capability as their `instance`, so keep those
1031+
responses private too. Default v1 daemons
1032+
started without `--shared-policy` retain their existing transport and behavior.
1033+
This boundary excludes privileged OS administrators and arbitrary same-user
1034+
writers, as does the trusted-storage contract.
1035+
9931036
### Ownership, operation tokens and attachment
9941037

9951038
1. Persist a bootstrap token, then call `owners.prepare` with

‎tgrep-cli/src/serve/shared/managed.rs‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

3-
use super::protocol_v2::{MARKER, Registration, Request, VIEW_MARKER, ViewRegistration};
3+
use super::protocol_v2::{
4+
AUTH_MARKER, Authentication, MARKER, Registration, Request, VIEW_MARKER, ViewRegistration,
5+
};
46
use super::server::{Options, QuerySource};
57
use anyhow::{Context, Result, ensure};
68
use notify::{EventKind, Watcher};
@@ -106,7 +108,8 @@ fn hello(namespace: &Namespace) -> Result<Value> {
106108
"versioned-views","operation-replay","atomic-base-advancement","adaptive-evaluation",
107109
"instance-owner-guards","catalog-pages","reference-aware-collection",
108110
"versioned-allocations","bound-checkpoints","offline-maintenance","stop-if-idle",
109-
"bounded-storage-inventory","namespace-discovery","maintenance-diagnostics"
111+
"bounded-storage-inventory","namespace-discovery","maintenance-diagnostics",
112+
"private-rpc-authentication"
110113
],
111114
"profile":IndexingProfile::default(),
112115
"policy":namespace.policy()?,"allocation":namespace.allocation()?,
@@ -357,6 +360,7 @@ impl Drop for Observer {
357360

358361
struct State {
359362
registration: Registration,
363+
authentication: Authentication,
360364
namespace: Arc<Namespace>,
361365
views: ViewManager,
362366
watches: WatchSettings,
@@ -704,7 +708,7 @@ impl State {
704708
fn legacy_registration(&self) -> super::protocol::Registration {
705709
super::protocol::Registration {
706710
protocol: 1,
707-
instance: self.registration.instance.to_string(),
711+
instance: self.authentication.legacy_token.to_string(),
708712
repository: self.registration.repository.clone(),
709713
pid: self.registration.pid,
710714
port: self.registration.port,
@@ -1467,7 +1471,7 @@ fn respond(mut stream: TcpStream, request: Option<&Request>, state: &State, resu
14671471
let response = match result {
14681472
Ok(mut data) if request.is_some_and(|request| request.protocol == 1) => {
14691473
data["protocol"] = json!(1);
1470-
data["instance"] = json!(state.registration.instance);
1474+
data["instance"] = json!(state.authentication.legacy_token);
14711475
data["repository"] = json!(state.registration.repository);
14721476
json!({"jsonrpc":"2.0","id":id,"result":data})
14731477
}
@@ -1493,18 +1497,13 @@ fn respond(mut stream: TcpStream, request: Option<&Request>, state: &State, resu
14931497
}
14941498

14951499
struct DaemonRegistration {
1496-
path: PathBuf,
1497-
identity: managed::FileIdentity,
1498-
sentinel: (PathBuf, managed::FileIdentity),
1500+
files: Vec<(PathBuf, managed::FileIdentity)>,
14991501
_git_lock: File,
15001502
}
15011503

15021504
impl Drop for DaemonRegistration {
15031505
fn drop(&mut self) {
1504-
for (path, identity) in [
1505-
(&self.path, &self.identity),
1506-
(&self.sentinel.0, &self.sentinel.1),
1507-
] {
1506+
for (path, identity) in self.files.iter().rev() {
15081507
if let Err(error) = managed::remove_control_file(path, identity)
15091508
&& error.source_io_kind() != Some(std::io::ErrorKind::NotFound)
15101509
{
@@ -1545,6 +1544,8 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
15451544
port: listener.local_addr()?.port(),
15461545
storage: namespace.path().into(),
15471546
};
1547+
let authentication = Authentication::new(registration.clone())?;
1548+
let legacy_instance = authentication.legacy_token.clone();
15481549
let monitors = Arc::new(Mutex::new(HashMap::new()));
15491550
let observer_registration = registration.clone();
15501551
let observer_registry = Arc::clone(&monitors);
@@ -1590,7 +1591,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
15901591
json!(super::protocol::ViewRegistration {
15911592
daemon: super::protocol::Registration {
15921593
protocol: 1,
1593-
instance: observer_registration.instance.to_string(),
1594+
instance: legacy_instance.to_string(),
15941595
repository: observer_registration.repository.clone(),
15951596
pid: observer_registration.pid,
15961597
port: observer_registration.port,
@@ -1604,7 +1605,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16041605
json!({"protocol":2,"namespace":observer_registration.namespace,"instance":observer_registration.instance,
16051606
"managed":true,"view":slot.id()})
16061607
};
1607-
let sentinel_identity = managed::publish_control_file(&sentinel, &sentinel_value)?;
1608+
let sentinel_identity = managed::publish_private_control_file(&sentinel, &sentinel_value)?;
16081609
let monitor = Arc::new(Mutex::new(Monitor {
16091610
watcher: None,
16101611
watched: HashSet::new(),
@@ -1660,6 +1661,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16601661
let (jobs, jobs_rx) = mpsc::sync_channel(policy.work.queue_items as usize);
16611662
let state = Arc::new(State {
16621663
registration: registration.clone(),
1664+
authentication,
16631665
namespace,
16641666
views,
16651667
watches,
@@ -1675,21 +1677,29 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16751677
lifecycle_epoch: AtomicU64::new(1),
16761678
scheduler_snapshot: Mutex::new(json!({"initializing":true})),
16771679
});
1680+
let mut registration_guard = DaemonRegistration {
1681+
files: Vec::with_capacity(3),
1682+
_git_lock: git_lock,
1683+
};
1684+
let authentication_path = repository.common_dir().join(AUTH_MARKER);
1685+
let authentication_identity = managed::publish_private_control_file(
1686+
&authentication_path,
1687+
&serde_json::to_value(&state.authentication)?,
1688+
)?;
1689+
registration_guard
1690+
.files
1691+
.push((authentication_path, authentication_identity));
16781692
let marker = repository.common_dir().join(MARKER);
16791693
let marker_identity = managed::publish_control_file(&marker, &json!(registration))?;
1694+
registration_guard.files.push((marker, marker_identity));
16801695
let sentinel = repository.common_dir().join(super::MARKER);
16811696
let sentinel_value = if policy.storage == managed::policy::StorageMode::CompatibilityRetainAll {
16821697
json!(state.legacy_registration())
16831698
} else {
16841699
json!({"protocol":2,"namespace":registration.namespace,"instance":registration.instance})
16851700
};
1686-
let sentinel_identity = managed::publish_control_file(&sentinel, &sentinel_value)?;
1687-
let _registration = DaemonRegistration {
1688-
path: marker,
1689-
identity: marker_identity,
1690-
sentinel: (sentinel, sentinel_identity),
1691-
_git_lock: git_lock,
1692-
};
1701+
let sentinel_identity = managed::publish_private_control_file(&sentinel, &sentinel_value)?;
1702+
registration_guard.files.push((sentinel, sentinel_identity));
16931703
eprintln!(
16941704
"shared-v2 listening on {} (namespace {})",
16951705
registration.port, registration.namespace
@@ -1796,7 +1806,24 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
17961806
&mut stream,
17971807
managed::MAX_REQUEST_BYTES as u64,
17981808
)?;
1799-
let value: Value = serde_json::from_slice(&line)?;
1809+
let mut value: Value = serde_json::from_slice(&line)?;
1810+
let legacy_protocol = value["protocol"] == 1;
1811+
let authorization = if legacy_protocol {
1812+
value["instance"].as_str()
1813+
} else {
1814+
value["authorization"].as_str()
1815+
};
1816+
if !state.authentication.accepts(authorization, legacy_protocol) {
1817+
return Err(Error::new(
1818+
ErrorCategory::Permission,
1819+
"rpc-authentication-failed",
1820+
"managed RPC requires the current private daemon credential",
1821+
)
1822+
.into());
1823+
}
1824+
if !legacy_protocol && let Some(object) = value.as_object_mut() {
1825+
object.remove("authorization");
1826+
}
18001827
let request: Request = if value["protocol"] == 1 {
18011828
if state.namespace.header().storage
18021829
!= managed::policy::StorageMode::CompatibilityRetainAll
@@ -1811,7 +1838,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
18111838
jsonrpc: legacy.jsonrpc,
18121839
protocol: legacy.protocol,
18131840
namespace: state.registration.namespace.clone(),
1814-
instance: Id::parse(legacy.instance)?,
1841+
instance: state.registration.instance.clone(),
18151842
repository: legacy.repository,
18161843
method: legacy.method,
18171844
params: legacy.params,

‎tgrep-cli/src/serve/shared/protocol_v2.rs‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use tgrep_core::managed::{
1515

1616
pub const MARKER: &str = "tgrep-daemon-v2.json";
1717
pub const VIEW_MARKER: &str = "tgrep-view-v2.json";
18+
pub const AUTH_MARKER: &str = "tgrep-daemon-v2.auth.json";
1819

19-
#[derive(Clone, Debug, Serialize, Deserialize)]
20+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
2021
#[serde(deny_unknown_fields)]
2122
pub(super) struct Registration {
2223
pub protocol: u32,
@@ -49,8 +50,54 @@ pub(super) struct Request {
4950
pub id: Value,
5051
}
5152

53+
#[derive(Clone, Serialize, Deserialize)]
54+
#[serde(deny_unknown_fields)]
55+
pub(super) struct Authentication {
56+
pub registration: Registration,
57+
pub token: Id,
58+
pub legacy_token: Id,
59+
}
60+
61+
impl Authentication {
62+
pub fn new(registration: Registration) -> Result<Self> {
63+
Ok(Self {
64+
registration,
65+
token: Id::new()?,
66+
legacy_token: Id::new()?,
67+
})
68+
}
69+
70+
pub fn read(repository: &Repository, registration: &Registration) -> Result<Self> {
71+
let value = managed::read_private_control_file(&repository.common_dir().join(AUTH_MARKER))?;
72+
let authentication: Self = serde_json::from_value(value)?;
73+
if authentication.registration != *registration {
74+
return Err(Error::new(
75+
ErrorCategory::StaleIdentity,
76+
"rpc-registration-mismatch",
77+
"public daemon registration differs from its private credential",
78+
)
79+
.into());
80+
}
81+
Ok(authentication)
82+
}
83+
84+
pub fn accepts(&self, token: Option<&str>, legacy: bool) -> bool {
85+
let expected = if legacy {
86+
&self.legacy_token
87+
} else {
88+
&self.token
89+
};
90+
token.is_some_and(|token| {
91+
// Hash equality is constant-time; identity comparisons are not credentials.
92+
token.len() == expected.as_str().len()
93+
&& blake3::hash(token.as_bytes()) == blake3::hash(expected.as_str().as_bytes())
94+
})
95+
}
96+
}
97+
5298
pub(super) struct Client {
5399
pub registration: Registration,
100+
authentication: Authentication,
54101
}
55102

56103
impl Client {
@@ -65,15 +112,23 @@ impl Client {
65112
&& registration.port != 0,
66113
"incompatible managed daemon registration"
67114
);
68-
let client = Self { registration };
115+
let authentication = Authentication::read(repository, &registration)?;
116+
let client = Self {
117+
registration,
118+
authentication,
119+
};
69120
let hello = client.request("hello", json!({}))?;
70121
ensure!(
71122
hello["storage_schema"] == managed::STORAGE_VERSION
72123
&& hello["capabilities"]
73124
.as_array()
74-
.is_some_and(|capabilities| capabilities
75-
.iter()
76-
.any(|capability| capability == "versioned-views")),
125+
.is_some_and(|capabilities| {
126+
["versioned-views", "private-rpc-authentication"]
127+
.into_iter()
128+
.all(|required| {
129+
capabilities.iter().any(|capability| capability == required)
130+
})
131+
}),
77132
"daemon does not support managed view negotiation for {}",
78133
root.display()
79134
);
@@ -85,6 +140,7 @@ impl Client {
85140
let request = json!({
86141
"jsonrpc":"2.0","protocol":managed::PROTOCOL_VERSION,
87142
"namespace":registration.namespace,"instance":registration.instance,"repository":registration.repository,
143+
"authorization":self.authentication.token,
88144
"id":1,"method":method,"params":params
89145
});
90146
let bytes = serde_json::to_vec(&request)?;

0 commit comments

Comments
 (0)