Skip to content

Commit a0f277b

Browse files
Merge pull request #4930 from linuxfoundation/unicron-address-codeql-and-dependabot-vulns-2
Fix more vulns
2 parents 63c3355 + 0f5ce5d commit a0f277b

8 files changed

Lines changed: 1767 additions & 2275 deletions

File tree

cla-backend/cla/models/github_models.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def clear_caches():
101101
cla.log.info(f"{fn} - cleared github_user_cache")
102102
return {"status": "OK"}
103103
except Exception as e:
104-
cla.log.error(f"{fn} - error clearing caches: {e}")
105-
return {"status": f"Error clearing caches: {e}"}
104+
cla.log.error(f"{fn} - error clearing caches", exc_info=True)
105+
return {"status": "Error clearing caches"}
106106

107107
@dataclass
108108
class CommitLite:
@@ -213,11 +213,11 @@ def user_from_session(self, request, get_redirect_url):
213213
fn = "github_models.user_from_session"
214214
cla.log.debug(f"{fn} - loading session from request")
215215
session = self._get_request_session(request)
216-
cla.log.debug(f"{fn} - session loaded (keys={list(session.keys())})")
216+
cla.log.debug(f"{fn} - session loaded")
217217

218218
# We can already have token in the session
219219
if "github_oauth2_token" in session:
220-
cla.log.debug(f"{fn} - using existing session GitHub OAuth2 token")
220+
cla.log.debug(f"{fn} - using existing session GitHub OAuth2 authentication")
221221
user = self.get_or_create_user(request)
222222
if user is None:
223223
cla.log.debug(f"{fn} - cannot find user, returning HTTP 404 status")
@@ -226,7 +226,7 @@ def user_from_session(self, request, get_redirect_url):
226226
return user
227227

228228
authorization_url, csrf_token = self.get_authorization_url_and_state(None, None, None, ["user:email"], state='user-from-session')
229-
cla.log.debug(f"{fn} - obtained GitHub OAuth2 state from authorization - storing CSRF token in the session...")
229+
cla.log.debug(f"{fn} - obtained GitHub OAuth2 state from authorization - storing state in the session")
230230
session["github_oauth2_state"] = csrf_token
231231
cla.log.debug(f"{fn} - redirecting user to GitHub OAuth2 authorization URL")
232232
# We must redirect to GitHub OAuth app for authentication, it will return you to /v2/github/installation which will handle returning user data
@@ -252,7 +252,7 @@ def sign_request(self, installation_id, github_repository_id, change_request_id,
252252
# Not sure if we need a different token for each installation ID...
253253
cla.log.debug(f"{fn} - Loading session from request")
254254
session = self._get_request_session(request)
255-
cla.log.debug(f"{fn} - Adding github details to session: {list(session.keys())} which is type: {type(session)}...")
255+
cla.log.debug(f"{fn} - Adding github details to session")
256256
session["github_installation_id"] = installation_id
257257
session["github_repository_id"] = github_repository_id
258258
session["github_change_request_id"] = change_request_id
@@ -264,14 +264,14 @@ def sign_request(self, installation_id, github_repository_id, change_request_id,
264264
cla.log.debug(f'{fn} - stored origin url in session')
265265

266266
if "github_oauth2_token" in session:
267-
cla.log.debug(f"{fn} - Using existing session GitHub OAuth2 token")
267+
cla.log.debug(f"{fn} - Using existing session GitHub OAuth2 authentication")
268268
return self.redirect_to_console(installation_id, github_repository_id, change_request_id, origin_url, request)
269269
else:
270270
cla.log.debug(f"{fn} - No existing GitHub OAuth2 token - building authorization url and state")
271271
authorization_url, state = self.get_authorization_url_and_state(
272272
installation_id, github_repository_id, int(change_request_id), ["user:email"]
273273
)
274-
cla.log.debug(f"{fn} - Obtained GitHub OAuth2 state from authorization - storing state in the session...")
274+
cla.log.debug(f"{fn} - Obtained GitHub OAuth2 state from authorization - storing state in the session")
275275
session["github_oauth2_state"] = state
276276
cla.log.debug(f"{fn} - redirecting user to GitHub OAuth2 authorization URL")
277277
raise falcon.HTTPFound(authorization_url)
@@ -307,7 +307,7 @@ def _get_request_session(self, request) -> dict: # pylint: disable=no-self-use
307307
session = {}
308308
request.context["session"] = session
309309

310-
cla.log.debug(f"{fn} - loaded session (keys={list(session.keys())})")
310+
cla.log.debug(f"{fn} - loaded session")
311311

312312
return session
313313

@@ -375,7 +375,7 @@ def oauth2_redirect(self, state, code, request): # pylint: disable=too-many-arg
375375
padded_state = state + "=" * (-len(state) % 4)
376376
state_data = json.loads(base64.urlsafe_b64decode(padded_state.encode()).decode())
377377
except (ValueError, json.JSONDecodeError, binascii.Error) as err:
378-
cla.log.warning(f"{fn} - failed to decode state, error: {err}")
378+
cla.log.warning(f"{fn} - failed to decode state, error occurred")
379379
raise falcon.HTTPBadRequest("Invalid OAuth2 state", "Invalid OAuth2 state")
380380

381381
state_token = state_data.get("csrf")
@@ -398,9 +398,9 @@ def oauth2_redirect(self, state, code, request): # pylint: disable=too-many-arg
398398
try:
399399
token = self._fetch_token(client_id, state, token_url, client_secret, code)
400400
except Exception as err:
401-
cla.log.warning(f"{fn} - GitHub OAuth2 error: {err}. Likely bad or expired code, returning HTTP 404 state.")
402-
raise falcon.HTTPBadRequest("OAuth2 code is invalid or expired")
403-
cla.log.debug(f"{fn} - oauth2 token received - storing token in session")
401+
cla.log.warning(f"{fn} - GitHub OAuth2 error. Likely bad or expired code, returning HTTP 400 status.")
402+
raise falcon.HTTPBadRequest("OAuth2 code is invalid or expired", "OAuth2 code is invalid or expired")
403+
cla.log.debug(f"{fn} - oauth2 authentication received - storing in session")
404404
session["github_oauth2_token"] = token
405405
user = self.get_or_create_user(request)
406406
if user is None:
@@ -421,7 +421,7 @@ def oauth2_redirect(self, state, code, request): # pylint: disable=too-many-arg
421421
client_secret = os.environ["GH_OAUTH_SECRET"]
422422
cla.log.debug(f"{fn} - fetching oauth2 token from configured GitHub endpoint")
423423
token = self._fetch_token(client_id, state, token_url, client_secret, code)
424-
cla.log.debug(f"{fn} - oauth2 token received - storing token in session")
424+
cla.log.debug(f"{fn} - oauth2 authentication received - storing in session")
425425
session["github_oauth2_token"] = token
426426
cla.log.debug(f"{fn} - redirecting the user back to the contributor console")
427427
return self.redirect_to_console(installation_id, github_repository_id, change_request_id, origin_url, request)
@@ -1049,7 +1049,7 @@ def update_change_request(self, installation_id, github_repository_id, change_re
10491049
pull_request_id=str(change_request_id),
10501050
)
10511051
except Exception as e:
1052-
cla.log.error(f"{fn} - problem saving PR metadata for PR: {pull_request.number}, error: {e}")
1052+
cla.log.error(f"{fn} - problem saving PR metadata for PR: {pull_request.number}")
10531053

10541054
# Find users who have signed and who have not signed.
10551055
signed = []
@@ -1073,7 +1073,7 @@ def update_change_request(self, installation_id, github_repository_id, change_re
10731073
try:
10741074
future.result()
10751075
except Exception as e:
1076-
cla.log.error(f"{fn} - Exception in commit author thread for PR: {pull_request.number}, error: {e}")
1076+
cla.log.error(f"{fn} - Exception in commit author thread for PR: {pull_request.number}")
10771077

10781078
# Skip allowlisted bots per org/repo GitHub login/email regexps
10791079
missing, allowlisted = self.skip_allowlisted_bots(github_org, repository.get_repository_name(), missing)
@@ -1431,9 +1431,9 @@ def get_or_create_user(self, request):
14311431
# Could not get GitHub user data - maybe user revoked CLA app permissions?
14321432
session = self._get_request_session(request)
14331433

1434-
del session["github_oauth2_state"]
1435-
del session["github_oauth2_token"]
1436-
cla.log.warning(f"{fn} - Deleted OAuth2 session data - retrying token exchange next time")
1434+
session.pop("github_oauth2_state", None)
1435+
session.pop("github_oauth2_token", None)
1436+
cla.log.warning(f"{fn} - Deleted OAuth2 session data - retrying authentication exchange next time")
14371437
raise falcon.HTTPError(
14381438
"400 Bad Request", "github_oauth2_token", "Token permissions have been rejected, please try again"
14391439
)
@@ -1520,16 +1520,16 @@ def get_user_data(self, session, client_id): # pylint: disable=no-self-use
15201520
fn = "cla.models.github_models.get_user_data"
15211521
token = session.get("github_oauth2_token")
15221522
if token is None:
1523-
cla.log.error(f"{fn} - unable to load github_oauth2_token from session (keys={list(session.keys())})")
1523+
cla.log.error(f"{fn} - unable to load github_oauth2_token from session")
15241524
return {"error": "could not get user data from session"}
15251525

15261526
oauth2 = OAuth2Session(client_id, token=token)
15271527
request = oauth2.get("https://api.github.com/user")
15281528
github_user = request.json()
15291529
cla.log.debug(f"{fn} - GitHub user data: %s", github_user)
15301530
if "message" in github_user:
1531-
cla.log.error(f'{fn} - Could not get user data with OAuth2 token: {github_user["message"]}')
1532-
return {"error": "Could not get user data: %s" % github_user["message"]}
1531+
cla.log.error(f'{fn} - Could not get user data with OAuth2 authentication')
1532+
return {"error": "Could not get user data"}
15331533
return github_user
15341534

15351535
def get_user_emails(self, session: dict, client_id: str) -> Union[List[str], dict]: # pylint: disable=no-self-use
@@ -1589,13 +1589,14 @@ def _fetch_github_emails(self, session: dict, client_id: str) -> Union[List[dict
15891589
# as expected
15901590
token = session.get("github_oauth2_token")
15911591
if token is None:
1592-
cla.log.warning(f"{fn} - unable to load github_oauth2_token from the session - session is empty")
1592+
cla.log.warning(f"{fn} - unable to load authentication token from the session - session is empty")
1593+
return {"error": "Could not get user emails"}
15931594
oauth2 = OAuth2Session(client_id, token=token)
15941595
request = oauth2.get("https://api.github.com/user/emails")
15951596
resp = request.json()
15961597
if "message" in resp:
1597-
cla.log.warning(f'{fn} - could not get user emails with OAuth2 token: {resp["message"]}')
1598-
return {"error": "Could not get user emails: %s" % resp["message"]}
1598+
cla.log.warning(f'{fn} - could not get user emails with OAuth2 authentication')
1599+
return {"error": "Could not get user emails"}
15991600
return resp
16001601

16011602
def process_reopened_pull_request(self, data):
@@ -2187,7 +2188,7 @@ def pygithub_graphql(g, query: str, variables: dict | None = None):
21872188
errs = data["errors"]
21882189
paths = [e.get("path") for e in errs]
21892190
msgs = [e.get("message") for e in errs]
2190-
cla.log.error(f"GraphQL errors: {msgs} (paths={paths}, all={errs!r})")
2191+
cla.log.error(f"GraphQL errors occurred")
21912192
return None
21922193
return data.get("data")
21932194
except Exception as exc:
@@ -2535,7 +2536,7 @@ def get_co_author_commits(co_author, commit_sha, pr, installation_id) -> Tuple[U
25352536
cla.log.debug(f"{fn} - Detected noreply GitHub email with ID: {id_str}, login: {login_str}")
25362537
user = github.get_github_user_by_id(github_id, installation_id)
25372538
except Exception as ex:
2538-
cla.log.warning(f"{fn} - Error fetching user by ID {id_str}: {ex}")
2539+
cla.log.warning(f"{fn} - Error fetching user by ID {id_str}")
25392540
user = None
25402541

25412542
# 2. Check for "username@users.noreply.github.com"
@@ -2547,7 +2548,7 @@ def get_co_author_commits(co_author, commit_sha, pr, installation_id) -> Tuple[U
25472548
cla.log.debug(f"{fn} - Detected noreply GitHub email with login: {login_str}")
25482549
user = github.get_github_user_by_login(login_str, installation_id)
25492550
except Exception as ex:
2550-
cla.log.warning(f"{fn} - Error fetching user by login {login_str}: {ex}")
2551+
cla.log.warning(f"{fn} - Error fetching user by login {login_str}")
25512552
user = None
25522553

25532554
# 3. Try to find user by email via GitHub APIs
@@ -2584,7 +2585,7 @@ def get_co_author_commits(co_author, commit_sha, pr, installation_id) -> Tuple[U
25842585
try:
25852586
user = github.get_github_user_by_id(github_id, installation_id)
25862587
except Exception as ex:
2587-
cla.log.warning(f"{fn} - Error fetching user by ID {github_id}: {ex}")
2588+
cla.log.warning(f"{fn} - Error fetching user by ID {github_id}")
25882589
user = None
25892590
except Exception as ex:
25902591
# user not found

cla-backend/cla/utils.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,13 +1277,7 @@ def fetch_token(client_id, state, token_url, client_secret, code, redirect_uri=N
12771277
oauth2 = OAuth2Session(client_id, state=state, scope=["user:email"], redirect_uri=redirect_uri)
12781278
else:
12791279
oauth2 = OAuth2Session(client_id, state=state, scope=["user:email"])
1280-
#cla.log.debug(
1281-
# f"{fn} - oauth2.fetch_token - "
1282-
# f"token_url: {token_url}, "
1283-
# f"client_id: {client_id}, "
1284-
# f"client_secret: {client_secret}, "
1285-
# f"code: {code}"
1286-
#)
1280+
cla.log.debug(f"{fn} - oauth2.fetch_token called")
12871281
return oauth2.fetch_token(token_url, client_secret=client_secret, code=code)
12881282

12891283

@@ -1683,7 +1677,7 @@ def lookup_user_github_username(user_github_id: int) -> Optional[str]:
16831677

16841678
github_user = r.json()
16851679
if "message" in github_user:
1686-
cla.log.warning(f"Unable to lookup user from id: {user_github_id} " f'- message: {github_user["message"]}')
1680+
cla.log.warning(f"Unable to lookup user from id: {user_github_id} - API error occurred")
16871681
return None
16881682
else:
16891683
if "login" in github_user:
@@ -1716,7 +1710,7 @@ def lookup_user_github_id(user_github_username: str) -> Optional[int]:
17161710

17171711
github_user = r.json()
17181712
if "message" in github_user:
1719-
cla.log.warning(f"Unable to lookup user from id: {user_github_username} " f'- message: {github_user["message"]}')
1713+
cla.log.warning(f"Unable to lookup user from id: {user_github_username} - API error occurred")
17201714
return None
17211715
else:
17221716
if "id" in github_user:
@@ -1748,8 +1742,14 @@ def lookup_gitlab_org_members(organization_id):
17481742
r = requests.get(f"{cla.config.PLATFORM_GATEWAY_URL}/cla-service/v4/gitlab/group/{organization_id}/members")
17491743
r.raise_for_status()
17501744
except requests.exceptions.HTTPError as err:
1751-
cla.log.warning(f"Could not fetch gitlab org users: {err}")
1752-
return {f"error: Could not get user gitlab group id: {organization_id} members: {err}"}
1745+
status_code = err.response.status_code if hasattr(err, 'response') and err.response is not None else "unknown"
1746+
cla.log.warning(
1747+
f"Could not fetch gitlab org users for organization_id={organization_id}: "
1748+
f"status_code={status_code}"
1749+
)
1750+
# Return an empty list so callers that expect an iterable of member dicts
1751+
# can safely handle the error case without type errors.
1752+
return []
17531753
return r.json()["list"]
17541754

17551755

@@ -2029,7 +2029,16 @@ def extract_pull_request_number(pull_request_message):
20292029
fn = "extract_pull_request_number"
20302030
pull_request_number = None
20312031
try:
2032-
first_line = pull_request_message.splitlines()[0]
2032+
if not pull_request_message or not pull_request_message.strip():
2033+
cla.log.debug(f"{fn} - empty or whitespace-only message")
2034+
return None
2035+
2036+
lines = pull_request_message.splitlines()
2037+
if not lines or not lines[0].strip():
2038+
cla.log.debug(f"{fn} - no valid lines in message")
2039+
return None
2040+
2041+
first_line = lines[0]
20332042
cla.log.debug(f"{fn} - checking line '{first_line}")
20342043
# Case 1: "Merge pull request #N"
20352044
matches = re.match(r'^Merge pull request #(\d+)', first_line)
@@ -2056,7 +2065,7 @@ def extract_pull_request_number(pull_request_message):
20562065
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data: {pull_request_message} by matching first '#N'")
20572066
return pull_request_number
20582067
else:
2059-
cla.log.warning(f"{fn} - error - unable to extract pull request number from message: {pull_request_message}")
2060-
except Exception as e:
2061-
cla.log.warning(f"{fn} - error - unable to extract pull request number from message: {pull_request_message}, error: {e}")
2068+
cla.log.warning(f"{fn} - error - unable to extract pull request number from message")
2069+
except (ValueError, AttributeError, IndexError):
2070+
cla.log.warning(f"{fn} - error - unable to extract pull request number from message, parse error occurred")
20622071
return pull_request_number

0 commit comments

Comments
 (0)