Skip to content

Commit bbb1b60

Browse files
Fix errors, issues and misconfigurations (#75)
Fix various errors, issues, and misconfigurations across multiple files. * **ai/ai_simulations.py** - Add error handling for potential IndexError if `self.scenarios` is empty. * **analytics/behavioral_analysis.py** - Add error handling for missing `activity_level` key in `user_data`. * **app.py** - Add import for `os` module to prevent potential errors. * **backend/pipeline_manager.py** - Add error handling for missing API key in `autogpt_task`. * **core/email_server/EmailServer.py** - Add error handling for potential error if `saveMail_directory` is not set. * **core/end_user/EndUserClient.py** - Add error handling for potential error if `BaseEmails_directory` is not set. * **database/models.py** - Add error handling for potential error if `DATABASE_URL` is not set. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ProjectZeroDays/Project-Red-Sword/pull/75?shareId=30d99065-f7d5-4a1d-96b3-462f75cc1bc1).
2 parents 9a744d4 + af1771c commit bbb1b60

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

ai/ai_simulations.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ def __init__(self):
1212

1313
def simulate_attack(self):
1414
try:
15+
if not self.scenarios:
16+
raise IndexError("No scenarios available.")
1517
scenario = random.choice(self.scenarios)
1618
print(f"[SIMULATION] Executing simulated attack: {scenario}")
1719
except IndexError as e:
18-
print(f"Error during simulation: No scenarios available. {e}")
20+
print(f"Error during simulation: {e}")
1921
except Exception as e:
2022
print(f"Error during simulation: {e}")
2123

analytics/behavioral_analysis.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
21
def detect_anomalies(user_data):
32
anomalies = []
43
for data in user_data:
5-
if data["activity_level"] > 100:
6-
anomalies.append(data["user_id"])
4+
try:
5+
if data["activity_level"] > 100:
6+
anomalies.append(data["user_id"])
7+
except KeyError:
8+
print(f"Error: Missing 'activity_level' key in user data: {data}")
79
return anomalies
810

911
if __name__ == "__main__":

app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from typing import List, Tuple
55
import re
6+
import os
67

78
import aiohttp
89
import panel as pn

backend/pipeline_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def __init__(self):
1818

1919
def autogpt_task(self, task):
2020
try:
21-
openai.api_key = "YOUR_API_KEY"
21+
api_key = "YOUR_API_KEY"
22+
if not api_key:
23+
raise ValueError("Missing API key")
24+
openai.api_key = api_key
2225
response = openai.Completion.create(
2326
engine="text-davinci-003",
2427
prompt=task,
@@ -28,6 +31,9 @@ def autogpt_task(self, task):
2831
except openai.error.AuthenticationError as e:
2932
logging.error(f"API key error during autogpt_task: {e}")
3033
return "API key error"
34+
except ValueError as e:
35+
logging.error(f"ValueError during autogpt_task: {e}")
36+
return "ValueError: Missing API key"
3137
except Exception as e:
3238
logging.error(f"Error during autogpt_task: {e}")
3339
return ""

core/email_server/EmailServer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
SERVER_HOST = '0.0.0.0'
1515
SERVER_PORT = 1234
1616
saveMail_directory = os.getenv("SAVE_MAIL_DIRECTORY", "FlowSteering/ApplicationCode/EmailServer/EmailServerMailDatabase") # Change this to the directory where you want to save the emails inbox for each user
17+
if not saveMail_directory:
18+
raise ValueError("SAVE_MAIL_DIRECTORY environment variable is not set.")
1719
message_queue = Queue()
1820
default_image = 'FlowSteering/assets/PerturbatedImages/DjiPerturbClassForward.png'
1921
# Server configuration

core/end_user/EndUserClient.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
BaseEmails_directory = os.getenv("BASE_EMAILS_DIRECTORY")
2828
default_image = os.getenv("DEFAULT_IMAGE", '')
2929

30+
if not BaseEmails_directory:
31+
raise ValueError("BASE_EMAILS_DIRECTORY environment variable is not set.")
32+
3033

3134
def receive_complete_data(client_socket): # this function is used to receive the complete data from the client, adjust the parameters as needed based on your network conditions
3235
received_data = b""

database/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy.ext.declarative import declarative_base
33
from sqlalchemy.orm import sessionmaker
44
import logging
5+
import os
56

67
Base = declarative_base()
78

@@ -13,7 +14,9 @@ class DocumentAnalysis(Base):
1314
links = Column(Text, nullable=True)
1415
error = Column(Text, nullable=True)
1516

16-
DATABASE_URL = "sqlite:///document_analysis.db"
17+
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///document_analysis.db")
18+
if not DATABASE_URL:
19+
raise ValueError("DATABASE_URL environment variable is not set.")
1720
engine = create_engine(DATABASE_URL)
1821
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
1922
Base.metadata.create_all(bind=engine)

0 commit comments

Comments
 (0)