Skip to content

Commit 4231bee

Browse files
committed
fix tests (after ssrf merge)
1 parent 220e111 commit 4231bee

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

src/backend/core/tests/api/test_messages_import.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint: disable=redefined-outer-name, unused-argument, no-value-for-parameter, too-many-lines
33

44
import datetime
5-
import socket
65
from unittest.mock import patch
76

87
from django.core.files.storage import storages
@@ -23,18 +22,18 @@
2322

2423
@pytest.fixture(autouse=True)
2524
def _mock_ssrf_dns():
26-
"""Short-circuit SSRF DNS validation for IMAP import tests.
25+
"""Short-circuit SSRF hostname validation for IMAP import tests.
2726
2827
The IMAP endpoint validates the server hostname via
2928
``core.services.ssrf.validate_hostname``; tests use unresolvable fixtures
30-
like ``imap.example.com`` so we return a public IP to reach the mocked
31-
IMAP task code.
29+
like ``imap.example.com`` so we bypass validation at its call site.
30+
Patching ``validate_hostname`` (rather than ``socket.getaddrinfo``) keeps
31+
the mock scoped to SSRF checks and avoids redirecting unrelated DNS
32+
lookups (e.g. to the S3 test fixture backend).
3233
"""
3334
with patch(
34-
"core.services.ssrf.socket.getaddrinfo",
35-
return_value=[
36-
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))
37-
],
35+
"core.services.importer.imap.validate_hostname",
36+
return_value=["93.184.216.34"],
3837
):
3938
yield
4039

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
"""Shared fixtures for importer tests."""
22

3-
import socket
43
from unittest import mock
54

65
import pytest
76

87

98
@pytest.fixture(autouse=True)
109
def _mock_ssrf_dns():
11-
"""Short-circuit SSRF DNS validation for IMAP tests.
10+
"""Short-circuit SSRF hostname validation for IMAP tests.
1211
1312
The IMAP import path validates the server hostname via
14-
``core.services.ssrf.validate_hostname`` which calls ``socket.getaddrinfo``.
15-
Test fixtures use unresolvable hostnames like ``imap.example.com``, so we
16-
return a valid public IP here to let tests reach the mocked IMAP code.
13+
``core.services.ssrf.validate_hostname``. Test fixtures use unresolvable
14+
hostnames like ``imap.example.com``, so we bypass validation to let tests
15+
reach the mocked IMAP code. Patching ``validate_hostname`` at its call
16+
site (rather than ``socket.getaddrinfo``) keeps the mock scoped to SSRF
17+
checks and avoids redirecting unrelated DNS lookups (e.g. to the S3 test
18+
fixture backend).
1719
"""
1820
with mock.patch(
19-
"core.services.ssrf.socket.getaddrinfo",
20-
return_value=[
21-
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))
22-
],
21+
"core.services.importer.imap.validate_hostname",
22+
return_value=["93.184.216.34"],
2323
):
2424
yield

0 commit comments

Comments
 (0)