SSTI 脆弱性の修正

flask-app プロジェクト内の pages/a1.py を開きます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from flask import (
    Blueprint,
    request,
    render_template_string
)

bp = Blueprint(
    "a1", __name__,
    template_folder='templates',
    static_folder='static'
)

@bp.route("/A1")
def a1():
    ### Server-Side Template Injection
    name = request.args.get("name", "")
    with open("templates/a1.html") as f:
        template = f.read()
    content = template.replace("{{ name }}", name)
    return render_template_string(content)

このページは、HTML レスポンスのレンダリングに render_template_string を使用しています。render_template_string は文字列から直接テンプレートをレンダリングします。これは、文字列が有効なPythonコードを含むように変更された場合に、潜在的なサーバーサイドテンプレートインジェクション (SSTI) の脆弱性を引き起こす可能性があります。

潜在的な SSTI 攻撃を防ぐために、render_template に置き換えます。これは、テンプレートの変更を許可しないのでより安全です。これを修正するために、a1.py のコードを次のように置き換えます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from flask import (
    Blueprint,
    request,
    render_template
)

bp = Blueprint(
    "a1", __name__,
    template_folder='templates',
    static_folder='static'
)

@bp.route("/A1")
def a1():
    name = request.args.get("name", "")
    return render_template("a1.html", name=name)

a1.py で変更を保存し、次に移ります。