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

Phew._handle_request does not ensure content-length of data read #56

Open
ccrighton opened this issue Aug 12, 2023 · 1 comment
Open

Comments

@ccrighton
Copy link

ccrighton commented Aug 12, 2023

When content-type is application/x-www-form-urlencoded, the current implementation makes a single call to read:

 if request.headers["content-type"].startswith("application/x-www-form-urlencoded"):
        form_data = await reader.read(int(request.headers["content-length"]))
        request.form = _parse_query_string(form_data.decode())

Unfortunately, this doesn't always read all content-length bytes. It's necessary to loop until all bytes are read e.g.

  if request.headers["content-type"].startswith("application/x-www-form-urlencoded"):
    form_data = b""
    content_length = int(request.headers["content-length"])
    while content_length > 0:
        data = await reader.read(content_length)
        if len(data) == 0:
          break
        content_length -= len(data)
        form_data += data
    request.form = _parse_query_string(form_data.decode())

I was posting a textarea with about 1400 bytes. It was only reading about 250.

Other parts of the implementation may need to be checked to ensure that no other similar issue is present.

@jimkoke
Copy link

jimkoke commented Feb 28, 2024

I ran into this problem, too, but with a form with email addresses. I fixed it in a similar manner. In that case, the parser did not have a complete name=value pair, but only sometimes as the email addresses I entered changed length.

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

2 participants