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