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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]
### Changes
- `POST /delegate` and `POST /revoke` refuse a validly signed request that has no standing, or is outside its signed time window, before taking any lock. Before, it took the guards of the delegation chain it cited, queued for the write order of the spaces it named and, on PostgreSQL, held a pooled connection in an open transaction until it was refused. Now it is refused at once:
- a delegation whose issuer neither owns the spaces it names nor holds a stored parent granted to it;
- a revocation by someone who may not revoke its target, or of a target that is not stored;
- a renewal lease not issued by the delegator of the original it renews;
- an expired or not-yet-valid delegation or revocation.
The checks run again inside the transaction, which decides. A delegation already stored still answers as registered when posted again, even after it has expired. **Clients that post a child without waiting for its parent's response must retry:** a child whose parent is not yet stored is now refused at once with 401 `Cannot find parent delegation`. Before, it could sometimes wait for the parent's write and be admitted. Refusal messages are unchanged, except that a stranger's renewal lease is now refused as unauthorized even where its window or shape was also wrong. `/delegate` also checks the request's signature before the Policy/v3 admission reads the graph, so a badly signed delegation citing a policy root answers 401 `Failed to verify signature` instead of 403. Details: TC-3000.
- SQLite stamp replay deadlines now use the same timestamp encoding as cleanup (TC-2920).
- PostgreSQL database artifacts use uncompressed external storage for new writes to avoid repeated decompression during chunked reads (TC-2917). Existing large artifacts require an operator re-toast; verify both storage settings after rollout and follow `scripts/tc2917-README.md` if lock contention deferred the migration.
- Encryption: v2 column format with key ids, per-purpose keys and recorded format metadata. Optional strict reads are off by default. Details: TC-2516, TC-2517.
Expand Down
63 changes: 60 additions & 3 deletions tinycloud-core/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,20 @@ where
}

pub async fn delegate(&self, delegation: Delegation) -> Result<TransactResult, TxError<B, K>> {
self.delegate_verified(delegation, &PreVerified::default())
.await
}

/// As [`Self::delegate`], with the signature `verified` holds, from
/// [`Self::preverify_delegation`] on these exact bytes, so a caller can
/// check it before its own reads (TC-3000). When `verified` does not
/// cover these bytes, their signature is checked here, still before any
/// read or lock.
pub async fn delegate_verified(
&self,
delegation: Delegation,
verified: &PreVerified,
) -> Result<TransactResult, TxError<B, K>> {
if self.secrets.renewal_seed().is_some_and(|seed| {
crate::renewal::renewal_identity_from_seed(seed).0 == delegation.0.delegator
}) {
Expand All @@ -2309,8 +2323,13 @@ where
let renewal_lease = crate::renewal::lease_original_from_signed(&delegation)?.is_some();
// TC-2986: the signature is checked before any lock, so a forged or
// slow one never holds the guards or the spaces' write order.
let verified =
PreVerified::delegation(&self.conn, &delegation, self.encryption.as_ref()).await?;
let checked;
let verified = if verified.contains(&delegation.content_hash()) {
verified
} else {
checked = self.preverify_delegation(&delegation).await?;
&checked
};
let parent_hashes: Vec<Hash> = delegation
.0
.parents
Expand All @@ -2319,6 +2338,41 @@ where
.map(Hash::from)
.collect();
let retained_hash = delegation.content_hash();
// TC-3000: its time window and its issuer's standing are checked on
// the pool too, so a stranger's or an expired delegation never queues
// behind the guards and write order of the chain and spaces it names.
let precheck = if renewal_lease {
delegation::check_owner_renewal_lease(&delegation).map_err(TxError::from)
} else {
delegation::precheck(
&self.conn,
&delegation,
self.delegated_sql_caveats,
self.named_keys,
self.renewal_identity
.as_ref()
.map(|identity| identity.renewer_did.as_str()),
)
.await
.map_err(TxError::from)
};
if let Err(refused) = precheck {
// A delegation already stored answers as before, even once it
// has expired: these exact signed bytes are already registered.
if !renewal_lease
&& delegation::Entity::find_by_id(retained_hash)
.one(&self.conn)
.await
.map_err(TxError::Db)?
.is_some()
{
for space in delegation.0.spaces() {
crate::request_telemetry::observe_space(space);
}
return Ok(already_registered_result(retained_hash));
}
return Err(refused);
}
// Capture expected row counts before consuming the delegation.
let expected_ability_count = delegation.0.capabilities.len() as u64;
let expected_parent_count = parent_hashes.len() as u64;
Expand Down Expand Up @@ -2350,7 +2404,7 @@ where
Err(e) => return Err(TxError::Db(e)),
}
match self
.transact_verified(vec![Event::Delegation(Box::new(delegation))], &verified)
.transact_verified(vec![Event::Delegation(Box::new(delegation))], verified)
.await
{
Err(TxError::EpochInsert(epoch_err)) => {
Expand Down Expand Up @@ -2465,6 +2519,9 @@ where
pub async fn revoke(&self, revocation: Revocation) -> Result<TransactResult, TxError<B, K>> {
// TC-2986: the signature is checked before any lock.
let verified = PreVerified::revocation(&revocation).await?;
// TC-3000: and its time window, target and revoker, on the pool, so a
// stranger's or an expired revocation takes no lock.
revocation::precheck(&self.conn, &revocation).await?;
let mut roots = vec![Hash::from(revocation.0.revoked)];
roots.extend(revocation.0.parents.iter().copied().map(Hash::from));
let revocation_start = Instant::now();
Expand Down
Loading
Loading