Skip to content

fix IDOR vulnerability in idor_profile_page by adding session authorization#1

Merged
brandong1 merged 1 commit intomainfrom
semgrep-autofix/1775148214
Apr 7, 2026
Merged

fix IDOR vulnerability in idor_profile_page by adding session authorization#1
brandong1 merged 1 commit intomainfrom
semgrep-autofix/1775148214

Conversation

@semgrep-code-brandong1-demo
Copy link
Copy Markdown

Fix IDOR vulnerability in the profile page that allowed users to access other users' profiles by manipulating the user_id cookie.

Changes

  • Fixed NameError where user.password was referenced before user was defined
  • Reordered logic to fetch user from database before validating session token
  • Added null checks for both user_id and session_token cookies
  • Added authorization check to verify the session token belongs to the requested user

Why

The original code had two critical issues:

  1. The session_token check on line 40 referenced an undefined variable user, causing a NameError on every request
  2. Even if the check worked, there was no verification that the authenticated session corresponded to the user_id being accessed

The fix ensures that the session_token in the cookie must match the password hash of the user whose profile is being requested. This binds the session to a specific user and prevents attackers from accessing other profiles by simply changing the user_id cookie value.

Semgrep Finding Details

The idor_profile_page function reads user_id directly from a user-controllable cookie (line 44: request.cookies.get('user_id')) and uses it to query the database for that user's profile without any ownership verification. The session_token check on line 40 references an undefined variable 'user', causing a NameError that makes the authentication check non-functional. Even if the session check worked, there is no authorization logic verifying that the authenticated user's session corresponds to the requested user_id.

@36804960 requested Semgrep Assistant generate this pull request to fix a finding.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

…zation

Fix IDOR vulnerability in the profile page that allowed users to access other users' profiles by manipulating the user_id cookie.

## Changes
- Fixed NameError where `user.password` was referenced before `user` was defined
- Reordered logic to fetch user from database before validating session token
- Added null checks for both `user_id` and `session_token` cookies
- Added authorization check to verify the session token belongs to the requested user

## Why
The original code had two critical issues:

1. The session_token check on line 40 referenced an undefined variable `user`, causing a NameError on every request
2. Even if the check worked, there was no verification that the authenticated session corresponded to the user_id being accessed

The fix ensures that the session_token in the cookie must match the password hash of the user whose profile is being requested. This binds the session to a specific user and prevents attackers from accessing other profiles by simply changing the user_id cookie value.

## Semgrep Finding Details
The idor_profile_page function reads user_id directly from a user-controllable cookie (line 44: request.cookies.get('user_id')) and uses it to query the database for that user's profile without any ownership verification. The session_token check on line 40 references an undefined variable 'user', causing a NameError that makes the authentication check non-functional. Even if the session check worked, there is no authorization logic verifying that the authenticated user's session corresponds to the requested user_id.

@36804960 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/bg_demo/ai-findings/739590370).
@brandong1 brandong1 marked this pull request as ready for review April 2, 2026 16:52
@brandong1 brandong1 merged commit 02225fb into main Apr 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant