fix(manager-core): F-S-001 cap kv/docs/pubsub/queue payload sizes (default 256 KB)
Files (per-file cap), secrets (64 KB default), and email (25 MB default)
already enforce limits; kv::set, docs::create/update, pubsub::publish_durable
and queue::enqueue accepted any JSON value straight to a JSONB column with
no size validation. An anonymous public-HTTP script could fill disk via
queue::enqueue or amplify a single publish into N outbox rows × payload bytes.
Adds four new error variants:
- KvError::ValueTooLarge { limit, actual }
- DocsError::ValueTooLarge { limit, actual }
- PubsubError::MessageTooLarge { limit, actual }
- QueueError::PayloadTooLarge { limit, actual }
Each stateful service grows a `max_value_bytes` field with:
- Conservative 256 KB default (DEFAULT_KV_MAX_VALUE_BYTES etc.).
- New `with_max_*` constructor preserving the old `new()` signature.
- Env-knob reader (`*_max_*_from_env()`) — mirrors SecretsConfig::from_env.
Validation runs at the entry point BEFORE authz so an anonymous DoS doesn't
pay a membership lookup per attempt.
Wired via env knobs:
- PICLOUD_KV_MAX_VALUE_BYTES
- PICLOUD_DOCS_MAX_VALUE_BYTES
- PICLOUD_PUBSUB_MAX_MESSAGE_BYTES
- PICLOUD_QUEUE_MAX_PAYLOAD_BYTES
Documented in CLAUDE.md runtime config table.
New unit test in queue_service verifying the cap fires before authz.
AUDIT.md anchor: F-S-001. Depends on F-Q-004 (Backend variant).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -70,6 +70,26 @@ fn load_i64(dst: &mut i64, key: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Default per-message JSON-encoded payload cap (256 KB). Override with
|
||||
/// `PICLOUD_PUBSUB_MAX_MESSAGE_BYTES`.
|
||||
pub const DEFAULT_PUBSUB_MAX_MESSAGE_BYTES: usize = 256 * 1024;
|
||||
|
||||
/// Read `PICLOUD_PUBSUB_MAX_MESSAGE_BYTES`; invalid values fall back to
|
||||
/// the conservative default with a warning.
|
||||
#[must_use]
|
||||
pub fn pubsub_max_message_bytes_from_env() -> usize {
|
||||
if let Ok(v) = std::env::var("PICLOUD_PUBSUB_MAX_MESSAGE_BYTES") {
|
||||
match v.trim().parse::<usize>() {
|
||||
Ok(n) if n > 0 => return n,
|
||||
_ => tracing::warn!(
|
||||
value = %v,
|
||||
"ignoring invalid PICLOUD_PUBSUB_MAX_MESSAGE_BYTES (want a positive integer)"
|
||||
),
|
||||
}
|
||||
}
|
||||
DEFAULT_PUBSUB_MAX_MESSAGE_BYTES
|
||||
}
|
||||
|
||||
pub struct PubsubServiceImpl {
|
||||
repo: Arc<dyn PubsubRepo>,
|
||||
authz: Arc<dyn AuthzRepo>,
|
||||
@@ -80,6 +100,7 @@ pub struct PubsubServiceImpl {
|
||||
topics: Option<Arc<dyn TopicRepo>>,
|
||||
secrets: Option<Arc<dyn AppSecretsRepo>>,
|
||||
token_config: SubscriberTokenConfig,
|
||||
max_message_bytes: usize,
|
||||
}
|
||||
|
||||
impl PubsubServiceImpl {
|
||||
@@ -92,9 +113,16 @@ impl PubsubServiceImpl {
|
||||
topics: None,
|
||||
secrets: None,
|
||||
token_config: SubscriberTokenConfig::conservative(),
|
||||
max_message_bytes: DEFAULT_PUBSUB_MAX_MESSAGE_BYTES,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_max_message_bytes(mut self, n: usize) -> Self {
|
||||
self.max_message_bytes = n;
|
||||
self
|
||||
}
|
||||
|
||||
/// Attach the v1.1.6 realtime surface: the in-process broadcaster
|
||||
/// (publish fan-out to SSE subscribers), the topic registry +
|
||||
/// app-secrets repo (subscriber-token minting), and the TTL config.
|
||||
@@ -142,6 +170,17 @@ impl PubsubService for PubsubServiceImpl {
|
||||
if topic.trim().is_empty() {
|
||||
return Err(PubsubError::EmptyTopic);
|
||||
}
|
||||
// Reject oversized messages BEFORE the authz check so an
|
||||
// anonymous public-script DoS doesn't pay the membership lookup.
|
||||
let encoded_len = serde_json::to_vec(&message)
|
||||
.map(|v| v.len())
|
||||
.map_err(|e| PubsubError::Rejected(format!("encode message: {e}")))?;
|
||||
if encoded_len > self.max_message_bytes {
|
||||
return Err(PubsubError::MessageTooLarge {
|
||||
limit: self.max_message_bytes,
|
||||
actual: encoded_len,
|
||||
});
|
||||
}
|
||||
self.check_publish(cx).await?;
|
||||
|
||||
// `published_at` is stamped once on the manager side so every
|
||||
|
||||
Reference in New Issue
Block a user