Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 3: Denial of Service vulnerability #80

Open
mtlynch opened this issue Aug 16, 2022 · 0 comments
Open

Chapter 3: Denial of Service vulnerability #80

mtlynch opened this issue Aug 16, 2022 · 0 comments

Comments

@mtlynch
Copy link

mtlynch commented Aug 16, 2022

Chapter 3 accepts arbitrary uploads from remote users, but it doesn't limit the size of the upload.

userID := req.FormValue("userid")

A malicious user could upload a very large file and exhaust server RAM or fill the disk.

A possible fix is to use http.MaxBytesReader to limit the size of the upload to some reasonable maximum for image files (e.g. 10 MiB).

Calling ParseMultipartForm with an explicit RAM limit also protects the server from exhausting RAM trying to process large uploads:

const maxImageBytes = 10 << 20 // 10 MiB

func uploaderHandler(w http.ResponseWriter, req *http.Request) {
	// Read a maximum of maxImageBytes, plus a little extra room for multipart fields.
	r.Body = http.MaxBytesReader(w, r.Body, maxImageBytes +1024)
	if err := r.ParseMultipartForm(maxImageBytes); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	userID := req.FormValue("userid")
	...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant