Add CSRF checks to news.
parent
47ebf77f24
commit
ca5ad07f26
|
|
@ -26,6 +26,11 @@ func NewsHandler(app *App) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == http.MethodPost && loggedIn && isAdmin {
|
if r.Method == http.MethodPost && loggedIn && isAdmin {
|
||||||
|
if !app.validateCSRFToken(r, session) {
|
||||||
|
http.Error(w, "Invalid CSRF token", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if action := r.URL.Query().Get("action"); action == "delete" {
|
if action := r.URL.Query().Get("action"); action == "delete" {
|
||||||
newsIDStr := r.URL.Query().Get("id")
|
newsIDStr := r.URL.Query().Get("id")
|
||||||
newsID, err := strconv.Atoi(newsIDStr)
|
newsID, err := strconv.Atoi(newsIDStr)
|
||||||
|
|
@ -85,6 +90,7 @@ func NewsHandler(app *App) http.HandlerFunc {
|
||||||
BasePath: app.Config.ThreadrDir,
|
BasePath: app.Config.ThreadrDir,
|
||||||
StaticPath: app.Config.ThreadrDir + "/static",
|
StaticPath: app.Config.ThreadrDir + "/static",
|
||||||
CurrentURL: r.URL.RequestURI(),
|
CurrentURL: r.URL.RequestURI(),
|
||||||
|
CSRFToken: app.csrfToken(session),
|
||||||
},
|
},
|
||||||
News: newsItems,
|
News: newsItems,
|
||||||
IsAdmin: isAdmin,
|
IsAdmin: isAdmin,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
<p>{{.Content}}</p>
|
<p>{{.Content}}</p>
|
||||||
{{if $.IsAdmin}}
|
{{if $.IsAdmin}}
|
||||||
<form method="post" action="{{$.BasePath}}/news/?action=delete&id={{.ID}}" style="display:inline;">
|
<form method="post" action="{{$.BasePath}}/news/?action=delete&id={{.ID}}" style="display:inline;">
|
||||||
|
<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">
|
||||||
<button type="submit" onclick="return confirm('Are you sure you want to delete this news item?')">Delete</button>
|
<button type="submit" onclick="return confirm('Are you sure you want to delete this news item?')">Delete</button>
|
||||||
</form>
|
</form>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
@ -34,6 +35,7 @@
|
||||||
<section>
|
<section>
|
||||||
<h3>Post New Announcement</h3>
|
<h3>Post New Announcement</h3>
|
||||||
<form method="post" action="{{.BasePath}}/news/">
|
<form method="post" action="{{.BasePath}}/news/">
|
||||||
|
<input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
|
||||||
<label for="title">Title:</label>
|
<label for="title">Title:</label>
|
||||||
<input type="text" id="title" name="title" required maxlength="255"><br>
|
<input type="text" id="title" name="title" required maxlength="255"><br>
|
||||||
<label for="content">Content:</label>
|
<label for="content">Content:</label>
|
||||||
|
|
@ -46,4 +48,4 @@
|
||||||
{{template "cookie_banner" .}}
|
{{template "cookie_banner" .}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue