Add middleware to set Cache-Control header for challenge HTML (#132)

* Add middleware to set Cache-Control header for challenge HTML

* Add `NoStoreCache` middleware function in `internal/headers.go` to set Cache-Control: no-store header
* Apply `NoStoreCache` middleware in `cmd/anubis/main.go` to set Cache-Control header for challenge HTML

* docs: Add no-cache header information for challenge page

* docs: Update changelog to reflect no-store Cache-Control header addition for challenge page

* refactor: rename variable for clarity and update caching middleware in RenderIndex

* chore: move changes to the unreleased section

Signed-off-by: Jason Cameron <git@jasoncameron.dev>

---------

Signed-off-by: Jason Cameron <jasoncameron.all@gmail.com>
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
This commit is contained in:
Jason Cameron 2025-03-29 21:15:50 -04:00 committed by GitHub
parent 168329fff0
commit 3683f95933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 5 deletions

View file

@ -162,7 +162,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (s *Server) challengeFor(r *http.Request, difficulty int) string {
fp := sha256.Sum256(s.priv.Seed())
data := fmt.Sprintf(
challengeData := fmt.Sprintf(
"Accept-Language=%s,X-Real-IP=%s,User-Agent=%s,WeekTime=%s,Fingerprint=%x,Difficulty=%d",
r.Header.Get("Accept-Language"),
r.Header.Get("X-Real-Ip"),
@ -171,7 +171,7 @@ func (s *Server) challengeFor(r *http.Request, difficulty int) string {
fp,
difficulty,
)
return internal.SHA256sum(data)
return internal.SHA256sum(challengeData)
}
func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) {
@ -326,9 +326,12 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) RenderIndex(w http.ResponseWriter, r *http.Request) {
templ.Handler(
web.Base("Making sure you're not a bot!", web.Index()),
).ServeHTTP(w, r)
handler := internal.NoStoreCache(
templ.Handler(
web.Base("Making sure you\\'re not a bot!", web.Index()),
),
)
handler.ServeHTTP(w, r)
}
func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {