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

Request header "Content-Type: application/json; charset=test" cause an error #3562

Closed
Pentusha opened this issue Jan 21, 2019 · 4 comments
Closed

Comments

@Pentusha
Copy link
Contributor

Pentusha commented Jan 21, 2019

Long story short

Request's header Content-Type encoding have not validated properly.
bytes_body.decode(encoding) raise as error "LookupError: unknown encoding: test"

Expected behaviour

Status code 415 or 422
Encoding have to be limited with standard encodings.

Actual behaviour

500 Internal Server Error

Server got itself in trouble
Error handling request
Traceback (most recent call last):
  File "/home/pentusha/.venvs/test/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/home/pentusha/.venvs/test/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/home/pentusha/projects/test/api/app2.py", line 5, in post_handler
    body = await request.text()
  File "/home/pentusha/.venvs/test/lib/python3.7/site-packages/aiohttp/web_request.py", line 579, in text
    return bytes_body.decode(encoding)
LookupError: unknown encoding: test

Steps to reproduce

from aiohttp import web

async def post_handler(request):
    body = await request.text()
    return web.Response(body)

def main():
    app = web.Application()
    app.add_routes([
        web.post('/', post_handler)
    ])
    web.run_app(app, host='0.0.0.0', port=8000)

if __name__ == '__main__':
    main()
curl 'http://0:8000/' -H 'Content-Type: application/json; charset=test' -d '{}'

Your environment

aiohttp==3.5.4
Python 3.7.0

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #1011 (POST with application/json headers), #3242 (Error), #2206 (json request for certain media types fail), #2120 (Invalid content-type header causes LookupError), and #1197 (Flask request headers).

@asvetlov
Copy link
Member

Thanks for the report.
415 looks good probably.
Would you make a pull request?

@aio-libs-bot aio-libs-bot added bug and removed good first issue Good for newcomers enhancement labels Jan 21, 2019
@socketpair
Copy link
Contributor

Agree, 415 is appropriate

Pentusha pushed a commit to Pentusha/aiohttp that referenced this issue Jan 21, 2019
Pentusha pushed a commit to Pentusha/aiohttp that referenced this issue Jan 23, 2019
request.charset no longer causes ValueError.
request.read and request.post raises HTTPUnsupportedMediaType.
Unit test test_content_type_from_spec_with_bad_charset was removed.
asvetlov pushed a commit that referenced this issue May 10, 2019
* Raise 415 status code in case of invalid Content-Type encoding (#3562)

* Unit tests for #3562

* Сorrections after review (#3562)

request.charset no longer causes ValueError.
request.read and request.post raises HTTPUnsupportedMediaType.
Unit test test_content_type_from_spec_with_bad_charset was removed.
@asvetlov
Copy link
Member

Fixed by #3564

@lock lock bot added the outdated label May 20, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants