Skip to content

Commit

Permalink
test: Add test case to reproduce bug abhinavsingh#1267
Browse files Browse the repository at this point in the history
  • Loading branch information
dongfangtianyu committed Aug 12, 2023
1 parent 30574fd commit e86a2be
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/plugin/test_http_proxy_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
import base64
import gzip
import json
import selectors
Expand All @@ -30,6 +31,7 @@
from proxy.common.utils import bytes_, build_http_request, build_http_response
from proxy.http.responses import (
NOT_FOUND_RESPONSE_PKT, PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT,
PROXY_AUTH_FAILED_RESPONSE_PKT,
)
from proxy.common.constants import DEFAULT_HTTP_PORT, PROXY_AGENT_HEADER_VALUE
from .utils import get_plugin_by_test_name
Expand Down Expand Up @@ -552,3 +554,73 @@ async def test_shortlink_plugin_external(self) -> None:
),
)
self.assertFalse(self.protocol_handler.work.has_buffer())

@pytest.mark.asyncio # type: ignore[misc]
@pytest.mark.parametrize(
"_setUp",
(
('test_auth_plugin'),
),
indirect=True,
) # type: ignore[misc]
async def test_auth_plugin(self) -> None:
self.flags.auth_code = base64.b64encode(bytes_("admin:123456"))

request = b'\r\n'.join([
b'GET http://www.facebook.com/tr/ HTTP/1.1',
b'Host: www.facebook.com',
b'User-Agent: proxy.py v2.4.4rc5.dev3+g95b646a.d20230811',
b'',
b'',
])

self._conn.recv.return_value = request
self.mock_selector.return_value.select.side_effect = [
[(
selectors.SelectorKey(
fileobj=self._conn.fileno(),
fd=self._conn.fileno(),
events=selectors.EVENT_READ,
data=None,
),
selectors.EVENT_READ,
)],
]
await self.protocol_handler._run_once()
self.assertEqual(
self.protocol_handler.work.buffer[0],
PROXY_AUTH_FAILED_RESPONSE_PKT,
)

@pytest.mark.asyncio # type: ignore[misc]
@pytest.mark.parametrize(
"_setUp",
(
('test_auth_plugin'),
),
indirect=True,
) # type: ignore[misc]
async def test_auth_plugin_bypass(self) -> None:
self.flags.auth_code = base64.b64encode(bytes_("admin:123456"))

# miss requests header when https and HTTP 1.0
request = b'CONNECT www.facebook.com:443 HTTP/1.0\r\n\r\n'

self._conn.recv.return_value = request
self.mock_selector.return_value.select.side_effect = [
[(
selectors.SelectorKey(
fileobj=self._conn.fileno(),
fd=self._conn.fileno(),
events=selectors.EVENT_READ,
data=None,
),
selectors.EVENT_READ,
)],
]
await self.protocol_handler._run_once()

self.assertEqual(
self.protocol_handler.work.buffer[0],
PROXY_AUTH_FAILED_RESPONSE_PKT,
)
3 changes: 3 additions & 0 deletions tests/plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
FilterByUpstreamHostPlugin, RedirectToCustomServerPlugin,
)
from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.proxy.auth import AuthPlugin


def get_plugin_by_test_name(test_name: str) -> Type[HttpProxyBasePlugin]:
Expand All @@ -36,4 +37,6 @@ def get_plugin_by_test_name(test_name: str) -> Type[HttpProxyBasePlugin]:
plugin = FilterByURLRegexPlugin
elif test_name == 'test_shortlink_plugin':
plugin = ShortLinkPlugin
elif test_name == 'test_auth_plugin':
plugin = AuthPlugin
return plugin

0 comments on commit e86a2be

Please sign in to comment.