fix(security): remediate 2026-05-12 audit findings (Stages 2–5)
Remediation of the 2026-05-12/13 audits (78 findings + cluster gaps), tracked in deploy-k3s/SECURITY.md, plus fixes from two independent post-remediation reviews. Auth & sessions: - SHA-256 hashed auth-token storage (C1); prior-token cache eviction on re-login (MEDIUM-1) - local Google JWKS verification, iss/aud/exp checks (C2/C3) - constant-time login + generic errors (L1/LIVE-L11/LIVE-L13) - per-account login lockout keyed on distinct source IPs (M5/MEDIUM-3) - verified-email gating, login rate limiting (LIVE-L19, H1-H3) IAP & webhooks: - Apple/Google cross-account replay protection (C5/C6/C10/C13, H5/H6) - migrations 000003-000006 (token hashing, IAP replay, audit_log + webhook_event_log table creation, append-only audit log) Authorization & races: - file-ownership owner-OR-member fix (C7), atomic share-code join (C9/H9), device-token reassignment (C8/LOW-3) Secrets & deploy: - secrets file-mounted at /etc/honeydue/secrets, not env (F8); Redis password out of the ConfigMap (HIGH-1); B2 keys reconciled - digest-pinned images, admin ingress hardening, CSP/HSTS, /metrics lockdown; kubeconfig 0600, etcd secrets-encryption, fail2ban + unattended-upgrades at provision; secret-rotation runbook Build, vet, and the full test suite (incl. -race) pass; the goose migration chain is verified against PostgreSQL 16. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"github.com/treytartt/honeydue-api/internal/models"
|
||||
)
|
||||
@@ -194,6 +195,60 @@ func (r *ResidenceRepository) HasAccess(residenceID, userID uint) (bool, error)
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// JoinWithShareCode atomically redeems a one-time share code (audit C9/H9):
|
||||
// it locks the share-code row, re-checks validity under the lock, adds the
|
||||
// user to the residence, and deactivates the code — all in one transaction.
|
||||
// Concurrent redemptions of the same code serialize on the row lock; the
|
||||
// loser sees is_active=false and is rejected. A failure to deactivate aborts
|
||||
// the whole join. Returns gorm.ErrRecordNotFound for an unknown, inactive, or
|
||||
// expired code so the caller can map every case to one generic error.
|
||||
func (r *ResidenceRepository) JoinWithShareCode(code string, userID uint) (residenceID uint, alreadyMember bool, err error) {
|
||||
err = r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var sc models.ResidenceShareCode
|
||||
if e := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("code = ?", code).First(&sc).Error; e != nil {
|
||||
return e
|
||||
}
|
||||
if !sc.IsActive || (sc.ExpiresAt != nil && time.Now().UTC().After(*sc.ExpiresAt)) {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
residenceID = sc.ResidenceID
|
||||
|
||||
// Already a member (owner or shared user)?
|
||||
var accessCount int64
|
||||
if e := tx.Raw(`
|
||||
SELECT COUNT(*) FROM (
|
||||
SELECT 1 FROM residence_residence
|
||||
WHERE id = ? AND owner_id = ? AND is_active = true
|
||||
UNION
|
||||
SELECT 1 FROM residence_residence_users
|
||||
WHERE residence_id = ? AND user_id = ?
|
||||
) ac
|
||||
`, sc.ResidenceID, userID, sc.ResidenceID, userID).Scan(&accessCount).Error; e != nil {
|
||||
return e
|
||||
}
|
||||
if accessCount > 0 {
|
||||
alreadyMember = true
|
||||
return nil
|
||||
}
|
||||
|
||||
if e := tx.Exec(
|
||||
"INSERT INTO residence_residence_users (residence_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING",
|
||||
sc.ResidenceID, userID,
|
||||
).Error; e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
// One-time use: deactivate the code. A failure here aborts the join.
|
||||
if e := tx.Model(&models.ResidenceShareCode{}).
|
||||
Where("id = ?", sc.ID).Update("is_active", false).Error; e != nil {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return residenceID, alreadyMember, err
|
||||
}
|
||||
|
||||
// IsOwner checks if a user is the owner of a residence
|
||||
func (r *ResidenceRepository) IsOwner(residenceID, userID uint) (bool, error) {
|
||||
var count int64
|
||||
|
||||
Reference in New Issue
Block a user