Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 76 additions & 13 deletions loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,7 @@ fn start_with(
register(workspace, &mut existing, &repository, base)?;
existing
} else {
let gh_config = std::env::var_os("GH_CONFIG_DIR")
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".config/gh")))
.filter(|path| path.is_dir())
.map(|path| path.canonicalize())
.transpose()?;
if let Some(path) = &gh_config {
for parent in path.ancestors() {
if parent.join(".git").exists() {
bail!("forge credentials must be outside every repository/worktree");
}
}
}
let gh_config = resolve_gh_config(workspace)?;
Config {
schema_version: 1,
account: account.id.name.clone(),
Expand Down Expand Up @@ -332,6 +320,81 @@ fn start_with(
snapshot(config, &dir)
}

/// The forge configuration directory a NEW private workspace records and
/// binds read-only at [`GH_CONFIG`], or `None` when none may be mounted.
///
/// The daemon-owned GitHub App directories under the workspace root's own
/// `.loom/` are deliberately not recorded ([`daemon_owned_gh_config`]): an
/// App-only host delivers its installation token per exec as `GH_TOKEN`
/// (`forge_auth`, #13916), so binding the directory would only re-expose
/// the credential inside the seat — and it lives inside the daemon's own
/// checkout, which the ancestor check below rightly refuses. Before #14744
/// that refusal kept unattended `session_reconcile` from ever creating a
/// seat on an App-only host, because the daemon activates that directory
/// as its process-global `GH_CONFIG_DIR` at startup. Every other directory
/// at or under a git checkout keeps the refusal.
fn resolve_gh_config(workspace: &Path) -> Result<Option<PathBuf>> {
resolve_gh_config_with(
workspace,
std::env::var_os("GH_CONFIG_DIR"),
dirs::home_dir().map(|home| home.join(".config/gh")),
crate::credential_preflight::primary_workspace_root(),
)
}

/// [`resolve_gh_config`] over an injected `GH_CONFIG_DIR` value and
/// home-default directory (tests): the same resolution and the same
/// ancestor refusal for every directory the daemon does not own.
pub(super) fn resolve_gh_config_with(
workspace: &Path,
gh_config_dir: Option<std::ffi::OsString>,
home_default: Option<PathBuf>,
primary_root: Option<PathBuf>,
) -> Result<Option<PathBuf>> {
let resolved = gh_config_dir
.map(PathBuf::from)
.or(home_default)
.filter(|path| path.is_dir())
.map(|path| path.canonicalize())
.transpose()?
.filter(|path| !daemon_owned_gh_config(workspace, primary_root.as_deref(), path));
if let Some(path) = &resolved {
for parent in path.ancestors() {
if parent.join(".git").exists() {
bail!("forge credentials must be outside every repository/worktree");
}
}
}
Ok(resolved)
}

/// `true` when `path` (already canonicalized) is one of the daemon-owned
/// GitHub App credential directories: the process-global `GH_CONFIG_DIR` the
/// daemon activates at startup
/// ([`crate::credential_preflight::github_app_gh_config_dir`]) or a per-owner
/// sibling ([`github_app_gh_config_dir_for_owner`]), under either the daemon's
/// primary workspace root (the credential owner: startup config belongs to it,
/// whichever registered checkout acts) or the acting `workspace`.
///
/// Both sides are compared canonicalized: `path` already is, so each expected
/// location is canonicalized too (an aliased `.loom` or `gh-config` resolves
/// to the same place). A root that cannot be resolved owns nothing, so the
/// caller's refusal stands.
fn daemon_owned_gh_config(workspace: &Path, primary_root: Option<&Path>, path: &Path) -> bool {
std::iter::once(workspace)
.chain(primary_root)
.filter_map(|root| root.canonicalize().ok())
.any(|root| {
let canon = |p: PathBuf| p.canonicalize().unwrap_or(p);
if path == canon(crate::credential_preflight::github_app_gh_config_dir(&root)) {
return true;
}
let owners = canon(root.join(".loom").join("gh-config-by-owner"));
path.strip_prefix(&owners)
.is_ok_and(|rest| rest.components().count() == 1)
})
}

/// Unattended recovery of an already-configured private session whose
/// container is stopped or gone (#12772). Unlike [`start`] it never provisions:
/// it resolves the persisted identity, resumes a stopped container or recreates
Expand Down
142 changes: 142 additions & 0 deletions loom-daemon/src/tokens_pool/private_workspace/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,148 @@ fn https_remote_rejects_credentials_and_unsupported_transports() {
}
}

#[test]
fn daemon_owned_gh_config_dir_under_a_checkout_is_not_recorded() {
let ws = tempfile::tempdir().expect("tempdir");
let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path());
std::fs::create_dir_all(&dir).expect("app dir");
std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout");
assert!(matches!(
lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None),
Ok(None)
));
}

