Skip to content

Redundant boundary if files/data is empty #862

Closed

Description

@b0g3r

If you try to send a post request with empty data and files — you will create a body with one --boundary-- string, which can break the server.

It's because httpx create MultipartStream for data={}, files={}:

elif isinstance(data, dict):
if files is not None:
return MultipartStream(data=data, files=files, boundary=boundary)

And this MultipartStream doesn't have special behavior for empty data/files:

for field in self.iter_fields(data, files):
body.write(b"--%s\r\n" % boundary)
body.write(field.render_headers())
body.write(field.render_data())
body.write(b"\r\n")
body.write(b"--%s--\r\n" % boundary)

Reproducible example:

from httpx.content_streams import encode

stream = encode(data={}, files={}, boundary=b"+++")
print(b"".join([part for part in stream]))
# b'--+++--\r\n'

I see two ways to solve it:

  1. Check files not only for None but also for emptiness in _content_streams.decode
  2. Check the count of fields in MultipartStream.__init__ and doesn't write anything if fields are empty.

I already opened PR (#861) with a test example. If someone tells me which of these options is better, I will be happy to implement it 🙌

Activity

  1. added
    http/2Issues and PRs related to HTTP/2
    multipartIssues and PRs relative to multipart uploads
    and removed
    http/2Issues and PRs related to HTTP/2
    on Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    multipartIssues and PRs relative to multipart uploads

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions