diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 3f3dd68ba..6f502e1c7 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -129,3 +129,7 @@ **Vulnerability:** The URL validation logic correctly blocked non-global IP addresses and `localhost`, but failed to block internal domain extensions such as `.internal` or `.local` (or exact matches for `internal`). This could allow attackers to bypass SSRF protections by resolving these internal top-level domains. **Learning:** Checking for `localhost` alone is insufficient to prevent SSRF against internal network resources, as modern environments and protocols utilize `.internal` and `.local` domains for internal routing. **Prevention:** Always explicitly check and block domains matching `.internal`, `.local`, or `internal` (alongside `localhost`) when validating URLs for global reachability to prevent SSRF bypasses. +## 2025-02-23 - CRLF Injection in Email Headers +**Vulnerability:** The `in_reply_to` and `references` fields on the `SendEmailRequest` model lacked explicit validation, opening up an opportunity for header injection by appending `\r\n`. +**Learning:** While the email service internally checks some headers, relying on the API boundary's Pydantic model ensures bad input is stopped early and consistently. Pydantic regex patterns aren't sufficient on their own for all string contexts due to encoding/decoding inconsistencies. +**Prevention:** Always use `@field_validator` with explicit `mode="before"` string matching to reject `chr(10)` and `chr(13)` across all user-controlled email header fields. Use `isinstance(value, str)` before string operations to prevent runtime errors if input is missing or malformed. diff --git a/backend/api/emails.py b/backend/api/emails.py index 5cfa77a77..2b0a9dbd6 100644 --- a/backend/api/emails.py +++ b/backend/api/emails.py @@ -5,7 +5,7 @@ from sqlalchemy import func, or_, select from db.session import get_db from db.models import Email -from pydantic import BaseModel, EmailStr, Field +from pydantic import BaseModel, EmailStr, Field, field_validator import datetime import time from typing import Literal @@ -693,6 +693,14 @@ class SendEmailRequest(BaseModel): in_reply_to: str | None = None # O3: email threading support references: str | None = None + @field_validator("to", "subject", "in_reply_to", "references", mode="before") + @classmethod + def reject_crlf(cls, v: str | None) -> str | None: + if isinstance(v, str): + if chr(10) in v or chr(13) in v: + raise ValueError("CR/LF injection detected") + return v + @router.post("/send") async def send_email_endpoint( diff --git a/backend/tests/test_emails_api.py b/backend/tests/test_emails_api.py index 6149576c7..7bffa6ff7 100644 --- a/backend/tests/test_emails_api.py +++ b/backend/tests/test_emails_api.py @@ -1819,21 +1819,35 @@ def fake_validate_smtp_destination(smtp_server, smtp_port, *, resolve_host=True) ) +@pytest.mark.parametrize( + ("header_field", "header_value"), + [ + ("subject", "Quarter plan\rBcc: attacker@example.com"), + ("subject", "Quarter plan\nBcc: attacker@example.com"), + ("in_reply_to", "\rBcc: attacker@example.com"), + ("in_reply_to", "\nBcc: attacker@example.com"), + ("references", "\rBcc: attacker@example.com"), + ("references", "\nBcc: attacker@example.com"), + ("to", "victim@example.com\rBcc: attacker@example.com"), + ("to", "victim@example.com\nBcc: attacker@example.com"), + ], +) @patch("api.emails.send_email", return_value={"status": "simulated", "simulated": True}) -def test_send_email_endpoint_rejects_header_injection_subject(mock_send_email): +def test_send_email_endpoint_rejects_header_injection( + mock_send_email, header_field, header_value +): from fastapi.testclient import TestClient from main import app client = TestClient(app, headers={"X-User-Id": "testuser"}) + payload = { + "to": "test@example.com", + "subject": "Quarter plan", + "body": "This is a reply.", + } + payload[header_field] = header_value - response = client.post( - "/api/emails/send", - json={ - "to": "test@example.com", - "subject": "Re: Test\r\nBcc: attacker@example.com", - "body": "This is a reply.", - }, - ) + response = client.post("/api/emails/send", json=payload) assert response.status_code == 422 mock_send_email.assert_not_called()