-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Insecure way to handle json requests #2174
Comments
Please use latest aiohttp. See #1723 |
That's not what I need. I'm not trying to get the request from a server but to handle the request sent to the server. Now the way to handle will cause CSRF attacks. An error should be raised or directly return 405 instead of getting the json result. Please read my code carefully. |
For preventing CSRF you need much more than just content type check. |
But when I want to make a json web service I just need to check the content type. (See here i.e. |
Well, would you make a PR? |
Yes, I've made the PR. When one tries getting the JSON data, |
I think the proper PR should follow client API design: async def json(self, *, encoding: str=None,
loads: JSONDecoder=DEFAULT_JSON_DECODER,
content_type: Optional[str]='application/json') -> Any:
"""Read and decodes JSON response."""
if self._body is None:
await self.read()
if content_type:
ctype = self.headers.get(hdrs.CONTENT_TYPE, '').lower()
if not _is_expected_content_type(ctype, content_type):
raise ContentTypeError(
self.request_info,
self.history,
message=('Attempt to decode JSON with '
'unexpected mimetype: %s' % ctype),
headers=self.headers)
if encoding is None:
encoding = self.get_encoding()
return loads(self._body.decode(encoding)) # type: ignore |
Long story short
BaseRequest.json coroutine returns request body directly, whose
Content-Type
should have been checked.Expected behaviour
Content-Type
should be ensured to beapplication/json
so that one can make sure it is sent by ajax, since there are always csrf attacks while ajax can not be forged cross site.Actual behaviour
Content-Type
is not checked.Steps to reproduce
I setup the server using following code.
And I request the server with following command.
Actually flask handles json requests only with
Content-Type: application/json
Environment
The text was updated successfully, but these errors were encountered: