Skip to content

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

Closed
@Pentusha

Description

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions