Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
seonghobae marked this conversation as resolved.
Resolved
10 changes: 9 additions & 1 deletion backend/api/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 23 additions & 9 deletions backend/tests/test_emails_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<parent@example.com>\rBcc: attacker@example.com"),
("in_reply_to", "<parent@example.com>\nBcc: attacker@example.com"),
("references", "<root@example.com>\rBcc: attacker@example.com"),
("references", "<root@example.com>\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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

assert response.status_code == 422
mock_send_email.assert_not_called()
Expand Down
Loading