Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6633d17
fix(node): bound REST blob reads
Sep 7, 2026
95d295c
test(node): cover blob authorization denials.
Sep 7, 2026
d824cfa
fix(node): Hide Git diagnostics from blob error responses.
Sep 9, 2026
3d7032b
fix(node): close REST blob admission and error gaps
Sep 9, 2026
fc2d043
fix(node): acquire blob permits pre-DB and cover route/fallback arms
Sep 10, 2026
c63527c
fix(node): confirm blob absence against a readable store
Sep 12, 2026
8977ed4
fix(node): bound REST blob response delivery
Sep 12, 2026
e1fe66e
test(git): distinguish every bounded ref fallback
Sep 12, 2026
4344b77
docs(node): describe the cat-file blob read path
Sep 12, 2026
49aebb2
fix(node): prevent caching of authorized blob content
Sep 12, 2026
c566455
fix(node): pin non-blob denial and map unreadable store to 503
Sep 12, 2026
df5a283
style(repos): simplify blob semaphore permit assertion for clippy
Sep 13, 2026
f0d0025
fix(git): classify unreadable store with ProbeError and tighten blob …
Sep 13, 2026
eb1222d
test(node): assert full blob admission release after read errors
Sep 14, 2026
c15acf5
test(git): require probe completion before reprobe budget denial
Sep 14, 2026
77e9b76
test(node): make Windows regression fixtures portable
Sep 14, 2026
5d238ca
fix(sync): use a supported Windows promisor blob filter
Sep 14, 2026
2c7f60a
test(git): route write-then-exec fixtures through write_blob_probe_fi…
Sep 15, 2026
2bb398e
test(sync): assert configured partialclonefilter and pin platform con…
Sep 15, 2026
bbab52e
test(api,git): close write-fd window in exec'd fixtures
Sep 17, 2026
ad003b3
fix(api): clamp authorize_repo_read with acquire timeout in get_blob
Sep 18, 2026
a2c996a
docs(config): describe the blob authorization deadline
Sep 20, 2026
5f588c1
test(api): bound the blob authorization timeout regression
Sep 20, 2026
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
Next Next commit
fix(node): bound REST blob reads
Resolve REST blob paths to immutable object IDs, enforce size and output ceilings, and retain admission through response delivery.

Refs #204
  • Loading branch information
euxaristia
euxaristia committed Sep 7, 2026
commit 6633d1795607a94f29f4d621885dbf9d22ac4933
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ GITLAWB_GIT_SERVICE_TIMEOUT_SECS=600
# cost centers. Must be positive; set very large to effectively disable. Default 30.
GITLAWB_GIT_ACQUIRE_TIMEOUT_SECS=30

# Max concurrent git READ ops (upload-pack + the upload-pack info/refs
# advertisement) served at once, a global pool separate from the push pool below.
# Max concurrent git READ ops (upload-pack, its info/refs advertisement, and REST
# blob reads) served at once, a global pool separate from the push pool below.
# REST blobs also have a fixed four-response sub-pool and a 32 MiB per-response
# ceiling; their permits remain held until the response body finishes or disconnects.
# The anon receive-pack info/refs advertisement has its OWN pool (see below), not
# this one. Over-cap sheds a clean 503 + Retry-After. Anonymous reads draw from
# here, so pair it with GITLAWB_MAX_CONCURRENT_READS_PER_CALLER (below) so one
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ Important node settings:
| `GITLAWB_ENFORCE_OWNER_PUSH` | Require the authenticated pusher to be the repo owner on `git-receive-pack`. **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. Delegated and CI keys count as non-owners: a UCAN `git/push` capability is verified but not yet honored for authorization, so they cannot push while this is on. Set `false` only for a rolling upgrade; see [`docs/RUN-A-NODE.md`](docs/RUN-A-NODE.md). |
| `GITLAWB_AUTO_SYNC` | Enable automatic sync from known peers. |
| `GITLAWB_MAX_PACK_BYTES` | Max git pack body size for smart-HTTP routes. |
| `GITLAWB_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. |
| `GITLAWB_GIT_SERVICE_TIMEOUT_SECS` | Max seconds a served git upload-pack, receive-pack, `info/refs` advertisement, or REST blob read 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. |
| `GITLAWB_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. |
| `GITLAWB_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 `GITLAWB_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. |
| `GITLAWB_MAX_CONCURRENT_GIT_OPS` | Max concurrent served git READ ops (upload-pack, its `info/refs` advertisement, and REST blob reads) across all callers; over-cap sheds a 503 + Retry-After. Anonymous reads draw from this pool, so pair it with `GITLAWB_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. REST blob responses are limited to 32 MiB each and a dedicated four-request pool holds admission through body delivery, bounding retained blob data to 128 MiB. |
| `GITLAWB_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. |
| `GITLAWB_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 `GITLAWB_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. |
| `GITLAWB_MAX_CONCURRENT_PIN_TASKS` | Max post-push pin loops (IPFS + Pinata) running concurrently across all repos. This caps how many loops RUN at once, not how much object-id list memory the node retains: on the local IPFS path a loop parked waiting for a permit still holds its full list. Do not size memory from this knob alone. A loop over cap waits, never drops a pin. Default 8. |
Expand Down
185 changes: 161 additions & 24 deletions crates/gitlawb-node/src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::extract::{Extension, Path, Query, State};
use axum::http::StatusCode;
use axum::response::Response;
use axum::Json;
use bytes::Bytes;
use bytes::{Buf, Bytes};
use std::sync::Arc;

use crate::auth::{caller_authorized_to_push, AuthenticatedDid};
Expand All @@ -21,6 +21,40 @@ use crate::webhooks;
/// The git all-zeros object id — the create/delete sentinel in a ref update.
const ZERO_SHA: &str = "0000000000000000000000000000000000000000";

/// REST blob responses share the same served-object ceiling as `/ipfs/{cid}`.
const MAX_SERVED_BLOB_BYTES: u64 = crate::api::ipfs::MAX_SERVED_OBJECT_BYTES;

/// A dedicated pool bounds retained REST blob bodies independently of smart-HTTP
/// traffic. At the per-response ceiling, the default caps live blob data at 128 MiB.
pub(crate) const MAX_CONCURRENT_BLOB_READS: usize = 4;

const BLOB_RESPONSE_CHUNK_BYTES: usize = 64 * 1024;

struct BlobResponseStream {
content: Bytes,
_git_permit: tokio::sync::OwnedSemaphorePermit,
_blob_permit: tokio::sync::OwnedSemaphorePermit,
_caller_permit: Option<crate::rate_limit::PerCallerPermit>,
}

impl futures::Stream for BlobResponseStream {
type Item = std::result::Result<Bytes, std::convert::Infallible>;

fn poll_next(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
let this = self.get_mut();
if this.content.is_empty() {
return std::task::Poll::Ready(None);
}
let len = this.content.len().min(BLOB_RESPONSE_CHUNK_BYTES);
let chunk = Bytes::copy_from_slice(&this.content[..len]);
this.content.advance(len);
std::task::Poll::Ready(Some(Ok(chunk)))
}
}

/// The set of blob OIDs withheld from **anonymous** replication for a repo, or
/// `None` when the repo must not replicate at all (private / mode A /
/// undetermined — fail closed). This is the anonymous replication gate:
Expand Down Expand Up @@ -413,47 +447,94 @@ pub async fn list_commits(
pub async fn get_blob(
State(state): State<AppState>,
Path((owner, name, file_path)): Path<(String, String, String)>,
crate::rate_limit::PeerAddr(peer): crate::rate_limit::PeerAddr,
headers: axum::http::HeaderMap,
auth: Option<Extension<AuthenticatedDid>>,
) -> Result<Response> {
use axum::http::header;
use axum::response::IntoResponse;

// Unnormalized paths ("../..", "./", "//") can't resolve in `git show`
// and crawlers combinatorially explode them from relative links — that's
// a client error, not a 500.
let file_path = file_path.trim_matches('/');
if file_path.is_empty()
|| file_path
.split('/')
.any(|seg| seg.is_empty() || seg == "." || seg == "..")
|| file_path.split('/').any(|seg| {
seg.is_empty() || seg == "." || seg == ".." || seg.chars().any(char::is_control)
})
{
return Err(AppError::BadRequest("invalid file path".into()));
}

if state.git_read_semaphore.available_permits() == 0
|| state.git_blob_semaphore.available_permits() == 0
{
tracing::warn!(
"served-git concurrency cap reached; shedding blob request with 503 (pre-DB)"
);
return Err(AppError::Overloaded(
"git service at capacity, retry shortly".into(),
));
}

let caller = auth.as_ref().map(|e| e.0 .0.as_str());
let gate_path = format!("/{file_path}");
let (record, _rules) =
crate::api::authorize_repo_read(&state, &owner, &name, caller, &gate_path).await?;

let disk_path = state
.repo_store
.acquire(&record.owner_did, &record.name)
.await
.map_err(|e| AppError::Git(e.to_string()))?;
let head_ref = store::resolve_head(&disk_path, &record.default_branch);
let content = store::read_file(&disk_path, &head_ref, file_path).map_err(|e| {
let msg = e.to_string();
// `git show ref:path` on a path absent from the tree is a 404,
// not a server error
if msg.contains("does not exist in")
|| msg.contains("invalid object name")
|| msg.contains("exists on disk, but not in")
{
AppError::NotFound(format!("file not found: {file_path}"))
} else {
AppError::Git(msg)
let caller_key = read_caller_key(&headers, peer, state.push_limiter_trust);
let caller_permit = acquire_read_caller_permit(
&state.git_read_per_caller,
caller_key.as_deref(),
&name,
"REST blob",
)?;
let blob_permit = git_permit(&state.git_blob_semaphore)?;
let permit = git_permit(&state.git_read_semaphore)?;

let acquire_deadline = std::time::Duration::from_secs(state.config.git_acquire_timeout_secs);
let disk_path = tokio::time::timeout(
acquire_deadline,
state.repo_store.acquire(&record.owner_did, &record.name),
)
.await
.map_err(|_elapsed| {
tracing::warn!(repo = %name, "repo acquire timed out; shedding blob request with 503");
AppError::Overloaded("git service acquisition timed out, retry shortly".into())
})?
.map_err(|e| AppError::Git(e.to_string()))?;

let default_branch = record.default_branch.clone();
let read_path = file_path.to_string();
let git_bin = state.git_bin.clone();
let deadline = std::time::Instant::now()
+ std::time::Duration::from_secs(state.config.git_service_timeout_secs);
let (read, permit, blob_permit, caller_permit) = tokio::task::spawn_blocking(move || {
let read = store::read_file_bounded(
&git_bin,
&disk_path,
&default_branch,
&read_path,
MAX_SERVED_BLOB_BYTES,
deadline,
);
(read, permit, blob_permit, caller_permit)
})
.await
.map_err(|e| AppError::Internal(anyhow::anyhow!("blob read task failed: {e}")))?;
let read = read.map_err(|e| git_service_app_error(&e))?;

let content = match read {
store::BoundedFileRead::Found(content) => content,
store::BoundedFileRead::Missing => {
return Err(AppError::NotFound(format!("file not found: {file_path}")));
}
})?;
store::BoundedFileRead::TooLarge { size, max } => {
tracing::warn!(repo = %name, path = %file_path, size, max, "REST blob exceeds served size limit");
return Err(AppError::PayloadTooLarge(format!(
"file exceeds the maximum served size of {max} bytes"
)));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
};

// Guess content type
let mime = match file_path.rsplit('.').next() {
Expand All @@ -467,7 +548,24 @@ pub async fn get_blob(
_ => "application/octet-stream",
};

Ok(([(header::CONTENT_TYPE, mime)], content).into_response())
let content_len = content.len();
let stream = BlobResponseStream {
content: Bytes::from(content),
_git_permit: permit,
_blob_permit: blob_permit,
_caller_permit: caller_permit,
};
let mut response = Response::new(axum::body::Body::from_stream(stream));
response.headers_mut().insert(
header::CONTENT_TYPE,
axum::http::HeaderValue::from_static(mime),
);
response.headers_mut().insert(
header::CONTENT_LENGTH,
axum::http::HeaderValue::from_str(&content_len.to_string())
.expect("a decimal content length is a valid header value"),
);
Ok(response)
}

/// GET /api/v1/repos/:owner/:repo/tree (root listing)
Expand Down Expand Up @@ -3343,6 +3441,45 @@ mod tests {
const OWNER_SHORT: &str = "z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH";
const STRANGER_DID: &str = "did:key:z6Mkffonly5tranger0000000000000000000000000000000";

#[tokio::test]
async fn blob_response_holds_admission_until_the_body_is_dropped() {
use futures::StreamExt;

let git = Arc::new(tokio::sync::Semaphore::new(1));
let blob = Arc::new(tokio::sync::Semaphore::new(1));
let callers = crate::rate_limit::PerCallerConcurrency::new(1, 100);
let source = vec![b'x'; BLOB_RESPONSE_CHUNK_BYTES * 2];
let source_start = source.as_ptr() as usize;
let source_end = source_start + source.len();
let mut stream = BlobResponseStream {
content: Bytes::from(source),
_git_permit: git.clone().try_acquire_owned().unwrap(),
_blob_permit: blob.clone().try_acquire_owned().unwrap(),
_caller_permit: callers.try_acquire("source"),
};

let first = stream.next().await.unwrap().unwrap();
assert_eq!(first.len(), BLOB_RESPONSE_CHUNK_BYTES);
assert!(git.clone().try_acquire_owned().is_err());
assert!(blob.clone().try_acquire_owned().is_err());
assert!(callers.try_acquire("source").is_none());

let second = stream.next().await.unwrap().unwrap();
assert!(stream.next().await.is_none());
for chunk in [&first, &second] {
let chunk_start = chunk.as_ptr() as usize;
assert!(
chunk_start < source_start || chunk_start >= source_end,
"emitted chunks must not retain the full source allocation"
);
}

drop(stream);
assert!(git.try_acquire_owned().is_ok());
assert!(blob.try_acquire_owned().is_ok());
assert!(callers.try_acquire("source").is_some());
}

#[test]
fn upload_pack_request_finalizes_only_with_done_pktline() {
let want = "0032want 1111111111111111111111111111111111111111\n";
Expand Down
3 changes: 3 additions & 0 deletions crates/gitlawb-node/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ mod tests {
peer_write_rate_limiter: RateLimiter::new(600, Duration::from_secs(3600)),
shutdown_tx: tokio::sync::watch::channel(false).0,
git_read_semaphore: Arc::new(tokio::sync::Semaphore::new(64)),
git_blob_semaphore: Arc::new(tokio::sync::Semaphore::new(
crate::api::repos::MAX_CONCURRENT_BLOB_READS,
)),
git_write_semaphore: Arc::new(tokio::sync::Semaphore::new(64)),
git_push_advert_semaphore: Arc::new(tokio::sync::Semaphore::new(64)),
git_encrypt_semaphore: Arc::new(tokio::sync::Semaphore::new(64)),
Expand Down
11 changes: 6 additions & 5 deletions crates/gitlawb-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ pub struct Config {
/// cap is a different axis (500 connections each fan out to git +
/// pack-objects + threads). Size below the process budget with headroom.
///
/// This is the READ pool (`git_read_semaphore`): upload-pack and the UPLOAD-PACK
/// `info/refs` advertisement only. The authenticated push POST draws from a
/// This is the READ pool (`git_read_semaphore`): upload-pack, the UPLOAD-PACK
/// `info/refs` advertisement, and REST blob reads. The authenticated push POST draws from a
/// separate write pool (`max_concurrent_git_pushes`) that anonymous reads can
/// never reach, and each read caller is additionally bounded by
/// `max_concurrent_reads_per_caller`, so an anonymous flood cannot shed the actual
Expand Down Expand Up @@ -422,9 +422,10 @@ pub struct Config {
)]
pub max_concurrent_pin_tasks: usize,

/// Maximum concurrent read operations (`upload-pack` and the upload-pack
/// `info/refs` advertisement) a single caller may hold at once, so one caller
/// cannot monopolize the `max_concurrent_git_ops` read pool (#174). Callers are
/// Maximum concurrent read operations (`upload-pack`, the upload-pack
/// `info/refs` advertisement, and REST blob reads) a single caller may hold at
/// once, so one caller cannot monopolize the `max_concurrent_git_ops` read pool
/// (#174). Callers are
/// keyed on the RESOLVED SOURCE IP, never the DID — a signature does not move a
/// caller off this cap, so an authenticated client cannot mint DIDs to escape it.
/// IMPORTANT: the source-IP key is only as granular as `GITLAWB_TRUSTED_PROXY`.
Expand Down
18 changes: 18 additions & 0 deletions crates/gitlawb-node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub enum AppError {
#[error("invalid request: {0}")]
BadRequest(String),

#[error("payload too large: {0}")]
PayloadTooLarge(String),

/// A DID was well-formed enough to carry to a resolver but no verifying key
/// could be derived from it. Its own code rather than plain `bad_request`
/// because the auth middleware already answers `unresolvable_did` for the
Expand Down Expand Up @@ -162,6 +165,11 @@ impl IntoResponse for AppError {
// IcaptchaProofRequired is handled above (it carries extra headers/fields).
AppError::IcaptchaProofRequired { .. } => unreachable!("handled before this match"),
AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, "bad_request", msg.clone()),
AppError::PayloadTooLarge(msg) => (
StatusCode::PAYLOAD_TOO_LARGE,
"payload_too_large",
msg.clone(),
),
AppError::UnresolvableDid(msg) => {
(StatusCode::BAD_REQUEST, "unresolvable_did", msg.clone())
}
Expand Down Expand Up @@ -274,6 +282,16 @@ mod tests {
);
}

#[test]
fn payload_too_large_maps_to_413() {
assert_eq!(
AppError::PayloadTooLarge("x".into())
.into_response()
.status(),
StatusCode::PAYLOAD_TOO_LARGE
);
}

#[test]
fn overloaded_maps_to_503_with_retry_after() {
let resp = AppError::Overloaded("x".into()).into_response();
Expand Down
Loading
Loading