Files
PiCloud/crates/manager-core/tests/projects_repo.rs
MechaCat02 86448beb06 fix(security): server-authoritative approval gate + auth/session/file hardening
Remediate the HIGH and security-relevant findings from the 2026-07-11 audit.

H1 — the per-env approval gate is now server-authoritative. The governing
project is resolved from the target node's nearest-claimed ancestor
(`governing_env_policy`/`_tree` + `governing_project_id` +
`ProjectRepository::get_environments_by_id`), independent of the client-supplied
`[project]`. Omitting or spoofing the project block can no longer skip a gate the
owning project established; a to-create group resolves its declared parent's
chain so a fresh subtree node inherits the gate. Fails closed on any read error.

H2 — the API-key prefix slice (`&rest[..8]`) is now the boundary-safe
`rest.get(..8)`, so an attacker-supplied multibyte bearer can't panic the request
task (unauthenticated per-request DoS). Regression test added.

C1 — admin sessions gain an absolute lifetime cap (migration 0070,
`PICLOUD_SESSION_ABSOLUTE_TTL_HOURS`, default 30d): `lookup` filters it, `touch`
clamps the sliding bump to it, so a continuously-used or stolen-but-warm token
self-expires. Mirrors the data-plane app-user cap.

C2 — `Cache-Control: no-store` on the login and API-key-mint responses (the two
that return a raw credential), so a proxy/CDN/browser cache can't retain it.

B8 — file downloads are header-safe: `sanitize_stored_filename` guarantees a
valid `HeaderValue` (no panic on a control-char name) and BOTH the per-app and
group download paths now set attachment + `X-Content-Type-Options: nosniff` +
a restrictive CSP, closing a group-path stored-XSS gap.

Also folds in the server-side plan-warning plumbing (`plan_warnings`,
`PlanResult::warnings`) and the `app_only_reject` message helper that the CLI
plan-preview change builds on, plus operator security notes (reads-open shared-
topic SSE; the `--env` label is advisory, not a boundary).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 23:47:35 +02:00

162 lines
5.8 KiB
Rust

//! §7 repo round-trip: `list_with_owner` pairs each group with its owning
//! project's slug (`None` when unclaimed), and `ancestors()` surfaces
//! `owner_project` for every node — the nearest-claimed fold the ownership
//! claim path (M1.3) relies on.
//!
//! Deterministic; skips cleanly when `DATABASE_URL` is unset.
use picloud_manager_core::group_repo::{GroupRepository, PostgresGroupRepository};
use picloud_manager_core::project_repo::{PostgresProjectRepository, ProjectRepository};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use uuid::Uuid;
async fn pool_or_skip() -> Option<PgPool> {
let Ok(url) = std::env::var("DATABASE_URL") else {
eprintln!("projects_repo: DATABASE_URL unset — skipping");
return None;
};
let pool = PgPoolOptions::new()
.max_connections(2)
.connect(&url)
.await
.expect("connect to DATABASE_URL");
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("apply migrations");
Some(pool)
}
fn uniq(prefix: &str) -> String {
format!("{prefix}-{}", &Uuid::new_v4().to_string()[..8])
}
#[tokio::test]
async fn list_with_owner_and_ancestors_surface_ownership() {
let Some(pool) = pool_or_skip().await else {
return;
};
let groups = PostgresGroupRepository::new(pool.clone());
let projects = PostgresProjectRepository::new(pool.clone());
// A project + a parent/child group pair; claim the CHILD only (a parent is
// deliberately left unclaimed to pin the `None` path).
let proj_slug = uniq("proj");
let (proj_id,): (Uuid,) =
sqlx::query_as("INSERT INTO projects (slug, name) VALUES ($1, $1) RETURNING id")
.bind(&proj_slug)
.fetch_one(&pool)
.await
.expect("insert project");
let parent = groups
.create(&uniq("parent"), "Parent", None, None)
.await
.expect("create parent");
let child_slug = uniq("child");
let child = groups
.create(&child_slug, "Child", None, Some(parent.id))
.await
.expect("create child");
sqlx::query("UPDATE groups SET owner_project = $1 WHERE id = $2")
.bind(proj_id)
.bind(child.id.into_inner())
.execute(&pool)
.await
.expect("claim child");
// list_with_owner: the child pairs with the project slug; the parent is
// unclaimed (None). (The list spans the whole instance — filter by slug.)
let listed = groups.list_with_owner().await.expect("list_with_owner");
let child_row = listed
.iter()
.find(|(g, _)| g.slug == child_slug)
.expect("child present in list_with_owner");
assert_eq!(child_row.1.as_deref(), Some(proj_slug.as_str()));
let parent_row = listed
.iter()
.find(|(g, _)| g.id == parent.id)
.expect("parent present");
assert_eq!(parent_row.1, None, "unclaimed group has no owner slug");
// ancestors(child) is nearest-first and carries owner_project on each node —
// the child holds its claim, the parent is None. This is the exact fold
// input the nearest-claimed resolution walks.
let chain = groups.ancestors(child.id).await.expect("ancestors");
assert_eq!(chain[0].id, child.id, "ancestors is nearest-first");
assert!(
chain[0].owner_project.is_some(),
"child ancestor row carries its claim"
);
let parent_in_chain = chain
.iter()
.find(|g| g.id == parent.id)
.expect("parent in chain");
assert!(parent_in_chain.owner_project.is_none());
// Project reads round-trip by slug and by id.
let by_slug = projects.get_by_slug(&proj_slug).await.expect("get_by_slug");
assert_eq!(by_slug.as_ref().map(|p| p.id.into_inner()), Some(proj_id));
let by_id = projects
.get_by_id(by_slug.unwrap().id)
.await
.expect("get_by_id");
assert_eq!(by_id.map(|p| p.slug), Some(proj_slug));
}
#[tokio::test]
async fn get_environments_by_slug_returns_persisted_policy() {
// §3 M3 hermetic gate: the persisted per-env policy round-trips by project
// slug. Backs `ApplyService::governing_env_policy` (the `governing` half).
let Some(pool) = pool_or_skip().await else {
return;
};
let projects = PostgresProjectRepository::new(pool.clone());
let proj_slug = uniq("penv");
let (proj_id,): (Uuid,) =
sqlx::query_as("INSERT INTO projects (slug, name) VALUES ($1, $1) RETURNING id")
.bind(&proj_slug)
.fetch_one(&pool)
.await
.expect("insert project");
for (env, confirm) in [("production", true), ("staging", false)] {
sqlx::query(
"INSERT INTO project_environments (project_id, env_name, confirm) VALUES ($1, $2, $3)",
)
.bind(proj_id)
.bind(env)
.bind(confirm)
.execute(&pool)
.await
.expect("insert env policy");
}
let policy = projects
.get_environments_by_slug(&proj_slug)
.await
.expect("get_environments_by_slug");
assert_eq!(policy.get("production"), Some(&true));
assert_eq!(policy.get("staging"), Some(&false));
assert_eq!(policy.len(), 2);
// An unknown slug yields an empty policy (no gate).
let empty = projects
.get_environments_by_slug("no-such-project")
.await
.expect("get_environments_by_slug");
assert!(empty.is_empty());
// Audit 2026-07-11 H1: the gate now loads the policy by the SERVER-resolved
// project id (from the node's ownership walk), not the client slug. The
// by-id read must return the same persisted policy as the by-slug read.
let by_id = projects
.get_environments_by_id(proj_id.into())
.await
.expect("get_environments_by_id");
assert_eq!(by_id.get("production"), Some(&true));
assert_eq!(by_id.get("staging"), Some(&false));
assert_eq!(by_id.len(), 2);
}