Parse the prefix on the prefix pages before looking it up (closes #71)
check / check (push) Waiting to run

Both prefix pages passed the URL text to the lookup as typed. A prefix
that does not parse made the lookup fail with an error that was not "no
route found", so the pages answered 500; a valid prefix written in any
form other than the one the tables store answered 404 although the
route exists. Both handlers now parse the prefix with net/netip, mask
it and look up the text form it prints, and answer 400 for a prefix
that does not parse.

Model: opus-5-5
This commit is contained in:
2026-10-14 13:46:24 +00:00
parent 00e51d284f
commit 83559c664c
2 changed files with 71 additions and 4 deletions
+19 -4
View File
@@ -7,6 +7,7 @@ import (
"errors"
"net"
"net/http"
"net/netip"
"runtime"
"sort"
"strconv"
@@ -748,8 +749,15 @@ func (s *Server) handlePrefixDetailJSON() http.HandlerFunc {
return
}
// Combine prefix and length into CIDR notation
prefix := prefixParam + "/" + lenParam
// Look the prefix up in the text form net/netip prints, the form the
// tables store, so that any valid way of writing it finds the route.
parsed, err := netip.ParsePrefix(prefixParam + "/" + lenParam)
if err != nil {
writeJSONError(w, http.StatusBadRequest, err.Error())
return
}
prefix := parsed.Masked().String()
routes, err := s.db.GetPrefixDetailsContext(r.Context(), prefix)
if err != nil {
@@ -912,8 +920,15 @@ func (s *Server) handlePrefixDetail() http.HandlerFunc {
return
}
// Combine prefix and length into CIDR notation
prefix := prefixParam + "/" + lenParam
// Look the prefix up in the text form net/netip prints, the form the
// tables store, so that any valid way of writing it finds the route.
parsed, err := netip.ParsePrefix(prefixParam + "/" + lenParam)
if err != nil {
http.Error(w, "Invalid prefix", http.StatusBadRequest)
return
}
prefix := parsed.Masked().String()
routes, err := s.db.GetPrefixDetailsContext(r.Context(), prefix)
if err != nil {