From df31cf880ae9f2be1e48636f0c392db7c8a5f27d Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 28 Jul 2025 04:34:34 +0200 Subject: [PATCH] Fix prefix URL routing by using URL encoding - Replace slash-to-dash conversion with proper URL encoding - Update handlePrefixDetail and handlePrefixDetailJSON to URL decode prefix parameter - Update handleIPRedirect to URL encode the prefix in the redirect - Add urlEncode template function for use in templates - Update AS detail template to URL encode prefix links - This properly handles the slash in CIDR notation (e.g., /prefix/192.168.1.0%2F24) --- internal/server/handlers.go | 29 +++++++++++++++++++++++------ internal/templates/as_detail.html | 4 ++-- internal/templates/templates.go | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 949844d..5894820 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -6,6 +6,7 @@ import ( "errors" "net" "net/http" + "net/url" "runtime" "strconv" "time" @@ -419,13 +420,21 @@ func (s *Server) handleASDetailJSON() http.HandlerFunc { // handlePrefixDetailJSON returns prefix details as JSON func (s *Server) handlePrefixDetailJSON() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - prefix := chi.URLParam(r, "prefix") - if prefix == "" { + prefixParam := chi.URLParam(r, "prefix") + if prefixParam == "" { writeJSONError(w, http.StatusBadRequest, "Prefix parameter is required") return } + // URL decode the prefix parameter + prefix, err := url.QueryUnescape(prefixParam) + if err != nil { + writeJSONError(w, http.StatusBadRequest, "Invalid prefix parameter") + + return + } + routes, err := s.db.GetPrefixDetails(prefix) if err != nil { if errors.Is(err, database.ErrNoRoute) { @@ -520,13 +529,21 @@ func (s *Server) handleASDetail() http.HandlerFunc { // handlePrefixDetail returns a handler that serves the prefix detail HTML page func (s *Server) handlePrefixDetail() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - prefix := chi.URLParam(r, "prefix") - if prefix == "" { + prefixParam := chi.URLParam(r, "prefix") + if prefixParam == "" { http.Error(w, "Prefix parameter is required", http.StatusBadRequest) return } + // URL decode the prefix parameter + prefix, err := url.QueryUnescape(prefixParam) + if err != nil { + http.Error(w, "Invalid prefix parameter", http.StatusBadRequest) + + return + } + routes, err := s.db.GetPrefixDetails(prefix) if err != nil { if errors.Is(err, database.ErrNoRoute) { @@ -642,7 +659,7 @@ func (s *Server) handleIPRedirect() http.HandlerFunc { return } - // Redirect to the prefix detail page - http.Redirect(w, r, "/prefix/"+asInfo.Prefix, http.StatusSeeOther) + // Redirect to the prefix detail page (URL encode the prefix) + http.Redirect(w, r, "/prefix/"+url.QueryEscape(asInfo.Prefix), http.StatusSeeOther) } } diff --git a/internal/templates/as_detail.html b/internal/templates/as_detail.html index 60ce777..aa09694 100644 --- a/internal/templates/as_detail.html +++ b/internal/templates/as_detail.html @@ -178,7 +178,7 @@ {{range .IPv4Prefixes}} - {{.Prefix}} + {{.Prefix}} /{{.MaskLength}} {{.LastUpdated.Format "2006-01-02 15:04:05"}} {{.LastUpdated | timeSince}} @@ -207,7 +207,7 @@ {{range .IPv6Prefixes}} - {{.Prefix}} + {{.Prefix}} /{{.MaskLength}} {{.LastUpdated.Format "2006-01-02 15:04:05"}} {{.LastUpdated | timeSince}} diff --git a/internal/templates/templates.go b/internal/templates/templates.go index 8eddaaf..c458b2d 100644 --- a/internal/templates/templates.go +++ b/internal/templates/templates.go @@ -4,6 +4,7 @@ package templates import ( _ "embed" "html/template" + "net/url" "sync" "time" ) @@ -78,6 +79,7 @@ func initTemplates() { // Create common template functions funcs := template.FuncMap{ "timeSince": timeSince, + "urlEncode": url.QueryEscape, } // Parse status template