fix: buffer template execution to prevent corrupt HTML responses (closes #42)
Add renderTemplate helper method on Handlers that renders templates to a bytes.Buffer first, then writes to the ResponseWriter only on success. This prevents partial/corrupt HTML when template execution fails partway through. Applied to all template rendering call sites in: - setup.go (HandleSetupGET, renderSetupError) - auth.go (HandleLoginGET, HandleLoginPOST error paths) - dashboard.go (HandleDashboard) - app.go (HandleAppNew, HandleAppCreate, HandleAppDetail, HandleAppEdit, HandleAppUpdate, HandleAppDeployments)
This commit is contained in:
@@ -13,11 +13,7 @@ func (h *Handlers) HandleLoginGET() http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
data := h.addGlobals(map[string]any{}, request)
|
||||
|
||||
err := tmpl.ExecuteTemplate(writer, "login.html", data)
|
||||
if err != nil {
|
||||
h.log.Error("template execution failed", "error", err)
|
||||
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
h.renderTemplate(writer, tmpl, "login.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +38,7 @@ func (h *Handlers) HandleLoginPOST() http.HandlerFunc {
|
||||
|
||||
if username == "" || password == "" {
|
||||
data["Error"] = "Username and password are required"
|
||||
_ = tmpl.ExecuteTemplate(writer, "login.html", data)
|
||||
h.renderTemplate(writer, tmpl, "login.html", data)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -50,7 +46,7 @@ func (h *Handlers) HandleLoginPOST() http.HandlerFunc {
|
||||
user, authErr := h.auth.Authenticate(request.Context(), username, password)
|
||||
if authErr != nil {
|
||||
data["Error"] = "Invalid username or password"
|
||||
_ = tmpl.ExecuteTemplate(writer, "login.html", data)
|
||||
h.renderTemplate(writer, tmpl, "login.html", data)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -60,7 +56,7 @@ func (h *Handlers) HandleLoginPOST() http.HandlerFunc {
|
||||
h.log.Error("failed to create session", "error", sessionErr)
|
||||
|
||||
data["Error"] = "Failed to create session"
|
||||
_ = tmpl.ExecuteTemplate(writer, "login.html", data)
|
||||
h.renderTemplate(writer, tmpl, "login.html", data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user