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:
@@ -18,11 +18,7 @@ func (h *Handlers) HandleSetupGET() http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
data := h.addGlobals(map[string]any{}, request)
|
||||
|
||||
err := tmpl.ExecuteTemplate(writer, "setup.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, "setup.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +58,7 @@ func (h *Handlers) renderSetupError(
|
||||
"Username": username,
|
||||
"Error": errorMsg,
|
||||
}, request)
|
||||
_ = tmpl.ExecuteTemplate(writer, "setup.html", data)
|
||||
h.renderTemplate(writer, tmpl, "setup.html", data)
|
||||
}
|
||||
|
||||
// HandleSetupPOST handles the setup form submission.
|
||||
|
||||
Reference in New Issue
Block a user