Skip to content

Commit e1f7db1

Browse files
committed
fix: improve security for session cookies and streamline logging in account setup
1 parent 737cb0c commit e1f7db1

3 files changed

Lines changed: 6 additions & 11 deletions

File tree

gearbox/cmd/server/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ func main() {
152152
}
153153
logger.Info("authentication manager initialized")
154154

155-
// Enable secure cookies if TLS is configured
156-
if cfg.TLSCertPath != "" && cfg.TLSKeyPath != "" {
157-
authManager.SetSecure(true)
155+
// Secure cookies are enabled by default; disable only when TLS is not configured
156+
if cfg.TLSCertPath == "" || cfg.TLSKeyPath == "" {
157+
authManager.SetSecure(false)
158+
logger.Warn("TLS not configured — session cookies will be sent over HTTP (insecure)")
158159
}
159160

160161
// Initialize email service

gearbox/internal/framework/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewManager(db *database.DB, sessionSecret string, timeout time.Duration, lo
4343
Path: "/",
4444
MaxAge: int(timeout.Seconds()),
4545
HttpOnly: true,
46-
Secure: false, // Will be true if HTTPS is enabled
46+
Secure: true, // Default to secure; SetSecure(false) only for non-TLS dev environments
4747
SameSite: http.SameSiteStrictMode,
4848
}
4949

gearbox/internal/framework/handler/users.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,10 @@ func (h *Handler) CompleteAccountSetupPost(w http.ResponseWriter, r *http.Reques
321321
}
322322

323323
// Set password and email (doesn't require current password)
324-
h.logger.Info("🔧 DEBUG: Setting password and email", "user_id", user.ID, "new_email", newEmail)
325324
if err := h.authManager.SetPasswordAndEmail(r, user.ID, newPassword, newEmail); err != nil {
326325
http.Redirect(w, r, "/settings/complete-account-setup?error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
327326
return
328327
}
329-
h.logger.Info("✅ DEBUG: Password and email set successfully")
330328

331329
// SECURITY: Auto-delete admin credentials file after successful setup
332330
credentialsFile := "data/admin-credentials.txt" //#nosec G101 -- Not a credential; this is a file path constant
@@ -343,23 +341,19 @@ func (h *Handler) CompleteAccountSetupPost(w http.ResponseWriter, r *http.Reques
343341
}
344342

345343
// Get updated user record (with new email and cleared MustChangePassword flag)
346-
h.logger.Info("🔍 DEBUG: Fetching updated user record", "user_id", user.ID)
347344
updatedUser, err := h.authManager.GetDB().GetUserByID(user.ID)
348345
if err != nil {
349346
h.logger.Error("Failed to get updated user after account setup", "error", err)
350347
http.Redirect(w, r, "/settings/complete-account-setup?error=Setup+completed+but+session+error", http.StatusSeeOther)
351348
return
352349
}
353-
h.logger.Info("📋 DEBUG: Updated user fetched", "must_change_password", updatedUser.MustChangePassword, "email", updatedUser.Email)
354350

355351
// Create a fresh session with the updated user
356-
h.logger.Info("🔐 DEBUG: Creating fresh session for updated user")
357352
if err := h.authManager.CreateSessionForUser(w, r, updatedUser); err != nil {
358-
h.logger.Error("❌ FAILED to create session after account setup", "error", err)
353+
h.logger.Error("failed to create session after account setup", "error", err)
359354
http.Redirect(w, r, "/login?message=Setup+complete.+Please+log+in.", http.StatusSeeOther)
360355
return
361356
}
362-
h.logger.Info("✅ DEBUG: Session created successfully, redirecting to /")
363357

364358
http.Redirect(w, r, "/", http.StatusSeeOther)
365359
}

0 commit comments

Comments
 (0)