#[test]
fn a_per_owner_daemon_gh_config_dir_under_a_checkout_is_not_recorded() {
let ws = tempfile::tempdir().expect("tempdir");
let dir = crate::credential_preflight::github_app_gh_config_dir_for_owner(ws.path(), "org");
std::fs::create_dir_all(&dir).expect("owner dir");
std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout");
assert!(matches!(
lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None),
Ok(None)
));
}

#[test]
fn a_symlinked_spelling_of_the_daemon_owned_gh_config_dir_is_not_recorded() {
use std::os::unix::fs::symlink;
let ws = tempfile::tempdir().expect("tempdir");
let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path());
std::fs::create_dir_all(&dir).expect("app dir");
symlink(&dir, ws.path().join("linked-gh-config")).expect("symlink");
let linked = ws.path().join("linked-gh-config");
assert!(matches!(
lifecycle::resolve_gh_config_with(ws.path(), Some(linked.into_os_string()), None, None),
Ok(None)
));
}

#[test]
fn a_gh_config_dir_under_a_checkout_the_daemon_does_not_own_is_refused() {
let ws = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(ws.path().join(".git")).expect("checkout");
let dir = ws.path().join("credentials/gh");
std::fs::create_dir_all(&dir).expect("gh dir");
let error =
lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None)
.expect_err("a foreign dir under a checkout must be refused");
assert!(error
.to_string()
.contains("forge credentials must be outside every repository/worktree"));
}

#[test]
fn a_home_default_gh_dir_under_a_checkout_is_refused() {
let ws = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(ws.path().join(".git")).expect("checkout");
let home_default = ws.path().join(".config/gh");
std::fs::create_dir_all(&home_default).expect("gh dir");
let error = lifecycle::resolve_gh_config_with(ws.path(), None, Some(home_default), None)
.expect_err("the home-default dir under a checkout must be refused");
assert!(error
.to_string()
.contains("forge credentials must be outside every repository/worktree"));
}

#[test]
fn a_gh_config_dir_outside_every_checkout_is_recorded() {
let ws = tempfile::tempdir().expect("tempdir");
let elsewhere = tempfile::tempdir().expect("tempdir");
let dir = elsewhere.path().join("gh");
std::fs::create_dir_all(&dir).expect("gh dir");
assert_eq!(
lifecycle::resolve_gh_config_with(
ws.path(),
Some(dir.clone().into_os_string()),
None,
None
)
.expect("an ordinary dir is recorded"),
Some(dir.canonicalize().expect("canonical"))
);
}

#[test]
fn the_primary_workspace_app_dir_is_not_recorded_for_a_distinct_acting_checkout() {
let primary = tempfile::tempdir().expect("tempdir");
let acting = tempfile::tempdir().expect("tempdir");
let dir = crate::credential_preflight::github_app_gh_config_dir(primary.path());
std::fs::create_dir_all(&dir).expect("app dir");
std::fs::create_dir_all(primary.path().join(".git")).expect("daemon checkout");
std::fs::create_dir_all(acting.path().join(".git")).expect("acting checkout");
let primary_root = Some(primary.path().to_path_buf());
assert!(matches!(
lifecycle::resolve_gh_config_with(
acting.path(),
Some(dir.clone().into_os_string()),
None,
primary_root
),
Ok(None)
));
// Without the primary root the acting checkout does not own it: refused.
assert!(lifecycle::resolve_gh_config_with(
acting.path(),
Some(dir.into_os_string()),
None,
None
)
.is_err());
}

#[test]
fn an_aliased_app_dir_location_is_not_recorded() {
use std::os::unix::fs::symlink;
let ws = tempfile::tempdir().expect("tempdir");
let real = ws.path().join("real-gh-config");
std::fs::create_dir_all(&real).expect("real dir");
std::fs::create_dir_all(ws.path().join(".loom")).expect(".loom");
symlink(&real, ws.path().join(".loom/gh-config")).expect("symlink");
std::fs::create_dir_all(ws.path().join(".git")).expect("checkout");
let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path());
assert!(matches!(
lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None),
Ok(None)
));
}

#[test]
fn an_unrelated_dir_under_the_primary_checkout_is_still_refused() {
let primary = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(primary.path().join(".git")).expect("checkout");
let dir = primary.path().join("credentials/gh");
std::fs::create_dir_all(&dir).expect("gh dir");
assert!(lifecycle::resolve_gh_config_with(
primary.path(),
Some(dir.into_os_string()),
None,
Some(primary.path().to_path_buf())
)
.is_err());
}

fn config() -> Config {
Config {
schema_version: 1,
Expand Down
Loading