Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitize input in Django Rest Framework

If I send something like

{
    "description": "Hello World <script>alert('hacked');</script>"
}

to my django rest framework view, I want to get rid of the the script tags.

  1. Is there a convenient way to do this, that does not involve overwriting all the things and add strip_tags?
  2. What else is to do to sanitize input?
  3. Did I really overread that section in the drf docs or isn't that covered?
like image 648
shredding Avatar asked Apr 14 '15 19:04

shredding


People also ask

Does Django sanitize input?

Django by default sanitizes quoted data.

How do you disinfect input in python?

To sanitize a string input which you want to store to the database (for example a customer name) you need either to escape it or plainly remove any quotes (', ") from it. This effectively prevents classical SQL injection which can happen if you are assembling an SQL query from strings passed by the user.

What is Validated_data in Django REST?

validated_data is an OrderedDict and you can see it only after is_valid() and is_valid() == True.

What is Restapi in Django?

Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.


1 Answers

Ignore the answers here, they are terrible.

Use bleach. You won't get every edge case. This is the situation to use a library in. Your client has control of the client side by definition.

like image 96
dreamriver Avatar answered Oct 13 '22 16:10

dreamriver