Cross-Site Scripting (XSS)
Concept
Vulnerable Scenario
Example Code (Vulnerable)
@app.route('/post_comment', methods=['POST'])
def post_comment():
comment = request.form['comment']
# Store the comment in the database
database.execute("INSERT INTO comments (text) VALUES (?)", (comment,))
return redirect('/blog')
@app.route('/blog')
def blog():
comments = database.execute("SELECT * FROM comments")
return render_template('blog.html', comments=comments)<!-- blog.html -->
<h1>Blog Comments</h1>
<div data-gb-custom-block data-tag="for">
<div class="comment">
{{ comment.text | safe }}
</div>
</div>
Explanation
Prevention
Example Code (Secure)
Conclusion
Semgrep Rule
Last updated