Skip to content

Commit 1226e20

Browse files
committed
Merge branch 'main' of github.com:msoedov/agentic_security
2 parents c2671fd + c94aa54 commit 1226e20

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

agentic_security/http_spec.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
from enum import Enum
33

4+
from urllib.parse import urlparse
45
import httpx
56
from pydantic import BaseModel
67

@@ -159,6 +160,12 @@ def parse_http_spec(http_spec: str) -> LLMSpec:
159160
# Extract the method and URL from the first line
160161
method, url = lines[0].split(" ")[0:2]
161162

163+
# Check url validity
164+
valid_url= urlparse(url)
165+
# if missing the correct formatting ://, urlparse.netloc will be empty
166+
if valid_url.scheme not in ("http", "https") or not valid_url.netloc:
167+
raise InvalidHTTPSpecError(f"Invalid URL: {url}. Ensure it starts with 'http://' or 'https://'")
168+
162169
# Initialize headers and body
163170
headers = {}
164171
body = ""

agentic_security/probe_data/audio_generator.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import logging
23
import os
34
import platform
45
import subprocess
@@ -7,6 +8,16 @@
78
import httpx
89
from cache_to_disk import cache_to_disk
910

11+
# Configure logging
12+
logging.basicConfig(level=logging.INFO)
13+
logger = logging.getLogger(__name__)
14+
15+
16+
class AudioGenerationError(Exception):
17+
"""Custom exception for errors during audio generation."""
18+
19+
pass
20+
1021

1122
def encode(content: bytes) -> str:
1223
encoded_content = base64.b64encode(content).decode("utf-8")
@@ -41,12 +52,25 @@ def generate_audio_mac_wav(prompt: str) -> bytes:
4152
# Read the WAV file into memory
4253
with open(temp_wav_path, "rb") as f:
4354
audio_bytes = f.read()
55+
56+
except subprocess.CalledProcessError as e:
57+
logger.error(f"Subprocess error: {e}")
58+
raise AudioGenerationError("Failed to generate or convert audio.") from e
59+
except FileNotFoundError as e:
60+
logger.error(f"File not found: {e}")
61+
raise AudioGenerationError("Required file not found.") from e
62+
except Exception as e:
63+
logger.exception("Unexpected error occurred.")
64+
raise AudioGenerationError(
65+
"An unexpected error occurred during audio generation."
66+
) from e
4467
finally:
45-
# Clean up the temporary files
46-
if os.path.exists(temp_aiff_path):
47-
os.remove(temp_aiff_path)
48-
if os.path.exists(temp_wav_path):
49-
os.remove(temp_wav_path)
68+
for path in (temp_aiff_path, temp_wav_path):
69+
try:
70+
if os.path.exists(path):
71+
os.remove(path)
72+
except Exception as e:
73+
logger.warning(f"Failed to delete temporary file {path}: {e}")
5074

5175
# Return the audio bytes
5276
return audio_bytes

0 commit comments

Comments
 (0)