Skip to content

Set content type to application/json for flask.make_response with JSON data#8

Open
pixeebot[bot] wants to merge 1 commit into
masterfrom
pixeebot/drip-2024-09-03-pixee-python/flask-json-response-type
Open

Set content type to application/json for flask.make_response with JSON data#8
pixeebot[bot] wants to merge 1 commit into
masterfrom
pixeebot/drip-2024-09-03-pixee-python/flask-json-response-type

Conversation

@pixeebot
Copy link
Copy Markdown

@pixeebot pixeebot Bot commented Sep 3, 2024

The default mimetype for make_response in Flask is 'text/html'. This is true even when the response contains JSON data.
If the JSON contains (unsanitized) user-supplied input, a malicious user may supply HTML code which leaves the application vulnerable to cross-site scripting (XSS).
This fix explicitly sets the response type to application/json when the response body is JSON data to avoid this vulnerability. Our changes look something like this:

from flask import make_response, Flask
import json

app = Flask(__name__)

@app.route("/test")
def foo(request):
    json_response = json.dumps({ "user_input": request.GET.get("input") })
-   return make_response(json_response)
+   return make_response(json_response, {'Content-Type':'application/json'})
More reading

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: pixee:python/flask-json-response-type

@pixeebot
Copy link
Copy Markdown
Author

pixeebot Bot commented Sep 11, 2024

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants