Skip to content
New issue

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

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

Already on GitHub? # to your account

MultipartParseError: Did not find boundary character #123

Closed
yecril23pl opened this issue Feb 23, 2024 · 3 comments · Fixed by #124
Closed

MultipartParseError: Did not find boundary character #123

yecril23pl opened this issue Feb 23, 2024 · 3 comments · Fixed by #124

Comments

@yecril23pl
Copy link
Contributor

yecril23pl commented Feb 23, 2024

I tried using multipart from Python module http.server. It did not work. What am I doing wrong (apart from everything)?

Server script

#!/usr/bin/python3
# coding=ISO-8859-2
import multipart
from os import environ
from sys import stdin

def on_field(field):
	pass

def on_file(file):
	print (file.field_name)

def get_content_type ():
	return environ .get ('CONTENT_TYPE')

def get_content_length ():
	return environ .get ('CONTENT_LENGTH')

print('''Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML ><HTML LANG="pl" ><TITLE >Przyjęcie formularza</TITLE
 ><H1 >Wynik zapytania</H1 ><P >&hellip; '''
  )

if False:
	print (get_content_type (), input ())
else:
  multipart.parse_form (
  {
	  'Content-Type' : get_content_type (),
	  'Content-Length' : get_content_length ()
  }, stdin, on_field, on_file)

Server command

python -m http.server --cgi

Payload

POST /cgi-bin/upload HTTP/1
Host: 0.0.0.0:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------250220064916703015541512867370
Content-Length: 239
Origin: http://0.0.0.0:8000
Connection: keep-alive
Referer: http://0.0.0.0:8000/
Upgrade-Insecure-Requests: 1

-----------------------------250220064916703015541512867370
Content-Disposition: form-data; name="FILE"; filename=".bash_aliases"
Content-Type: application/octet-stream


-----------------------------250220064916703015541512867370--

Server log

127.0.0.1 - - [23/Feb/2024 16:03:37] "POST /cgi-bin/upload HTTP/1.1" 200 -
Did not find boundary character '-' at index 2
Traceback (most recent call last):
  File "/home/krzysztof/cgi-bin/upload", line 28, in <module>
    multipart.parse_form (
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1902, in parse_form
    parser.write(buff)
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1794, in write
    return self.parser.write(data)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1076, in write
    l = self._internal_write(data, data_len)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1183, in _internal_write
    raise e
multipart.exceptions.MultipartParseError: Did not find boundary character '-' at index 2
127.0.0.1 - - [23/Feb/2024 16:03:37] CGI script exit code 1
@yecril23pl
Copy link
Contributor Author

The script fails in the same way when called directly:

#!/bin/sh
CONTENT_TYPE='multipart/form-data; boundary=---------------------------250220064916703015541512867370'
CONTENT_LENGTH=239
export CONTENT_TYPE CONTENT_LENGTH
exec \
/home/krzysztof/cgi-bin/upload <<'EOF'
-----------------------------250220064916703015541512867370
Content-Disposition: form-data; name="FILE"; filename=".bash_aliases"
Content-Type: application/octet-stream


-----------------------------250220064916703015541512867370--
EOF

@yecril23pl
Copy link
Contributor Author

yecril23pl commented Feb 23, 2024

The error message seems wrong, '-' is what we have found.

The error message should be

Expected boundary character %r, got %r

After applying (backported) #124, I get:

multipart.exceptions.MultipartParseError: Expected boundary character 45, got '-' at index 2

@yecril23pl
Copy link
Contributor Author

yecril23pl commented Feb 23, 2024

boundary [index + 0o2] == 0o55
'-' == '\55'
'\55' != 0o55

🤡

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant