diff --git a/crates/dav/src/calendar/update.rs b/crates/dav/src/calendar/update.rs index 9dff669d..0d209e73 100644 --- a/crates/dav/src/calendar/update.rs +++ b/crates/dav/src/calendar/update.rs @@ -330,8 +330,9 @@ impl CalendarUpdateRequestHandler for Server { } // Validate quota - let extra_bytes = - (bytes.len() as u64).saturating_sub(u32::from(event.inner.size) as u64); + // inbuxa: against what the event is charged (its names and + // properties too), not the bare iCalendar length + let extra_bytes = (new_event.size() as u64).saturating_sub(event.inner.size() as u64); if extra_bytes > 0 { self.has_available_quota(self.account(account_id).await?.as_ref(), extra_bytes) .await?; @@ -481,13 +482,13 @@ impl CalendarUpdateRequestHandler for Server { } // Validate quota - if !bytes.is_empty() { - self.has_available_quota( - self.account(account_id).await?.as_ref(), - bytes.len() as u64, - ) - .await?; - } + // inbuxa: against what the event is charged, not the bare + // iCalendar length, which let a PUT run past the limit + self.has_available_quota( + self.account(account_id).await?.as_ref(), + event.size() as u64, + ) + .await?; // Prepare write batch let mut batch = BatchBuilder::new(); diff --git a/crates/dav/src/card/update.rs b/crates/dav/src/card/update.rs index 0bb0c6fb..052953bc 100644 --- a/crates/dav/src/card/update.rs +++ b/crates/dav/src/card/update.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use super::assert_is_unique_uid; @@ -185,14 +187,6 @@ impl CardUpdateRequestHandler for Server { } } - // Validate quota - let extra_bytes = - (bytes.len() as u64).saturating_sub(u32::from(card.inner.size) as u64); - if extra_bytes > 0 { - self.has_available_quota(self.account(account_id).await?.as_ref(), extra_bytes) - .await?; - } - // Build node let mut new_card = card .deserialize::() @@ -200,6 +194,15 @@ impl CardUpdateRequestHandler for Server { new_card.size = bytes.len() as u32; new_card.card = vcard; + // Validate quota + // inbuxa: against what the card is charged (its names and + // properties too), not the bare vCard length + let extra_bytes = (new_card.size() as u64).saturating_sub(card.inner.size() as u64); + if extra_bytes > 0 { + self.has_available_quota(self.account(account_id).await?.as_ref(), extra_bytes) + .await?; + } + // Prepare write batch let mut batch = BatchBuilder::new(); let etag = new_card @@ -258,15 +261,6 @@ impl CardUpdateRequestHandler for Server { ) .await?; - // Validate quota - if !bytes.is_empty() { - self.has_available_quota( - self.account(account_id).await?.as_ref(), - bytes.len() as u64, - ) - .await?; - } - // Build node let card = ContactCard { names: vec![DavName { @@ -278,6 +272,12 @@ impl CardUpdateRequestHandler for Server { ..Default::default() }; + // Validate quota + // inbuxa: against what the card is charged, not the bare vCard + // length, which let a PUT run past the limit + self.has_available_quota(self.account(account_id).await?.as_ref(), card.size() as u64) + .await?; + // Prepare write batch let mut batch = BatchBuilder::new(); let document_id = self diff --git a/crates/dav/src/file/update.rs b/crates/dav/src/file/update.rs index 121abf27..1f2d23d2 100644 --- a/crates/dav/src/file/update.rs +++ b/crates/dav/src/file/update.rs @@ -164,9 +164,21 @@ impl FileUpdateRequestHandler for Server { return Err(DavError::Code(StatusCode::METHOD_NOT_ALLOWED)); } + // Build node + let mut new_node = node.deserialize::().caused_by(trc::location!())?; + let new_file = new_node.file.as_mut().unwrap(); + new_file.media_type = headers + .content_type + .filter(|ct| !ct.is_empty() && *ct != "application/octet-stream") + .map(|v| v.to_string()); + new_file.size = bytes.len() as u32; + new_node.modified = now() as i64; + // Validate quota - let extra_bytes = (bytes.len() as u64) - .saturating_sub(u32::from(node.inner.file.as_ref().unwrap().size) as u64); + // inbuxa: against what the file is charged (its name and + // properties too), not the bare content length; checked before + // the blob is written, so a refused PUT stores nothing + let extra_bytes = (new_node.size() as u64).saturating_sub(node.inner.size() as u64); if extra_bytes > 0 { self.has_available_quota(self.account(account_id).await?.as_ref(), extra_bytes) .await?; @@ -177,17 +189,7 @@ impl FileUpdateRequestHandler for Server { .put_temporary_blob(account_id, &bytes, 60) .await .caused_by(trc::location!())?; - - // Build node - let mut new_node = node.deserialize::().caused_by(trc::location!())?; - let new_file = new_node.file.as_mut().unwrap(); - new_file.blob_hash = blob_hash; - new_file.media_type = headers - .content_type - .filter(|ct| !ct.is_empty() && *ct != "application/octet-stream") - .map(|v| v.to_string()); - new_file.size = bytes.len() as u32; - new_node.modified = now() as i64; + new_node.file.as_mut().unwrap().blob_hash = blob_hash; // Prepare write batch let mut batch = BatchBuilder::new(); @@ -243,29 +245,14 @@ impl FileUpdateRequestHandler for Server { ) .await?; - // Validate quota - if !bytes.is_empty() { - self.has_available_quota( - self.account(account_id).await?.as_ref(), - bytes.len() as u64, - ) - .await?; - } - - // Write blob - let (blob_hash, blob_hold) = self - .put_temporary_blob(account_id, &bytes, 60) - .await - .caused_by(trc::location!())?; - // Build node let now = now(); - let node = FileNode { + let mut node = FileNode { parent_id, name: resource_name.to_string(), display_name: None, file: Some(FileProperties { - blob_hash, + blob_hash: Default::default(), size: bytes.len() as u32, media_type: headers.content_type.map(|v| v.to_string()), executable: false, @@ -280,6 +267,20 @@ impl FileUpdateRequestHandler for Server { .unwrap_or_default(), }; + // Validate quota + // inbuxa: against what the file is charged, not the bare content + // length, which let a PUT run past the limit; checked before the + // blob is written, so a refused PUT stores nothing + self.has_available_quota(self.account(account_id).await?.as_ref(), node.size() as u64) + .await?; + + // Write blob + let (blob_hash, blob_hold) = self + .put_temporary_blob(account_id, &bytes, 60) + .await + .caused_by(trc::location!())?; + node.file.as_mut().unwrap().blob_hash = blob_hash; + // Prepare write batch let mut batch = BatchBuilder::new(); let document_id = self diff --git a/tests/src/webdav/put_get.rs b/tests/src/webdav/put_get.rs index 046e56fa..04462c84 100644 --- a/tests/src/webdav/put_get.rs +++ b/tests/src/webdav/put_get.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use types::collection::Collection; @@ -296,6 +298,10 @@ pub async fn test(test: &TestServer) { .with_status(StatusCode::CREATED); let mut num_success = 0; let mut did_fail = false; + // inbuxa: deleted items stay counted while they're kept, so each + // type gets 1024 bytes on top of what's already used + let limit = mike_used(test).await + 1024; + set_mike_quota(test, limit).await; for i in 0..100 { let content = resource_type.generate(); @@ -304,9 +310,11 @@ pub async fn test(test: &TestServer) { let response = mike_noquota .request_with_headers("PUT", &format!("{path}file{i}"), [], &content) .await; - if available > content.len() as u64 { + // inbuxa: an item is charged a little more than its body (its + // name and properties too), so one that fits by body length + // alone can still be refused; what matters is it never goes past + if response.status == StatusCode::CREATED && available > content.len() as u64 { num_success += 1; - response.with_status(StatusCode::CREATED); } else { response .with_status(StatusCode::PRECONDITION_FAILED) @@ -314,6 +322,10 @@ pub async fn test(test: &TestServer) { did_fail = true; break; } + assert!( + mike_used(test).await <= limit, + "Quota test failed: {resource_type:?} PUT {i} went past the quota" + ); } if !did_fail { panic!("Quota test failed: {} files created", num_success); @@ -322,6 +334,24 @@ pub async fn test(test: &TestServer) { panic!("Quota test failed: no files created"); } + // inbuxa: with room for the body but not for what the item is + // charged, a new item is refused rather than taking the account + // past its quota + let content = resource_type.generate(); + let used = mike_used(test).await; + set_mike_quota(test, used + content.len() as u64).await; + mike_noquota + .request_with_headers("PUT", &format!("{path}edge"), [], &content) + .await + .with_status(StatusCode::PRECONDITION_FAILED) + .with_failed_precondition("D:quota-not-exceeded", ""); + assert_eq!( + mike_used(test).await, + used, + "Quota test failed: {resource_type:?} PUT that only fit by body length was counted" + ); + set_mike_quota(test, 1024).await; + mike_noquota .request("DELETE", &path, "") .await @@ -632,3 +662,21 @@ END:VCALENDAR mike_noquota.delete_default_containers().await; test.assert_is_empty().await; } + +async fn mike_used(test: &TestServer) -> u64 { + test.server + .get_used_quota_account(test.account("mike@example.com").id().document_id()) + .await + .unwrap() + .max(0) as u64 +} + +async fn set_mike_quota(test: &TestServer, quota: u64) { + test.account("admin@example.com") + .registry_update_object( + ObjectType::Account, + test.account("mike@example.com").id(), + json!({ Property::Quotas: { StorageQuota::MaxDiskQuota.as_str(): quota } }), + ) + .await; +}