Skip to content

Commit

Permalink
[client] Handle invalid proxy settings
Browse files Browse the repository at this point in the history
If `HTTP_PROXY` / `HTTPS_PROXY` environment variables are set and the
URL is invalid (e.g.: it doesn't contains a schema like localhost:8001)
the client will throw the following exception:
```
'NoneType' object has no attribute 'rfind'
  File "/codechecker/build/CodeChecker/cc_bin/CodeChecker.py", line 174, in <module>
    main(commands)
  File "/codechecker/build/CodeChecker/cc_bin/CodeChecker.py", line 141, in main
    sys.exit(args.func(args))
  File "//codechecker/build/CodeChecker/lib/python3/codechecker_client/cmd/store.py", line 856, in main
    traceback.print_stack()
```
From this error message it is hard to understand the real problem.

Unfortunately Thrift does not handle the use case when invalid proxy format
is used (e.g.: no schema is specified). For this reason we need to verify
the proxy format in our side.
  • Loading branch information
csordasmarton committed Mar 4, 2021
1 parent ef1b0b8 commit 804cefd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
36 changes: 36 additions & 0 deletions web/client/codechecker_client/thrift_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@

LOG = get_logger('system')

PROXY_VALIDATION_NEEDED = True


def proxy_settings_are_valid(transport):
"""
Return True if no proxy settings are used or the proxy format it valid.
"""
if not transport.using_proxy():
return True

return transport.host and isinstance(transport.port, int)


def validate_proxy_format(transport):
"""
It will check the proxy settings once and validate the proxy settings.
If the proxy settings are invalid, it will print an error message and
stop the program.
"""
global PROXY_VALIDATION_NEEDED

if PROXY_VALIDATION_NEEDED:
if not proxy_settings_are_valid(transport):
LOG.error("Invalid proxy format! Check your "
"HTTP_PROXY/HTTPS_PROXY environment variables if "
"these are in the right format:"
"'http[s]://host:port'.")
sys.exit(1)

PROXY_VALIDATION_NEEDED = False


def truncate_arg(arg, max_len=100):
""" Truncate the given argument if the length is too large. """
Expand All @@ -38,6 +69,11 @@ def ThriftClientCall(function):
funcName = function.__name__

def wrapper(self, *args, **kwargs):
# Thrift do not handle the use case when invalid proxy format is used
# (e.g.: no schema is specified). For this reason we need to verify
# the proxy format in our side.
validate_proxy_format(self.transport)

self.transport.open()
func = getattr(self.client, funcName)
try:
Expand Down
26 changes: 24 additions & 2 deletions web/tests/functional/cmdline/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def setUp(self):
test_class = self.__class__.__name__
print('Running ' + test_class + ' tests in ' + test_workspace)

codechecker_cfg = env.import_test_cfg(test_workspace)[
self.codechecker_cfg = env.import_test_cfg(test_workspace)[
'codechecker_cfg']
self.server_url = env.parts_to_url(codechecker_cfg)
self.server_url = env.parts_to_url(self.codechecker_cfg)

# Get the test project configuration from the prepared test workspace.
self._testproject_data = env.setup_test_proj_cfg(test_workspace)
Expand Down Expand Up @@ -123,6 +123,28 @@ def test_runs_filter(self):
self.assertEqual(0, ret)
self.assertEqual(1, len(json.loads(res)))

def test_proxy_settings(self):
""" Test proxy settings validation. """
server_url = f"{self.codechecker_cfg['viewer_host']}:" \
f"{str(self.codechecker_cfg['viewer_port'])}"

env = self.codechecker_cfg['check_env'].copy()
env['HTTP_PROXY'] = server_url

res_cmd = [self._codechecker_cmd, 'cmd', 'runs',
'--url', str(self.server_url)]
ret, _, err = run_cmd(res_cmd, env=env)
self.assertEqual(1, ret)
self.assertIn("Invalid proxy format", err)

env['HTTP_PROXY'] = f"http://{server_url}"
_, _, err = run_cmd(res_cmd, env=env)

# We can't check the return code here, because on host machine it will
# be zero, but on the GitHub action's job it will be 1 with "Failed to
# connect to the server" error message.
self.assertNotIn("Invalid proxy format", err)

def test_runs_row(self):
""" Test cmd row output type. """
env = self._test_config['codechecker_cfg']['check_env']
Expand Down

0 comments on commit 804cefd

Please sign in to comment.