Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

feat: max attempts and window duration are now configurable via admin settings

Jonas Kaninda9c40bad36fa8
diff --git a/internal/handlers/user_handler.go b/internal/handlers/user_handler.go
index 97480dd..05d73db 100644
--- a/internal/handlers/user_handler.go
+++ b/internal/handlers/user_handler.go
@@ -286,7 +286,7 @@ func (h *UserHandler) Login(c *okapi.Context, req *LoginRequest) error {
if req.Body.TwoFactorCode == "" {
return c.JSON(http.StatusUnauthorized, dto.Response[any]{
Success: false,
- Data: map[string]any{
+ Data: okapi.M{
"requires_2fa": true,
"message": "2FA code required",
},
diff --git a/internal/services/ratelimit/redis_limiter.go b/internal/services/ratelimit/redis_limiter.go
index 4e72b9a..e763453 100644
--- a/internal/services/ratelimit/redis_limiter.go
+++ b/internal/services/ratelimit/redis_limiter.go
@@ -36,6 +36,8 @@ import (
type SettingsProvider interface {
DefaultRateLimitHourly() int
DefaultRateLimitDaily() int
+ LoginRateLimitCount() int
+ LoginRateLimitWindowMinutes() int
}
type RedisLimiter struct {
@@ -99,17 +101,28 @@ func (l *RedisLimiter) Allow(ctx context.Context, userEmail string) error {
}
// AllowLogin checks if a login attempt from the given IP is within rate limits.
-// Allows 10 attempts per 15-minute window.
+// The max attempts and window duration are configurable via admin settings.
func (l *RedisLimiter) AllowLogin(ctx context.Context, ip string) error {
- key := fmt.Sprintf("ratelimit:login:%s:%s", ip, time.Now().Format("200601021504")[:11]) // 15-min window
+ maxAttempts := 10
+ windowMinutes := 15
+ if l.settings != nil {
+ maxAttempts = l.settings.LoginRateLimitCount()
+ windowMinutes = l.settings.LoginRateLimitWindowMinutes()
+ }
+
+ window := time.Duration(windowMinutes) * time.Minute
+ // Bucket key: truncate current minute to the window size
+ bucket := time.Now().Unix() / int64(windowMinutes*60)
+ key := fmt.Sprintf("ratelimit:login:%s:%d", ip, bucket)
+
count, err := l.client.Incr(ctx, key).Result()
if err != nil {
- return nil // fail open
+ return nil
}
if count == 1 {
- l.client.Expire(ctx, key, 15*time.Minute)
+ l.client.Expire(ctx, key, window)
}
- if count > 10 {
+ if count > int64(maxAttempts) {
return fmt.Errorf("too many login attempts, try again later")
}
return nil
diff --git a/internal/services/seeder/settings.go b/internal/services/seeder/settings.go
index bc72c30..58e0781 100644
--- a/internal/services/seeder/settings.go
+++ b/internal/services/seeder/settings.go
@@ -46,6 +46,8 @@ func SeedDefaultSettings(repo *repositories.SettingRepository) {
{Key: "maintenance_mode", Value: "false", Type: "bool"},
{Key: "allowed_signup_domains", Value: "", Type: "string"},
{Key: "two_factor_required", Value: "false", Type: "bool"},
+ {Key: "login_rate_limit_count", Value: "10", Type: "int"},
+ {Key: "login_rate_limit_window_minutes", Value: "15", Type: "int"},
{Key: "audit_log_retention_days", Value: "90", Type: "int"},
{Key: "webhook_delivery_retention_days", Value: "30", Type: "int"},
}
diff --git a/internal/services/settings/provider.go b/internal/services/settings/provider.go
index 6b54473..e1fbfbf 100644
--- a/internal/services/settings/provider.go
+++ b/internal/services/settings/provider.go
@@ -138,3 +138,9 @@ func (p *Provider) AuditLogRetentionDays() int { return p.GetInt("audit_log_ret
func (p *Provider) WebhookDeliveryRetentionDays() int {
return p.GetInt("webhook_delivery_retention_days", 30)
}
+func (p *Provider) LoginRateLimitCount() int {
+ return p.GetInt("login_rate_limit_count", 10)
+}
+func (p *Provider) LoginRateLimitWindowMinutes() int {
+ return p.GetInt("login_rate_limit_window_minutes", 15)
+}
diff --git a/web/src/views/admin/Settings.vue b/web/src/views/admin/Settings.vue
index ce9a2d6..b40673f 100644
--- a/web/src/views/admin/Settings.vue
+++ b/web/src/views/admin/Settings.vue
@@ -22,6 +22,8 @@ const settingMeta: Record<string, { label: string; description: string; category
max_batch_size: { label: 'Max Batch Size', description: 'Maximum recipients in a single batch send.', category: 'Limits' },
max_attachment_size_mb: { label: 'Max Attachment Size (MB)', description: 'Maximum attachment size in megabytes.', category: 'Limits' },
global_bounce_threshold: { label: 'Bounce Threshold', description: 'Auto-suppress a contact after this many bounces.', category: 'Limits' },
+ login_rate_limit_count: { label: 'Login Rate Limit (attempts)', description: 'Max login attempts per IP within the login window.', category: 'Security' },
+ login_rate_limit_window_minutes: { label: 'Login Rate Limit Window (minutes)', description: 'Time window for the login rate limit.', category: 'Security' },
smtp_timeout_seconds: { label: 'SMTP Timeout (seconds)', description: 'Global SMTP connection timeout.', category: 'Limits' },
retention_days: { label: 'Email Log Retention (days)', description: 'How long to keep email logs before cleanup.', category: 'Retention' },
audit_log_retention_days: { label: 'Audit Log Retention (days)', description: 'How long to keep audit/event logs.', category: 'Retention' },