== How to fix it in Python Standard Library

=== Code examples

include::../../common/fix/code-rationale.adoc[]

==== Noncompliant code example

[source,python,diff-id=1,diff-type=noncompliant]
----
import logging

logger = logging.getLogger(__name__)

@app.route('/example')
def log():
    data = request.args["data"]
    logger.log(logging.CRITICAL, "%s", data) # Noncompliant
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
import logging
import base64

logger = logging.getLogger(__name__)

@app.route('/example')
def log():
    data = request.args["data"]
    if data.isalnum():
        logger.log(logging.CRITICAL, "%s", data)
    else:
        logger.log(logging.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.
