2023-03-07 17:16:47 +01:00

42 lines
925 B
Plaintext

== How to fix it in Flask
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
import logging
app = Flask(__name__)
@app.route('/example')
def log():
data = request.args["data"]
app.logger.critical("%s", data) # Noncompliant
----
==== Compliant solution
[source,python,diff-id=1,diff-type=compliant]
----
import logging
import base64
app = Flask(__name__)
@app.route('/example')
def log():
data = request.args["data"]
if data.isalnum():
app.logger.critical("%s", data)
else:
app.logger.critical("Invalid Input: %s", base64.b64encode(data.encode('UTF-8')))
----
include::../../common/fix/how-does-this-work.adoc[]
Here, the example compliant code uses the `isalnum` function to ensure the untrusted data is safe. If not, it performs Base64 encoding to protect from log injection.