Compare commits

...
Author SHA1 Message Date
beardthelion 7e9a5be6d4 test(node): complete listing-denial regressions for the new gates 2026-11-07 15:08:26 -06:00
beardthelion 0140c9cd86 fix(node): pass SignatureBodyLimit state to optional_signature layers 2026-11-07 06:57:14 -06:00
beardthelion 5274433503 Merge remote-tracking branch 'origin/main' into bug_fix_2
# Conflicts:
#	crates/gitlawb-node/src/server.rs
2026-11-07 06:50:06 -06:00
beardthelionandeuxarist 0cdb4bcc7c fix(api,sync): Delimit positional arguments in git clone mirror invocations (#539)
* fix(api,sync): Delimit positional arguments in git clone mirror invocations

Pass "--" before positional path/URL arguments in Command invocations
for "git clone --mirror" across repos.rs, sync.rs, and gl mirror.rs.

Fixes #374

* style(gl): Format args array in mirror.rs

Fix cargo fmt line-length break for git clone --mirror arguments.

Refs #376

* fix(node): Pin the git clone delimiter against remote-argument injection

The delimiter change had no test, so nothing stopped it being reverted as
cosmetic. What it guards is real: git parses a remote beginning with a
dash as an option, and --upload-pack=<cmd> is one git hands to a shell.

No path reaches that today. Peer URLs are gated by is_public_http_url, so
the origin clone_repo composes can only begin with http:// or https://;
fork paths are joins under repos_dir; and clap rejects a leading-dash
positional in gl mirror. The delimiter is what removes git's dependence
on those three unrelated gates staying correct.

Assert git reports the whole argument as a repository it cannot find,
which is only true once the delimiter forces it to be a path.
Undelimited, git consumes it as an option and fails against the
destination instead, never naming the injected string, so the assertion
is red before the fix and green after.

Refs #374

* fix(sync,gl): Delimit positional arguments in fetch_repo and gl clone

Carry the positional argument delimiter into fetch_repo when setting the
remote origin URL and into setup_partial_clone across all clone branches.
Correct the error string quoted in the clone regression test docstring.

Refs #374

* test(gl,node): Pin the argv delimiter on every git sink this PR changed

The delimiter contract is duplicated across five direct `git` argv
constructions, but only `sync::clone_repo` had a test that went red when
`--` was deleted. Removing or misplacing the delimiter in any other sink
left the suite green, so the hardening rested on review rather than on
the tests.

Each changed sink now has a subprocess-facing regression driven through
real git: the three `setup_partial_clone` shapes (plain, `--branch`, and
the sparse/promisor arm), `fetch_repo`'s `remote set-url`, `gl mirror`,
and the fork clone.

Two of these could not carry the obvious assertion:

- `git_global` and `git_run` both format failures as
  `git {args:?} failed: {stderr}`, so the injected value is already in
  the message via the debug-printed argv. Asserting that the error
  merely contains it passes with `--` deleted. The clone tests assert on
  git's own single-quoted `repository '<value>'` instead, which only
  appears when git read the value as a repository.
- `remote set-url` does not fail on a delimited option-shaped value, it
  stores it. The `fetch_repo` test asserts the stored URL rather than an
  error: undelimited, git exits 129 with `unknown option` and leaves the
  previous URL in place.

`gl mirror` and `fork_repo` needed their argv extracted to be testable.
`mirror::run` loads a keypair and contacts a node before reaching the
clone, and bails with a message that interpolates the source whether or
not the delimiter is present. The extraction keeps the production
`.status()` call, so a large mirror clone still streams git's progress
to the terminal instead of being captured.

Every one of the five was confirmed red with its delimiter removed and
green with it restored.

Refs #374

* fix(gl): Route the last three clone warnings through emit_warning

`emit_warning` sanitizes a line before writing it, which strips ANSI
escape and bidi characters from untrusted text. Three warning sites in
the blob-recovery paths still used a bare `eprintln!` while interpolating
`oid`, which comes from node JSON or an Arweave gateway manifest, so
those escapes reached the user's terminal raw.

Refs #374

* style(gl): elide the lifetime in mirror_clone_args

* test(gl,node): pin LC_ALL=C on git-subprocess stderr assertions

---------

Co-authored-by: euxarist <[email protected]>
2026-11-07 12:38:49 +00:00
0e5a1608e2 feat(node): persist upstream mirror configuration and transition state (#538)
* feat(node): persist upstream mirror transition state

Origin-Session: local-d6a143 | Codex | 12 prompts

* fix(node): reject null mirror transition state

Origin-Session: local-d6a143 | Codex | 14 prompts

* fix(node): address mirror state review findings

Origin-Session: local-d6a143 | Codex | 16 prompts

* fix(node): close mirror review blockers

Origin-Session: local-d6a143 | Codex | 20 prompts

* fix(node): align peer URL validation message

Origin-Session: local-d6a143 | Codex | 20 prompts

* fix(node): close remaining mirror review findings

Origin-Session: local-d6a143 | Codex | 21 prompts

* test(node): track the Db pool field change in the mirror-state migration tests

* fix(node): revalidate stored webhook URLs at delivery and cover the subdomain repeated-dot case

* docs(node): document the invalid_url webhook delivery outcome

---------

Co-authored-by: KRATOS <[email protected]>
Co-authored-by: Kevin Codex <[email protected]>
2026-11-07 03:50:15 +00:00
beardthelionandGravirei 37947208c5 docs: state that GITLAWB_ENFORCE_OWNER_PUSH defaults to true (#537)
* docs: state that GITLAWB_ENFORCE_OWNER_PUSH defaults to true

PR #330 flipped the code default to true and updated .env.example, the
README env-var table, the AWS guide, the macOS app, and docs/RUN-A-NODE.md,
but two prose mentions in operator-facing docs still describe the
opposite. SECURITY.md line 57 ("Repository write authorization defaults"
section) and README.md line 69 (Known limitations) both say the flag
"defaults to `false`", which contradicts the live runtime and the rest
of the same files. Reviewer 2 closed #224 with this as the P2 finding
to fix in the config-and-policy split.

The correct text already exists in the README env-var table; the prose
restatements were missed. Bring the prose into line with the code.

- SECURITY.md: rewrite the section to lead with the default being true
  and describe `false` as the explicit rolling-upgrade override.
- README.md: rewrite the known-limitations bullet to state the default
  is true and the `false` opt-out is a rolling-upgrade only.
- Add `crates/gitlawb-node/tests/owner_push_default_docs.rs`: a
  regression guard that reads the four surfaces (clap default, .env.example,
  README known-limitations prose, README env-var table, SECURITY.md) and
  asserts each one says the default is `true`. Reverting either doc edit
  turns one named test red; flipping `default_value_t` back to `false`
  turns the env example test red. Each surface is checked independently
  so a stale doc lies alone.

No code change. No new migration. No dependency change. Compiles clean,
clippy clean under `-D warnings`.

Refs: closes the P2 finding from the review of #224.

* fix(docs): clarify default value consistency for GITLAWB_ENFORCE_OWNER_PUSH

* docs(readme): drop the default-open roadmap line and the em-dash punctuation

---------

Co-authored-by: Gravirei <[email protected]>
2026-11-07 00:38:41 +00:00
beardthelion 5ed3381a6b fix(gl): truncate node-supplied display strings at a char boundary (#350) (#536)
* fix(gl): truncate node-supplied display strings at a char boundary (#350)

Every timestamp/id/SHA the CLI prints was cut with &s[..N] or
&s[..N.min(s.len())]. The first panics on a short value, the second
still panics when byte N is inside a multi-byte char. A shared
text::truncate cuts at a char boundary and covers all sites, including
two the issue did not name (node events, agent did) and the same shape
in bounty, changelog, ipfs pin listing, and the merge-commit print.

* fix(gl): bound truncate by bytes, not chars (#460)

Address PR review feedback: char_indices().nth(max) capped characters,
so a multibyte char near the cut could exceed the byte limit. Start at
max.min(len) and walk back to the nearest char boundary instead.

* fix(gl): bound the bounty leaderboard did suffix at a char boundary
2026-11-06 20:38:05 +00:00
beardthelion 9b915b09a7 fix(attest): bound did:key method-id length before base58 decode (#360) (#535)
gitlawb-core added the same guard in 73bf132c; the second copy of the
parser in gitlawb-attest never got it, so an untrusted signer string of
arbitrary length reaches a decoder whose cost is quadratic in input.
2026-11-06 18:14:06 +00:00
77ac1037cb fix(icaptcha-client): parse GITLAWB_ICAPTCHA_INSECURE as truthy only (#227) (#533)
* fix(icaptcha-client): parse GITLAWB_ICAPTCHA_INSECURE as truthy only

Presence checks treated =0/=false/empty as enabled. Only explicit 1/true
now enable loopback HTTP trust relaxation (#227).

* fix(icaptcha-client): address #246 review (trim test, fmt, lock)

Pin trim with a whitespace truthy case, recover from a poisoned
ICAPTCHA_ENV_LOCK, document trim + warn visibility, and cargo fmt.

* fix(icaptcha-client): do not log raw ICAPTCHA_INSECURE value

The non-truthy warn path previously recorded the full trimmed env
value. That is arbitrary deployment input; report only that the
setting is invalid (#246 review).

* fix(icaptcha-client): pure insecure flag helpers for #246 review

Warn on present non-Unicode GITLAWB_ICAPTCHA_INSECURE (fail closed).
Pass insecure mode into is_https_with / resolve_solver_url_with so
tests no longer mutate process-global env (removes InsecureEnv race).

* fix(icaptcha-client): cover real env read + fix warn spacing

Add a serialized test that drives icaptcha_insecure_enabled() against
the process env so presence-check / typo / always-true mutations fail.
Collapse the mid-sentence space run in the non-truthy warning (#246).

* fix(icaptcha-client): put the non-truthy warning on one literal line

---------

Co-authored-by: Ayush7614 <[email protected]>
Co-authored-by: Kevin Codex <[email protected]>
2026-11-06 17:26:16 +00:00
db98b3957a fix(attest): make verify_all lenient under RequireAll (#359) (#532)
* fix(attest): make verify_all lenient under RequireAll (#359)

Registry::verify_all propagated every per-attestation error via ?, so a
single malformed or unknown attestation aborted the whole batch. Under
Policy::RequireAll that is a DoS vector: any third party can attach an
attestation, so an unrelated attacker/spam/v1 could block an otherwise
valid cert.

Under RequireAll, drop entries that fail to verify instead of aborting;
the required_types check remains the gate and only counts fully_verified
entries, so a required type present only as a malformed attestation still
fails with RequiredMissing. AcceptKnown and RejectUnknown stay strict.

Adds tests for both: a malformed extra is dropped without aborting, and a
malformed-only required type is still rejected.

Closes #359

* test(attest): lock the strict batch policies with an error-propagation test

AcceptKnown and RejectUnknown were only exercised per-attestation, so a
cleanup applying RequireAll's filter_map(Result::ok) shape to the strict
arms would fail open with every test green. Drive both policies through
verify_all with a wrong-cert-hash member and assert the batch surfaces
the underlying error. Also correct the RequireAll doc: unknown valid
types are retained as unverified, not dropped — only real Err paths are.

---------

Co-authored-by: andreolf <[email protected]>
Co-authored-by: Kevin Codex <[email protected]>
2026-11-06 16:54:42 +00:00
Gravirei ac9ea61927 All three findings addressed on bug_fix_2:
P2 — signed_scoped_anchors_full_did_form_matches_stored_slug_through_build_router now seeds
  a second repo under a different owner and asserts irys-other-repo-tx is absent. Negative
  control: flipping the production filter to None made the test go RED with count: 2 showing
  the cross-repo leak.

  P3 — let short = owner_did.split(':').next_back().unwrap() replaced with let short =
  crate::db::normalize_owner_key(&owner_did).to_string(). Reader and writer now share one
  convention.

  P3 — PR #134 description updated: pins bullet now describes the actual pre-merge layering
  (and cites the axum-layer-vs-merge-pitfall memory entry), and round 2's stored-slug
  normalization is added as its own bullet with the regression's coverage.

  5/5 anchor-related tests pass, cargo fmt --check clean.
2026-08-30 17:13:53 +06:00
Gravirei 4242bb85ad fix(node): normalize scoped anchor SQL filter to stored repo slug
After authorize_repo_read succeeds, the SQL filter used the raw ?repo=
string (format!("{owner}/{name}")). Anchor rows are stored with
normalize_owner_key(owner_did)/name (short key), so a caller using
?repo=did🔑.../name passed authz but got anchors: [] — a false
empty page that broke callers that legitimately pass the full DID
form. Build the filter from record.owner_did normalized and
record.name instead, matching the shape the seeder uses.
2026-08-30 11:24:12 +06:00
Gravirei 8dd745e98b fix(node): keep /api/v1/ipfs/pins out of the CID resolver's per-IP flood brake
Follow-up to #134: the previous reordering put `/api/v1/ipfs/pins` and
`/ipfs/{cid}` under the same `rate_limit_by_ip` + `IpRateLimiter` layer
stack, so both routes shared the resolver's flood brake bucket. That
bucket is documented as the once-per-request brake for `GET /ipfs/{cid}`
because the resolver can drive bounded-but-expensive repo walks. The
pin listing is a single `list_pinned_cids()` call and has no reason to
share the bucket. Sharing meant /ipfs/{cid} traffic could exhaust the
bucket and 429 the pins endpoint, and signed pin polling could exhaust
the bucket and 429 legitimate CID reads.

Compose `ipfs_routes` from two sub-routers with independent policies,
then merge:
  * `/ipfs/{cid}` keeps `optional_signature` + `rate_limit_by_ip` + the
    `IpRateLimiter` extension. Same behavior as before #134.
  * `/api/v1/ipfs/pins` gets only `optional_signature`. Auth-required
    (handler denies with 401), but no rate limit — it has nothing to
    flood.

Two new `build_router` regressions exercise the bucket independence
through `TrustedProxy::XForwardedFor`:
  * 5 signed pin requests against a size-2 CID bucket all 200, then a
    `/ipfs/{cid}` from the same IP also 200 (pins did not debit the
    bucket).
  * A `/ipfs/{cid}` exhausts a size-1 CID bucket (200, then 429), then
    a signed pin from the same IP is 200 (the bucket does not gate
    pins).

All 65 tests in `server::tests::` + `test_support::tests::ipfs_*`
still pass, including the existing 429 tests for the CID resolver.
2026-08-29 10:52:55 +06:00
Gravirei f49ed79336 fix(node): gate scoped /api/v1/arweave/anchors reads on repository visibility
Closes the review follow-up on #134: a signed non-reader could pass the
auth-layer check on `?repo=<owner>/<name>` and obtain private-repo anchor
metadata (ref names, old/new SHAs, CIDs, irys tx ids). The auth check
proved only that the signature was valid; it did not bind the request to
the canonical repo-read policy.

* Parse the slug through `validate_repo_slug` (same helper the sync path
  uses) and call `authorize_repo_read` before the SQL query. Missing
  repos, quarantined mirrors, and signed non-readers all collapse to the
  standard 404 (`repo_not_found`).
* The unscoped global listing stays auth-only (#121 narrowing; the wider
  permissionless-identity enumeration is the #136 class, out of scope).
* Clamp the limit to `[0, 200]` so `?limit=-1` no longer reaches Postgres
  as `LIMIT -1`.

Five new `build_router` regressions cover the full production contract
through the real `optional_signature` middleware: scoped unsigned 401,
scoped owner 200, scoped non-reader 404 with no anchor metadata leaked
in the body, scoped missing-repo 404 indistinguishable from non-reader,
and `?limit=-1` clamped to an empty 200.
2026-08-29 03:32:01 +06:00
Gravirei 752e117f07 fix(node): format arweave routes for consistency in build_router function 2026-08-28 16:09:52 +06:00
Gravirei f70bb0008e fix(node): enforce authentication on /api/v1/ipfs/pins and /api/v1/arweave/anchors routes 2026-08-28 16:04:51 +06:00
Gravirei 063a5e689f fix(node): gate /ipfs/pins and /arweave/anchors behind authentication (#134)
Add auth rejection to list_pins and list_anchors handlers. The pin index
spans the entire node and would expose metadata for every object ever
pushed; anonymous callers must not see it.

- Require AuthenticatedDid extension in both handlers, return 401 when absent
- Add server.rs regression test for anonymous rejection through build_router
- Fix closed-pool tests to pass authenticated requests (test 503 path)
2026-08-26 16:22:29 +06:00
31 changed files with 2838 additions and 122 deletions

No files matched your search

+3 -3
View File
@@ -66,7 +66,7 @@ Good today:
Known limitations:
- Repository write authorization is not secure by default: `GITLAWB_ENFORCE_OWNER_PUSH` defaults to `false` for compatibility, so a valid HTTP Signature identifies a pusher but does not enforce owner-only pushes.
- Repository write authorization is secure by default: `GITLAWB_ENFORCE_OWNER_PUSH` defaults to `true`, so a valid HTTP Signature identifies a pusher and the node additionally requires that pusher to be the repository owner. Set `GITLAWB_ENFORCE_OWNER_PUSH=false` only for the duration of a rolling upgrade whose pushers are not yet the repo owner. Authentication is not authorization on its own (anyone can mint a `did:key` and sign), so owner enforcement is the only thing that prevents every signed caller from pushing to every repository, private ones included.
- UCAN proof chains are validated when supplied, but UCAN capabilities are not consulted by write authorization and the root issuer is not independently trust-anchored. UCANs therefore do not yet grant scoped collaborator access.
- 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.
- Read visibility is not a blanket data-classification boundary: task, 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.
@@ -400,7 +400,7 @@ Important node settings:
| `GITLAWB_P2P_KEY` | Path to the node's persistent libp2p identity key file, which fixes the PeerId across restarts. Must name a key file inside a directory, such as `/data/keys/p2p.key` or `./keys/p2p.key`. Two kinds of problem are handled differently. A value that cannot name a securable key file at all (a bare filename, `..` traversal, a trailing `/`, the filesystem root, or a `~/` path that escapes home) is refused before the node binds, so the process exits. Anything wrong with the storage itself (a symlinked or non-directory parent, an unsafe ancestor or working directory, an unreadable or malformed key) leaves p2p off while HTTP keeps serving; the node logs it at error level as `p2p_identity_key_load_failed` and sets the `gitlawb_p2p_identity_key_load_failed` metric to 1. Note that `/health` still reports healthy in that state, so alert on the log event or the metric rather than on the health check. Correcting the storage and restarting restores the same PeerId. On Unix a new key is created `0600` inside a `0700` directory. A key directory that grants access beyond its owner is tightened to `0700` on start; one that is closed too far is refused with a `chmod 700` remedy rather than widened, since a directory locked down on purpose is an operator decision. Rotate the key only when the node logs that the directory was group- or world-writable (or the key file itself was group- or world-readable). An ordinary `0755` directory is tightened too, but others could not read a `0600` key or replace its directory entry. An existing key file is never modified: one with group or other bits set is rejected (`chmod 600`), and so is one this node cannot read. On other platforms no permissions are enforced. Default `~/.gitlawb/p2p.key`; point it at a persistent volume when running in a container. |
| `GITLAWB_BOOTSTRAP_DISABLE_SEEDS` | Disable embedded seed peers for isolated dev/test networks. |
| `GITLAWB_REQUIRE_SIGNED_PEER_WRITES` | Require peer writes to prove their DID on every transport: RFC 9421 signatures on the announce/sync routes, and an Ed25519 payload signature on gossip ref-update events. Defaults to `false` during the staged rollout below. Upgrade every gossip publisher before enabling, or their updates are dropped with no error on their side. |
| `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_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. |
@@ -553,7 +553,7 @@ Short-term priorities:
2. Add Docker and installer smoke tests.
3. Improve operator docs and `gl doctor` checks.
4. Harden peer writes and publish the signed-peer rollout plan.
5. Close default-open write authorization and wire trusted UCAN delegation into repository permissions.
5. Wire trusted UCAN delegation into repository write authorization.
6. Add threaded, line-level pull-request discussions and enforce approval requirements on merges.
7. Add metrics for pushes, fetches, pack sizes, peer sync, failed auth, and webhooks.
+3 -3
View File
@@ -54,9 +54,9 @@ We will acknowledge receipt within 48 hours and aim to release a fix within 14 d
These are documented limitations of the current live release. They should be prioritized without breaking existing nodes during rolling upgrades.
### Repository write authorization defaults
- `git-receive-pack` verifies HTTP Signatures, but `GITLAWB_ENFORCE_OWNER_PUSH` defaults to `false` for compatibility during rollout.
- **Impact:** With the default setting, a valid signature authenticates the pusher but does not require that DID to be the repository owner.
- **Mitigation:** Set `GITLAWB_ENFORCE_OWNER_PUSH=true` on nodes where owner-only pushes are required. Confirm that every legitimate pusher uses the owner DID before enabling it.
- `git-receive-pack` verifies HTTP Signatures and, with the default configuration, also requires the pusher DID to be the repository owner: `GITLAWB_ENFORCE_OWNER_PUSH` defaults to `true`.
- **Impact:** With the default setting, a valid signature authenticates the pusher *and* the pusher must be the repository owner. Authentication is not authorization on its own — anyone can mint a `did:key` and sign — so owner enforcement is the only thing that prevents every signed caller from pushing to every repository, including private ones.
- **Mitigation:** `GITLAWB_ENFORCE_OWNER_PUSH=false` is the explicit rolling-upgrade compatibility override. Disable it only for the duration of an upgrade whose pushers are not yet the repo owner, then turn it back on. 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.
### UCAN delegation and revocation
- The middleware validates a supplied UCAN's complete proof chain, but a root token is accepted without an independently trusted issuer anchor. `Ucan::can` is not yet used by write handlers, so a UCAN does not grant scoped repository access.
+17
View File
@@ -504,6 +504,23 @@ mod tests {
assert!(matches!(err, Error::Did(_)));
}
#[test]
fn verify_rejects_oversized_method_id_before_decoding() {
let sk = fresh();
let mut att = dummy_attestation(&sk, sample_cert_hash());
// A well-formed prefix on an absurdly long id must fail the length
// bound, not the decoder: base58 decode cost is quadratic in input
// length, so reaching it at all is the defect.
att.signer = format!("did:key:z{}", "1".repeat(256));
let err = att.verify_signature(sample_cert_hash()).unwrap_err();
// Match the bound's own message: any other Did error (a decode or
// length mismatch downstream) means the oversized input was decoded.
match err {
Error::Did(m) => assert!(m.contains("too long"), "unexpected: {m}"),
e => panic!("expected Error::Did, got {e:?}"),
}
}
#[test]
fn verify_rejects_bad_signature_base64() {
let sk = fresh();
+127 -11
View File
@@ -150,23 +150,48 @@ impl Registry {
}
/// Verify a batch, then enforce `RequireAll`.
///
/// Under [`Policy::RequireAll`] this never short-circuits: an attestation
/// that fails to verify — bad signature, cert-hash mismatch, or malformed
/// payload, the actual `Err` paths of [`Registry::verify`] — is dropped
/// from the result rather than aborting the batch. A valid attestation of
/// a type with NO registered verifier is not one of those: under
/// `RequireAll` it verifies to `Ok` with `fully_verified: false` and stays
/// in the result, preserving the distinction between *untrusted* and
/// *invalid* (see `require_all_is_lenient_on_unknown_types_in_the_batch`).
/// Any third party can attach an attestation, so a hard error here would
/// let an unrelated `attacker/spam/v1` deny-of-service the whole cert (the
/// DoS vector the module docstring warns about). The `required_types`
/// check below is the real gate, and it only counts `fully_verified`
/// entries — so a required type represented solely by a malformed
/// attestation still fails with [`Error::RequiredMissing`].
///
/// Under `AcceptKnown` and `RejectUnknown` the batch stays strict: the
/// first verification error is surfaced to the caller.
pub fn verify_all(
&self,
attestations: &[Attestation],
expected_cert_hash: [u8; 32],
) -> Result<Vec<VerifiedAttestation>> {
let mut verified = Vec::with_capacity(attestations.len());
for a in attestations {
verified.push(self.verify(a, expected_cert_hash)?);
if self.policy != Policy::RequireAll {
let mut verified = Vec::with_capacity(attestations.len());
for a in attestations {
verified.push(self.verify(a, expected_cert_hash)?);
}
return Ok(verified);
}
if self.policy == Policy::RequireAll {
for required in &self.required_types {
let present = verified
.iter()
.any(|v| &v.type_ == required && v.fully_verified);
if !present {
return Err(Error::RequiredMissing(required.clone()));
}
// RequireAll: drop entries that fail to verify instead of aborting.
let verified: Vec<VerifiedAttestation> = attestations
.iter()
.filter_map(|a| self.verify(a, expected_cert_hash).ok())
.collect();
for required in &self.required_types {
let present = verified
.iter()
.any(|v| &v.type_ == required && v.fully_verified);
if !present {
return Err(Error::RequiredMissing(required.clone()));
}
}
Ok(verified)
@@ -324,6 +349,38 @@ mod tests {
.any(|v| v.type_ == "other/v1" && !v.fully_verified));
}
/// The strict policies must surface a batch member's verification error
/// through `verify_all`, not launder it into a partial result. This binds
/// the POLICY BOUNDARY itself: only the `RequireAll` arm may drop failing
/// entries, and an apparently harmless cleanup that applies its
/// `filter_map(Result::ok)` shape to the strict arms would silently turn
/// the fail-closed contracts into fail-open — with every per-attestation
/// `verify` test still green, because none of them go through the batch
/// path. The failing entry is a wrong-cert-hash attestation (signed over a
/// different hash than the batch expects), a genuine `Err` under every
/// policy.
#[test]
fn strict_policies_propagate_a_batch_members_error() {
let sk = fresh();
let cert_hash = sample_hash();
let mut wrong_hash = sample_hash();
wrong_hash[0] ^= 0xff;
for policy in [Policy::AcceptKnown, Policy::RejectUnknown] {
let mut reg = Registry::new().with_policy(policy);
reg.register(DemoVerifier);
let good = signed_demo(&sk, cert_hash, "ok");
let bad = signed_demo(&sk, wrong_hash, "ok");
let err = reg
.verify_all(&[good, bad], cert_hash)
.expect_err("a strict policy must fail the whole batch");
assert!(
matches!(err, Error::Signature(_) | Error::CertHashMismatch),
"expected the underlying verification error, got {err:?}"
);
}
}
/// A required type that is present but unverified (no verifier registered
/// even though the type appears in `required_types`) must fail
/// `verify_all` with `RequiredMissing`. Catches a misconfiguration where
@@ -341,6 +398,65 @@ mod tests {
assert!(matches!(err, Error::RequiredMissing(t) if t == "demo/v1"));
}
/// A cert-hash that does not match `sample_hash()`, so an attestation
/// signed against it fails `verify_signature` when the batch is checked
/// against `sample_hash()`.
fn other_hash() -> [u8; 32] {
let mut h = sample_hash();
h[0] ^= 0xff;
h
}
/// Under `RequireAll`, a malformed attestation (here: signed against the
/// wrong cert hash, so it fails signature verification) attached alongside a
/// valid required one must not abort the batch. The junk is dropped and the
/// cert still verifies. Before the fix, `verify_all` propagated the error
/// via `?` and the whole batch failed — the DoS vector the docstring warns
/// about.
#[test]
fn require_all_drops_a_malformed_extra_and_still_accepts() {
let sk = fresh();
let cert_hash = sample_hash();
let mut reg = Registry::new()
.with_policy(Policy::RequireAll)
.require_types(["demo/v1"]);
reg.register(DemoVerifier);
// Signed against `other_hash()`, so it will not verify under `cert_hash`.
let malformed = signed_demo(&sk, other_hash(), "boom");
let real = signed_demo(&sk, cert_hash, "ok");
let verified = reg
.verify_all(&[malformed, real], cert_hash)
.expect("a malformed extra must not abort the batch under RequireAll");
assert_eq!(verified.len(), 1, "the malformed entry should be dropped");
assert!(verified
.iter()
.any(|v| v.type_ == "demo/v1" && v.fully_verified));
}
/// Leniency must not open a hole: if the *only* attestation of a required
/// type is malformed, dropping it leaves the required type absent and
/// `verify_all` must still fail with `RequiredMissing`. Guards against a fix
/// that skips errors so eagerly it lets an unverified required type pass.
#[test]
fn require_all_rejects_when_only_a_malformed_required_type_is_present() {
let sk = fresh();
let cert_hash = sample_hash();
let mut reg = Registry::new()
.with_policy(Policy::RequireAll)
.require_types(["demo/v1"]);
reg.register(DemoVerifier);
// The sole demo/v1 is bound to the wrong cert hash → fails verification.
let malformed_required = signed_demo(&sk, other_hash(), "ok");
let err = reg
.verify_all(&[malformed_required], cert_hash)
.unwrap_err();
assert!(matches!(err, Error::RequiredMissing(t) if t == "demo/v1"));
}
#[test]
fn payload_check_failure_rejects() {
let sk = fresh();
+56 -9
View File
@@ -1,12 +1,13 @@
//! GET /api/v1/arweave/anchors — list Arweave ref-update anchors.
use axum::{
extract::{Query, State},
extract::{Extension, Query, State},
Json,
};
use serde::Deserialize;
use crate::error::Result;
use crate::db::normalize_owner_key;
use crate::error::{AppError, Result};
use crate::state::AppState;
#[derive(Debug, Deserialize)]
@@ -21,17 +22,62 @@ fn default_limit() -> i64 {
}
/// GET /api/v1/arweave/anchors
///
/// `?repo=<owner>/<name>` returns anchors for one repository and binds the
/// request to the canonical read-authorization path used by every other
/// repo-scoped read: the same `authorize_repo_read` helper, with the caller
/// identity attached. Missing repositories, quarantined mirrors, and signed
/// non-readers all collapse to the standard `404` (no existence oracle). The
/// unscoped listing is auth-only — the #121 contract — and does not admit
/// visibility filtering, which is the #136 stale-index class and explicitly
/// out of scope for this auth slice.
pub async fn list_anchors(
State(state): State<AppState>,
Query(q): Query<ListAnchorsQuery>,
auth: Option<Extension<crate::auth::AuthenticatedDid>>,
) -> Result<Json<serde_json::Value>> {
let limit = q.limit.min(200);
// Bare `?` so connection-class sqlx failures downcast to `AppError::Db` and
// map to 503 `db_unavailable` (not 500 via `.map_err(AppError::Internal)`) (#251).
let anchors = state
.db
.list_arweave_anchors(q.repo.as_deref(), limit)
.await?;
// The route's `optional_signature` layer is permissive (it admits unsigned
// legacy callers) so this in-handler check is the actual gate. A signed
// non-reader still passes this check and proceeds to the authz step below.
let caller = auth.as_ref().map(|e| e.0 .0.as_str());
if caller.is_none() {
return Err(AppError::Unauthorized(
"authentication required for anchor listing".into(),
));
}
// Clamp before the SQL query. A negative or non-numeric limit must not
// reach Postgres as `LIMIT -1` (which it rejects as a db error / 500) — the
// contract is the same default ceiling as the listing page itself.
let limit = q.limit.clamp(0, 200);
let anchors = if let Some(repo_slug) = q.repo.as_deref() {
// Parse the user-supplied "owner/name" through the same slug validator
// the sync path uses, so a malformed query is a 400, not a 500.
let (owner, name) = match crate::git::repo_store::validate_repo_slug(repo_slug) {
Ok(parts) => parts,
Err(e) => return Err(AppError::BadRequest(format!("invalid ?repo: {e}"))),
};
// Read gate. Returns `RepoNotFound` (→ 404) indistinguishably for
// missing repos, quarantined mirrors, and signed non-readers.
let (record, _rules) =
crate::api::authorize_repo_read(&state, owner, name, caller, "/").await?;
// Build the SQL filter from the canonical stored slug, NOT from the
// user-supplied `?repo=` string. Anchor rows are written as
// `{normalize_owner_key(owner_did)}/{name}` (the short form), so a
// request that passes authz with the full `did:key:…/name` form would
// match zero rows and return a false empty page. The gate already
// verified the repo is readable; `record.owner_did` is the canonical
// identity and `record.name` is the stored name.
let stored_slug = format!("{}/{}", normalize_owner_key(&record.owner_did), record.name);
state
.db
.list_arweave_anchors(Some(&stored_slug), limit)
.await?
} else {
// No scope → no repo-read decision. Auth-only.
state.db.list_arweave_anchors(None, limit).await?
};
Ok(Json(serde_json::json!({
"anchors": anchors,
@@ -60,6 +106,7 @@ mod closed_pool_tests {
.oneshot(
Request::builder()
.uri("/api/v1/arweave/anchors")
.extension(crate::auth::AuthenticatedDid("did:key:test".into()))
.body(axum::body::Body::empty())
.unwrap(),
)
+10 -1
View File
@@ -2133,7 +2133,15 @@ async fn gate_and_serve(
/// Returns all CIDs that have been pinned to the local IPFS node from git
/// objects received via push. Each entry includes the git SHA-256 hex, the
/// CIDv1 string, and the timestamp when it was pinned.
pub async fn list_pins(State(state): State<AppState>) -> Result<Json<serde_json::Value>> {
pub async fn list_pins(
State(state): State<AppState>,
auth: Option<Extension<crate::auth::AuthenticatedDid>>,
) -> Result<Json<serde_json::Value>> {
if auth.is_none() {
return Err(crate::error::AppError::Unauthorized(
"authentication required for pin listing".into(),
));
}
// Bare `?` so connection-class sqlx failures downcast to `AppError::Db` and
// map to 503 `db_unavailable` (not 500 via `.map_err(AppError::Internal)`) (#251).
let pins = state.db.list_pinned_cids().await?;
@@ -2426,6 +2434,7 @@ mod closed_pool_tests {
.oneshot(
Request::builder()
.uri("/api/v1/ipfs/pins")
.extension(crate::auth::AuthenticatedDid("did:key:test".into()))
.body(axum::body::Body::empty())
.unwrap(),
)
+42 -10
View File
@@ -70,6 +70,8 @@ pub struct PeerResponse {
pub reachable: bool,
}
pub(crate) const PUBLIC_HTTP_URL_REQUIREMENT: &str = "must be a public http(s) URL (no loopback, private, localhost, .localhost, .internal, or .local hosts)";
/// Extract an IPv4 address embedded in an IPv6 literal across the transition
/// formats that carry one: IPv4-mapped (`::ffff:a.b.c.d`), IPv4-compatible
/// (`::a.b.c.d`), 6to4 (`2002:WWXX:YYZZ::/16`), and the NAT64 well-known prefix
@@ -104,8 +106,8 @@ fn embedded_ipv4(v6: std::net::Ipv6Addr) -> Option<std::net::Ipv4Addr> {
/// Whether a peer `http_url` is a public http(s) endpoint safe to register.
/// Rejects non-http(s) schemes, loopback/unspecified/private/link-local IPs,
/// and `localhost` / `.local` / `.internal` hostnames. Used at announce time
/// and by the boot-time prune of already-poisoned rows.
/// and `localhost` / `.localhost` / `.local` / `.internal` hostnames. Used at
/// announce time and by the boot-time prune of already-poisoned rows.
pub fn is_public_http_url(raw: &str) -> bool {
let url = match reqwest::Url::parse(raw) {
Ok(u) => u,
@@ -125,6 +127,7 @@ pub fn is_public_http_url(raw: &str) -> bool {
host.truncate(host.trim_end_matches('.').len());
if host.is_empty()
|| host == "localhost"
|| host.ends_with(".localhost")
|| host.ends_with(".local")
|| host.ends_with(".internal")
{
@@ -260,9 +263,9 @@ pub async fn announce(
// and turn our outbound sync-notify fan-out into an SSRF probe — and bury
// the real peers under junk so node-origin repos stop replicating.
if !is_public_http_url(&req.http_url) {
return Err(AppError::BadRequest(
"http_url must be a public http(s) URL (no loopback, private, or .internal/.local hosts)".into(),
));
return Err(AppError::BadRequest(format!(
"http_url {PUBLIC_HTTP_URL_REQUIREMENT}"
)));
}
// Reject self-announcements: a peer row whose http_url is our own public
@@ -541,6 +544,7 @@ mod tests {
fn rejects_loopback_private_and_internal() {
for bad in [
"http://localhost:7545",
"http://node.localhost:7545",
"http://127.0.0.1:5432/",
"http://localhost:22/",
"http://0.0.0.0:7545",
@@ -620,11 +624,14 @@ mod tests {
#[test]
fn rejects_repeated_trailing_root_dots() {
// Stripping a single root dot leaves `localhost.`, which matches
// neither the equality nor the suffix check. The subdomain form
// (`node.localhost..`) is not asserted here: `.localhost` is not yet a
// rejected suffix on this base, so that case belongs to whichever
// change adds it rather than to this one.
for bad in ["http://localhost../", "http://localhost...:7545"] {
// neither the equality nor the suffix check. The subdomain form now
// exercises the `.localhost` suffix rejection together with the extra
// trailing dot.
for bad in [
"http://localhost../",
"http://localhost...:7545",
"http://node.localhost../",
] {
assert!(!is_public_http_url(bad), "{bad:?} must be rejected");
}
}
@@ -1582,6 +1589,31 @@ mod tests {
)
}
#[sqlx::test]
async fn announce_rejects_localhost_subdomains_with_an_accurate_error(pool: PgPool) {
let state = test_state(pool).await;
let did = Keypair::generate().did().to_string();
let resp = announce_only(state.clone())
.oneshot(announce_as(
&did,
&announce_body(&did, "https://node.localhost:7545"),
))
.await
.unwrap();
let (status, error, message) = status_and_error(resp).await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(error, "bad_request");
assert_eq!(
message,
"http_url must be a public http(s) URL (no loopback, private, localhost, .localhost, .internal, or .local hosts)"
);
assert!(
snapshot(&state.db, &did).await.is_none(),
"a rejected .localhost announce must leave no row behind"
);
}
/// U4 scenario 1, and the first test the keyid branch has ever had: a caller
/// who proved control of one DID must not announce another's. Kills
/// neutralizing the handler's keyid comparison, and kills demoting this
+37 -4
View File
@@ -3508,6 +3508,17 @@ pub struct ForkRepoRequest {
}
/// POST /api/v1/repos/:owner/:repo/fork
/// Argv for the fork's mirror clone, with `--` before the first positional (#374).
///
/// Both positionals here are server-derived paths rather than caller-supplied
/// URLs, so this is defence in depth rather than a reachable injection. It is
/// still the same argv contract as the other clone sites, and it is extracted for
/// the same reason: so the delimiter's placement is pinned by a test instead of
/// resting on review.
fn fork_clone_args<'a>(source: &'a str, dest: &'a str) -> [&'a str; 5] {
["clone", "--mirror", "--", source, dest]
}
pub async fn fork_repo(
State(state): State<AppState>,
Extension(auth): Extension<AuthenticatedDid>,
@@ -3593,12 +3604,10 @@ pub async fn fork_repo(
// Clone the source repo as a mirror
let output = std::process::Command::new("git")
.args([
"clone",
"--mirror",
.args(fork_clone_args(
source_path.to_str().unwrap_or(""),
disk_path.to_str().unwrap_or(""),
])
))
.output()
.map_err(|e| AppError::Git(format!("git clone --mirror failed: {e}")))?;
@@ -4065,6 +4074,30 @@ fn dedupe_canonical_repos(rows: Vec<(RepoRecord, i64)>) -> Vec<(RepoRecord, i64)
#[cfg(test)]
mod tests {
use super::*;
/// #374: the fork path builds its own `git clone --mirror` argv. Driven
/// through real git so a missing or misplaced `--` fails, matching the
/// coverage on the `gl clone`, `gl mirror`, and sync sinks.
#[test]
fn fork_clone_never_parses_the_source_as_a_git_option() {
let td = tempfile::TempDir::new().unwrap();
let dest = td.path().join("fork.git");
let injected = "--upload-pack=false";
let out = std::process::Command::new("git")
.env("LC_ALL", "C")
.args(fork_clone_args(injected, dest.to_str().unwrap()))
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(!out.status.success(), "an option-shaped source must fail");
assert!(
stderr.contains(&format!("repository '{injected}'")),
"git must name the source as the repository, not consume it as an option: {stderr}"
);
}
use crate::auth::caller_authorized_to_push;
use crate::error::AppError;
use gitlawb_core::identity::Keypair;
+67 -4
View File
@@ -46,9 +46,10 @@ pub async fn create_webhook(
// endpoints (SSRF). Delivery runs on the shared no-redirect client
// (main.rs), which closes the 3xx-to-internal bounce.
if !crate::api::peers::is_public_http_url(&req.url) {
return Err(AppError::BadRequest(
"webhook URL must be a public http(s) URL (no loopback, private, or .internal/.local hosts)".into(),
));
return Err(AppError::BadRequest(format!(
"webhook URL {}",
crate::api::peers::PUBLIC_HTTP_URL_REQUIREMENT
)));
}
let events = req.events.unwrap_or_else(|| vec!["*".into()]);
@@ -141,7 +142,19 @@ pub async fn delete_webhook(
#[cfg(test)]
mod tests {
use crate::api::peers::is_public_http_url;
use axum::extract::{Extension, Path, State};
use axum::Json;
use chrono::Utc;
use gitlawb_core::identity::Keypair;
use sqlx::PgPool;
use uuid::Uuid;
use super::{create_webhook, CreateWebhookRequest};
use crate::api::peers::{is_public_http_url, PUBLIC_HTTP_URL_REQUIREMENT};
use crate::auth::AuthenticatedDid;
use crate::db::RepoRecord;
use crate::error::AppError;
use crate::test_support::test_state;
// create_webhook gates req.url through is_public_http_url. Pin the exact
// SSRF targets from issue #81 so the webhook path can never regress to the
@@ -152,6 +165,7 @@ mod tests {
"http://127.0.0.1:5432/",
"http://169.254.169.254/latest/meta-data/",
"http://localhost/",
"http://node.localhost/",
"http://10.0.0.5/",
"http://[::1]/",
// IPv6 transition encodings smuggling loopback v4 (6to4 / NAT64).
@@ -169,4 +183,53 @@ mod tests {
assert!(is_public_http_url("https://hooks.example.com/gitlawb"));
assert!(is_public_http_url("http://203.0.113.10:7545/"));
}
#[sqlx::test]
async fn webhook_localhost_rejection_uses_shared_public_url_contract(pool: PgPool) {
let state = test_state(pool).await;
let owner = Keypair::generate().did().to_string();
let repo_id = Uuid::new_v4().to_string();
state
.db
.create_repo(&RepoRecord {
id: repo_id.clone(),
name: "webhook-contract".to_string(),
owner_did: owner.clone(),
description: None,
is_public: true,
default_branch: "main".to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
disk_path: "/tmp/webhook-contract.git".to_string(),
forked_from: None,
machine_id: None,
})
.await
.unwrap();
let result = create_webhook(
State(state.clone()),
Extension(AuthenticatedDid(owner.clone())),
Path((owner, "webhook-contract".to_string())),
Json(CreateWebhookRequest {
url: "https://node.localhost/hook".to_string(),
secret: None,
events: None,
}),
)
.await;
match result {
Err(AppError::BadRequest(message)) => assert_eq!(
message,
format!("webhook URL {PUBLIC_HTTP_URL_REQUIREMENT}")
),
Err(error) => panic!("unexpected webhook rejection: {error}"),
Ok(_) => panic!("a .localhost webhook must be rejected"),
}
assert!(
state.db.list_webhooks(&repo_id).await.unwrap().is_empty(),
"a rejected .localhost webhook must leave no row behind"
);
}
}
+836
View File
@@ -23,6 +23,133 @@ pub struct RepoRecord {
pub machine_id: Option<String>,
}
/// Which side is authoritative for a continuously mirrored repository.
///
/// A repository without an `upstream_url` has no mirror status. Transitioning
/// states always carry a durable job id and phase so a node restart can resume
/// or roll back instead of guessing which side owns writes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(not(test), allow(dead_code))]
pub enum MirrorStatus {
Inbound,
TransitioningInboundToOutbound,
Outbound,
TransitioningOutboundToInbound,
}
#[cfg_attr(not(test), allow(dead_code))]
impl MirrorStatus {
fn from_db(value: &str) -> Result<Self> {
match value {
"inbound" => Ok(Self::Inbound),
"transitioning_inbound_to_outbound" => Ok(Self::TransitioningInboundToOutbound),
"outbound" => Ok(Self::Outbound),
"transitioning_outbound_to_inbound" => Ok(Self::TransitioningOutboundToInbound),
_ => anyhow::bail!("invalid mirror status stored in database"),
}
}
fn is_transitioning(self) -> bool {
matches!(
self,
Self::TransitioningInboundToOutbound | Self::TransitioningOutboundToInbound
)
}
}
/// Durable progress marker for a mirror-authority transition job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(not(test), allow(dead_code))]
pub enum MirrorTransitionPhase {
Queued,
Starting,
InitializingMirrorFetch,
DrainingWrites,
FinalizingMirrorFetch,
FinalizingMirrorPush,
CommittingTargetStatus,
Completed,
RollingBack,
}
#[cfg_attr(not(test), allow(dead_code))]
impl MirrorTransitionPhase {
fn from_db(value: &str) -> Result<Self> {
match value {
"queued" => Ok(Self::Queued),
"starting" => Ok(Self::Starting),
"initializing_mirror_fetch" => Ok(Self::InitializingMirrorFetch),
"draining_writes" => Ok(Self::DrainingWrites),
"finalizing_mirror_fetch" => Ok(Self::FinalizingMirrorFetch),
"finalizing_mirror_push" => Ok(Self::FinalizingMirrorPush),
"committing_target_status" => Ok(Self::CommittingTargetStatus),
"completed" => Ok(Self::Completed),
"rolling_back" => Ok(Self::RollingBack),
_ => anyhow::bail!("invalid mirror transition phase stored in database"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(not(test), allow(dead_code))]
pub struct RepoMirrorState {
pub repo_id: String,
pub upstream_url: String,
pub status: MirrorStatus,
pub transition_id: Option<Uuid>,
pub transition_phase: Option<MirrorTransitionPhase>,
pub updated_at: DateTime<Utc>,
}
#[cfg_attr(not(test), allow(dead_code))]
impl RepoMirrorState {
fn validate(&self) -> Result<()> {
validate_mirror_upstream_url(&self.upstream_url)?;
match (
self.status.is_transitioning(),
self.transition_id,
self.transition_phase,
) {
(true, Some(_), Some(phase)) if phase != MirrorTransitionPhase::Completed => Ok(()),
(false, None, None) => Ok(()),
(false, Some(_), Some(MirrorTransitionPhase::Completed)) => Ok(()),
_ => anyhow::bail!("mirror status and transition metadata are inconsistent"),
}
}
}
#[cfg_attr(not(test), allow(dead_code))]
fn validate_mirror_upstream_url(raw: &str) -> Result<reqwest::Url> {
if raw.is_empty() || raw.len() > 2048 {
anyhow::bail!("mirror upstream URL must contain 1 to 2048 bytes");
}
if raw.chars().any(|c| c.is_whitespace() || c.is_control()) {
anyhow::bail!("mirror upstream URL contains whitespace or control characters");
}
let url = reqwest::Url::parse(raw).context("parsing mirror upstream URL")?;
if url.scheme() != "https" {
anyhow::bail!("mirror upstream URL must use HTTPS");
}
if !crate::api::peers::is_public_http_url(raw) {
anyhow::bail!("mirror upstream URL must use a public host");
}
if url.host_str().is_none() || url.path().trim_matches('/').is_empty() {
anyhow::bail!("mirror upstream URL must include a host and repository path");
}
if !url.username().is_empty() || url.password().is_some() {
anyhow::bail!("mirror upstream URL must not embed credentials");
}
if url.query().is_some() || url.fragment().is_some() {
anyhow::bail!("mirror upstream URL must not contain a query or fragment");
}
Ok(url)
}
/// One row of a keyset page from [`Db::list_repos_page_for_scan`].
///
/// Carries the row's quarantine flag inline (so the IPFS scan needs no separate
@@ -1158,6 +1285,79 @@ const MIGRATIONS: &[Migration] = &[
"ALTER TABLE pin_repair_sweep ADD COLUMN IF NOT EXISTS discovery_cursor_id TEXT NOT NULL DEFAULT ''",
],
},
// Reservation: open branches currently claim migration versions through
// v29. Gaps are valid, while reusing a version can silently skip one
// branch's schema when both eventually land. v30 and v31 are intentionally
// separate so validation does not inherit v30's stronger table lock.
Migration {
version: 30,
name: "repo_upstream_mirror_state",
stmts: &[
// Nullable, default-free columns keep this migration metadata-only
// for every existing non-mirror repository.
"ALTER TABLE repos ADD COLUMN IF NOT EXISTS upstream_url TEXT",
"ALTER TABLE repos ADD COLUMN IF NOT EXISTS mirror_status TEXT",
"ALTER TABLE repos ADD COLUMN IF NOT EXISTS mirror_transition_id UUID",
"ALTER TABLE repos ADD COLUMN IF NOT EXISTS mirror_transition_phase TEXT",
"ALTER TABLE repos ADD COLUMN IF NOT EXISTS mirror_updated_at TEXT",
// A constraint name does not prove its definition. Replace any
// same-named manual or partial-recovery artifact before installing
// the exact invariant this migration promises.
"ALTER TABLE repos DROP CONSTRAINT IF EXISTS repos_mirror_state_valid",
// PostgreSQL accepts a CHECK whose result is NULL. Wrap the complete
// invariant in IS TRUE so a missing status or phase cannot slip
// through three-valued logic.
r#"ALTER TABLE repos ADD CONSTRAINT repos_mirror_state_valid CHECK ((
(
upstream_url IS NULL
AND mirror_status IS NULL
AND mirror_transition_id IS NULL
AND mirror_transition_phase IS NULL
AND mirror_updated_at IS NULL
)
OR
(
upstream_url IS NOT NULL
AND upstream_url LIKE 'https://%'
AND upstream_url <> 'https://'
AND mirror_updated_at IS NOT NULL
AND (
(
mirror_status IN ('inbound', 'outbound')
AND (
(mirror_transition_id IS NULL AND mirror_transition_phase IS NULL)
OR
(mirror_transition_id IS NOT NULL AND mirror_transition_phase = 'completed')
)
)
OR
(
mirror_status IN (
'transitioning_inbound_to_outbound',
'transitioning_outbound_to_inbound'
)
AND mirror_transition_id IS NOT NULL
AND mirror_transition_phase IN (
'queued',
'starting',
'initializing_mirror_fetch',
'draining_writes',
'finalizing_mirror_fetch',
'finalizing_mirror_push',
'committing_target_status',
'rolling_back'
)
)
)
)
) IS TRUE) NOT VALID"#,
],
},
Migration {
version: 31,
name: "validate_repo_upstream_mirror_state",
stmts: &["ALTER TABLE repos VALIDATE CONSTRAINT repos_mirror_state_valid"],
},
];
/// Max distinct source repos recorded per pinned object (F1, #173 jatmn round 8).
@@ -1284,6 +1484,72 @@ impl Db {
Ok(())
}
/// Attach an external HTTPS upstream to a canonical repository in inbound
/// mode. This is deliberately idempotent only for the same untouched
/// inbound configuration; it never overwrites an existing upstream or
/// resets a transition that may be in progress.
#[cfg_attr(not(test), allow(dead_code))]
pub async fn configure_inbound_mirror(
&self,
repo_id: &str,
upstream_url: &str,
) -> Result<RepoMirrorState> {
Uuid::parse_str(repo_id)
.context("continuous mirrors require a canonical UUID repository id")?;
let upstream_url = validate_mirror_upstream_url(upstream_url)?.to_string();
let updated_at = Utc::now().to_rfc3339();
let row = sqlx::query(
"UPDATE repos
SET upstream_url = $2,
mirror_status = 'inbound',
mirror_transition_id = NULL,
mirror_transition_phase = NULL,
mirror_updated_at = COALESCE(mirror_updated_at, $3)
WHERE id = $1
AND (
upstream_url IS NULL
OR (
upstream_url = $2
AND mirror_status = 'inbound'
AND mirror_transition_id IS NULL
AND mirror_transition_phase IS NULL
)
)
RETURNING id, upstream_url, mirror_status, mirror_transition_id,
mirror_transition_phase, mirror_updated_at",
)
.bind(repo_id)
.bind(&upstream_url)
.bind(&updated_at)
.fetch_optional(&self.pool)
.await?;
let row = row.context(
"repository does not exist or already has a different/transitioning mirror configuration",
)?;
row_to_repo_mirror_state(row)
}
/// Return the durable mirror state, or `None` for a normal non-mirrored
/// repository (and for an unknown id). Callers that need to distinguish
/// those cases must resolve the repository first through the auth-gated
/// repository path.
#[cfg_attr(not(test), allow(dead_code))]
pub async fn get_repo_mirror_state(&self, repo_id: &str) -> Result<Option<RepoMirrorState>> {
let row = sqlx::query(
"SELECT id, upstream_url, mirror_status, mirror_transition_id,
mirror_transition_phase, mirror_updated_at
FROM repos
WHERE id = $1 AND upstream_url IS NOT NULL",
)
.bind(repo_id)
.fetch_optional(&self.pool)
.await?;
row.map(row_to_repo_mirror_state).transpose()
}
/// Register a mirrored repo from a peer in the local DB so git smart HTTP can serve it.
/// Uses INSERT OR IGNORE (SQLite) / ON CONFLICT DO NOTHING (Postgres) so it's idempotent.
pub async fn upsert_mirror_repo(
@@ -3930,6 +4196,576 @@ impl Db {
// ── Row helpers ───────────────────────────────────────────────────────────────
#[cfg_attr(not(test), allow(dead_code))]
fn row_to_repo_mirror_state(r: sqlx::postgres::PgRow) -> Result<RepoMirrorState> {
let repo_id = r
.try_get("id")
.context("reading mirror repository id from database")?;
let upstream_url = r
.try_get("upstream_url")
.context("reading mirror upstream URL from database")?;
let status = r
.try_get::<String, _>("mirror_status")
.context("reading mirror status from database")?;
let status = MirrorStatus::from_db(&status)?;
let transition_id = r
.try_get("mirror_transition_id")
.context("reading mirror transition id from database")?;
let transition_phase = r
.try_get::<Option<String>, _>("mirror_transition_phase")
.context("reading mirror transition phase from database")?
.as_deref()
.map(MirrorTransitionPhase::from_db)
.transpose()?;
let updated_at = r
.try_get::<String, _>("mirror_updated_at")
.context("reading mirror update timestamp from database")?
.parse::<DateTime<Utc>>()
.context("invalid mirror_updated_at stored in database")?;
let state = RepoMirrorState {
repo_id,
upstream_url,
status,
transition_id,
transition_phase,
updated_at,
};
state.validate()?;
Ok(state)
}
#[cfg(test)]
mod mirror_state_tests {
use super::{
row_to_repo_mirror_state, validate_mirror_upstream_url, Db, MirrorStatus,
MirrorTransitionPhase, RepoMirrorState, RepoRecord,
};
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use uuid::Uuid;
fn ts(value: &str) -> DateTime<Utc> {
value.parse().unwrap()
}
fn repo(id: Uuid) -> RepoRecord {
RepoRecord {
id: id.to_string(),
name: "mirror-fixture".to_string(),
owner_did: "did:key:z6MkMirrorOwner".to_string(),
description: None,
is_public: true,
default_branch: "main".to_string(),
created_at: ts("2026-08-14T00:00:00Z"),
updated_at: ts("2026-08-14T00:00:00Z"),
disk_path: format!("/srv/repos/{id}.git"),
forked_from: None,
machine_id: None,
}
}
async fn migrated_db(pool: PgPool) -> Db {
let db = Db::for_testing(pool);
db.run_migrations().await.unwrap();
db
}
#[test]
fn upstream_url_accepts_https_forges_without_credentials() {
for valid in [
"https://github.com/Gitlawb/node.git",
"https://gitlab.example.com/platform/subgroup/node",
"https://ghe.internal.example/org/repo.git",
] {
validate_mirror_upstream_url(valid).unwrap();
}
}
#[test]
fn upstream_url_rejects_unsafe_or_ambiguous_forms() {
for invalid in [
"",
"http://github.com/Gitlawb/node.git",
"ssh://[email protected]/Gitlawb/node.git",
"file:///etc/passwd",
"https://user:[email protected]/Gitlawb/node.git",
"https://github.com/Gitlawb/node.git?token=secret",
"https://github.com/Gitlawb/node.git#main",
"https://github.com/",
"https://github.com/Gitlawb/node.git\n",
"https://localhost/Gitlawb/node.git",
"https://forge.localhost/Gitlawb/node.git",
"https://127.0.0.1/Gitlawb/node.git",
"https://[::1]/Gitlawb/node.git",
"https://169.254.169.254/Gitlawb/node.git",
"https://10.0.0.5/Gitlawb/node.git",
"https://172.16.0.5/Gitlawb/node.git",
"https://192.168.1.5/Gitlawb/node.git",
"https://forge.internal/Gitlawb/node.git",
"https://[::ffff:127.0.0.1]/Gitlawb/node.git",
"https://[64:ff9b::7f00:1]/Gitlawb/node.git",
] {
assert!(
validate_mirror_upstream_url(invalid).is_err(),
"unsafe mirror URL was accepted: {invalid:?}"
);
}
}
#[test]
fn transition_metadata_must_match_the_status() {
let base = RepoMirrorState {
repo_id: Uuid::new_v4().to_string(),
upstream_url: "https://github.com/Gitlawb/node.git".to_string(),
status: MirrorStatus::Inbound,
transition_id: None,
transition_phase: None,
updated_at: Utc::now(),
};
base.validate().unwrap();
let mut invalid = base.clone();
invalid.status = MirrorStatus::TransitioningInboundToOutbound;
assert!(invalid.validate().is_err());
invalid.transition_id = Some(Uuid::new_v4());
invalid.transition_phase = Some(MirrorTransitionPhase::DrainingWrites);
invalid.validate().unwrap();
invalid.transition_phase = Some(MirrorTransitionPhase::Completed);
assert!(invalid.validate().is_err());
}
#[sqlx::test]
async fn migrations_v30_v31_upgrade_an_existing_repo_without_rewriting_it(pool: PgPool) {
let db = migrated_db(pool).await;
// Recreate the pre-v30 shape, then add data written by the old node.
sqlx::query("ALTER TABLE repos DROP COLUMN upstream_url CASCADE")
.execute(&db.pool)
.await
.unwrap();
for column in [
"mirror_status",
"mirror_transition_id",
"mirror_transition_phase",
"mirror_updated_at",
] {
sqlx::query(&format!("ALTER TABLE repos DROP COLUMN {column}"))
.execute(&db.pool)
.await
.unwrap();
}
sqlx::query("DELETE FROM schema_migrations WHERE version IN (30, 31)")
.execute(&db.pool)
.await
.unwrap();
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
db.run_migrations().await.unwrap();
let still_there: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM repos WHERE id = $1")
.bind(&existing.id)
.fetch_one(&db.pool)
.await
.unwrap();
assert_eq!(
still_there, 1,
"the existing repository must survive v30/v31"
);
let mirror_values: (Option<String>, Option<String>, Option<Uuid>, Option<String>) =
sqlx::query_as(
"SELECT upstream_url, mirror_status, mirror_transition_id,
mirror_transition_phase
FROM repos WHERE id = $1",
)
.bind(&existing.id)
.fetch_one(&db.pool)
.await
.unwrap();
assert_eq!(mirror_values, (None, None, None, None));
let recorded: Vec<(i64, String)> = sqlx::query_as(
"SELECT version, name FROM schema_migrations
WHERE version IN (30, 31) ORDER BY version",
)
.fetch_all(&db.pool)
.await
.unwrap();
assert_eq!(
recorded,
vec![
(30, "repo_upstream_mirror_state".to_string()),
(31, "validate_repo_upstream_mirror_state".to_string()),
]
);
let constraint_validated: bool = sqlx::query_scalar(
"SELECT convalidated FROM pg_constraint
WHERE conrelid = 'repos'::regclass
AND conname = 'repos_mirror_state_valid'",
)
.fetch_one(&db.pool)
.await
.unwrap();
assert!(constraint_validated, "v31 must validate the v30 constraint");
// Production entry point remains idempotent after the upgrade.
db.run_migrations().await.unwrap();
}
#[sqlx::test]
async fn migration_replaces_a_same_named_weaker_constraint(pool: PgPool) {
let db = migrated_db(pool).await;
sqlx::query("ALTER TABLE repos DROP CONSTRAINT repos_mirror_state_valid")
.execute(&db.pool)
.await
.unwrap();
sqlx::query(
"ALTER TABLE repos ADD CONSTRAINT repos_mirror_state_valid CHECK (TRUE) NOT VALID",
)
.execute(&db.pool)
.await
.unwrap();
sqlx::query("DELETE FROM schema_migrations WHERE version IN (30, 31)")
.execute(&db.pool)
.await
.unwrap();
db.run_migrations().await.unwrap();
let definition: String = sqlx::query_scalar(
"SELECT pg_get_constraintdef(oid)
FROM pg_constraint
WHERE conrelid = 'repos'::regclass
AND conname = 'repos_mirror_state_valid'",
)
.fetch_one(&db.pool)
.await
.unwrap();
assert!(
definition.contains("transitioning_inbound_to_outbound")
&& !definition.eq_ignore_ascii_case("CHECK (true)"),
"migration preserved an impostor constraint: {definition}"
);
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
let malformed = sqlx::query(
"UPDATE repos
SET upstream_url = 'https://github.com/Gitlawb/node.git',
mirror_status = 'transitioning_inbound_to_outbound',
mirror_updated_at = '2026-08-14T00:00:00Z'
WHERE id = $1",
)
.bind(&existing.id)
.execute(&db.pool)
.await;
assert!(
malformed.is_err(),
"replacement constraint accepted missing transition metadata"
);
}
#[sqlx::test]
async fn inbound_configuration_round_trips_without_overwriting_state(pool: PgPool) {
let db = migrated_db(pool).await;
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
assert!(db
.get_repo_mirror_state(&existing.id)
.await
.unwrap()
.is_none());
let first = db
.configure_inbound_mirror(&existing.id, "https://github.com/Gitlawb/node.git")
.await
.unwrap();
assert_eq!(first.status, MirrorStatus::Inbound);
assert_eq!(first.transition_id, None);
assert_eq!(first.transition_phase, None);
// An exact retry is safe, but a different source cannot replace it.
let original_updated_at = DateTime::parse_from_rfc3339("2026-01-02T03:04:05Z")
.unwrap()
.with_timezone(&Utc);
sqlx::query("UPDATE repos SET mirror_updated_at = $2 WHERE id = $1")
.bind(&existing.id)
.bind(original_updated_at.to_rfc3339())
.execute(&db.pool)
.await
.unwrap();
let retry = db
.configure_inbound_mirror(&existing.id, "https://github.com/Gitlawb/node.git")
.await
.unwrap();
assert_eq!(retry.updated_at, original_updated_at);
assert!(db
.configure_inbound_mirror(&existing.id, "https://github.com/Gitlawb/other.git",)
.await
.is_err());
let stored = db
.get_repo_mirror_state(&existing.id)
.await
.unwrap()
.unwrap();
assert_eq!(stored.upstream_url, first.upstream_url);
assert_eq!(stored.status, MirrorStatus::Inbound);
assert_eq!(stored.updated_at, original_updated_at);
}
#[sqlx::test]
async fn malformed_mirror_row_returns_an_error_instead_of_panicking(pool: PgPool) {
let row = sqlx::query(
"SELECT 'repo-id'::text AS id,
'https://github.com/Gitlawb/node.git'::text AS upstream_url,
'inbound'::text AS mirror_status,
NULL::uuid AS mirror_transition_id,
NULL::text AS mirror_transition_phase,
NULL::text AS mirror_updated_at",
)
.fetch_one(&pool)
.await
.unwrap();
let error = row_to_repo_mirror_state(row).unwrap_err();
assert!(
error
.to_string()
.contains("reading mirror update timestamp from database"),
"unexpected error: {error:#}"
);
}
#[sqlx::test]
async fn inbound_configuration_never_resets_an_active_transition(pool: PgPool) {
let db = migrated_db(pool).await;
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
db.configure_inbound_mirror(&existing.id, "https://github.com/Gitlawb/node.git")
.await
.unwrap();
let transition_id = Uuid::new_v4();
sqlx::query(
"UPDATE repos
SET mirror_status = 'transitioning_inbound_to_outbound',
mirror_transition_id = $2,
mirror_transition_phase = 'draining_writes'
WHERE id = $1",
)
.bind(&existing.id)
.bind(transition_id)
.execute(&db.pool)
.await
.unwrap();
let retry = db
.configure_inbound_mirror(&existing.id, "https://github.com/Gitlawb/node.git")
.await;
assert!(retry.is_err(), "configuration must not reset a transition");
let stored = db
.get_repo_mirror_state(&existing.id)
.await
.unwrap()
.unwrap();
assert_eq!(stored.status, MirrorStatus::TransitioningInboundToOutbound);
assert_eq!(stored.transition_id, Some(transition_id));
assert_eq!(
stored.transition_phase,
Some(MirrorTransitionPhase::DrainingWrites)
);
}
#[sqlx::test]
async fn database_constraint_rejects_partial_or_insecure_mirror_state(pool: PgPool) {
let db = migrated_db(pool).await;
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
let partial = sqlx::query(
"UPDATE repos
SET upstream_url = 'https://github.com/Gitlawb/node.git',
mirror_status = 'transitioning_inbound_to_outbound',
mirror_updated_at = '2026-08-14T00:00:00Z'
WHERE id = $1",
)
.bind(&existing.id)
.execute(&db.pool)
.await;
assert!(partial.is_err(), "transition metadata must be durable");
// PostgreSQL CHECK constraints accept NULL results. These cases pin
// the outer `IS TRUE` guard so SQL three-valued logic cannot admit a
// row that the Rust reader rejects or panics while decoding.
struct InvalidNullState<'a> {
case: &'a str,
status: Option<&'a str>,
transition_id: Option<Uuid>,
transition_phase: Option<&'a str>,
}
let invalid_null_states = [
InvalidNullState {
case: "missing mirror status",
status: None,
transition_id: None,
transition_phase: None,
},
InvalidNullState {
case: "stable status with job but no phase",
status: Some("inbound"),
transition_id: Some(Uuid::new_v4()),
transition_phase: None,
},
InvalidNullState {
case: "transition with job but no phase",
status: Some("transitioning_inbound_to_outbound"),
transition_id: Some(Uuid::new_v4()),
transition_phase: None,
},
];
for invalid in invalid_null_states {
let result = sqlx::query(
"UPDATE repos
SET upstream_url = 'https://github.com/Gitlawb/node.git',
mirror_status = $2,
mirror_transition_id = $3,
mirror_transition_phase = $4,
mirror_updated_at = '2026-08-14T00:00:00Z'
WHERE id = $1",
)
.bind(&existing.id)
.bind(invalid.status)
.bind(invalid.transition_id)
.bind(invalid.transition_phase)
.execute(&db.pool)
.await;
assert!(result.is_err(), "constraint accepted {}", invalid.case);
}
let insecure = sqlx::query(
"UPDATE repos
SET upstream_url = 'http://github.com/Gitlawb/node.git',
mirror_status = 'inbound',
mirror_updated_at = '2026-08-14T00:00:00Z'
WHERE id = $1",
)
.bind(&existing.id)
.execute(&db.pool)
.await;
assert!(
insecure.is_err(),
"the database must reject non-HTTPS upstreams"
);
}
#[sqlx::test]
async fn database_constraint_accepts_the_complete_transition_vocabulary(pool: PgPool) {
let db = migrated_db(pool).await;
let existing = repo(Uuid::new_v4());
db.create_repo(&existing).await.unwrap();
let phases = [
"queued",
"starting",
"initializing_mirror_fetch",
"draining_writes",
"finalizing_mirror_fetch",
"finalizing_mirror_push",
"committing_target_status",
"rolling_back",
];
for status in [
"transitioning_inbound_to_outbound",
"transitioning_outbound_to_inbound",
] {
for phase in phases {
sqlx::query(
"UPDATE repos
SET upstream_url = 'https://github.com/Gitlawb/node.git',
mirror_status = $2,
mirror_transition_id = $3,
mirror_transition_phase = $4,
mirror_updated_at = '2026-08-14T00:00:00Z'
WHERE id = $1",
)
.bind(&existing.id)
.bind(status)
.bind(Uuid::new_v4())
.bind(phase)
.execute(&db.pool)
.await
.unwrap_or_else(|error| panic!("{status}/{phase} was rejected: {error}"));
}
}
// A completed job remains auditable after authority reaches its stable
// target status; the initial never-transitioned state uses two NULLs.
sqlx::query(
"UPDATE repos
SET mirror_status = 'outbound',
mirror_transition_id = $2,
mirror_transition_phase = 'completed'
WHERE id = $1",
)
.bind(&existing.id)
.bind(Uuid::new_v4())
.execute(&db.pool)
.await
.unwrap();
let stored = db
.get_repo_mirror_state(&existing.id)
.await
.unwrap()
.unwrap();
assert_eq!(stored.status, MirrorStatus::Outbound);
assert_eq!(
stored.transition_phase,
Some(MirrorTransitionPhase::Completed)
);
}
#[sqlx::test]
async fn continuous_mirror_uuid_gate_leaves_peer_rows_untouched(pool: PgPool) {
let db = migrated_db(pool).await;
db.upsert_mirror_repo("z6MkPeer", "repo", "/srv/peer/repo.git", None, false)
.await
.unwrap();
let error = db
.configure_inbound_mirror("z6MkPeer/repo", "https://github.com/Gitlawb/node.git")
.await
.unwrap_err();
assert!(
format!("{error:#}")
.contains("continuous mirrors require a canonical UUID repository id"),
"unexpected error: {error:#}"
);
let unchanged: (Option<String>, Option<String>) =
sqlx::query_as("SELECT upstream_url, mirror_status FROM repos WHERE id = $1")
.bind("z6MkPeer/repo")
.fetch_one(&db.pool)
.await
.unwrap();
assert_eq!(unchanged, (None, None));
assert!(db
.get_repo_mirror_state("z6MkPeer/repo")
.await
.unwrap()
.is_none());
}
}
fn row_to_repo(r: sqlx::postgres::PgRow) -> RepoRecord {
let created_str: String = r.get("created_at");
let updated_str: String = r.get("updated_at");
+3 -1
View File
@@ -314,7 +314,9 @@ pub fn record_sync_processed(status: &str) {
}
/// Record one webhook delivery attempt outcome. `result` ∈
/// {ok, http_error, network_error, skipped}.
/// {ok, http_error, network_error, skipped, invalid_url}.
/// `invalid_url` is a policy skip: the stored hook URL failed the public-URL
/// check at delivery time and the POST was never sent.
pub fn record_webhook_delivery(result: &str) {
if let Some(c) = WEBHOOK_DELIVERIES.get() {
c.with_label_values(&[result]).inc();
+708 -17
View File
@@ -237,35 +237,62 @@ pub fn build_router(state: AppState) -> Router {
.layer(axum::Extension(push_limiter));
// ── IPFS content-addressed retrieval and pin listing ──────────────────
// `/ipfs/{cid}` carries `optional_signature` so `get_by_cid` sees the caller
// identity and can apply per-repo visibility (#110); anonymous callers stay
// anonymous and still read genuinely public content. `/api/v1/ipfs/pins`
// stays unsigned — gating the pin index is tracked separately (#121).
// `/ipfs/{cid}` also carries a per-IP flood brake: it is anon-reachable and each
// request can drive a full-history git walk, so the per-IP rate limiter is the
// outermost layer (rejects a flood before the walk-admission work), mirroring the
// push/create routers. The extension MUST be attached or rate_limit_by_ip is a
// silent no-op. `/api/v1/ipfs/pins` (no walk) is merged in unbraked, as before.
// Two independent sub-routers, then merged. They share a URL prefix family
// but have separate rate-limit policies and must not share a bucket.
//
// `/ipfs/{cid}` (CID resolver): carries `optional_signature` so `get_by_cid`
// sees the caller identity and can apply per-repo visibility (#110); anon
// callers stay anonymous and still read genuinely public content. The
// per-IP flood brake is layered on because the resolver is anon-reachable
// and each request can drive a full-history git walk — the brake is the
// outermost layer (rejects a flood before the walk-admission work), mirroring
// the push/create routers. The `IpRateLimiter` extension MUST be attached
// or `rate_limit_by_ip` is a silent no-op.
//
// `/api/v1/ipfs/pins` (pin listing): now carries `optional_signature` only
// (#121). The handler rejects requests without a verified `AuthenticatedDid`
// with 401. It does NOT carry the CID flood brake — `list_pins` is a single
// `list_pinned_cids()` call, no walk, and routing pins through the resolver's
// bucket would let `/ipfs/{cid}` traffic exhaust the bucket and 429 the
// pins endpoint, or let signed pin polling exhaust the bucket for legitimate
// CID reads. The two surfaces have separate availability contracts.
//
// Both sub-routers are built first with their own layer sets, then merged.
// This is the structure the prior routing guidance called for and the
// earlier 429 test for `/ipfs/{cid}` (test_support.rs) assumes.
let ipfs_limiter = rate_limit::IpRateLimiter {
limiter: state.ipfs_rate_limiter.clone(),
trust: state.push_limiter_trust,
};
let ipfs_routes = Router::new()
let ipfs_cid_routes = Router::new()
.route("/ipfs/{cid}", get(ipfs::get_by_cid))
.layer(middleware::from_fn_with_state(
auth::SignatureBodyLimit(SIGNED_BODY_LIMIT),
auth::optional_signature,
))
.layer(middleware::from_fn(rate_limit::rate_limit_by_ip))
.layer(axum::Extension(ipfs_limiter))
.merge(Router::new().route("/api/v1/ipfs/pins", get(ipfs::list_pins)));
.layer(axum::Extension(ipfs_limiter));
let ipfs_pins_routes = Router::new()
.route("/api/v1/ipfs/pins", get(ipfs::list_pins))
.layer(middleware::from_fn_with_state(
auth::SignatureBodyLimit(SIGNED_BODY_LIMIT),
auth::optional_signature,
));
let ipfs_routes = ipfs_cid_routes.merge(ipfs_pins_routes);
// ── Arweave permanent anchors ──────────────────────────────────────────
// List endpoint only (public; issue #134 tracks surfacing
// visibility rules on list). The verify endpoint and its probe
// are deferred to the vertical slice with the uploader and a
// real provider contract (reviewer 2, #26 split 2/4 round 5).
let arweave_routes = Router::new().route("/api/v1/arweave/anchors", get(arweave::list_anchors));
// `list_anchors` rejects callers without a verified `AuthenticatedDid`, so
// unsigned enumeration is denied. The same `optional_signature` layer used
// on the other read surfaces is applied here, closing the anonymous
// anchor-listing gap alongside `/api/v1/ipfs/pins`. The verify endpoint
// and its probe are deferred to the vertical slice with the uploader and
// a real provider contract.
let arweave_routes = Router::new()
.route("/api/v1/arweave/anchors", get(arweave::list_anchors))
.layer(middleware::from_fn_with_state(
auth::SignatureBodyLimit(SIGNED_BODY_LIMIT),
auth::optional_signature,
));
// ── Bounty routes (write — require HTTP Signature) ─────────────────
let bounty_write_routes = add_auth_layers(
@@ -672,3 +699,667 @@ async fn p2p_info(State(state): State<AppState>) -> Json<serde_json::Value> {
None => Json(json!({ "enabled": false })),
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use sqlx::PgPool;
use tower::ServiceExt;
use crate::db::RepoRecord;
use crate::test_support::test_state;
/// Regression: anonymous callers must not see the pin/anchor index (#121, #134).
#[sqlx::test]
async fn unsigned_get_pins_and_anchors_is_401_through_build_router(pool: PgPool) {
let state = test_state(pool).await;
// Seed identifiable rows so the denial oracle proves the 401 body omits
// them: a status-only check would stay green if a later change served
// the listing before the auth layer rejected.
state
.db
.record_pinned_cid("seeded-pin-oid", "bafyPIN-SECRET-DO-NOT-LEAK", None)
.await
.expect("seed pin");
let owner_did = "did:key:zSEEDOWNERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let repo = seed_repo_inline(owner_did, "seeded-repo");
state.db.create_repo(&repo).await.expect("seed repo");
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: "zSEEDOWNERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/seeded-repo",
owner_did,
ref_name: "refs/heads/secret",
old_sha: "0".repeat(64).as_str(),
new_sha: "1".repeat(64).as_str(),
cid: Some("bafytest"),
irys_tx_id: "irys-secret-tx-DO-NOT-LEAK",
arweave_url: "https://arweave.net/secret-tx",
node_did: "did:key:zNODE",
})
.await
.expect("seed anchor");
let router = build_router(state);
let pins = Request::builder()
.method("GET")
.uri("/api/v1/ipfs/pins?limit=50")
.body(Body::empty())
.unwrap();
let pins_resp = router.clone().oneshot(pins).await.unwrap();
assert_eq!(
pins_resp.status(),
StatusCode::UNAUTHORIZED,
"anonymous pin listing must be rejected"
);
let bytes = axum::body::to_bytes(pins_resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
!body.contains("bafyPIN-SECRET-DO-NOT-LEAK"),
"the 401 body must not leak the seeded pin cid; body was: {body}"
);
let anchors = Request::builder()
.method("GET")
.uri("/api/v1/arweave/anchors?limit=50")
.body(Body::empty())
.unwrap();
let anchors_resp = router.oneshot(anchors).await.unwrap();
assert_eq!(
anchors_resp.status(),
StatusCode::UNAUTHORIZED,
"anonymous anchors listing must be rejected"
);
let bytes = axum::body::to_bytes(anchors_resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
!body.contains("irys-secret-tx-DO-NOT-LEAK"),
"the 401 body must not leak the anchor's irys tx id; body was: {body}"
);
}
/// Regression: a real RFC-9421 signature produced exactly as `gl` does — built
/// with `gitlawb_core::http_sig::sign_request` over a GET, headers attached,
/// and sent through the actual `build_router` — is verified by the
/// `optional_signature` layer that wraps the pin/anchor routes, and the
/// handler returns 200. Pairs with the anonymous-denial test above; one
/// proves headers are required, the other proves a valid header is honored.
/// Without this test the unsigned-denial test would stay green even if the
/// `optional_signature` layer were never wired onto these routes, because
/// signed and unsigned requests would fail identically (#134 review).
#[sqlx::test]
async fn signed_get_pins_and_anchors_succeeds_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let state = test_state(pool).await;
let router = build_router(state);
let kp = Keypair::generate();
let pins_path = "/api/v1/ipfs/pins";
let signed = sign_request(&kp, "GET", pins_path, b"");
let pins = Request::builder()
.method("GET")
.uri(pins_path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let pins_resp = router.clone().oneshot(pins).await.unwrap();
assert_eq!(
pins_resp.status(),
StatusCode::OK,
"a valid signature on /api/v1/ipfs/pins must be honored through build_router"
);
let anchors_path = "/api/v1/arweave/anchors";
let signed = sign_request(&kp, "GET", anchors_path, b"");
let anchors = Request::builder()
.method("GET")
.uri(anchors_path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let anchors_resp = router.oneshot(anchors).await.unwrap();
assert_eq!(
anchors_resp.status(),
StatusCode::OK,
"a valid signature on /api/v1/arweave/anchors must be honored through build_router"
);
}
/// Companion to the two regressions above: a request that *carries* signature
/// headers but whose signature does not verify (here, garbled) must be denied
/// with 401, not silently treated as anonymous and re-checked by the handler.
/// This pins the failure mode of the `optional_signature` layer: when the
/// caller claims to be signed, the layer must commit to verifying — there is
/// no fall-through path that lets a bad signature bypass auth.
#[sqlx::test]
async fn malformed_signature_on_pins_is_401_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let state = test_state(pool).await;
let router = build_router(state);
let kp = Keypair::generate();
let path = "/api/v1/ipfs/pins";
let mut signed = sign_request(&kp, "GET", path, b"");
// Flip a character well inside the signature value (base64) so the header
// still parses but does not verify against the key.
let mut tampered = signed.signature.clone();
let mid = tampered.len() / 2;
let flipped = if tampered.as_bytes()[mid] == b'A' {
'B'
} else {
'A'
};
tampered.replace_range(mid..mid + 1, &flipped.to_string());
signed.signature = tampered;
let req = Request::builder()
.method("GET")
.uri(path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::UNAUTHORIZED,
"a malformed signature must be rejected by the auth layer, not silently accepted"
);
}
/// The same malformed-signature rejection must hold on the anchors route,
/// which carries its own `optional_signature` layer beside pins: a later
/// edit that dropped the layer here (but not on pins) would otherwise stay
/// green.
#[sqlx::test]
async fn malformed_signature_on_anchors_is_401_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let state = test_state(pool).await;
let router = build_router(state);
let kp = Keypair::generate();
let path = "/api/v1/arweave/anchors";
let mut signed = sign_request(&kp, "GET", path, b"");
let mut tampered = signed.signature.clone();
let mid = tampered.len() / 2;
let flipped = if tampered.as_bytes()[mid] == b'A' {
'B'
} else {
'A'
};
tampered.replace_range(mid..mid + 1, &flipped.to_string());
signed.signature = tampered;
let req = Request::builder()
.method("GET")
.uri(path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::UNAUTHORIZED,
"a malformed signature must be rejected by the auth layer, not silently accepted"
);
}
// ── Scoped anchor contract (P1 follow-up: ?repo= must authorize_repo_read) ──
//
// The follow-up review (after the auth-layer wiring landed) found that a
// signed but unauthorized caller could still obtain scoped anchor metadata
// because the `?repo=` branch handed the user-supplied string straight to
// the SQL filter. These tests exercise the full production contract end to
// end: real RFC-9421 signature → `optional_signature` layer →
// `authorize_repo_read` → SQL. They fail closed if the authz call is moved
// after the query or removed entirely.
/// Inline repo seed for the scoped-anchor tests. Mirrors the shape in
/// `test_support::tests::seed_repo` without taking a cross-module private
/// helper as a dependency.
fn seed_repo_inline(owner_did: &str, name: &str) -> RepoRecord {
let now = chrono::Utc::now();
RepoRecord {
id: uuid::Uuid::new_v4().to_string(),
name: name.to_string(),
owner_did: owner_did.to_string(),
description: None,
is_public: true,
default_branch: "main".to_string(),
created_at: now,
updated_at: now,
disk_path: format!("/tmp/{name}"),
forked_from: None,
machine_id: None,
}
}
/// Unsigned scoped anchor request → 401, identical to the global anchor
/// 401 test. Proves the auth layer fires before any scope decision (no
/// existence oracle via the `?repo=` path either).
#[sqlx::test]
async fn unsigned_scoped_anchors_is_401_through_build_router(pool: PgPool) {
let state = test_state(pool).await;
// Seed an anchor under a repo the request claims to scope to, so the
// denial oracle proves the 401 body omits it rather than merely
// returning the right status over an empty table.
let owner_did = "did:key:zSCOPEDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let repo = seed_repo_inline(owner_did, "priv");
state.db.create_repo(&repo).await.expect("seed repo");
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: "zSCOPEDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/priv",
owner_did,
ref_name: "refs/heads/secret",
old_sha: "0".repeat(64).as_str(),
new_sha: "1".repeat(64).as_str(),
cid: Some("bafytest"),
irys_tx_id: "irys-scoped-secret-DO-NOT-LEAK",
arweave_url: "https://arweave.net/scoped-secret",
node_did: "did:key:zNODE",
})
.await
.expect("seed anchor");
let router = build_router(state);
let resp = router
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/arweave/anchors?repo=did:key:zSCOPED%2Fpriv")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::UNAUTHORIZED,
"unsigned scoped anchor request must be 401, before any scope lookup"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
!body.contains("irys-scoped-secret-DO-NOT-LEAK"),
"the 401 body must not leak the anchor's irys tx id; body was: {body}"
);
}
/// Signed owner on a private repo → 200, anchor metadata returned. Mirrors
/// the existing `list_webhooks_accepts_a_real_gl_signature_e2e` shape: real
/// signature, real middleware, real `authorize_repo_read`.
#[sqlx::test]
async fn signed_scoped_anchors_owner_succeeds_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let owner_did = kp.did().to_string();
let state = test_state(pool).await;
let mut repo = seed_repo_inline(&owner_did, "scoped-priv");
repo.is_public = false;
state.db.create_repo(&repo).await.expect("seed repo");
let short = owner_did.split(':').next_back().unwrap().to_string();
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: &format!("{short}/scoped-priv"),
owner_did: &owner_did,
ref_name: "refs/heads/main",
old_sha: "0".repeat(64).as_str(),
new_sha: "1".repeat(64).as_str(),
cid: Some("bafytest"),
irys_tx_id: "irys-owner-tx",
arweave_url: "https://arweave.net/owner-tx",
node_did: "did:key:zNODE",
})
.await
.expect("seed anchor");
let path = format!("/api/v1/arweave/anchors?repo={short}/scoped-priv");
let signed = sign_request(&kp, "GET", &path, b"");
let req = Request::builder()
.method("GET")
.uri(&path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let router = build_router(state);
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"the owner of a private repo must see their scoped anchors"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
body.contains("irys-owner-tx"),
"owner must see the anchor's irys tx id; body was: {body}"
);
// Owner must see the ref name + new SHA so this is not just a count oracle.
assert!(
body.contains("refs/heads/main"),
"owner must see the ref name; body was: {body}"
);
// Sanity: a second test would need a different repo name to avoid
// colliding on `did:key:zSCOPED/scoped-priv` in the anchors table.
// Distinct repo names per test keep the rows queryable in isolation.
let _ = repo;
}
/// #121 R3: the SQL filter must use the canonical stored slug, not the
/// user-supplied `?repo=` string. Anchor rows are stored as
/// `{normalize_owner_key(owner_did)}/{name}` (the short form). Authz
/// passes for the full-DID form, but a literal `WHERE repo=$1` against
/// the full DID would match zero rows and return a false empty page,
/// breaking callers that legitimately use the full `did:key:…/name`
/// form in `?repo=`. RED before the fix: handler built the filter from
/// the raw `?repo=` string, so this query returned `anchors: []`.
#[sqlx::test]
async fn signed_scoped_anchors_full_did_form_matches_stored_slug_through_build_router(
pool: PgPool,
) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let owner_did = kp.did().to_string();
let state = test_state(pool).await;
let mut repo = seed_repo_inline(&owner_did, "scoped-did-form");
repo.is_public = false;
state.db.create_repo(&repo).await.expect("seed repo");
// Use the same slug-construction the writer uses
// (`db::normalize_owner_key`) so the test reader and the production
// writer can't drift if a future change moves the slug off the
// short-form. P3 (reviewer-3): the previous `split(':').next_back()`
// hand-rolled a third convention that agreed with the writer for
// `did:key` owners and diverged for every other method.
let short = crate::db::normalize_owner_key(&owner_did).to_string();
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: &format!("{short}/scoped-did-form"),
owner_did: &owner_did,
ref_name: "refs/heads/main",
old_sha: "0".repeat(64).as_str(),
new_sha: "1".repeat(64).as_str(),
cid: Some("bafytest"),
irys_tx_id: "irys-did-form-tx",
arweave_url: "https://arweave.net/did-form-tx",
node_did: "did:key:zNODE",
})
.await
.expect("seed anchor");
// P2 (reviewer-3): the previous suite only proved the authorized
// row is present. A correct filter must ALSO prove that anchors
// for OTHER repos do not leak into the response — the failure
// mode of the original bug was a cross-repo leak, not a
// fail-safe empty page. Seed a second repo (also under the
// same owner, so the authz test stays scoped) with its own
// irys_tx_id and assert it is absent from the body.
let other_owner = {
let kp2 = Keypair::generate();
kp2.did().to_string()
};
let mut other_repo = seed_repo_inline(&other_owner, "other-repo");
other_repo.is_public = true;
state
.db
.create_repo(&other_repo)
.await
.expect("seed other repo");
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: &format!(
"{}/other-repo",
crate::db::normalize_owner_key(&other_owner)
),
owner_did: &other_owner,
ref_name: "refs/heads/main",
old_sha: "0".repeat(64).as_str(),
new_sha: "2".repeat(64).as_str(),
cid: Some("bafyother"),
irys_tx_id: "irys-other-repo-tx",
arweave_url: "https://arweave.net/other-tx",
node_did: "did:key:zNODE",
})
.await
.expect("seed other anchor");
// The caller passes the FULL DID form in ?repo=. validate_repo_slug
// accepts it, authorize_repo_read resolves it, and the handler MUST
// then translate to the canonical stored slug (short form) before
// running the SQL — otherwise this returns 200 with anchors: [].
let path = format!("/api/v1/arweave/anchors?repo={owner_did}/scoped-did-form");
let signed = sign_request(&kp, "GET", &path, b"");
let req = Request::builder()
.method("GET")
.uri(&path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let router = build_router(state);
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"authz passed via the full DID form, so the response must be 200"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
body.contains("irys-did-form-tx"),
"the SQL filter must use the stored short slug, not the full DID — \
a false-empty page would break callers that pass ?repo=did:key:…/name. \
body was: {body}"
);
assert!(
!body.contains("irys-other-repo-tx"),
"the scoped anchor filter must exclude other repos' anchors — \
a cross-repo leak would expose metadata of repos the caller \
did not authorize. body was: {body}"
);
let _ = repo;
}
/// Signed non-reader on a private repo → 404, anchor metadata MUST NOT
/// leak. This is the test that catches the "auth-but-no-authz" bug if
/// the gate is moved after the SQL query or removed entirely. The 404
/// is the standard `repo_not_found` shape — indistinguishable from the
/// missing-repo case below.
#[sqlx::test]
async fn signed_scoped_anchors_non_reader_is_404_no_leak_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let owner_kp = Keypair::generate();
let stranger_kp = Keypair::generate();
let owner_did = owner_kp.did().to_string();
let stranger_did = stranger_kp.did().to_string();
let state = test_state(pool).await;
let mut repo = seed_repo_inline(&owner_did, "scoped-priv-nr");
repo.is_public = false;
state.db.create_repo(&repo).await.expect("seed repo");
let short_owner = owner_did.split(':').next_back().unwrap().to_string();
state
.db
.record_arweave_anchor(&crate::db::RecordAnchorInput {
repo: &format!("{short_owner}/scoped-priv-nr"),
owner_did: &owner_did,
ref_name: "refs/heads/secret",
old_sha: "2".repeat(64).as_str(),
new_sha: "3".repeat(64).as_str(),
cid: Some("bafytest"),
irys_tx_id: "irys-secret-tx-DO-NOT-LEAK",
arweave_url: "https://arweave.net/secret-tx",
node_did: "did:key:zNODE",
})
.await
.expect("seed anchor");
let _ = stranger_did; // signature carries the DID; this is just documentation
let path = format!("/api/v1/arweave/anchors?repo={short_owner}/scoped-priv-nr");
let signed = sign_request(&stranger_kp, "GET", &path, b"");
let req = Request::builder()
.method("GET")
.uri(&path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let router = build_router(state);
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"a signed non-reader must be 404 on a private repo's anchors"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8_lossy(&bytes);
assert!(
!body.contains("irys-secret-tx-DO-NOT-LEAK"),
"the 404 body must not leak the anchor's irys tx id; body was: {body}"
);
assert!(
!body.contains("refs/heads/secret"),
"the 404 body must not leak the ref name; body was: {body}"
);
let v: serde_json::Value = serde_json::from_slice(&bytes).expect("json body");
assert_eq!(
v["error"], "repo_not_found",
"non-reader denial must carry the same error code as a missing repo"
);
}
/// Signed caller on a missing repo → 404, same shape as the non-reader
/// case. This is the indistinguishability half: a probe cannot tell
/// "private" from "absent" from the response.
#[sqlx::test]
async fn signed_scoped_anchors_missing_repo_is_404_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let state = test_state(pool).await;
let short = kp
.did()
.to_string()
.split(':')
.next_back()
.unwrap()
.to_string();
let path = format!("/api/v1/arweave/anchors?repo={short}/does-not-exist");
let signed = sign_request(&kp, "GET", &path, b"");
let req = Request::builder()
.method("GET")
.uri(&path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let router = build_router(state);
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"a missing repo must 404 indistinguishably from a non-readable private repo"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let v: serde_json::Value = serde_json::from_slice(&bytes).expect("json body");
assert_eq!(
v["error"], "repo_not_found",
"the 404 body must carry the standard repo_not_found error code"
);
}
/// `?limit=-1` must not crash as `LIMIT -1` (Postgres 500). It clamps to
/// zero and returns 200 with an empty list — the same shape as a valid
/// listing that happens to have no rows in the configured range.
#[sqlx::test]
async fn signed_anchors_negative_limit_clamps_to_zero_through_build_router(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let state = test_state(pool).await;
let path = "/api/v1/arweave/anchors?limit=-1";
let signed = sign_request(&kp, "GET", path, b"");
let req = Request::builder()
.method("GET")
.uri(path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature)
.body(Body::empty())
.unwrap();
let router = build_router(state);
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"?limit=-1 must clamp, not 500"
);
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let v: serde_json::Value = serde_json::from_slice(&bytes).expect("json body");
assert_eq!(v["count"], 0, "clamped limit yields an empty list");
assert_eq!(v["anchors"].as_array().map(|a| a.len()), Some(0));
}
}
+81 -1
View File
@@ -701,6 +701,7 @@ async fn clone_repo(remote_url: &str, local_path: &Path, mode: MirrorMode) -> an
if mode == MirrorMode::Promisor {
args.push("--filter=blob:limit=10g");
}
args.push("--");
args.push(remote_url);
args.push(local_str);
@@ -729,7 +730,10 @@ async fn clone_repo(remote_url: &str, local_path: &Path, mode: MirrorMode) -> an
async fn fetch_repo(local_path: &Path, remote_url: &str, mode: MirrorMode) -> anyhow::Result<()> {
let local_str = local_path.to_str().unwrap_or(".");
git_run(&["-C", local_str, "remote", "set-url", "origin", remote_url]).await?;
git_run(&[
"-C", local_str, "remote", "set-url", "origin", "--", remote_url,
])
.await?;
match mode {
MirrorMode::Promisor => {
@@ -1006,6 +1010,82 @@ mod tests {
assert_eq!(probe, PromisorProbe::NotPromisor);
}
/// #374: `clone_repo` interpolates the peer's origin URL into a `git
/// clone --mirror` argument list. Without a `--` delimiter git parses a
/// remote beginning with `-` as an option instead of a repository.
///
/// The delimiter is what forces the same string to be a repository, and
/// git says so in its own words: it reports the whole argument as a
/// repository it cannot find. Undelimited, git consumes it as an option
/// and fails against the *destination* instead (`fatal: repository '<dest>' does not exist`),
/// never naming the injected string — which is why this assertion is RED
/// before the fix and GREEN after.
///
/// The payload is deliberately free of `:` and spaces: a colon would make
/// git read the argument as an ssh-style `host:path` URL and report a
/// hostname rather than a repository, which is a different code path.
#[tokio::test]
async fn clone_never_parses_a_remote_as_a_git_option() {
let td = TempDir::new().unwrap();
let dest = td.path().join("mirror.git");
let injected = "--upload-pack=false";
let err = clone_repo(injected, &dest, MirrorMode::Plain)
.await
.expect_err("a remote that is not a repository must fail the clone")
.to_string();
assert!(
err.contains(injected),
"git must report the argument as a repository, not consume it as an option: {err}"
);
}
/// #374: `fetch_repo` interpolates the peer's origin URL into
/// `git remote set-url`, a second argv construction with the same contract as
/// the clone. It needs its own proof: the clone test cannot reach this path.
///
/// The assertion is on stored state rather than on the error string, and that
/// is deliberate. `git_run` formats its failure as `git {args:?} failed:
/// {stderr}`, so the injected value is in the message via the debug-printed
/// argv whether or not the delimiter is present — asserting on containment
/// would pass with `--` deleted. Delimited, `set-url` succeeds and stores the
/// value verbatim; undelimited, git exits 129 with `unknown option` and leaves
/// the previous URL in place, so this goes red without the fix.
///
/// The fetch that follows the rewrite fails, because the stored URL is not a
/// repository. That is expected and ignored: the property under test is what
/// `set-url` wrote, not whether the subsequent fetch could succeed.
#[tokio::test]
async fn fetch_never_parses_a_remote_as_a_git_option() {
let td = TempDir::new().unwrap();
let local = td.path().join("mirror.git");
let local_str = local.to_str().unwrap();
let placeholder = "http://example.invalid/x.git";
assert!(Command::new("git")
.args(["init", "-q", "--bare", local_str])
.status()
.unwrap()
.success());
assert!(Command::new("git")
.args(["-C", local_str, "remote", "add", "origin", placeholder])
.status()
.unwrap()
.success());
let injected = "--upload-pack=false";
let _ = fetch_repo(&local, injected, MirrorMode::Plain).await;
assert_eq!(
git_config_get(local_str, "remote.origin.url")
.await
.as_deref(),
Some(injected),
"set-url must store an option-shaped remote verbatim, not reject it as an option"
);
}
#[tokio::test]
async fn probe_reports_unknown_on_git_error() {
// A path git cannot resolve as a repo at all (exit 128) is an indeterminate
+175
View File
@@ -16560,6 +16560,181 @@ mod tests {
);
}
/// #134 R2 (split buckets): signed `/api/v1/ipfs/pins` polling must NOT debit
/// the CID resolver's per-IP flood brake. The pre-fix composition merged
/// pins under the same router as `/ipfs/{cid}`, so every signed pin
/// request consumed a token from `ipfs_rate_limiter` and a flood of pinned
/// traffic could 429 legitimate CID reads. RED before the split (pins
/// debited the bucket): a flood of pins followed by a CID request → 429
/// on the CID. GREEN after the split: the CID bucket is untouched.
#[sqlx::test]
async fn ipfs_pins_do_not_debit_the_cid_bucket(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let owner_did = kp.did().to_string();
let slug = owner_did.replace([':', '/'], "_");
let short = owner_did.split(':').next_back().unwrap().to_string();
let mut state = test_state(pool).await;
// Bucket size 2: any third /ipfs/{cid} request from the same IP would
// 429 if the bucket were touched by the preceding pin requests.
state.ipfs_rate_limiter = crate::rate_limit::RateLimiter::new(2, Duration::from_secs(3600));
state.ipfs_work_rate_limiter =
crate::rate_limit::RateLimiter::new(600, Duration::from_secs(3600));
state.push_limiter_trust = crate::rate_limit::TrustedProxy::XForwardedFor;
// Seed a public, walk-free CID so /ipfs/{cid} serves cheaply. This
// matters because the proof half of the test fires a real CID request
// and needs the route bucket to be the only thing that can 429 it.
let fx = seed_cid_repos(&slug, &short, &["pinsdebit"]);
let bare = std::path::PathBuf::from("/tmp")
.join(&slug)
.join("pinsdebit.git");
let repo = seed_repo(&owner_did, "pinsdebit");
state.db.create_repo(&repo).await.expect("seed repo");
let cid = pin_cid_for(&bare, &fx.public_oid, &state.db).await;
let router = crate::server::build_router(state);
let peer_ip = "203.0.113.50";
// Five signed pin requests — more than the bucket — all must 200 (the
// pins route has no rate limit; the bucket only charges /ipfs/{cid}).
let pins_path = "/api/v1/ipfs/pins";
let pins_signed = sign_request(&kp, "GET", pins_path, b"");
for i in 0..5 {
let req = Request::builder()
.method(Method::GET)
.uri(pins_path)
.header("content-digest", &pins_signed.content_digest)
.header("signature-input", &pins_signed.signature_input)
.header("signature", &pins_signed.signature)
.header("x-forwarded-for", peer_ip)
.body(Body::empty())
.unwrap();
let resp = router.clone().oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"pin request {i} must not be rate-limited even though the CID bucket is size 2"
);
}
// A subsequent /ipfs/{cid} request from the same IP must still serve
// 200 — the bucket was untouched by the pin traffic. (If pins had
// debited it, the bucket would be empty and the CID request would 429.)
let cid_path = format!("/ipfs/{cid}");
let cid_signed = sign_request(&kp, "GET", &cid_path, b"");
let req = Request::builder()
.method(Method::GET)
.uri(&cid_path)
.header("content-digest", cid_signed.content_digest)
.header("signature-input", cid_signed.signature_input)
.header("signature", cid_signed.signature)
.header("x-forwarded-for", peer_ip)
.body(Body::empty())
.unwrap();
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"the CID bucket must still have tokens — pin traffic did not debit it"
);
}
/// #134 R2 (split buckets, second half): an EXHAUSTED CID bucket must
/// NOT block a valid signed `/api/v1/ipfs/pins` listing. The pre-fix
/// composition routed pins through the same middleware stack, so a CID
/// flood that exhausted the bucket would 429 the pins endpoint too. RED
/// before the split: a CID request that 429s the bucket makes the next
/// pin request 429 too. GREEN after the split: pins never see this
/// bucket and remain servable.
#[sqlx::test]
async fn ipfs_pins_survive_an_exhausted_cid_bucket(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
let kp = Keypair::generate();
let owner_did = kp.did().to_string();
let slug = owner_did.replace([':', '/'], "_");
let short = owner_did.split(':').next_back().unwrap().to_string();
let mut state = test_state(pool).await;
// Bucket size 1: the first /ipfs/{cid} request from the IP spends the
// only token; the second is 429 (proves exhaustion).
state.ipfs_rate_limiter = crate::rate_limit::RateLimiter::new(1, Duration::from_secs(3600));
state.ipfs_work_rate_limiter =
crate::rate_limit::RateLimiter::new(600, Duration::from_secs(3600));
state.push_limiter_trust = crate::rate_limit::TrustedProxy::XForwardedFor;
let fx = seed_cid_repos(&slug, &short, &["pinsexh"]);
let bare = std::path::PathBuf::from("/tmp")
.join(&slug)
.join("pinsexh.git");
let repo = seed_repo(&owner_did, "pinsexh");
state.db.create_repo(&repo).await.expect("seed repo");
let cid = pin_cid_for(&bare, &fx.public_oid, &state.db).await;
let router = crate::server::build_router(state);
let peer_ip = "203.0.113.60";
// First /ipfs/{cid} request — debits the single CID token, serves 200.
let cid_path = format!("/ipfs/{cid}");
let cid_signed = sign_request(&kp, "GET", &cid_path, b"");
let req = Request::builder()
.method(Method::GET)
.uri(&cid_path)
.header("content-digest", cid_signed.content_digest)
.header("signature-input", cid_signed.signature_input)
.header("signature", cid_signed.signature)
.header("x-forwarded-for", peer_ip)
.body(Body::empty())
.unwrap();
let resp = router.clone().oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"the first CID request spends the token and serves"
);
// Second /ipfs/{cid} request — bucket empty → 429 (proves the
// exhaustion is real and observable on this router). Re-sign because
// `SignedHeaders` holds owned `String`s.
let cid_signed_2 = sign_request(&kp, "GET", &cid_path, b"");
let req = Request::builder()
.method(Method::GET)
.uri(&cid_path)
.header("content-digest", cid_signed_2.content_digest)
.header("signature-input", cid_signed_2.signature_input)
.header("signature", cid_signed_2.signature)
.header("x-forwarded-for", peer_ip)
.body(Body::empty())
.unwrap();
let resp = router.clone().oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::TOO_MANY_REQUESTS,
"the second CID request from the same IP is 429 — bucket exhausted"
);
// A signed /api/v1/ipfs/pins request from the SAME IP must still
// serve 200 — pins are auth-only and never see this bucket.
let pins_path = "/api/v1/ipfs/pins";
let pins_signed = sign_request(&kp, "GET", pins_path, b"");
let req = Request::builder()
.method(Method::GET)
.uri(pins_path)
.header("content-digest", pins_signed.content_digest)
.header("signature-input", pins_signed.signature_input)
.header("signature", pins_signed.signature)
.header("x-forwarded-for", peer_ip)
.body(Body::empty())
.unwrap();
let resp = router.oneshot(req).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"an exhausted CID bucket must not block a valid signed pin listing"
);
}
/// U5 (R6): the two buckets are independent — the WORK budget can be exhausted
/// (429) WITHOUT draining the ROUTE bucket. Through the production router, route
/// generous (5) but work tight (1): one request drives two legacy probes, so the
+13
View File
@@ -79,6 +79,19 @@ async fn fire_event_async(
let delivery_id = uuid::Uuid::new_v4().to_string();
tokio::spawn(async move {
// Policy skip, not a delivery failure: the stored URL predates the
// create-time gate. The "invalid_url" label is documented on
// record_webhook_delivery in metrics.rs.
if !crate::api::peers::is_public_http_url(&hook.url) {
crate::metrics::record_webhook_delivery("invalid_url");
tracing::warn!(
url = %hook.url,
event = %event_name,
"webhook delivery skipped: stored URL is not a public http(s) target"
);
return;
}
let signature = hook.secret.as_deref().map(|s| sign_payload(s, &bytes));
let mut req = client
@@ -0,0 +1,177 @@
//! #26 Split PR 4 — `GITLAWB_ENFORCE_OWNER_PUSH` default is consistent across surfaces.
//!
//! Reviewer 2 (2026-08-28) closed PR #224 with a P2 finding: SECURITY.md and
//! README.md both stated the default was `false`, while `Config::enforce_owner_push`
//! declared `default_value_t = true` and the runtime, .env.example, and the env
//! var table on the README all agreed with the code. Two operator-facing
//! surfaces lied about the authorization policy. PR #330 flipped the code
//! default and updated .env.example / README env table / docs/RUN-A-NODE.md,
//! but missed these two prose mentions.
//!
//! This guard pins the documentation and example surfaces together. Each one
//! is read off the working tree at test time, not a copy in the binary, so a
//! revert of the doc edit turns it red:
//!
//! 1. `SECURITY.md` says the default is `true`.
//! 2. `README.md` says the default is `true` in the known-limitations prose.
//! 3. `README.md` says the default is `true` in the env-var table row.
//! 4. `.env.example` sets `GITLAWB_ENFORCE_OWNER_PUSH=true`.
//!
//! The runtime declared default (`Config::enforce_owner_push`'s
//! `default_value_t = true`) is protected by the
//! `enforce_owner_push_is_declared_true_independent_of_the_environment`
//! unit test in `config.rs`, which reads the parser directly and is
//! env-independent. The two checks together cover the surfaces that
//! produced the original doc-vs-code drift; this guard cannot reach into
//! the binary crate's `Config` type (it is private to the bin target,
//! which has no `lib.rs`) without a larger refactor, so the contract is
//! stated as the four documentation/example surfaces above.
//!
//! The four are checked independently. A check that just verified "at least
//! one surface says true" would pass while a stale SECURITY.md lied; a check
//! that only verified the binary would not catch the doc drift that
//! produced this finding in the first place.
use std::path::Path;
fn read(rel: &str) -> String {
let p = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
.join(rel);
std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display()))
}
/// Pin the prose mentions of `GITLAWB_ENFORCE_OWNER_PUSH` so a future doc
/// edit cannot reintroduce the "defaults to false" wording the reviewer
/// flagged. The wording in the test is what the docs MUST say about the
/// default; absence of any prose mention is also a fail because operators
/// rely on the docs to learn the default.
#[test]
fn security_md_states_owner_push_default_is_true() {
let security = read("SECURITY.md");
let section = section_after(&security, "### Repository write authorization defaults")
.unwrap_or_else(|| panic!("SECURITY.md is missing the owner-push section"));
assert!(
section.contains("defaults to `true`"),
"SECURITY.md owner-push section must state the default is true. \
Found:\n{section}"
);
assert!(
!section_claims_default_is_false(&section),
"SECURITY.md owner-push section must not claim the default is false. \
Found:\n{section}"
);
}
#[test]
fn readme_known_limitations_states_owner_push_default_is_true() {
let readme = read("README.md");
let limitations = section_after(&readme, "Known limitations:")
.unwrap_or_else(|| panic!("README.md is missing the Known limitations: section"));
// The owner-push line is one bullet in the limitations list. Pull the
// single line that mentions the env var so the assertion is local to
// the claim, not a substring of unrelated text.
let line = limitations
.lines()
.find(|l| l.contains("GITLAWB_ENFORCE_OWNER_PUSH"))
.unwrap_or_else(|| {
panic!(
"README.md Known limitations must mention GITLAWB_ENFORCE_OWNER_PUSH.\n\
Got:\n{limitations}"
)
});
assert!(
line.contains("defaults to `true`") || line.contains("defaults to true"),
"README Known-limitations owner-push bullet must state the default is true. \
Found:\n{line}"
);
assert!(
!section_claims_default_is_false(line),
"README Known-limitations owner-push bullet must not claim the default is false. \
Found:\n{line}"
);
}
#[test]
fn readme_env_var_table_states_owner_push_default_is_true() {
let readme = read("README.md");
let line = readme
.lines()
.find(|l| l.contains("GITLAWB_ENFORCE_OWNER_PUSH") && l.contains("|"))
.unwrap_or_else(|| {
panic!("README.md is missing the env-var-table row for GITLAWB_ENFORCE_OWNER_PUSH")
});
assert!(
line.contains("Defaults to `true`") || line.contains("defaults to `true`"),
"README env-var-table row for GITLAWB_ENFORCE_OWNER_PUSH must state the default is true. \
Found:\n{line}"
);
assert!(
!section_claims_default_is_false(line),
"README env-var-table row for GITLAWB_ENFORCE_OWNER_PUSH must not claim the default is false. \
Found:\n{line}"
);
}
#[test]
fn env_example_sets_owner_push_true() {
let env = read(".env.example");
let line = env
.lines()
.find(|l| l.trim_start().starts_with("GITLAWB_ENFORCE_OWNER_PUSH"))
.unwrap_or_else(|| panic!(".env.example is missing GITLAWB_ENFORCE_OWNER_PUSH"));
assert!(
line.contains("=true"),
".env.example GITLAWB_ENFORCE_OWNER_PUSH must be set to true. Found:\n{line}"
);
}
/// A claim that the default is false can hide in many phrasings. Match the
/// forms that have appeared in the wild on this branch: "defaults to `false`",
/// "defaults to false", "default is false", "is `false` for compatibility",
/// "is false for compatibility". The match is intentionally narrow — false
/// positives would suppress a real doc edit.
fn section_claims_default_is_false(section: &str) -> bool {
let lower = section.to_ascii_lowercase();
lower.contains("defaults to `false`")
|| lower.contains("defaults to false")
|| lower.contains("default is `false`")
|| lower.contains("default is false")
|| (lower.contains("`false`") && lower.contains("for compatibility"))
}
/// Return the prose after a marker up to the next heading boundary. The
/// marker is normally a markdown heading (e.g. `### Section`); if it has
/// no `#` prefix it is treated as a plain-text marker and the slice is
/// bounded by the next heading of any level. This matters because a
/// plain-text marker would otherwise produce `header_level = 0`, making
/// the same-or-higher-level stop condition unsatisfiable and letting the
/// slice run to EOF.
fn section_after(doc: &str, heading: &str) -> Option<String> {
let start = doc.find(heading)?;
let after = &doc[start..];
let header_level = heading.chars().take_while(|c| *c == '#').count();
// Skip past the heading line itself.
let body_start = after.find('\n')? + 1;
let body = &after[body_start..];
// Stop at the next heading of the same or higher level, or at the next
// heading of any level when the marker had no `#` prefix.
let mut end = body.len();
for line in body.lines() {
let level = line.chars().take_while(|c| *c == '#').count();
if level > 0 && (header_level == 0 || level <= header_level) {
end = line.as_ptr() as usize - body.as_ptr() as usize;
break;
}
}
Some(body[..end].to_string())
}
+1 -1
View File
@@ -88,7 +88,7 @@ async fn cmd_list(node: String, capability: Option<String>) -> Result<()> {
let short = did
.split(':')
.next_back()
.map(|s| &s[..s.len().min(16)])
.map(|s| crate::text::truncate(s, 16))
.unwrap_or("?");
let trust = agent["trust_score"].as_f64().unwrap_or(0.0);
let caps = agent["capabilities"]
+2 -2
View File
@@ -275,7 +275,7 @@ async fn cmd_list(
let title = b["title"].as_str().unwrap_or("?");
let amount = b["amount"].as_i64().unwrap_or(0);
let st = b["status"].as_str().unwrap_or("?");
let short_id = &id[..8.min(id.len())];
let short_id = crate::text::truncate(id, 8);
println!("{short_id} {st:<10} {amount:>12} $GITLAWB {title}");
}
}
@@ -403,7 +403,7 @@ async fn cmd_stats(node: String) -> Result<()> {
let did = entry["did"].as_str().unwrap_or("?");
let count = entry["completed"].as_i64().unwrap_or(0);
let earned = entry["total_earned"].as_i64().unwrap_or(0);
let short = &did[did.len().saturating_sub(8)..];
let short = crate::text::truncate_suffix(did, 8);
println!(
" {}. ...{short} {count} bounties {earned} $GITLAWB",
i + 1
+4 -1
View File
@@ -117,7 +117,10 @@ async fn cmd_list(repo: String, node: String, dir: Option<PathBuf>) -> Result<()
let id = cert["id"].as_str().unwrap_or("?");
let ref_name = cert["ref_name"].as_str().unwrap_or("?");
let new_sha = cert["new_sha"].as_str().unwrap_or("?");
let issued_at = cert["issued_at"].as_str().map(|s| &s[..19]).unwrap_or("?");
let issued_at = cert["issued_at"]
.as_str()
.map(|s| crate::text::truncate(s, 19))
.unwrap_or("?");
println!(" {id:.8} {issued_at} {ref_name} {new_sha:.12}");
}
Ok(())
+2 -2
View File
@@ -75,14 +75,14 @@ pub async fn run(args: ChangelogArgs) -> Result<()> {
for event in &events {
let ts = event["timestamp"].as_str().unwrap_or("?");
let date = &ts[..ts.len().min(10)];
let date = crate::text::truncate(ts, 10);
match event["type"].as_str().unwrap_or("") {
"commit" => {
let sha = event["sha"].as_str().unwrap_or("?");
let msg = event["message"].as_str().unwrap_or("?");
let first_line = msg.lines().next().unwrap_or(msg);
let short_sha = &sha[..sha.len().min(8)];
let short_sha = crate::text::truncate(sha, 8);
println!(" {date} commit {short_sha} {first_line}");
}
"pr_merged" => {
+89 -5
View File
@@ -163,8 +163,8 @@ pub fn setup_partial_clone(
if withheld_globs.is_empty() {
match branch {
Some(b) => git_global(&["clone", "-q", "--branch", b, remote_url, dest_str])?,
None => git_global(&["clone", "-q", remote_url, dest_str])?,
Some(b) => git_global(&["clone", "-q", "--branch", b, "--", remote_url, dest_str])?,
None => git_global(&["clone", "-q", "--", remote_url, dest_str])?,
}
return Ok(());
}
@@ -174,6 +174,7 @@ pub fn setup_partial_clone(
"-q",
"--filter=blob:none",
"--no-checkout",
"--",
remote_url,
dest_str,
])?;
@@ -388,7 +389,7 @@ async fn recover_encrypted_blobs(
let plaintext = match open_blob(&envelope, keypair) {
Ok(p) => p,
Err(e) => {
eprintln!("warning: could not decrypt {oid}: {e}");
emit_warning(&format!("warning: could not decrypt {oid}: {e}"));
continue;
}
};
@@ -406,7 +407,9 @@ async fn recover_encrypted_blobs(
recovered.push(p.clone());
}
} else {
eprintln!("warning: recovered blob {oid} hashed to {written}; discarding");
emit_warning(&format!(
"warning: recovered blob {oid} hashed to {written}; discarding"
));
}
}
Ok(recovered)
@@ -804,7 +807,9 @@ async fn recover_from_arweave(
recovered.push(p.clone());
}
} else {
eprintln!("warning: recovered blob {oid} hashed to {written}; discarding");
emit_warning(&format!(
"warning: recovered blob {oid} hashed to {written}; discarding"
));
}
}
Ok(recovered)
@@ -1090,6 +1095,85 @@ mod tests {
);
}
#[test]
fn setup_partial_clone_plain_without_withheld_paths() {
let (td, url) = bare_remote(&[("file.txt", b"hello\n")]);
let dest = td.path().join("dest");
setup_partial_clone(&dest, &url, &[], &[], None).unwrap();
assert!(dest.join("file.txt").exists());
}
#[test]
fn setup_partial_clone_with_branch_without_withheld_paths() {
let (td, url) = bare_remote(&[("file.txt", b"hello\n")]);
let branch_out = Command::new("git")
.args([
"-C",
td.path().join("origin").to_str().unwrap(),
"branch",
"--show-current",
])
.output()
.unwrap();
let branch = String::from_utf8(branch_out.stdout)
.unwrap()
.trim()
.to_string();
let dest = td.path().join("dest");
setup_partial_clone(&dest, &url, &[], &[], Some(&branch)).unwrap();
assert!(dest.join("file.txt").exists());
}
/// #374: every `setup_partial_clone` shape interpolates `remote_url` into a
/// `git clone` argv. The `--` delimiter is what forces an option-shaped
/// remote to be read as a repository rather than parsed as an option.
///
/// The assertion is on git's own words — `repository '<value>'`, single-quoted
/// — and not merely on the value appearing somewhere in the error. `git_global`
/// formats its failure as `git {args:?} failed: {stderr}`, so the injected
/// string is already present in the debug-printed argv whether or not the
/// delimiter is there; asserting on containment alone would pass with `--`
/// deleted. Undelimited, git consumes the value as an option, leaving the
/// destination as the only positional, and names *that* instead
/// (`repository '<dest>' does not exist`) — so this goes red without the fix.
///
/// The payload carries no `:` or space: a colon would make git read the
/// argument as an ssh-style `host:path` URL and report a hostname, which is a
/// different parse.
fn assert_option_shaped_remote_is_a_repository(withheld: &[String], branch: Option<&str>) {
let td = TempDir::new().unwrap();
let dest = td.path().join("dest");
let injected = "--upload-pack=false";
let err = setup_partial_clone(&dest, injected, withheld, &[], branch)
.expect_err("an option-shaped remote is not a repository and must fail")
.to_string();
assert!(
err.contains(&format!("repository '{injected}'")),
"git must name the argument as the repository, not consume it as an option: {err}"
);
}
#[test]
fn plain_clone_never_parses_a_remote_as_a_git_option() {
assert_option_shaped_remote_is_a_repository(&[], None);
}
#[test]
fn branch_clone_never_parses_a_remote_as_a_git_option() {
// Pins that `--branch <b>` stays ahead of the delimiter: if `--` were moved
// before `--branch`, git would read the branch name as a positional instead.
assert_option_shaped_remote_is_a_repository(&[], Some("main"));
}
#[test]
fn sparse_clone_never_parses_a_remote_as_a_git_option() {
// The withheld-globs arm is a separate argv construction
// (`--filter=blob:none --no-checkout`) and needs its own proof.
assert_option_shaped_remote_is_a_repository(&["/secret/**".to_string()], None);
}
#[test]
fn sparse_patterns_subtree_and_exact() {
assert_eq!(sparse_patterns("/secret/**"), vec!["/secret/".to_string()]);
+1 -5
View File
@@ -131,11 +131,7 @@ async fn cmd_list(node: String, dir: Option<PathBuf>) -> Result<()> {
let sha = pin["sha256_hex"].as_str().unwrap_or("?");
let pinned_at = pin["pinned_at"].as_str().unwrap_or("?");
// Trim pinned_at to date+time without subseconds
let ts = if pinned_at.len() >= 19 {
&pinned_at[..19]
} else {
pinned_at
};
let ts = crate::text::truncate(pinned_at, 19);
println!(" {cid}");
println!(" sha256: {sha}");
println!(" pinned: {ts}");
+34 -3
View File
@@ -251,7 +251,7 @@ async fn cmd_list(repo: String, node: String, dir: Option<PathBuf>) -> Result<()
let status = issue["status"].as_str().unwrap_or("?");
let created = issue["created_at"]
.as_str()
.map(|s| &s[..10])
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
let icon = match status {
"open" => "○",
@@ -365,10 +365,13 @@ async fn cmd_issue_comments(
let author_short = author
.split(':')
.next_back()
.map(|s| &s[..s.len().min(8)])
.map(|s| crate::text::truncate(s, 8))
.unwrap_or("?");
let cbody = c["body"].as_str().unwrap_or("");
let created = c["created_at"].as_str().map(|s| &s[..10]).unwrap_or("?");
let created = c["created_at"]
.as_str()
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" · {author_short} ({created})");
println!(" {cbody}");
println!();
@@ -439,6 +442,34 @@ mod tests {
.unwrap();
}
#[tokio::test]
async fn test_cmd_list_survives_malformed_timestamps() {
let dir = TempDir::new().unwrap();
write_identity(&dir);
let mut server = mockito::Server::new_async().await;
// A short timestamp used to panic on `&s[..10]`; a timestamp whose
// byte 10 is mid-char panics even with a len() guard.
let _m = server
.mock(
"GET",
mockito::Matcher::Regex(r"^/api/v1/repos/[^/]+/myrepo/issues$".to_string()),
)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"issues":[{"id":"a","title":"short ts","status":"open","created_at":"2026"},{"id":"b","title":"mb ts","status":"open","created_at":"2026-08-1é5T00:00:00Z"},{"id":"c","title":"empty ts","status":"open","created_at":""}]}"#)
.create_async()
.await;
cmd_list(
"myrepo".to_string(),
server.url(),
Some(dir.path().to_path_buf()),
)
.await
.unwrap();
}
#[tokio::test]
async fn test_cmd_create_success() {
let dir = TempDir::new().unwrap();
+1
View File
@@ -30,6 +30,7 @@ mod star;
mod status;
mod sync;
mod task;
mod text;
mod ucan_cmd;
mod visibility;
mod webhook;
+51 -2
View File
@@ -41,6 +41,17 @@ pub struct MirrorArgs {
pub dir: Option<PathBuf>,
}
/// Argv for the mirror clone, with `--` immediately before the first positional
/// so an option-shaped `source` cannot be parsed as a git option (#374).
///
/// This is a separate function purely so the delimiter's placement can be driven
/// through real git by a test. The production call keeps `.status()` rather than
/// `.output()`: a mirror clone of a large repo streams git's progress to the
/// user's terminal, and capturing it to assert on stderr here would swallow that.
fn mirror_clone_args(source: &str) -> [&str; 4] {
["clone", "--mirror", "--", source]
}
pub async fn run(args: MirrorArgs) -> Result<()> {
let source = args.source.trim_end_matches('/').to_string();
@@ -145,7 +156,7 @@ fn clone_mirror(source: &str, dest: &std::path::Path) -> Result<()> {
// bounded-runner rule governs node request handlers. Same spawn as before,
// only relocated into this helper.
let status = Command::new("git")
.args(["clone", "--mirror", source])
.args(mirror_clone_args(source))
.arg(dest)
.status()
.context("failed to run git clone — is git installed?")?;
@@ -186,6 +197,43 @@ pub fn extract_repo_name(url: &str) -> Option<String> {
mod tests {
use super::*;
/// #374: `gl mirror` interpolates the user-supplied source into a
/// `git clone --mirror` argv. `--` must sit immediately before the first
/// positional so an option-shaped source is read as a repository.
///
/// This drives the production argv through real git rather than asserting on
/// the array's contents, so it fails on a delimiter that is missing *or*
/// misplaced. It cannot go through `run()`: that loads an identity keypair and
/// contacts a node before reaching the clone, and its bail message interpolates
/// `{source}` unconditionally, so an assertion there would pass with `--` gone.
///
/// Asserting on git's single-quoted `repository '<value>'` is what makes this
/// red without the delimiter — undelimited, git consumes the value as an option
/// and names the destination as the repository instead.
#[test]
fn mirror_clone_never_parses_the_source_as_a_git_option() {
let td = tempfile::TempDir::new().unwrap();
let dest = td.path().join("mirror.git");
let injected = "--upload-pack=false";
let out = Command::new("git")
.env("LC_ALL", "C")
.args(mirror_clone_args(injected))
.arg(&dest)
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!out.status.success(),
"an option-shaped source is not a repository and must fail the clone"
);
assert!(
stderr.contains(&format!("repository '{injected}'")),
"git must name the source as the repository, not consume it as an option: {stderr}"
);
}
#[test]
fn test_extract_github_url() {
assert_eq!(
@@ -330,7 +378,8 @@ mod tests {
.create_async()
.await;
// We can't easily test the git subprocess calls, but we can test the
// The git subprocess argv is pinned separately by
// `mirror_clone_never_parses_the_source_as_a_git_option`; this covers the
// API error path by calling the create step directly via NodeClient.
let kp2 = gitlawb_core::identity::Keypair::generate();
let client = NodeClient::new(server.url(), Some(kp2));
+1 -1
View File
@@ -413,7 +413,7 @@ async fn cmd_status(node: String, dir: Option<PathBuf>) -> Result<()> {
let ref_name = ev["ref"].as_str().unwrap_or("?");
let ts = ev["timestamp"]
.as_str()
.map(|s| &s[..10.min(s.len())])
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" {ts} {repo} {ref_name}");
}
+1 -1
View File
@@ -97,7 +97,7 @@ async fn cmd_list(node: String) -> Result<()> {
let reachable = peer["reachable"].as_bool().unwrap_or(false);
let last_seen = peer["last_seen"]
.as_str()
.map(|s| &s[..10])
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("never");
let status = if reachable { "✓" } else { "✗" };
println!(" {status} {url}");
+13 -7
View File
@@ -304,7 +304,7 @@ async fn cmd_list(repo: String, node: String, dir: Option<PathBuf>) -> Result<()
let author_short = author
.split(':')
.next_back()
.map(|s| &s[..s.len().min(8)])
.map(|s| crate::text::truncate(s, 8))
.unwrap_or("?");
let status_icon = match status {
"open" => "○",
@@ -374,7 +374,7 @@ async fn cmd_view(repo: String, number: u64, node: String, dir: Option<PathBuf>)
let reviewer_short = reviewer
.split(':')
.next_back()
.map(|s| &s[..s.len().min(8)])
.map(|s| crate::text::truncate(s, 8))
.unwrap_or("?");
let rstatus = r["status"].as_str().unwrap_or("?");
let rbody = r["body"].as_str().unwrap_or("");
@@ -414,10 +414,13 @@ async fn cmd_view(repo: String, number: u64, node: String, dir: Option<PathBuf>)
let author_short = author
.split(':')
.next_back()
.map(|s| &s[..s.len().min(8)])
.map(|s| crate::text::truncate(s, 8))
.unwrap_or("?");
let cbody = c["body"].as_str().unwrap_or("");
let created = c["created_at"].as_str().map(|s| &s[..10]).unwrap_or("?");
let created = c["created_at"]
.as_str()
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" · {author_short} ({created})");
println!(" {cbody}");
}
@@ -464,7 +467,7 @@ async fn cmd_merge(repo: String, number: u64, node: String, dir: Option<PathBuf>
let sha = result["merge_sha"].as_str().unwrap_or("?");
println!("✓ Merged PR #{number}");
println!(" Merge commit: {}", &sha[..sha.len().min(12)]);
println!(" Merge commit: {}", crate::text::truncate(sha, 12));
Ok(())
}
@@ -555,10 +558,13 @@ async fn cmd_comments(repo: String, number: u64, node: String, dir: Option<PathB
let author_short = author
.split(':')
.next_back()
.map(|s| &s[..s.len().min(8)])
.map(|s| crate::text::truncate(s, 8))
.unwrap_or("?");
let cbody = c["body"].as_str().unwrap_or("");
let created = c["created_at"].as_str().map(|s| &s[..10]).unwrap_or("?");
let created = c["created_at"]
.as_str()
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" · {author_short} ({created})");
println!(" {cbody}");
println!();
+7 -4
View File
@@ -337,7 +337,10 @@ async fn cmd_list(node: String, dir: Option<PathBuf>) -> Result<()> {
let name = r["name"].as_str().unwrap_or("?");
let desc = r["description"].as_str().unwrap_or("");
let public = r["is_public"].as_bool().unwrap_or(true);
let updated = r["updated_at"].as_str().map(|s| &s[..10]).unwrap_or("?");
let updated = r["updated_at"]
.as_str()
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
let vis = if public { "public" } else { "private" };
println!(" {name:<24} {vis:<8} {updated} {desc}");
}
@@ -514,7 +517,7 @@ async fn cmd_replicas(repo: String, node: String, dir: Option<PathBuf>) -> Resul
let url = r["replica_url"].as_str().unwrap_or("?");
let registered = r["registered_at"]
.as_str()
.map(|s| &s[..10.min(s.len())])
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" {registered} {did} → {url}");
}
@@ -556,7 +559,7 @@ pub(crate) async fn cmd_commits(
.or_else(|| c["sha"].as_str())
.or_else(|| c["oid"].as_str())
.unwrap_or("?");
let short_sha = &sha[..sha.len().min(10)];
let short_sha = crate::text::truncate(sha, 10);
let msg = c["message"].as_str().unwrap_or("(no message)");
let first_line = msg.lines().next().unwrap_or(msg);
let author = c["author_name"]
@@ -566,7 +569,7 @@ pub(crate) async fn cmd_commits(
let date = c["date"]
.as_str()
.or_else(|| c["committer_date"].as_str())
.map(|s| &s[..10.min(s.len())])
.map(|s| crate::text::truncate(s, 10))
.unwrap_or("?");
println!(" {short_sha} {date} {first_line} ({author})");
}
+63
View File
@@ -0,0 +1,63 @@
//! Display helpers for strings that come off the wire unchecked.
/// Truncate `s` to at most `max` bytes, cutting at a char boundary.
///
/// `&s[..max]` panics when `s` is shorter than `max` bytes and when `max`
/// lands inside a multi-byte char; `s.len().min(max)` guards only the first
/// case. Node-supplied timestamps and ids hit both.
pub(crate) fn truncate(s: &str, max: usize) -> &str {
let mut end = max.min(s.len());
while !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
/// Keep at most the last `max` bytes of `s`, cutting at a char boundary.
///
/// `&s[s.len().saturating_sub(max)..]` panics when the start index lands
/// inside a multi-byte char; node-supplied ids hit that case.
pub(crate) fn truncate_suffix(s: &str, max: usize) -> &str {
let mut start = s.len().saturating_sub(max);
while !s.is_char_boundary(start) {
start += 1;
}
&s[start..]
}
#[cfg(test)]
mod tests {
use super::{truncate, truncate_suffix};
#[test]
fn cuts_ascii_at_max() {
assert_eq!(truncate("2026-08-15T12:34:56Z", 10), "2026-08-15");
}
#[test]
fn short_and_empty_inputs_return_whole() {
assert_eq!(truncate("2026", 10), "2026");
assert_eq!(truncate("", 10), "");
}
#[test]
fn multibyte_cut_does_not_panic() {
// Byte index 10 lands inside 'é'; the cut must land on the boundary
// before it (9 bytes) rather than panic or exceed the byte limit.
assert_eq!(truncate("2026-08-1\u{e9}5T12:34:56Z", 10), "2026-08-1");
// A string that is entirely multi-byte and shorter than max.
assert_eq!(truncate("\u{e9}\u{e9}", 10), "\u{e9}\u{e9}");
}
#[test]
fn suffix_cut_does_not_panic() {
// 'é' is two bytes; a start index of len-8 lands inside it.
assert_eq!(truncate_suffix("did:key:\u{e9}1234567", 8), "1234567");
}
#[test]
fn suffix_short_input_returns_whole() {
assert_eq!(truncate_suffix("short", 8), "short");
assert_eq!(truncate_suffix("", 8), "");
}
}
+213 -24
View File
@@ -88,20 +88,81 @@ fn sanitize_excerpt(s: &str) -> String {
out
}
/// Whether `u` parses as a trusted URL.
/// Fixed warning when `GITLAWB_ICAPTCHA_INSECURE` is present but not an
/// explicit truthy value. Does not include the raw env value (arbitrary
/// deployment input — secrets / terminal controls).
fn warn_icaptcha_insecure_non_truthy() {
// One literal line on purpose: a continuation escape here keeps emitting
// correct prose (Rust strips the next line's leading whitespace), but the
// single-line form makes that property visible without knowing the escape
// semantics, which is what the review asked for.
tracing::warn!(
"GITLAWB_ICAPTCHA_INSECURE is set but not truthy (expected 1 or true); loopback HTTP trust relaxation remains disabled"
);
}
/// Parse a possibly-present `GITLAWB_ICAPTCHA_INSECURE` string.
///
/// Production trusts only `https`. When `GITLAWB_ICAPTCHA_INSECURE` is set (a
/// runtime escape hatch for integration tests) the function also trusts
/// `http://127.0.0.1` and `http://localhost` so the full iCaptcha retry path
/// can be exercised against a local mockito server.
fn is_https(u: &str) -> bool {
/// Only an explicit truthy value (`1` / `true`, case-insensitive, after trim)
/// enables the loopback-HTTP trust relaxation. Presence alone is not enough:
/// `=0`, `=false`, or an empty value leave it off. A non-empty non-truthy
/// value emits [`warn_icaptcha_insecure_non_truthy`].
fn icaptcha_insecure_from_str(raw: Option<&str>) -> bool {
let Some(v) = raw else {
return false;
};
let t = v.trim();
if t.eq_ignore_ascii_case("1") || t.eq_ignore_ascii_case("true") {
true
} else {
if !t.is_empty() {
warn_icaptcha_insecure_non_truthy();
}
false
}
}
/// Interpret `std::env::var("GITLAWB_ICAPTCHA_INSECURE")` without reading the
/// environment again — used by production and by unit tests.
///
/// `NotPresent` → disabled. `NotUnicode` → disabled + the same fixed warning
/// as a present non-truthy value (fail closed; do not treat as unset).
fn icaptcha_insecure_from_var(result: Result<String, std::env::VarError>) -> bool {
match result {
Ok(v) => icaptcha_insecure_from_str(Some(&v)),
Err(std::env::VarError::NotUnicode(_)) => {
warn_icaptcha_insecure_non_truthy();
false
}
Err(std::env::VarError::NotPresent) => false,
}
}
/// Whether `GITLAWB_ICAPTCHA_INSECURE` explicitly enables the loopback-HTTP
/// trust relaxation used by integration tests.
///
/// Only an explicit truthy value (`1` / `true`, case-insensitive, after trim)
/// enables it. Presence alone is not enough: `=0`, `=false`, or an empty
/// value leave the relaxation off. A non-empty non-truthy value (including a
/// present non-Unicode value) emits a `tracing::warn!` (visible when the
/// `icaptcha_client` target is at WARN+, e.g. `RUST_LOG=icaptcha_client=warn`).
fn icaptcha_insecure_enabled() -> bool {
icaptcha_insecure_from_var(std::env::var("GITLAWB_ICAPTCHA_INSECURE"))
}
/// Whether `u` parses as a trusted URL, given a pre-parsed insecure flag.
///
/// Production trusts only `https`. When `insecure` is true the function also
/// trusts `http://127.0.0.1` and `http://localhost` so the full iCaptcha retry
/// path can be exercised against a local mockito server.
fn is_https_with(u: &str, insecure: bool) -> bool {
let Ok(parsed) = reqwest::Url::parse(u) else {
return false;
};
if parsed.scheme() == "https" {
return true;
}
if std::env::var_os("GITLAWB_ICAPTCHA_INSECURE").is_some()
if insecure
&& parsed.scheme() == "http"
&& matches!(parsed.host_str(), Some("127.0.0.1" | "localhost"))
{
@@ -139,7 +200,15 @@ fn origin_key(u: &str) -> Option<String> {
/// explicitly-configured host (never a node-discovered one), so a key is never
/// exfiltrated to an origin the operator did not choose.
fn resolve_solver_url(advertised: Option<&str>, operator: Option<&str>) -> (String, bool) {
let operator = operator.filter(|u| is_https(u));
resolve_solver_url_with(advertised, operator, icaptcha_insecure_enabled())
}
fn resolve_solver_url_with(
advertised: Option<&str>,
operator: Option<&str>,
insecure: bool,
) -> (String, bool) {
let operator = operator.filter(|u| is_https_with(u, insecure));
let operator_origin = operator.as_ref().and_then(|u| origin_key(u));
let default_origin = origin_key(DEFAULT_URL);
@@ -157,7 +226,7 @@ fn resolve_solver_url(advertised: Option<&str>, operator: Option<&str>) -> (Stri
};
let chosen = match advertised {
Some(a) if is_https(a) && allowed(&origin_key(a)) => a.to_string(),
Some(a) if is_https_with(a, insecure) && allowed(&origin_key(a)) => a.to_string(),
Some(a) => {
tracing::warn!(
advertised = %a,
@@ -389,27 +458,36 @@ mod tests {
use std::ffi::OsString;
use std::sync::{Mutex, MutexGuard};
/// Serializes tests that touch the process-global
/// `GITLAWB_ICAPTCHA_INSECURE` env var so they never race.
/// Serializes the real-env reader test so it never races with other
/// process-global env mutations (and recovers from a poisoned lock).
static ICAPTCHA_ENV_LOCK: Mutex<()> = Mutex::new(());
/// Set `GITLAWB_ICAPTCHA_INSECURE` for the test lifetime, restoring any
/// prior value on drop.
struct InsecureEnv {
struct EnvGuard {
_lock: MutexGuard<'static, ()>,
prev: Option<OsString>,
}
impl InsecureEnv {
fn new() -> Self {
let lock = ICAPTCHA_ENV_LOCK.lock().unwrap();
impl EnvGuard {
fn lock() -> MutexGuard<'static, ()> {
ICAPTCHA_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner())
}
fn with_value(val: &str) -> Self {
let lock = Self::lock();
let prev = std::env::var_os("GITLAWB_ICAPTCHA_INSECURE");
std::env::set_var("GITLAWB_ICAPTCHA_INSECURE", "1");
InsecureEnv { _lock: lock, prev }
std::env::set_var("GITLAWB_ICAPTCHA_INSECURE", val);
EnvGuard { _lock: lock, prev }
}
fn unset() -> Self {
let lock = Self::lock();
let prev = std::env::var_os("GITLAWB_ICAPTCHA_INSECURE");
std::env::remove_var("GITLAWB_ICAPTCHA_INSECURE");
EnvGuard { _lock: lock, prev }
}
}
impl Drop for InsecureEnv {
impl Drop for EnvGuard {
fn drop(&mut self) {
match self.prev.take() {
Some(v) => std::env::set_var("GITLAWB_ICAPTCHA_INSECURE", v),
@@ -418,6 +496,118 @@ mod tests {
}
}
/// #227 / #246: cover the production env read in `icaptcha_insecure_enabled`.
/// Pure-helper tests alone leave mutations of this function green.
#[test]
fn icaptcha_insecure_enabled_reads_real_env() {
{
let _g = EnvGuard::unset();
assert!(!icaptcha_insecure_enabled(), "unset must disable");
}
{
let _g = EnvGuard::with_value("0");
assert!(!icaptcha_insecure_enabled(), "=0 must disable");
}
{
let _g = EnvGuard::with_value("false");
assert!(!icaptcha_insecure_enabled(), "=false must disable");
}
{
let _g = EnvGuard::with_value("");
assert!(!icaptcha_insecure_enabled(), "empty must disable");
}
{
let _g = EnvGuard::with_value("1");
assert!(icaptcha_insecure_enabled(), "=1 must enable");
}
{
let _g = EnvGuard::with_value("true");
assert!(icaptcha_insecure_enabled(), "=true must enable");
}
{
let _g = EnvGuard::with_value("TRUE");
assert!(icaptcha_insecure_enabled(), "=TRUE must enable");
}
{
let _g = EnvGuard::with_value(" 1 ");
assert!(icaptcha_insecure_enabled(), "\" 1 \" (trimmed) must enable");
}
}
/// #227: `=0` / `=false` / empty / unset must NOT enable loopback HTTP;
/// only explicit truthy `1` / `true` may. Uses pure helpers so tests do
/// not mutate process-global environment (avoids races with parallel
/// resolver tests that also call `resolve_solver_url`).
#[test]
fn insecure_env_only_truthy_enables_loopback_http() {
assert!(
!is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some("0"))),
"=0 must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_from_str(Some("0")));
assert!(
!is_https_with(
"http://127.0.0.1",
icaptcha_insecure_from_str(Some("false"))
),
"=false must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_from_str(Some("false")));
assert!(
!is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some(""))),
"empty value must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_from_str(Some("")));
assert!(
!is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(None)),
"unset must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_from_str(None));
assert!(!icaptcha_insecure_from_var(Err(
std::env::VarError::NotPresent
)));
assert!(
is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some("1"))),
"=1 must enable loopback HTTP"
);
assert!(is_https_with(
"http://localhost",
icaptcha_insecure_from_str(Some("1"))
));
assert!(icaptcha_insecure_from_str(Some("1")));
assert!(
is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some("true"))),
"=true must enable loopback HTTP"
);
assert!(icaptcha_insecure_from_str(Some("true")));
assert!(
is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some("TRUE"))),
"=TRUE (case-insensitive) must enable loopback HTTP"
);
// Trim is load-bearing: whitespace around a truthy token must still enable.
assert!(
is_https_with("http://127.0.0.1", icaptcha_insecure_from_str(Some(" 1 "))),
"\" 1 \" (trimmed) must enable loopback HTTP"
);
assert!(icaptcha_insecure_from_str(Some(" 1 ")));
// Present non-Unicode: fail closed + same fixed warning path as non-truthy.
assert!(!icaptcha_insecure_from_var(Err(
std::env::VarError::NotUnicode(OsString::from("x"))
)));
// https remains trusted regardless of the insecure flag.
assert!(is_https_with("https://icaptcha.gitlawb.com", false));
assert!(is_https_with("https://icaptcha.gitlawb.com", true));
}
// ── P1: hostile error bodies must not reach the terminal raw ──────────
#[test]
@@ -543,13 +733,12 @@ mod tests {
#[test]
fn rejects_insecure_advertised_url_on_different_port() {
// With GITLAWB_ICAPTCHA_INSECURE=1, two loopback URLs on different
// With insecure mode enabled, two loopback URLs on different
// ports must be treated as distinct origins so a hostile node cannot
// redirect the bearer key to a different listener on localhost.
let _env = InsecureEnv::new();
let op = "http://localhost:3000";
let (url, key_trusted) = resolve_solver_url(Some("http://localhost:9000"), Some(op));
let (url, key_trusted) =
resolve_solver_url_with(Some("http://localhost:9000"), Some(op), true);
assert_eq!(
url, op,
"must fall back to operator origin, not use different port"