Skip to content
Closed
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
5 changes: 4 additions & 1 deletion pymisp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def __init__(self, url: str, key: str, ssl: bool | str = True, debug: bool = Fal
raise NoKey('Please provide your authorization key.')

self.root_url: str = url
# Warn if using HTTP instead of HTTPS
if self.root_url.startswith('http://'):
logger.warning('Using HTTP instead of HTTPS for MISP connection. This may cause redirect issues. Consider using HTTPS.')
self.key: str = key.strip()
self.ssl: bool | str = ssl
self.proxies: MutableMapping[str, str] | None = proxies
Expand Down Expand Up @@ -4136,7 +4139,7 @@ def _prepare_request(self, request_type: str, url: str, data: Iterable[Any] | Ma
logger.debug(prepped.headers)
settings = self.__session.merge_environment_settings(req.url, proxies=self.proxies or {}, stream=None,
verify=self.ssl, cert=self.cert)
return self.__session.send(prepped, timeout=self.timeout, **settings)
return self.__session.send(prepped, timeout=self.timeout, allow_redirects=True, **settings)

def _csv_to_dict(self, csv_content: str) -> list[dict[str, Any]]:
'''Makes a list of dict out of a csv file (requires headers)'''
Expand Down
80 changes: 80 additions & 0 deletions tests/test_api_redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python

from __future__ import annotations

import unittest
from unittest.mock import patch, MagicMock
import logging

from pymisp import PyMISP


class TestAPIRedirect(unittest.TestCase):
"""Test that API properly handles HTTP to HTTPS redirects and warnings"""

def setUp(self) -> None:
self.maxDiff = None

def test_http_url_warning(self) -> None:
"""Test that using HTTP URL generates a warning"""
# Test that the warning logic itself is correct
root_url = 'http://misp.local/'
logger = logging.getLogger('pymisp')

with self.assertLogs('pymisp', level='WARNING') as cm:
# Simulate the warning code from __init__
if root_url.startswith('http://'):
logger.warning('Using HTTP instead of HTTPS for MISP connection. This may cause redirect issues. Consider using HTTPS.')

self.assertTrue(any('HTTP instead of HTTPS' in message for message in cm.output))

def test_https_url_no_warning(self) -> None:
"""Test that using HTTPS URL does not generate a warning"""
# Test that HTTPS doesn't trigger the warning
root_url = 'https://misp.local/'
logger = logging.getLogger('pymisp')

# Manually check - no warning should be logged for HTTPS
with self.assertLogs('pymisp', level='DEBUG') as cm:
logger.debug('test message')
# Simulate the warning code from __init__
if root_url.startswith('http://'):
logger.warning('Using HTTP instead of HTTPS for MISP connection. This may cause redirect issues. Consider using HTTPS.')

# Verify no HTTP warning was logged
self.assertFalse(any('HTTP instead of HTTPS' in message for message in cm.output))

def test_allow_redirects_in_prepare_request(self) -> None:
"""Test that _prepare_request passes allow_redirects=True to session.send"""
# Create a minimal API instance
api = PyMISP.__new__(PyMISP)
api.root_url = 'https://misp.local/'
api.ssl = True
api.proxies = None
api.cert = None
api.auth = None
api.timeout = None

# Mock the session
mock_session = MagicMock()
mock_prepped = MagicMock()
mock_prepped.headers = {}
mock_session.prepare_request.return_value = mock_prepped
mock_session.merge_environment_settings.return_value = {}
mock_response = MagicMock()
mock_session.send.return_value = mock_response

api._PyMISP__session = mock_session

# Call _prepare_request
api._prepare_request('GET', 'events')

# Verify that session.send was called with allow_redirects=True
mock_session.send.assert_called_once()
call_args = mock_session.send.call_args
self.assertIn('allow_redirects', call_args.kwargs)
self.assertTrue(call_args.kwargs['allow_redirects'])


if __name__ == '__main__':
unittest.main()
Loading