Skip to content

Commit 895e07a

Browse files
committed
web: decode percent-encoded URL parameters
Chi stores URL parameters from the raw path without decoding percent-encoded characters. Bundle names containing spaces (e.g. "BNXT 25.11") appear as "BNXT%2025.11" in the URL and reach handlers still encoded, causing database lookups to fail with a 404. Add a urlParam() helper that wraps chi.URLParam with url.PathUnescape and use it for all string URL parameters across the web handlers. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 2d4ea4a commit 895e07a

9 files changed

Lines changed: 32 additions & 36 deletions

File tree

‎pkg/web/auth.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"strings"
1313
"time"
1414

15-
"github.com/go-chi/chi/v5"
16-
1715
"github.com/getpatchwork/patchwork/pkg/db"
1816
"github.com/getpatchwork/patchwork/pkg/events"
1917
"github.com/getpatchwork/patchwork/pkg/log"
@@ -128,7 +126,7 @@ func (h *webHandler) ConfirmHandler(w http.ResponseWriter, r *http.Request) {
128126
ctx := r.Context()
129127
q := db.GetQueries(ctx)
130128
pc := h.pageCtx(r)
131-
key := chi.URLParam(r, "key")
129+
key := urlParam(r, "key")
132130

133131
var conf db.EmailConfirmation
134132
err := q.DB.NewSelect().Model(&conf).

‎pkg/web/bundles.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strconv"
1212
"strings"
1313

14-
"github.com/go-chi/chi/v5"
1514
"github.com/uptrace/bun"
1615

1716
"github.com/getpatchwork/patchwork/pkg/db"
@@ -45,7 +44,7 @@ func (h *webHandler) BundleList(w http.ResponseWriter, r *http.Request) {
4544
func (h *webHandler) ProjectBundleList(w http.ResponseWriter, r *http.Request) {
4645
ctx := r.Context()
4746
q := db.GetQueries(ctx)
48-
linkname := chi.URLParam(r, "linkname")
47+
linkname := urlParam(r, "linkname")
4948

5049
project, err := q.GetProjectByLinkname(linkname)
5150
if err != nil {
@@ -86,8 +85,8 @@ type bundleDetailData struct {
8685
func (h *webHandler) BundleDetail(w http.ResponseWriter, r *http.Request) {
8786
ctx := r.Context()
8887
q := db.GetQueries(ctx)
89-
username := chi.URLParam(r, "username")
90-
bundlename := chi.URLParam(r, "bundlename")
88+
username := urlParam(r, "username")
89+
bundlename := urlParam(r, "bundlename")
9190
pc := h.pageCtx(r)
9291
user := getWebUser(r)
9392

@@ -149,8 +148,8 @@ func (h *webHandler) BundleUpdate(w http.ResponseWriter, r *http.Request) {
149148
ctx := r.Context()
150149
q := db.GetQueries(ctx)
151150
user := getWebUser(r)
152-
username := chi.URLParam(r, "username")
153-
bundlename := chi.URLParam(r, "bundlename")
151+
username := urlParam(r, "username")
152+
bundlename := urlParam(r, "bundlename")
154153

155154
if !h.validateCSRF(r) {
156155
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, bundlename), http.StatusFound)

‎pkg/web/covers.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package web
77

88
import (
99
"net/http"
10-
"net/url"
1110
"strconv"
1211

1312
"github.com/go-chi/chi/v5"
@@ -17,8 +16,8 @@ import (
1716
)
1817

1918
func (h *webHandler) CoverDetailPage(w http.ResponseWriter, r *http.Request) {
20-
linkname := chi.URLParam(r, "linkname")
21-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
19+
linkname := urlParam(r, "linkname")
20+
rawMsgid := urlParam(r, "msgid")
2221
ctx := r.Context()
2322
q := db.GetQueries(ctx)
2423
msgid := "<" + rawMsgid + ">"

‎pkg/web/mbox.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"net/http"
1212
"net/mail"
13-
"net/url"
1413
"regexp"
1514
"strconv"
1615
"strings"
@@ -199,8 +198,8 @@ func formatAddr(name, email string) string {
199198
}
200199

201200
func (h *webHandler) PatchMboxPage(w http.ResponseWriter, r *http.Request) {
202-
linkname := chi.URLParam(r, "linkname")
203-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
201+
linkname := urlParam(r, "linkname")
202+
rawMsgid := urlParam(r, "msgid")
204203
ctx := r.Context()
205204
q := db.GetQueries(ctx)
206205
msgid := "<" + rawMsgid + ">"
@@ -249,8 +248,8 @@ func (h *webHandler) servePatchMbox(w http.ResponseWriter, patch db.Patch, proje
249248
}
250249

251250
func (h *webHandler) CoverMboxPage(w http.ResponseWriter, r *http.Request) {
252-
linkname := chi.URLParam(r, "linkname")
253-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
251+
linkname := urlParam(r, "linkname")
252+
rawMsgid := urlParam(r, "msgid")
254253
ctx := r.Context()
255254
q := db.GetQueries(ctx)
256255
msgid := "<" + rawMsgid + ">"
@@ -393,8 +392,8 @@ func (h *webHandler) seriesPatchMbox(w http.ResponseWriter, r *http.Request, pat
393392
func (h *webHandler) BundleMbox(w http.ResponseWriter, r *http.Request) {
394393
ctx := r.Context()
395394
q := db.GetQueries(ctx)
396-
username := chi.URLParam(r, "username")
397-
bundlename := chi.URLParam(r, "bundlename")
395+
username := urlParam(r, "username")
396+
bundlename := urlParam(r, "bundlename")
398397

399398
var bundle db.Bundle
400399
err := q.DB.NewSelect().

‎pkg/web/patches.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
func (h *webHandler) PatchList(w http.ResponseWriter, r *http.Request) {
2424
ctx := r.Context()
2525
q := db.GetQueries(ctx)
26-
linkname := chi.URLParam(r, "linkname")
26+
linkname := urlParam(r, "linkname")
2727

2828
project, err := q.GetProjectByLinkname(linkname)
2929
if err != nil {
@@ -175,7 +175,7 @@ func (h *webHandler) PatchListAction(w http.ResponseWriter, r *http.Request) {
175175
ctx := r.Context()
176176
q := db.GetQueries(ctx)
177177
user := getWebUser(r)
178-
linkname := chi.URLParam(r, "linkname")
178+
linkname := urlParam(r, "linkname")
179179

180180
if !h.validateCSRF(r) {
181181
http.Redirect(w, r, "/project/"+linkname+"/list/", http.StatusFound)
@@ -298,8 +298,8 @@ func (h *webHandler) PatchListAction(w http.ResponseWriter, r *http.Request) {
298298
}
299299

300300
func (h *webHandler) PatchDetailPage(w http.ResponseWriter, r *http.Request) {
301-
linkname := chi.URLParam(r, "linkname")
302-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
301+
linkname := urlParam(r, "linkname")
302+
rawMsgid := urlParam(r, "msgid")
303303
ctx := r.Context()
304304
q := db.GetQueries(ctx)
305305
msgid := "<" + rawMsgid + ">"
@@ -481,8 +481,8 @@ func (h *webHandler) PatchUpdate(w http.ResponseWriter, r *http.Request) {
481481
}
482482
ctx := r.Context()
483483
q := db.GetQueries(ctx)
484-
linkname := chi.URLParam(r, "linkname")
485-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
484+
linkname := urlParam(r, "linkname")
485+
rawMsgid := urlParam(r, "msgid")
486486
msgid := "<" + rawMsgid + ">"
487487

488488
if !h.validateCSRF(r) {
@@ -556,8 +556,8 @@ func (h *webHandler) CommentAddressed(w http.ResponseWriter, r *http.Request) {
556556
}
557557
ctx := r.Context()
558558
q := db.GetQueries(ctx)
559-
linkname := chi.URLParam(r, "linkname")
560-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
559+
linkname := urlParam(r, "linkname")
560+
rawMsgid := urlParam(r, "msgid")
561561
msgid := "<" + rawMsgid + ">"
562562
commentID, _ := strconv.ParseInt(chi.URLParam(r, "commentID"), 10, 32)
563563

@@ -576,8 +576,8 @@ func (h *webHandler) CommentAddressed(w http.ResponseWriter, r *http.Request) {
576576
}
577577

578578
func (h *webHandler) PatchRawPage(w http.ResponseWriter, r *http.Request) {
579-
linkname := chi.URLParam(r, "linkname")
580-
rawMsgid, _ := url.PathUnescape(chi.URLParam(r, "msgid"))
579+
linkname := urlParam(r, "linkname")
580+
rawMsgid := urlParam(r, "msgid")
581581
ctx := r.Context()
582582
q := db.GetQueries(ctx)
583583
msgid := "<" + rawMsgid + ">"

‎pkg/web/profile.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func (h *webHandler) PasswordResetConfirm(w http.ResponseWriter, r *http.Request
372372
ctx := r.Context()
373373
q := db.GetQueries(ctx)
374374
pc := h.pageCtx(r)
375-
key := chi.URLParam(r, "key")
375+
key := urlParam(r, "key")
376376

377377
var conf db.EmailConfirmation
378378
err := q.DB.NewSelect().Model(&conf).

‎pkg/web/projects.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package web
88
import (
99
"net/http"
1010

11-
"github.com/go-chi/chi/v5"
12-
1311
"github.com/getpatchwork/patchwork/pkg/db"
1412
)
1513

@@ -34,7 +32,7 @@ func (h *webHandler) ProjectList(w http.ResponseWriter, r *http.Request) {
3432
func (h *webHandler) ProjectDetail(w http.ResponseWriter, r *http.Request) {
3533
ctx := r.Context()
3634
q := db.GetQueries(ctx)
37-
linkname := chi.URLParam(r, "linkname")
35+
linkname := urlParam(r, "linkname")
3836

3937
project, err := q.GetProjectByLinkname(linkname)
4038
if err != nil {

‎pkg/web/todo.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package web
88
import (
99
"net/http"
1010

11-
"github.com/go-chi/chi/v5"
12-
1311
"github.com/getpatchwork/patchwork/pkg/db"
1412
)
1513

@@ -72,7 +70,7 @@ func (h *webHandler) todoList(w http.ResponseWriter, r *http.Request) {
7270
ctx := r.Context()
7371
q := db.GetQueries(ctx)
7472
user := getWebUser(r)
75-
linkname := chi.URLParam(r, "linkname")
73+
linkname := urlParam(r, "linkname")
7674

7775
project, err := q.GetProjectByLinkname(linkname)
7876
if err != nil {

‎pkg/web/web.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import (
3030
"github.com/getpatchwork/patchwork/pkg/log"
3131
)
3232

33+
func urlParam(r *http.Request, key string) string {
34+
v, _ := url.PathUnescape(chi.URLParam(r, key))
35+
return v
36+
}
37+
3338
//go:embed static/*
3439
var staticFS embed.FS
3540

0 commit comments

Comments
 (0)