2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2020-06-30 12:50:28 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2020-06-30 12:50:28 +02:00
|
|
|
|
|
|
|
Flask
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
from flask import Response, request
|
|
|
|
from werkzeug.datastructures import Headers
|
|
|
|
|
|
|
|
@app.route('/route')
|
|
|
|
def route():
|
|
|
|
content_type = request.args["Content-Type"]
|
|
|
|
response = Response()
|
|
|
|
headers = Headers()
|
|
|
|
headers.add("Content-Type", content_type) # Noncompliant
|
|
|
|
response.headers = headers
|
|
|
|
return response
|
|
|
|
----
|
|
|
|
|
|
|
|
Django
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
import django.http
|
|
|
|
|
|
|
|
def route(request):
|
|
|
|
content_type = request.GET.get("Content-Type")
|
|
|
|
response = django.http.HttpResponse()
|
|
|
|
response.__setitem__('Content-Type', content_type) # Noncompliant
|
|
|
|
return response
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2020-06-30 12:50:28 +02:00
|
|
|
|
|
|
|
Flask
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
from flask import Response, request
|
|
|
|
from werkzeug.datastructures import Headers
|
|
|
|
import re
|
|
|
|
|
|
|
|
@app.route('/route')
|
|
|
|
def route():
|
|
|
|
content_type = request.args["Content-Type"]
|
|
|
|
allowed_content_types = r'application/(pdf|json|xml)'
|
|
|
|
response = Response()
|
|
|
|
headers = Headers()
|
|
|
|
if re.match(allowed_content_types, content_type):
|
|
|
|
headers.add("Content-Type", content_type) # Compliant
|
|
|
|
else:
|
|
|
|
headers.add("Content-Type", "application/json")
|
|
|
|
response.headers = headers
|
|
|
|
return response
|
|
|
|
----
|
|
|
|
Django
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:50:28 +02:00
|
|
|
----
|
|
|
|
import django.http
|
|
|
|
import re
|
|
|
|
|
|
|
|
def route(request):
|
|
|
|
content_type = request.GET.get("Content-Type")
|
|
|
|
allowed_content_types = r'application/(pdf|json|xml)'
|
|
|
|
response = django.http.HttpResponse()
|
|
|
|
if re.match(allowed_content_types, content_type):
|
|
|
|
response.__setitem__('Content-Type', content_type) # Compliant
|
|
|
|
else:
|
|
|
|
response.__setitem__('Content-Type', "application/json")
|
|
|
|
return response
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|