Skip to content
Open
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
12 changes: 12 additions & 0 deletions migrations/012_min_subscription_interval.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Migration 012: Enforce minimum bound on subscriptions.interval_seconds (#47)
--
-- Adds a CHECK constraint requiring interval_seconds >= 60 to prevent
-- malicious or buggy subscriptions from draining a payer's on-chain
-- allowance every keeper poll cycle.

ALTER TABLE subscriptions
DROP CONSTRAINT IF EXISTS subscriptions_interval_seconds_check;

ALTER TABLE subscriptions
ADD CONSTRAINT subscriptions_interval_seconds_check
CHECK (interval_seconds >= 60);
20 changes: 16 additions & 4 deletions src/services/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const MAX_CONSECUTIVE_FAILURES: i32 = 3;
/// Max subscriptions processed per keeper pass, to bound worst-case latency.
const KEEPER_BATCH_LIMIT: i64 = 50;

/// Minimum allowed subscription interval in seconds (60s / 1 minute).
/// Prevents rapid allowance draining and keeper spam (#47).
pub const MIN_SUBSCRIPTION_INTERVAL_SECS: i64 = 60;

#[derive(Debug, Default, serde::Serialize)]
pub struct KeeperRunSummary {
pub executed: usize,
Expand Down Expand Up @@ -64,10 +68,10 @@ impl SubscriptionService {
if amount <= 0.0 {
return Err(AppError::Validation("amount must be positive".into()));
}
if req.interval_seconds <= 0 {
return Err(AppError::Validation(
"interval_seconds must be positive".into(),
));
if req.interval_seconds < MIN_SUBSCRIPTION_INTERVAL_SECS {
return Err(AppError::Validation(format!(
"interval_seconds must be at least {MIN_SUBSCRIPTION_INTERVAL_SECS} seconds"
)));
}
if req.payer_account.trim().is_empty() || req.recipient_account.trim().is_empty() {
return Err(AppError::Validation(
Expand Down Expand Up @@ -423,6 +427,14 @@ mod tests {
"USDC:GISSUER"
);
}

#[test]
fn min_subscription_interval_is_at_least_one_minute() {
assert!(
MIN_SUBSCRIPTION_INTERVAL_SECS >= 60,
"minimum subscription interval must be at least 60 seconds to protect allowances"
);
}
}

/// End-to-end coverage requiring a real Postgres (`DATABASE_URL`) — see
Expand Down