Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask implementation #39

Merged
merged 21 commits into from
Oct 31, 2024
Merged

Flask implementation #39

merged 21 commits into from
Oct 31, 2024

Conversation

ChristianFredrikJohnsen
Copy link
Collaborator

Add web-page and change the pre-processing.

Ludvig Øvrevik and others added 21 commits October 29, 2024 22:08
…th same name as neat object). + refactor main.py -> play_genome().
Co-authored-by: Håkon Støren <[email protected]>
Co-authored-by: Christian Fredrik Johnsen <[email protected]>
avg_fitnesses = []
min_fitnesses = []

if not os.path.exists(fitness_file_path):

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix AI 2 months ago

To fix the problem, we need to validate the neat_name parameter to ensure it does not contain any malicious input that could lead to directory traversal or access to unauthorized files. We can achieve this by normalizing the path and ensuring it stays within a predefined safe directory. Additionally, we can use a whitelist of allowed directory names to further restrict the input.

  1. Normalize the path using os.path.normpath to remove any ".." segments.
  2. Ensure the normalized path starts with the base directory.
  3. Optionally, use a whitelist to restrict the allowed directory names.
Suggested changeset 1
app.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -158,3 +158,10 @@
     """Parse fitness values from the fitness file"""
-    fitness_file_path = f'data/{neat_name}/fitness/fitness_values.txt'
+    base_path = 'data'
+    fitness_file_path = os.path.normpath(os.path.join(base_path, neat_name, 'fitness', 'fitness_values.txt'))
+    
+    # Ensure the path is within the base directory
+    if not fitness_file_path.startswith(os.path.abspath(base_path)):
+        logging.error(f"Invalid path: {fitness_file_path}")
+        return [], [], [], []
+    
     generations = []
EOF
@@ -158,3 +158,10 @@
"""Parse fitness values from the fitness file"""
fitness_file_path = f'data/{neat_name}/fitness/fitness_values.txt'
base_path = 'data'
fitness_file_path = os.path.normpath(os.path.join(base_path, neat_name, 'fitness', 'fitness_values.txt'))

# Ensure the path is within the base directory
if not fitness_file_path.startswith(os.path.abspath(base_path)):
logging.error(f"Invalid path: {fitness_file_path}")
return [], [], [], []

generations = []
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
return [], [], [], []

try:
with open(fitness_file_path, 'r') as f:

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix AI 2 months ago

To fix the problem, we need to validate the neat_name parameter to ensure it does not contain any malicious input that could lead to directory traversal or unauthorized file access. We will:

  1. Normalize the path using os.path.normpath to remove any ".." segments.
  2. Ensure that the normalized path starts with the intended base directory.
  3. Optionally, use a whitelist of allowed directory names if the set of valid neat_name values is known and limited.
Suggested changeset 1
app.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -158,3 +158,4 @@
     """Parse fitness values from the fitness file"""
-    fitness_file_path = f'data/{neat_name}/fitness/fitness_values.txt'
+    base_path = 'data'
+    fitness_file_path = os.path.normpath(os.path.join(base_path, neat_name, 'fitness', 'fitness_values.txt'))
     generations = []
@@ -164,2 +165,6 @@
     
+    if not fitness_file_path.startswith(os.path.join(base_path, neat_name)):
+        logging.error(f"Invalid fitness file path: {fitness_file_path}")
+        return [], [], [], []
+    
     if not os.path.exists(fitness_file_path):
EOF
@@ -158,3 +158,4 @@
"""Parse fitness values from the fitness file"""
fitness_file_path = f'data/{neat_name}/fitness/fitness_values.txt'
base_path = 'data'
fitness_file_path = os.path.normpath(os.path.join(base_path, neat_name, 'fitness', 'fitness_values.txt'))
generations = []
@@ -164,2 +165,6 @@

if not fitness_file_path.startswith(os.path.join(base_path, neat_name)):
logging.error(f"Invalid fitness file path: {fitness_file_path}")
return [], [], [], []

if not os.path.exists(fitness_file_path):
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@ChristianFredrikJohnsen ChristianFredrikJohnsen merged commit 883bb80 into main Oct 31, 2024
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants