敏感信息的明文存储¶
ID: py/clear-text-storage-sensitive-data
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-312
- external/cwe/cwe-315
- external/cwe/cwe-359
Query suites:
- python-code-scanning.qls
- python-security-extended.qls
- python-security-and-quality.qls
以明文形式存储的敏感信息可被获取存储权限的攻击者访问。这对 cookie 来说尤其重要,因为 cookie 存储在最终用户的机器上。
建议¶
确保敏感信息在存储之前始终被加密。如果可能,完全避免将敏感信息放入 cookie 中。相反,首选在 cookie 中存储一个密钥,该密钥可用于查找敏感信息。
一般而言,仅在需要以明文形式使用敏感信息时才对其进行解密。
请注意,外部进程通常会存储应用程序的 standard out
和 standard error
流,这会导致记录的敏感信息也被存储。
示例¶
以下示例代码将用户凭据(在本例中为密码)以明文形式存储在 cookie 中
from flask import Flask, make_response, request
app = Flask("Leak password")
@app.route('/')
def index():
password = request.args.get("password")
resp = make_response(render_template(...))
resp.set_cookie("password", password)
return resp
相反,凭据应被加密,例如使用 cryptography
模块,或者根本不存储。