From 05f5de40eb05e571b4d8d5a62568b76de790a100 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Mon, 10 Aug 2026 14:21:57 +0000 Subject: [PATCH 1/4] Fix CRLF injection in email headers --- .jules/sentinel.md | 4 ++++ backend/api/emails.py | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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..116eda61d 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("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( From e7fd1b994f7be7c43494aca6cf92b4fc64285dfc Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Tue, 11 Aug 2026 16:00:30 +0900 Subject: [PATCH 2/4] test(email): cover CRLF injection across header fields --- backend/tests/test_emails_api.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/backend/tests/test_emails_api.py b/backend/tests/test_emails_api.py index 6149576c7..3833e954c 100644 --- a/backend/tests/test_emails_api.py +++ b/backend/tests/test_emails_api.py @@ -1819,21 +1819,33 @@ 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"), + ], +) @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() From 6059aa56ca7c3a5ea8b1dedf5ad4d22ec82be886 Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Tue, 11 Aug 2026 16:12:59 +0900 Subject: [PATCH 3/4] test(email): cover recipient header injection --- backend/tests/test_emails_api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/tests/test_emails_api.py b/backend/tests/test_emails_api.py index 3833e954c..7bffa6ff7 100644 --- a/backend/tests/test_emails_api.py +++ b/backend/tests/test_emails_api.py @@ -1828,6 +1828,8 @@ def fake_validate_smtp_destination(smtp_server, smtp_port, *, resolve_host=True) ("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}) From 0b41c9677d33a5cfe73ad9062c9124b8ee8fa90b Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Tue, 11 Aug 2026 16:13:20 +0900 Subject: [PATCH 4/4] fix(email): reject recipient CRLF at API boundary --- backend/api/emails.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/api/emails.py b/backend/api/emails.py index 116eda61d..2b0a9dbd6 100644 --- a/backend/api/emails.py +++ b/backend/api/emails.py @@ -693,7 +693,7 @@ class SendEmailRequest(BaseModel): in_reply_to: str | None = None # O3: email threading support references: str | None = None - @field_validator("subject", "in_reply_to", "references", mode="before") + @field_validator("to", "subject", "in_reply_to", "references", mode="before") @classmethod def reject_crlf(cls, v: str | None) -> str | None: if isinstance(v, str):