Skip to content

Commit

Permalink
Add error handling for non ascii header keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihweiLHBird committed Mar 11, 2023
1 parent a66c71f commit da9eb50
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions sanic/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from urllib.parse import quote

from sanic.compat import Header
from sanic.exceptions import ServerError
from sanic.exceptions import BadRequest, ServerError
from sanic.helpers import Default
from sanic.http import Stage
from sanic.log import error_logger, logger
Expand Down Expand Up @@ -132,12 +132,18 @@ async def create(
instance.response = None
setattr(instance.transport, "add_task", sanic_app.loop.create_task)

headers = Header(
[
(key.decode("ASCII"), value.decode(errors="surrogateescape"))
for key, value in scope.get("headers", [])
]
)
try:
headers = Header(
[
(
key.decode("ASCII"),
value.decode(errors="surrogateescape"),
)
for key, value in scope.get("headers", [])
]
)
except UnicodeDecodeError:
raise BadRequest("Header names can only contain US-ASCII characters")
instance.lifespan = Lifespan(instance)

if scope["type"] == "lifespan":
Expand Down

0 comments on commit da9eb50

Please sign in to comment.