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
19 changes: 19 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ TWIGPINE_DB_LOCK_POOL_MAX_CONNECTIONS=40
# twice this value plus the git work between them. Read it together with the pool
# size above and with TWIGPINE_GIT_SERVICE_TIMEOUT_SECS.
TWIGPINE_LOCK_HELD_TRANSFER_TIMEOUT_SECS=300
# Maximum seconds a repo write guard may hold its advisory-lock pool
# connection, measured from the moment the lock is taken. Every phase under
# the lock consumes this ONE deadline: the under-lock refresh, the caller's
# own under-lock work (for a push, the whole receive-pack subprocess, clamped
# to the lesser of TWIGPINE_GIT_SERVICE_TIMEOUT_SECS and the remaining
# budget), and the release-side upload/unlock. Without it a slow push pins a
# lock-pool slot for the SUM of every per-phase bound (~1500s worst case), so
# ~32 concurrent slow pushes deny every write on the node. Does NOT bound the
# acquire-side lock wait (TWIGPINE_GIT_ACQUIRE_TIMEOUT_SECS), post-git outcome
# bookkeeping, the ~4s the detached reaper holds the session while the git
# process group dies, or merge writes, which take their guards without this
# deadline: their git runs unbounded under the lock, and their refresh,
# publish and unlock keep their own bounds, so a slow merge is never rolled
# back for overrunning it. Issue writes keep the deadline. Unset, the
# effective bound is TWIGPINE_GIT_SERVICE_TIMEOUT_SECS itself (600 by
# default), so it tracks the git budget up or down. Set it only to tighten
# the hold below the git budget; a value above
# TWIGPINE_GIT_SERVICE_TIMEOUT_SECS is rejected at boot.
#TWIGPINE_WRITE_LOCK_HOLD_TIMEOUT_SECS=300
# Seconds a request waits for a pool connection before failing with 503.
TWIGPINE_DB_ACQUIRE_TIMEOUT_SECS=5
# Upper bound on each startup connect+migrate attempt, in seconds. Keep it
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Good today:

Known limitations:

- Repository write authorization is one switch: `TWIGPINE_ENFORCE_OWNER_PUSH`, which defaults to `true`. With it off — rolling upgrades only — a valid HTTP Signature identifies a pusher but does not authorize one, so every signed caller may push everywhere.
- UCAN capabilities are consulted on the push path only. There, the chain's root issuer is anchored to the repository owner, so an owner-rooted, time-bounded delegation that names the repository grants scoped collaborator access for pushing to it, for the `git/push`, `*`, or `repo/admin` actions. A `*` *resource* is refused: a delegation must name the repository it applies to, so it cannot grow to cover repos created later. Push-only cuts the other way too: a delegation grants no read access, so on a private repository the delegate must also be a reader (`twigpine visibility set`) — the two gates a push crosses are described in [`docs/RUN-A-NODE.md`](docs/RUN-A-NODE.md). The rest is unchanged: there is no revocation path, `nb` constraints are refused rather than interpreted, and no other route — reads, pull requests, issues, agents — consults capabilities at all.
- Repository write authorization is one switch: `TWIGPINE_ENFORCE_OWNER_PUSH`, which defaults to `true`. With it off (rolling upgrades only) a valid HTTP Signature identifies a pusher but does not authorize one, so every signed caller may push everywhere.
- UCAN capabilities are consulted on the push path only. There, the chain's root issuer is anchored to the repository owner, so an owner-rooted, time-bounded delegation that names the repository grants scoped collaborator access for pushing to it, for the `git/push`, `*`, or `repo/admin` actions. A `*` *resource* is refused: a delegation must name the repository it applies to, so it cannot grow to cover repos created later. Push-only cuts the other way too: a delegation grants no read access, so on a private repository the delegate must also be a reader (`twigpine visibility set`); the two gates a push crosses are described in [`docs/RUN-A-NODE.md`](docs/RUN-A-NODE.md). The rest is unchanged: there is no revocation path, `nb` constraints are refused rather than interpreted, and no other route (reads, pull requests, issues, agents) consults capabilities at all.
- Agent lifecycle revocation is not enforced by HTTP Signature authorization; do not rely on removing or revoking an agent record to block a compromised signer.
- Task list/get reads (REST and GraphQL) are repository/task-gated and omit `ucan_token`. Slash-form `repo_id` rows (mirror-hosted tasks) can appear on list for the delegator or trimmed assignee, but `GET /api/v1/tasks/{id}` and GraphQL `task` fail closed for every caller because there is no local repository backing to serve. Read visibility is not a blanket data-classification boundary: IPFS-pin and Arweave-anchor listings are not repository-gated; withheld path names can be visible to a root reader; and later visibility changes cannot retract content already announced or externally anchored.
- Peer writes are signed by upgraded nodes, but strict signed-peer enforcement is opt-in during rolling upgrades.
Expand Down Expand Up @@ -427,12 +427,13 @@ Important node settings:
| `TWIGPINE_BOOTSTRAP_DISABLE_SEEDS` | Disable embedded seed peers for isolated dev/test networks. |
| `TWIGPINE_REQUIRE_SIGNED_PEER_WRITES` | Require signed peer announce/sync writes. Defaults to `false` during the staged rollout below. |
| `TWIGPINE_REQUIRE_SIGNATURE_NONCE` | Require a signed `nonce` on write routes. Default false; see the staged rollout note above. |
| `TWIGPINE_ENFORCE_OWNER_PUSH` | Require the pusher on `git-receive-pack` to be the repo owner, or to present an owner-rooted, time-bounded UCAN `git/push` capability naming the repository (`X-Ucan`). **Defaults to `true`.** A `did:key` signature is authentication, not authorization — anyone can mint a key and sign — so with this off every signed caller may push to every repository, private ones included. A CI or delegated key with no such delegation counts as a non-owner and cannot push while this is on. The delegation clears the push gate only; on a private repository the delegate must also be a reader. Set `false` only for a rolling upgrade; see [`docs/RUN-A-NODE.md`](docs/RUN-A-NODE.md). |
| `TWIGPINE_ENFORCE_OWNER_PUSH` | Require the pusher on `git-receive-pack` to be the repo owner, or to present an owner-rooted, time-bounded UCAN `git/push` capability naming the repository (`X-Ucan`). **Defaults to `true`.** A `did:key` signature is authentication, not authorization (anyone can mint a key and sign), so with this off every signed caller may push to every repository, private ones included. A CI or delegated key with no such delegation counts as a non-owner and cannot push while this is on. The delegation clears the push gate only; on a private repository the delegate must also be a reader. Set `false` only for a rolling upgrade; see [`docs/RUN-A-NODE.md`](docs/RUN-A-NODE.md). |
| `TWIGPINE_AUTO_SYNC` | Enable automatic sync from known peers. |
| `TWIGPINE_RECONCILIATION_SWEEP` | Enable the hourly durability sweep that re-pins/backstops missing objects (default `true`; disabled when no IPFS/Pinata backend is configured). Public pin repair runs against any configured backend (IPFS, Pinata, or both). Encrypted recovery repair requires local IPFS (`TWIGPINE_IPFS_API`); Pinata-only nodes reconcile public pins only. |
| `TWIGPINE_MAX_PACK_BYTES` | Max git pack body size for smart-HTTP routes. |
| `TWIGPINE_GIT_SERVICE_TIMEOUT_SECS` | Max seconds a served git upload-pack, receive-pack, or `info/refs` advertisement may run before it is aborted (504). Default 600. Also bounds the withheld-blob classification walk (on both the upload-pack serve and receive-pack replication paths) and the push-side pin-candidate discovery (`rev-list` / `cat-file`), each reaped via process-group teardown at the deadline. On the path-scoped upload-pack path the classification walk and the pack serve share ONE deadline, so this value bounds their combined duration rather than granting each stage a full budget: a walk that consumes it leaves the serve nothing and the clone gets a 504. Serving large path-scoped repos may therefore need a higher value than they did when each stage was budgeted separately. Accepted range is 1 to 3153600000 (100 years), since the node derives deadlines from this value and a larger one cannot be represented. |
| `TWIGPINE_GIT_ACQUIRE_TIMEOUT_SECS` | Max seconds the storage-acquisition phase (Tigris HEAD/GET, push advisory-lock) of a served git op may run before the request is shed with a 503, separate from the git-run timeout. The concurrency permit is released on expiry so a stalled backend cannot pin the pool. Default 30. |
| `TWIGPINE_WRITE_LOCK_HOLD_TIMEOUT_SECS` | Max seconds a repo write guard may hold its advisory-lock pool connection, measured from lock-take. One deadline covers every phase under the lock: the under-lock refresh, the caller's under-lock work (for a push, the whole `receive-pack` subprocess, clamped to the lesser of `TWIGPINE_GIT_SERVICE_TIMEOUT_SECS` and the remaining budget), and the release-side upload/unlock. It bounds the lock-pool slot's hold, so a slow push cannot starve other writers for the sum of every per-phase bound. It does not bound the acquire-side lock wait (`TWIGPINE_GIT_ACQUIRE_TIMEOUT_SECS`), post-git outcome bookkeeping, the few seconds the detached reaper holds the session while the git process group dies, or merge writes, which take their guards without this deadline: their git runs unbounded under the lock, and their refresh, publish and unlock keep their own bounds, so a slow merge is never rolled back for overrunning it. Issue writes keep the deadline. Unset, the effective bound is `TWIGPINE_GIT_SERVICE_TIMEOUT_SECS` itself (600 by default), tracking the git budget; set it only to tighten the hold below the git budget. An explicit value above `TWIGPINE_GIT_SERVICE_TIMEOUT_SECS` is rejected at boot. |
| `TWIGPINE_MAX_CONCURRENT_GIT_OPS` | Max concurrent served git READ ops (upload-pack and its `info/refs` advertisement) across all callers; over-cap sheds a 503 + Retry-After. Anonymous reads draw from this pool, so pair it with `TWIGPINE_MAX_CONCURRENT_READS_PER_CALLER`. Pushes and the receive-pack advertisement have their own pools, so a read flood cannot shed an authenticated push. Default 128. |
| `TWIGPINE_MAX_CONCURRENT_GIT_PUSHES` | Max concurrent `git-receive-pack` POST operations, in a pool separate from the read pool. The anon receive-pack `info/refs` advertisement runs in a third pool of the same size, disjoint from both, so an advertisement flood cannot shed a push either. Two per-source push caps are derived from this value (`/8`, floor 1) and have no env var of their own. Over-cap sheds a 503 + Retry-After. Default 32. |
| `TWIGPINE_MAX_CONCURRENT_READS_PER_CALLER` | Max concurrent read ops a single caller may hold, so one caller cannot monopolize the read pool. Keyed on the resolved source IP, never the DID, and only as granular as `TWIGPINE_TRUSTED_PROXY`: left unset, a node behind an edge or NAT keys every caller on the edge IP and this collapses to one global cap. Default 16. |
Expand Down
51 changes: 51 additions & 0 deletions crates/twigpine-node/src/api/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,57 @@ mod lock_pool_shed_tests {
server.abort();
}

/// #282: issue writes keep the lock-hold deadline. `create_issue` is
/// reachable by any signed DID on a public repo, and its git is one small
/// ref write, so the stamped deadline is what bounds its refresh and
/// publish. Under a zero-length operator timeout the create must refuse,
/// and no issue ref may be left on disk.
#[sqlx::test]
async fn create_issue_keeps_the_lock_hold_deadline(pool: PgPool) {
use crate::git::repo_store::tests as rs_tests;

let mock = rs_tests::S3Mock::start().await;
let owner = "did:key:zISSUENOHOLDAAAAAAAAAAAAAAAAAAAAAAAA";
let repo_name = "nohold-create";
let mut state = crate::test_support::test_state(pool.clone()).await;
let disk = tempfile::TempDir::new().unwrap();
let mut seeded = seed_repo(owner, repo_name);
let live = crate::git::repo_store::validated_repo_disk_path(disk.path(), owner, repo_name)
.expect("repo path");
seeded.disk_path = live.to_string_lossy().to_string();
state.db.create_repo(&seeded).await.expect("seed repo");
crate::git::store::init_bare(&live).expect("init bare repo");
state.repo_store = crate::git::repo_store::RepoStore::for_testing_with_tigris(
disk.path().to_path_buf(),
crate::git::repo_store::build_lock_pool(&pool, 2, std::time::Duration::from_secs(1)),
rs_tests::mock_tigris(&mock),
)
.with_lock_hold_timeout(std::time::Duration::ZERO);

let res = create_issue(
State(state.clone()),
Extension(AuthenticatedDid(owner.to_string())),
Path((owner.to_string(), repo_name.to_string())),
Json(CreateIssueRequest {
title: "t".to_string(),
body: None,
signed_payload: None,
}),
)
.await;
assert!(
res.is_err(),
"a stamped-and-expired hold deadline must refuse the create"
);
let issues = git_issues::list_issues(&live).expect("list issues");
assert!(
issues.is_empty(),
"no issue ref may be left on disk after the refused create, got {issues:?}"
);

mock.shutdown();
}

/// The compensating rewrite after a refused publish must land on the ref the
/// close actually mutated. A close addressed by an 8-char prefix resolves to
/// the full id and closes `refs/gitlawb/issues/{full_id}`; a compensator
Expand Down
82 changes: 81 additions & 1 deletion crates/twigpine-node/src/api/pulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub async fn merge_pr(

let guard = state
.repo_store
.acquire_write(&record.owner_did, &record.name)
.acquire_write_without_hold_deadline(&record.owner_did, &record.name)
.await
.map_err(|e| crate::api::repos::acquire_write_app_error(&e, &name))?;
let disk_path = guard.path().to_path_buf();
Expand Down Expand Up @@ -878,6 +878,86 @@ mod merge_rollback_tests {
"a refused publish must rewind the local merge to the pre-merge tip"
);
}

/// #282: merge never consults the hold deadline, so it takes its guard
/// without one (`acquire_write_without_hold_deadline`). A merge that lands
/// under a zero-length operator timeout still publishes; a stamped
/// deadline would rewind the completed merge and 5xx the caller.
#[sqlx::test]
async fn merge_pr_without_hold_deadline_publishes(pool: PgPool) {
use crate::git::repo_store::tests as rs_tests;

let mock = rs_tests::S3Mock::start().await;
let owner = "did:key:zMERGENOHOLDAAAAAAAAAAAAAAAAAAAAAAAA";
let repo_name = "nohold-merge";
let mut state = crate::test_support::test_state(pool.clone()).await;
let disk = tempfile::TempDir::new().unwrap();
state.repo_store = crate::git::repo_store::RepoStore::for_testing_with_tigris(
disk.path().to_path_buf(),
crate::git::repo_store::build_lock_pool(&pool, 2, std::time::Duration::from_secs(1)),
rs_tests::mock_tigris(&mock),
)
.with_lock_hold_timeout(std::time::Duration::ZERO);
let live = crate::git::repo_store::validated_repo_disk_path(disk.path(), owner, repo_name)
.expect("repo path");
let (main_tip, _feature_tip) = seeded_merge_repo(&live);

let mut seeded = crate::db::RepoRecord {
id: Uuid::new_v4().to_string(),
name: repo_name.to_string(),
owner_did: owner.to_string(),
description: None,
is_public: true,
default_branch: "main".to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
disk_path: String::new(),
forked_from: None,
machine_id: None,
};
seeded.disk_path = live.to_string_lossy().to_string();
let repo_id = seeded.id.clone();
state.db.create_repo(&seeded).await.expect("seed repo");
let now = Utc::now().to_rfc3339();
state
.db
.create_pr(&PullRequest {
id: Uuid::new_v4().to_string(),
repo_id: repo_id.clone(),
number: 1,
title: "nohold".to_string(),
body: None,
author_did: owner.to_string(),
source_branch: "feature".to_string(),
target_branch: "main".to_string(),
status: "open".to_string(),
merged_by_did: None,
merged_at: None,
head_commit: None,
created_at: now.clone(),
updated_at: now,
})
.await
.expect("seed PR");

let res = merge_pr(
State(state.clone()),
Extension(AuthenticatedDid(owner.to_string())),
Path((owner.to_string(), repo_name.to_string(), 1)),
)
.await;
let _ = res.expect("a merge with no hold deadline must publish");

let merged = state.db.get_pr(&repo_id, 1).await.unwrap().unwrap();
assert_eq!(merged.status, "merged");
let tip_after = git(&live, &["rev-parse", "refs/heads/main"]);
assert_ne!(
tip_after, main_tip,
"the merge commit must stay on main, not be rewound"
);

mock.shutdown();
}
}

/// #173 F1 follow-up: merge holds no admission permit, so an exhausted write-lock
Expand Down
Loading
Loading