fix: make FormStrings parse form data correctly (#40558) (#40560)

backport #40558

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
wxiaoguangandsilverwind authored and GitHub committed 2026-11-27 12:53:45 +00:00
1 parent 1cadf5338b
commit 41b3d0d4e3
2 files changed
+11 -9

No files matched your search

+2 -9
View File
@@ -25,15 +25,8 @@ func (b *Base) FormString(key string, def ...string) string {
// FormStrings returns a values for the key in the form (including query parameters), similar to FormString
func (b *Base) FormStrings(key string) []string {
if b.Req.Form == nil {
if err := b.Req.ParseMultipartForm(32 << 20); err != nil {
return nil
}
}
if v, ok := b.Req.Form[key]; ok {
return v
}
return nil
_ = b.Req.ParseMultipartForm(ParseMultipartFormMaxMemory) // the same logic as http.Request.FormValue
return b.Req.Form[key]
}
func (b *Base) FormStringInt64s(key string) []int64 {
+9
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"gitea.dev/modules/setting"
@@ -50,3 +51,11 @@ func TestRedirect(t *testing.T) {
assert.JSONEq(t, `{"redirect":"/other"}`, resp.Body.String())
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestFormStrings(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/?q=1", strings.NewReader("oid=a&oid=b"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
b := NewBaseContextForTest(t, httptest.NewRecorder(), req)
assert.Equal(t, []string{"a", "b"}, b.FormStrings("oid"))
assert.Equal(t, []string{"1"}, b.FormStrings("q"))
}