Skip to content

Commit 6e0711e

Browse files
committed
added fixes for comments batch 1
1 parent 0c915c2 commit 6e0711e

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

web/server/codechecker_server/session_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ def __oauth_apply_templates(self):
323323
if param == 'callback_url':
324324
if not self.check_callback_url_format(
325325
provider_name, provider[param]):
326-
LOG.warning("Disabling OAuth "
327-
f"provider {provider_name} "
328-
"due to invalid callback URL format.")
326+
LOG.error("Disabling OAuth "
327+
f"provider {provider_name} "
328+
"due to invalid callback URL format.")
329329
provider['enabled'] = False
330330
except KeyError as e:
331331
LOG.warning(f"Parameter {param} in OAuth provider "
@@ -389,8 +389,8 @@ def get_oauth_config(self, provider):
389389
self.__auth_config["method_oauth"]["providers"][provider][
390390
"enabled"] = False
391391

392-
LOG.warning("OAuth configuration was set to default values. " +
393-
"Disabling oauth provider: %s", provider)
392+
LOG.error("OAuth configuration was set to default values. " +
393+
"Disabling oauth provider: %s", provider)
394394

395395
return self.__auth_config.get(
396396
'method_oauth', {}).get("providers", {}).get(provider, {})

web/tests/functional/authentication/oauth_server.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
# pylint: disable=invalid-name
1+
#
2+
# -------------------------------------------------------------------------
3+
#
4+
# Part of the CodeChecker project, under the Apache License v2.0 with
5+
# LLVM Exceptions. See LICENSE for license information.
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
#
8+
# -------------------------------------------------------------------------
9+
"""
10+
A mock OAuth server that simulates the behavior of an OAuth provider.
11+
"""
212

3-
from http.server import BaseHTTPRequestHandler, HTTPServer
413
import json
514
import os
15+
616
from authlib.oauth2.rfc7636 import create_s256_code_challenge as hash_s256
717

18+
from http.server import BaseHTTPRequestHandler, HTTPServer
19+
820
# Server config
921
HOSTNAME = "0.0.0.0"
1022
SERVERPORT = int(os.getenv("PORT")) if os.getenv("PORT") else 3000
@@ -116,24 +128,10 @@ def login_tester(self):
116128
params[key] = value
117129

118130
if "username" in query_params:
131+
# print(f"Login request with username: {params['username']}")
119132
query = f"{params['username']}:{params['password']}"
120133
query_result = self.users_by_data.get(query, None)
121-
# csrf attack case
122-
if params['username'] == "user_csrf":
123-
print("CSRF attack detected")
124-
state = "fake_state"
125-
code = query_result['code']
126-
code_challenge = params['code_challenge']
127-
# store code_challenge in the server
128-
self.code_challenges[code] = {
129-
"code_challenge": code_challenge,
130-
"code_challenge_method": params[
131-
'code_challenge_method']}
132-
133-
return self.show_json({"code": code,
134-
"state": state})
135-
# normal case
136-
elif query_result:
134+
if query_result:
137135
state = params['state']
138136
code = query_result['code']
139137
code_challenge = params['code_challenge']
@@ -149,7 +147,7 @@ def login_tester(self):
149147
except IndexError:
150148
return self.show_rejection("Invalid query parameters")
151149
except Exception as ex:
152-
print(f"Error: {ex}")
150+
print(f"Error in login_tester of OAuth mock server: {ex}")
153151
return self.show_rejection("Internal server error")
154152

155153
def get_user(self):
@@ -204,13 +202,15 @@ def handle_user_token_request(self):
204202
return self.show_rejection("Invalid code")
205203
return self.path
206204

205+
# pylint: disable=invalid-name
207206
def do_GET(self):
208207
if self.path.startswith("/login"):
209208
return self.login_tester()
210209
elif self.path.startswith("/get_user"):
211210
return self.get_user()
212211
return self.path
213212

213+
# pylint: disable=invalid-name
214214
def do_POST(self):
215215
if self.path.endswith("/token"):
216216
return self.handle_user_token_request()
@@ -220,8 +220,8 @@ def do_POST(self):
220220

221221
webServer = HTTPServer((HOSTNAME, SERVERPORT), OauthServer)
222222
webServer.allow_reuse_address = True
223-
print(f"Server started http://{HOSTNAME}:{SERVERPORT}")
223+
# print(f"OAuth mock server started on http://{HOSTNAME}:{SERVERPORT}")
224224

225225
webServer.serve_forever()
226226
webServer.server_close()
227-
print("Server stopped.")
227+
print(f"OAuth mock server stopped on http://{HOSTNAME}:{SERVERPORT}")

web/tests/functional/authentication/test_authentication.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ def try_login(self, provider, username, password):
225225
link = link.split('?')[0]
226226

227227
code, state = data['code'], data['state']
228+
229+
# CSRF attack case
230+
if username == "user_csrf":
231+
state = "FAKESTATE"
232+
228233
auth_string = f"{link}?code={code}&state={state}"
229234

230235
# PKCE attack case
@@ -295,15 +300,28 @@ def test_oauth_create_link(self):
295300
Tests functionality of create_link method
296301
that checks if it creates unique links correctly.
297302
"""
303+
304+
from urllib.parse import urlparse, parse_qs
305+
298306
auth_client = env.setup_auth_client(
299307
self._test_workspace, session_token='_PROHIBIT')
308+
session_factory = env.create_sqlalchemy_session(self._test_workspace)
300309

310+
# check 1
301311
link_github = auth_client.createLink("github")
302-
link_google = auth_client.createLink("google")
303-
312+
parsed_query = parse_qs(urlparse(link_github).query)
313+
state = parsed_query.get("state")[0]
314+
result = env.validate_oauth_session(session_factory, state)
304315
self.assertIsNotNone(link_github,
305316
"Authorization link for Github created empty")
317+
self.assertTrue(result, "create link wasn't seccesfully executed")
306318

319+
# check 2
320+
link_google = auth_client.createLink("google")
321+
parsed_query = parse_qs(urlparse(link_google).query)
322+
state = parsed_query.get("state")[0]
323+
result = env.validate_oauth_session(session_factory, state)
324+
self.assertTrue(result, "create link wasn't seccesfully executed")
307325
self.assertIsNotNone(link_google,
308326
"Authorization link for Google created empty")
309327

0 commit comments

Comments
 (0)