## 2024-05-18 - Prevent Path Traversal in Flat-File Storage
**Vulnerability:** The application accesses tasks, knowledge base files, and logs dynamically across `app.py`, `src/satya/core/storage.py`, and `src/satya/sdk/client.py`. These components constructed file paths via `os.path.join()` using unvalidated strings (like `filename`, `agent_name`, or `task_id`) directly, which allowed malicious actors or malfunctioning agents to traverse directories (e.g., passing `../../etc/passwd` to delete or read unauthorized files).
**Learning:** In a zero-database (flat-file) architecture, all IDs and names directly translate to file system paths. Because these flat directories are used exclusively without deep nesting, we can securely isolate file access using `os.path.basename()`. Additionally, we learned that the `shell=True` parameter in `src/satya/core/completion.py` is intentionally kept to support command shell operators in test tasks, and replacing it with `shell=False` causes test regressions.
**Prevention:** Always sanitize any filename or task ID with `os.path.basename()` before passing it to `os.path.join()` when interacting with storage directories (`TRUTH_DIR`, `TASKS_DIR`, `AGENTS_DIR`).
## 2024-05-18 - XSS and DoS Prevention in Streamlit Output Rendering
**Vulnerability:** `format_date` and `format_time_ago` functions in `app.py` were returning unescaped original strings in their exception handling blocks when parsing invalid ISO timestamp strings. Similarly, the activity log rendering crashed when attempting to parse invalid ISO timestamp strings. These unescaped values were subsequently injected into HTML components with `unsafe_allow_html=True` using `st.markdown`, posing Cross-Site Scripting (XSS) risks. The unhandled exception in activity log rendering also resulted in a Denial of Service (DoS) where the entire Streamlit page crashed and could not be rendered.
**Learning:** Any user-provided data output implicitly or explicitly to a frontend web page must be sanitized, especially when dealing with potential failure paths like parsing invalid input strings. The `st.markdown(..., unsafe_allow_html=True)` acts as a direct sink for unsanitized data, so all data flowing into it must be verified to be HTML escaped. Also, failure to parse strings in rendering loops can lead to fatal exceptions, crashing the dashboard entirely.
**Prevention:** All formatted outputs from date utilities that might fail to parse user data should fallback to `html.escape(str(invalid_input))`. Critical rendering blocks processing potentially malformed data (like timestamps from external agents) should use try-except blocks, falling back to an escaped string, to guarantee graceful and secure degradation instead of an unhandled `ValueError`.

## 2024-05-24 - Server-Side Request Forgery (SSRF) via Open Redirects and Link-Local Addresses
**Vulnerability:** The web scraper component was vulnerable to Server-Side Request Forgery (SSRF) because it didn't validate the resolved IPs of requested URLs and blindly followed redirects. This could allow an attacker to bypass initial URL validation using open redirects to access internal services or cloud metadata endpoints (e.g., AWS metadata at `169.254.169.254`).
**Learning:** Initial URL validation is insufficient if the HTTP client (like `requests`) automatically follows redirects to unsafe locations. Additionally, relying solely on `ip_address(ip).is_private` is dangerous because it does not cover link-local addresses (like `169.254.x.x`), which are commonly used for sensitive cloud metadata services. Furthermore, attempting to prevent Time-of-Check to Time-of-Use (TOCTOU) DNS Rebinding by constructing IP-based URLs and adding a custom `Host` header breaks TLS verification (SNI) for HTTPS in `requests` because the underlying `urllib3` validates the SSL certificate against the IP rather than the hostname.
**Prevention:** Always manually handle redirects (`allow_redirects=False`) up to a strict limit, and validate the URL and its resolved IP at *every* redirect hop. Use `ipaddress.ip_address(ip_str).is_global` to strictly ensure the IP is publicly routable, explicitly blocking both private and link-local addresses. Resolve relative redirect locations securely using `urllib.parse.urljoin(current_url, next_url)`. To maintain TLS compatibility in basic HTTP clients, one must often accept the minor TOCTOU risk and request the original hostname after validation, unless relying on more complex custom HTTP adapters.
## 2025-04-19 - Timing Attack Vulnerability in Authentication
**Vulnerability:** String comparison operators (==, !=, in) were used to check secrets and HMAC signatures in `is_agent_authorized`, `is_human_authorized`, and `verify_event_chain`. This allows timing attacks to guess secrets character by character.
**Learning:** Basic string comparisons exit early as soon as a mismatch is found, creating observable timing differences based on the length of the matching prefix.
**Prevention:** Always use `hmac.compare_digest` with explicit string casts (to handle `None`) when comparing secrets, API keys, or cryptographic signatures to ensure constant-time comparison.

## 2024-05-25 - Prevent Path Traversal in Chat Directory
**Vulnerability:** The application used unvalidated strings (`selected_agent` from the UI and `self.agent_name` in the SDK) to construct paths for reading and writing agent chat messages via `os.path.join()`. This created a path traversal vulnerability where a malicious agent could potentially access or overwrite files outside the intended chat directory by using path separators in their name.
**Learning:** Even though agent names are typically safe, any unvalidated input used in file path construction, especially across both server (`app.py`) and client (`client.py`) boundaries, poses a path traversal risk.
**Prevention:** Always sanitize agent names (and similar dynamic identifiers) using `os.path.basename()` before passing them to `os.path.join()`, ensuring the resulting path is constrained to the intended directory.
