The vulnerability lies in the AI-assisted message rewrite feature of Mattermost. An authenticated user could craft a request to the /posts/rewrite API endpoint, specifying a root_id of a post from a private channel or direct message they are not a member of. The backend code, specifically the buildThreadContextForRewrite function, would proceed to fetch the entire thread content associated with that root_id without first verifying if the requesting user had access to that channel. This fetched content, containing private messages, would then be passed to the AI service for processing. The patch addresses this by introducing an authorization check using GetPostIfAuthorized within the buildThreadContextForRewrite function. This ensures that before any thread content is fetched, the system verifies that the user has permission to access the post and its corresponding channel. Additionally, the calling function RewriteMessage was updated to properly handle and propagate the authorization error, preventing the rewrite process from continuing if the user lacks access.