From bd0e9fa1d09599551b85c3e4ad256605731efed4 Mon Sep 17 00:00:00 2001 From: Christian Monch Date: Sat, 17 Oct 2026 11:22:55 +0200 Subject: [PATCH 1/5] sec: prevent token leaks Simplify authentication error result messages. Log authentication failure information without externally provided information to prevent token leakage. --- dump_things_service/auth/__init__.py | 1 + dump_things_service/auth/forgejo.py | 83 +++++++++++++++------------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/dump_things_service/auth/__init__.py b/dump_things_service/auth/__init__.py index a2d1953..19e81cb 100644 --- a/dump_things_service/auth/__init__.py +++ b/dump_things_service/auth/__init__.py @@ -12,6 +12,7 @@ determine: from __future__ import annotations import abc +import base64 import dataclasses from typing import TYPE_CHECKING diff --git a/dump_things_service/auth/forgejo.py b/dump_things_service/auth/forgejo.py index c77c23f..8cf0be8 100644 --- a/dump_things_service/auth/forgejo.py +++ b/dump_things_service/auth/forgejo.py @@ -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}') @@ -121,9 +124,10 @@ 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}', @@ -131,22 +135,15 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache): 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 + logger.info(f'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(f'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(f'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,36 @@ 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, ) -- 2.54.1 From 34a31d37d6dbf4de56bd2222790bed7cbabf9329 Mon Sep 17 00:00:00 2001 From: Christian Monch Date: Sat, 17 Oct 2026 11:35:11 +0200 Subject: [PATCH 2/5] chore: update changelog --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f5fb6..a2394dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 beeing leaked to clients. + + # 6.6.3 (2026-10-07) ## Bugfixes -- 2.54.1 From cec602953223142af3b674d66d45a099ad7bfd1c Mon Sep 17 00:00:00 2001 From: Christian Monch Date: Sat, 17 Oct 2026 11:35:33 +0200 Subject: [PATCH 3/5] typo: fix a typo --- dump_things_service/auth/forgejo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dump_things_service/auth/forgejo.py b/dump_things_service/auth/forgejo.py index 8cf0be8..0ed8cf1 100644 --- a/dump_things_service/auth/forgejo.py +++ b/dump_things_service/auth/forgejo.py @@ -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: -- 2.54.1 From bdf67a9efb38d312a246ebd4ca423703a0c1cac5 Mon Sep 17 00:00:00 2001 From: Christian Monch Date: Sat, 17 Oct 2026 11:45:09 +0200 Subject: [PATCH 4/5] refactor: reformat code, remove unused imports --- dump_things_service/auth/__init__.py | 1 - dump_things_service/auth/forgejo.py | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/dump_things_service/auth/__init__.py b/dump_things_service/auth/__init__.py index 19e81cb..a2d1953 100644 --- a/dump_things_service/auth/__init__.py +++ b/dump_things_service/auth/__init__.py @@ -12,7 +12,6 @@ determine: from __future__ import annotations import abc -import base64 import dataclasses from typing import TYPE_CHECKING diff --git a/dump_things_service/auth/forgejo.py b/dump_things_service/auth/forgejo.py index 0ed8cf1..00b0985 100644 --- a/dump_things_service/auth/forgejo.py +++ b/dump_things_service/auth/forgejo.py @@ -134,15 +134,15 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache): }, timeout=_timeout, ) - except Timeout as e: - logger.info(f'timeout in request to %s',api_url) + except Timeout: + logger.info('timeout in request to %s', api_url) raise RemoteAuthenticationError() except requests.exceptions.RequestException as e: - logger.info(f'could not read from %s: %s', api_url, e) + logger.info('could not read from %s: %s', api_url, e) raise RemoteAuthenticationError() if r.status_code >= HTTP_300_MULTIPLE_CHOICES: - logger.info(f'invalid token: %d', r.status_code) + logger.info('invalid token: %d', r.status_code) raise RemoteAuthenticationError() return r.json() @@ -258,8 +258,7 @@ class ForgejoAuthenticationSource(AuthenticationSource, MethodCache): self.organization, ) logger.debug( - 'ForgejoAuthenticationSource: ' - 'organization_teams: %s', + 'ForgejoAuthenticationSource: organization_teams: %s', str(organization_teams), ) -- 2.54.1 From b6d2aa6aedcf0226845962209e2fe8cf9f007025 Mon Sep 17 00:00:00 2001 From: Christian Monch Date: Sat, 17 Oct 2026 11:48:42 +0200 Subject: [PATCH 5/5] spell: fix typo --- CHANGELOG.md | 2 +- dump_things_service/auth/forgejo.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2394dd..cdbd07e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - Ensure that tokens are not leaked to clients or logs. - Return generic error messages for authentication failures to prevent - authentication information from beeing leaked to clients. + authentication information from being leaked to clients. # 6.6.3 (2026-10-07) diff --git a/dump_things_service/auth/forgejo.py b/dump_things_service/auth/forgejo.py index 00b0985..abaec61 100644 --- a/dump_things_service/auth/forgejo.py +++ b/dump_things_service/auth/forgejo.py @@ -106,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. """ -- 2.54.1