The vulnerability lies in the authentication logic for API tokens in Infrahub. The root cause is twofold: the system failed to check if a token had been deleted, and it failed to check if a token had expired. This allowed any API token, even if deleted or expired, to grant access as long as the user account it was associated with was still active.
The analysis of the provided patches pinpoints the exact location of these flaws within the AccountTokenValidatorQuery class in backend/infrahub/core/account.py.
-
Deleted Tokens: The query_init method was responsible for building the database query to find a token. The original query did not include a condition to check if the token's status was 'active'. The patch rectifies this by adding WHERE r1.status = "active" AND r2.status = "active" clauses, effectively filtering out any tokens that have been marked as deleted.
-
Expired Tokens: The get_account_id method was responsible for returning the user's ID after the token was found. The original implementation performed no check on the token's expiration date. The patch introduces logic to fetch the expiration timestamp, compare it to the current time, and return None if the token is expired.
The function validate_token is the higher-level function that uses this vulnerable class, and it would appear in a stack trace during an exploit attempt. Therefore, monitoring calls to validate_token and the methods of AccountTokenValidatorQuery would be key to detecting exploitation of this vulnerability.