-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CSRF middleware not being able to extract token from `multipart/f…
- Loading branch information
Showing
2 changed files
with
31 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
"mime/multipart" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
|
@@ -499,6 +501,25 @@ func TestValuesFromForm(t *testing.T) { | |
return req | ||
} | ||
|
||
exampleMultiPartFormRequest := func(mod func(w *multipart.Writer)) *http.Request { | ||
var b bytes.Buffer | ||
w := multipart.NewWriter(&b) | ||
w.WriteField("name", "Jon Snow") | ||
w.WriteField("emails[]", "[email protected]") | ||
if mod != nil { | ||
mod(w) | ||
} | ||
|
||
fw, _ := w.CreateFormFile("upload", "my.file") | ||
fw.Write([]byte(`<div>hi</div>`)) | ||
w.Close() | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(b.String())) | ||
req.Header.Add(echo.HeaderContentType, w.FormDataContentType()) | ||
|
||
return req | ||
} | ||
|
||
var testCases = []struct { | ||
name string | ||
givenRequest *http.Request | ||
|
@@ -520,6 +541,14 @@ func TestValuesFromForm(t *testing.T) { | |
whenName: "emails[]", | ||
expectValues: []string{"[email protected]", "[email protected]"}, | ||
}, | ||
{ | ||
name: "ok, POST multipart/form, multiple value", | ||
givenRequest: exampleMultiPartFormRequest(func(w *multipart.Writer) { | ||
w.WriteField("emails[]", "[email protected]") | ||
}), | ||
whenName: "emails[]", | ||
expectValues: []string{"[email protected]", "[email protected]"}, | ||
}, | ||
{ | ||
name: "ok, GET form, single value", | ||
givenRequest: exampleGetFormRequest(nil), | ||
|
@@ -540,16 +569,6 @@ func TestValuesFromForm(t *testing.T) { | |
whenName: "nope", | ||
expectError: errFormExtractorValueMissing.Error(), | ||
}, | ||
{ | ||
name: "nok, POST form, form parsing error", | ||
givenRequest: func() *http.Request { | ||
req := httptest.NewRequest(http.MethodPost, "/", nil) | ||
req.Body = nil | ||
return req | ||
}(), | ||
whenName: "name", | ||
expectError: "valuesFromForm parse form failed: missing form body", | ||
}, | ||
{ | ||
name: "ok, cut values over extractorLimit", | ||
givenRequest: examplePostFormRequest(func(v *url.Values) { | ||
|