Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Projects/python-fb-example/requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests==2.32.0
pandas==2.2.3
jose==1.0.0
Flask==2.2.2
21 changes: 9 additions & 12 deletions Projects/python-fb-example/src/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# src/app.py
import requests
import pandas as pd
from flask import Flask, request, render_template_string

def fetch_data(url):
response = requests.get(url)
return response.json()
app = Flask(__name__)

def main():
url = 'https://api.github.com/events'
data = fetch_data(url)
df = pd.DataFrame(data)
print(df.head())
# A simple route that renders user input without proper sanitization
@app.route('/greet', methods=['GET'])
def greet():
user_name = request.args.get('name', 'Guest')
# Rendering user input directly without escaping
return render_template_string(f"<h1>Hello, {user_name}!</h1>")

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Static Application Security Testing (SAST) Vulnerability

Severity Finding
high
High
Untrusted input is included in web page content
Full description

Vulnerability Details

Rule ID: python-xss

Overview

XSS, or Cross-Site Scripting, is a type of vulnerability that allows an attacker to
inject malicious code into a website or web application.
This can allow the attacker to steal sensitive information from users, such as their
cookies or login credentials, or to perform unauthorized actions on their behalf.

Query operation

In the query we look for any user input that flows into
a potential output of the application.

Vulnerable example

In the following example, the Flask application takes a user-supplied parameter (name)
from the query string and renders it directly into an HTML template using the
render_template_string function. The issue is that
the user input is not properly sanitized or escaped, making it vulnerable to XSS attacks.

from flask import Flask, request, render_template_string

app = Flask(__name__)

@app.route('/')
def index():
    name = request.args.get('name', 'Guest')
    message = f'Hello, {name}!'
    return render_template_string('<h1>{}</h1>'.format(message))

if __name__ == '__main__':
app.run()

An attacker can exploit this vulnerability by injecting malicious JavaScript code into the
name parameter. For instance, they could modify the URL to include the following payload:
http://localhost:5000/?name=<script>alert('XSS')</script>

Remediation

When rendering templates, use parametrized variable assignments (which are automatically
escaped) instead of direct string manipulation -

@app.route('/')
def index():
    name = request.args.get('name', 'Guest')
    message = f'Hello, {name}!'
-    return render_template_string('<h1>{}</h1>'.format(message))
+    return render_template_string('<h1>{{ message }}</h1>', message=message)
Code Flows
Vulnerable data flow analysis result

↘️ request.args (at Projects/python-fb-example/src/app.py line 8)

↘️ request.args.get('name', 'Guest') (at Projects/python-fb-example/src/app.py line 8)

↘️ user_name (at Projects/python-fb-example/src/app.py line 10)

↘️ f"<h1>Hello, {user_name}!</h1>" (at Projects/python-fb-example/src/app.py line 10)

↘️ render_template_string(f"<h1>Hello, {user_name}!</h1>") (at Projects/python-fb-example/src/app.py line 10)

↘️ return render_template_string(f"<h1>Hello, {user_name}!</h1>") (at Projects/python-fb-example/src/app.py line 10)





if __name__ == '__main__':
main()
app.run(debug=True)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Static Application Security Testing (SAST) Vulnerability

Severity Finding
medium
Medium
Flask web application is running in debug mode
Full description

Vulnerability Details

Rule ID: python-flask-debug

Overview

Debug mode in a Flask app is a feature that allows the developer to see detailed
error messages and tracebacks when an error occurs. This can be useful for debugging
and troubleshooting, but it can also create a security vulnerability if the app is
deployed in debug mode. In debug mode, Flask will display detailed error messages and
tracebacks to the user, even if the error is caused by malicious input.
This can provide attackers with valuable information about the app's internal workings
and vulnerabilities, making it easier for them to exploit those vulnerabilities.

Query operation

In this query we look Flask applications that set the debug argument to True

Vulnerable example

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug=True)

In this example, the Flask application is set to run in debug mode by passing
debug=True as an argument to the app.run() function. This will make the application
emit potentially sensitive information to the users.

Remediation

When using app.run, omit the debug flag or set it to False -

if __name__ == '__main__':
-    app.run(debug=True)
+    app.run()



2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test this out
let's update this

I have new check for PR
Loading