HTTP 响应拆分¶
ID: py/http-response-splitting
Kind: path-problem
Security severity: 6.1
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-113
- external/cwe/cwe-079
Query suites:
- python-code-scanning.qls
- python-security-extended.qls
- python-security-and-quality.qls
直接将用户输入(例如 HTTP 请求参数)写入 HTTP 标头会导致 HTTP 响应拆分漏洞。
如果用户控制的输入用于允许换行符的 HTTP 标头,攻击者可以注入额外的标头或控制响应主体,从而导致 XSS 或缓存中毒等漏洞。
建议¶
确保不将包含换行符的用户输入写入 HTTP 标头。
示例¶
在以下示例中,标记为 BAD 的情况将用户输入写入标头名称。在 GOOD 的情况下,输入首先被转义,以不包含任何换行符。
@app.route("/example_bad")
def example_bad():
rfs_header = request.args["rfs_header"]
response = Response()
custom_header = "X-MyHeader-" + rfs_header
# BAD: User input is used as part of the header name.
response.headers[custom_header] = "HeaderValue"
return response
@app.route("/example_good")
def example_bad():
rfs_header = request.args["rfs_header"]
response = Response()
custom_header = "X-MyHeader-" + rfs_header.replace("\n", "").replace("\r","").replace(":","")
# GOOD: Line break characters are removed from the input.
response.headers[custom_header] = "HeaderValue"
return response