Skip to content

Commit f484834

Browse files
committed
api: fix redirect loop on unregistered sub-paths
The catch-all /api/* handler redirects unversioned paths to the latest API version. When the path already contains a version prefix (e.g. /api/1.4/patches/123/mbox), the redirect prepends another version prefix, causing an infinite redirect chain. Return 404 instead when the path already starts with a known version. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 3e9e553 commit f484834

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎pkg/api/api.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ func NewRouter(cfg *config.Config, database *bun.DB, baseURL string, bus db.Even
247247
latestPrefix := fmt.Sprintf("/api/%d.%d", latest.Major, latest.Minor)
248248
r.Get("/api/*", func(w http.ResponseWriter, r *http.Request) {
249249
rest := strings.TrimPrefix(r.URL.Path, "/api")
250+
for _, ver := range supportedVersions {
251+
vp := fmt.Sprintf("/%d.%d/", ver.Major, ver.Minor)
252+
if strings.HasPrefix(rest, vp) {
253+
http.NotFound(w, r)
254+
return
255+
}
256+
}
250257
http.Redirect(w, r, latestPrefix+rest, http.StatusMovedPermanently)
251258
})
252259

0 commit comments

Comments
 (0)