The analysis of the provided patch for the Zitadel vulnerability reveals that the root cause of the information disclosure is in the internal/query/user.go file. Specifically, the SearchUsers function was fetching a list of users and their total count from the database and then applying permission-based filtering on the returned list. However, the total count was not being recalculated after the filtering. This meant that any authenticated user could query the ListUsers endpoint and, while receiving an empty or filtered list of users in the response, the totalResult field would contain the total number of users in the entire Zitadel instance.
The patch addresses this by removing the usersCheckPermission function, which was responsible for the insecure post-query filtering. The logic is changed to incorporate permission checks directly into the database query itself (within the searchUsers function, which is called by SearchUsers). This ensures that the database returns only the users the requester has permission to see, and the count is also calculated based on this filtered set. The changes in the integration tests, where the expected totalResult is now explicitly checked against a known value instead of being copied from the response, further confirm that the fix correctly calculates the total number of visible users.