Skip to content

Commit

Permalink
Fix: Correctly handle 404 responses for invalid API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Nov 9, 2024
1 parent 657cada commit 6755f60
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/apiv1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/axllent/mailpit/internal/logger"
)

// NotFoundHandler is an API catch-all to return a 404 error
func NotFoundHandler(w http.ResponseWriter, _ *http.Request) {
fourOFour(w)
}

// FourOFour returns a basic 404 message
func fourOFour(w http.ResponseWriter) {
w.Header().Set("Referrer-Policy", "no-referrer")
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func apiRoutes() *mux.Router {
// return blank 200 response for OPTIONS requests for CORS
r.PathPrefix(config.Webroot + "api/v1/").Handler(middleWareFunc(apiv1.GetOptions)).Methods("OPTIONS")

// all other API routes should return a 404
r.PathPrefix(config.Webroot + "api/v1/").Handler(middleWareFunc(apiv1.NotFoundHandler))

return r
}

Expand Down

0 comments on commit 6755f60

Please sign in to comment.