Security: fix finding 2 #293

Merged
cmo merged 5 commits from sec_finding_2 into master 2026-10-17 09:52:27 +00:00
2 changed files with 57 additions and 41 deletions

View file

@ -1,3 +1,13 @@
# 6.6.4 (2026-XX-XX)
## Security
- Ensure that tokens are not leaked to clients or logs.
- Return generic error messages for authentication failures to prevent
authentication information from being leaked to clients.
# 6.6.3 (2026-10-07)
## Bugfixes

View file

@ -1,4 +1,4 @@
"""Use Forgejo instance to fetch token permissions, ids, and incomng_label
"""Use Forgejo instance to fetch token permissions, ids, and incoming_label
Note: for some reason, the request:
@ -28,7 +28,6 @@ from dump_things_service.auth import (
AuthenticationError,
AuthenticationInfo,
AuthenticationSource,
InvalidTokenError,
)
if TYPE_CHECKING:
@ -70,7 +69,11 @@ class MethodCache:
class RemoteAuthenticationError(AuthenticationError):
"""Exception for remote authentication errors."""
def __init__(self, status: int, message: str):
def __init__(
self,
status: int = HTTP_401_UNAUTHORIZED,
message: str = '',
):
self.status = status
self.message = message
super().__init__(f'Authentication failed with status {status}: {message}')
@ -103,7 +106,7 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
is created.
:param instance_id: Optional instance ID. Will be used to
disambiguate identical user IDs from different servers, default
to a hash ot `api_url`.
to a hash of `api_url`.
:param repository: Optional repository. If this is provided, access
will only be granted if the team has access to the repository.
"""
@ -121,32 +124,26 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
endpoint: str,
token: str,
):
api_url = f'{self.api_url}/{endpoint}'
try:
r = requests.get(
url=f'{self.api_url}/{endpoint}',
url=api_url,
headers={
'Accept': 'application/json',
'Authorization': f'token {token}',
},
timeout=_timeout,
)
except Timeout as e:
msg = f'timeout in request to {self.api_url}'
raise RemoteAuthenticationError(
status=HTTP_401_UNAUTHORIZED,
message=msg,
) from e
except Timeout:
logger.info('timeout in request to %s', api_url)
raise RemoteAuthenticationError()
except requests.exceptions.RequestException as e:
msg = f'could not read from {self.api_url}/{endpoint}'
raise RemoteAuthenticationError(
status=HTTP_401_UNAUTHORIZED,
message=msg,
) from e
logger.info('could not read from %s: %s', api_url, e)
raise RemoteAuthenticationError()
if r.status_code >= HTTP_300_MULTIPLE_CHOICES:
cleaned_text = r.text.replace(token, '***')
msg = f'invalid token: ({r.status_code}): {cleaned_text}'
raise InvalidTokenError(msg)
logger.info('invalid token: %d', r.status_code)
raise RemoteAuthenticationError()
return r.json()
@MethodCache.cache_temporary(duration=120)
@ -212,15 +209,14 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
def _get_unit_content(self, team: dict, unit_name: str) -> str:
permissions = team['units_map'].get(unit_name)
if not permissions:
logger.debug('no unit `repo.actions` in team %s', self.team)
msg = (
f'no `repo.{unit_name}`-unit defined for team `{self.team}` in '
f'organization {self.organization}'
)
raise RemoteAuthenticationError(
status=HTTP_401_UNAUTHORIZED,
message=msg,
logger.info(
'ForgejoAuthenticationSource: Authentication error: '
'no `repo.%s`-unit defined for team `%s` in organization %s',
unit_name,
self.team,
self.organization,
)
raise RemoteAuthenticationError()
return permissions
@MethodCache.cache_temporary(duration=60)
@ -229,6 +225,7 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
token: str,
) -> AuthenticationInfo:
logger.debug(
'ForgejoAuthenticationSource: Authentication error: '
'starting Forgejo authentication: %s, %s, %s',
self.api_url,
self.organization,
@ -239,12 +236,12 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
logger.debug('user_teams: %s', str(user_teams))
if self.team not in user_teams:
logger.debug("%s not in user's teams", self.team)
msg = f'token user is not member of team `{self.team}`'
raise RemoteAuthenticationError(
status=HTTP_401_UNAUTHORIZED,
message=msg,
logger.info(
'ForgejoAuthenticationSource: Authentication error: '
'token user is not member of team %s',
self.team,
)
raise RemoteAuthenticationError()
organization = self._get_organization(token)
user_info = self._get_user(token)
@ -260,26 +257,35 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache):
token,
self.organization,
)
logger.debug('organization_teams: %s', str(organization_teams))
logger.debug(
'ForgejoAuthenticationSource: organization_teams: %s',
str(organization_teams),
)
# Check that the configured team exists
team = organization_teams.get(self.team)
if not team:
logger.debug('%s not in organization teams', self.team)
if self.repository is not None:
msg = f'team `{self.team}` has no access to repository `{self.repository}`'
logger.info(
'ForgejoAuthenticationSource: Authentication error: '
'team `%s` has no access to repository `%s`',
self.team,
self.repository,
)
else:
msg = f'organization `{self.organization}` has no team `{self.team}`'
raise RemoteAuthenticationError(
status=HTTP_401_UNAUTHORIZED,
message=msg,
)
logger.info(
'ForgejoAuthenticationSource: Authentication error: '
'organization `%s` has no team `%s`',
self.organization,
self.team,
)
raise RemoteAuthenticationError()
# Get the repo.code permissions from the team definition
code_permissions = self._get_unit_content(team, 'repo.code')
action_permissions = self._get_unit_content(team, 'repo.actions')
logger.debug(
'authentication success, team permissions: %s, %s',
'ForgejoAuthenticationSource: authentication success, team permissions: %s, %s',
code_permissions,
action_permissions,
)