Prevent NPE if multipart form is empty #13

Merged
lunny merged 2 commits from zeripath/binding:prevent-panic-with-empty-multipart-form into master 2022-10-13 10:45:18 +00:00
Owner

In https://github.com/go-gitea/gitea/issues/19698 a fuzzer has passed an
empty multipart form which causes go-chi/binding to throw an NPE.

This PR simply protects against this by checking if the multipart form is nil
before trying to map it.

Signed-off-by: Andrew Thornton art27@cantab.net

In https://github.com/go-gitea/gitea/issues/19698 a fuzzer has passed an empty multipart form which causes go-chi/binding to throw an NPE. This PR simply protects against this by checking if the multipart form is nil before trying to map it. Signed-off-by: Andrew Thornton <art27@cantab.net>
zeripath added 1 commit 2022-10-09 12:28:18 +00:00
Prevent NPE if multipart form is empty
In https://github.com/go-gitea/gitea/issues/19698 a fuzzer has passed an
empty multipart form which causes go-chi/binding to throw an NPE.

This PR simply protects against this by checking if the multipart form is nil
before trying to map it.

Signed-off-by: Andrew Thornton <art27@cantab.net>
Some checks failed
continuous-integration/drone/pr Build is failing
f995d8c90c
zeripath added 1 commit 2022-10-09 12:28:25 +00:00
Merge branch 'master' into prevent-panic-with-empty-multipart-form
All checks were successful
continuous-integration/drone/pr Build is passing
4cb1bae3ff
wxiaoguang reviewed 2022-10-09 12:34:54 +00:00
@ -150,2 +150,4 @@
req.ParseForm()
}
if form == nil {
return append(errors, Validate(req, formStruct)...)
Member

It looks good.

ps: if the form, parseErr := multipartReader.ReadForm(MaxMemory) gets error, the error should have been reported by line 146.

Maybe the change code be:

			req.MultipartForm, err = multipartReader.ReadForm(MaxMemory)
			if err != nil {
				errors.Add([]string{}, ERR_DESERIALIZATION, err.Error())
			} else {
				if req.Form == nil {
					req.ParseForm()
				}
				for k, v := range form.Value {
					req.Form[k] = append(req.Form[k], v...)
				}
			}
		}
	}
	if req.MultipartForm != nil {
		errors = mapForm(formStructV, req.MultipartForm.Value, req.MultipartForm.File, errors)
	}
	return append(errors, Validate(req, formStruct)...)
}

-- OR --

Just return as early as multipartReader.ReadForm returns error. If the form is broken, it's not necessary to do req.ParseForm() for the broken form IMO.

It looks good. ps: if the `form, parseErr := multipartReader.ReadForm(MaxMemory)` gets error, the error should have been reported by line 146. Maybe the change code be: ``` req.MultipartForm, err = multipartReader.ReadForm(MaxMemory) if err != nil { errors.Add([]string{}, ERR_DESERIALIZATION, err.Error()) } else { if req.Form == nil { req.ParseForm() } for k, v := range form.Value { req.Form[k] = append(req.Form[k], v...) } } } } if req.MultipartForm != nil { errors = mapForm(formStructV, req.MultipartForm.Value, req.MultipartForm.File, errors) } return append(errors, Validate(req, formStruct)...) } ``` -- OR -- Just return as early as `multipartReader.ReadForm` returns error. If the form is broken, it's not necessary to do `req.ParseForm()` for the broken form IMO.
wxiaoguang approved these changes 2022-10-09 12:38:57 +00:00
lunny approved these changes 2022-10-13 10:44:25 +00:00
lunny merged commit b298916196 into master 2022-10-13 10:45:18 +00:00
zeripath deleted branch prevent-panic-with-empty-multipart-form 2022-10-23 10:53:20 +00:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: go-chi/binding#13
No description provided.