|
| 1 | +""" |
| 2 | +A01:2025 - Broken Access Control |
| 3 | +
|
| 4 | +Demonstrates IDOR, mass assignment, and missing function-level access control vulnerabilities. |
| 5 | +""" |
| 6 | + |
| 7 | +import uuid |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from fastapi import APIRouter, HTTPException |
| 11 | +from sqlmodel import select |
| 12 | + |
| 13 | +from app.modules.owasp_demo.models import SecretDocument, UserNote |
| 14 | +from app.modules.owasp_demo.schemas import ( |
| 15 | + DocumentPublic, |
| 16 | + NotePublic, |
| 17 | + UserProfileUpdate, |
| 18 | +) |
| 19 | +from app.modules.rbac.service import UserRoleService |
| 20 | +from app.modules.shared import CurrentUser, SessionDep |
| 21 | +from app.modules.users.models import User |
| 22 | + |
| 23 | +router = APIRouter( |
| 24 | + prefix="/a01", |
| 25 | + tags=["A01 - Broken Access Control"], |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@router.get("/vulnerable/documents", response_model=DocumentPublic) |
| 30 | +def get_document_vulnerable( |
| 31 | + session: SessionDep, |
| 32 | + doc_id: uuid.UUID | None = None, |
| 33 | +) -> Any: |
| 34 | + """ |
| 35 | + VULNERABLE: Insecure Direct Object Reference (IDOR) |
| 36 | +
|
| 37 | + Problem: No authentication or authorization check. |
| 38 | + Anyone can access any document by guessing/enumerating the ID. |
| 39 | +
|
| 40 | + Attack: Simply change the doc_id in the URL to access other users' documents. |
| 41 | + """ |
| 42 | + if doc_id: |
| 43 | + document = session.get(SecretDocument, doc_id) |
| 44 | + else: |
| 45 | + # Auto-fetch first available document for easy testing |
| 46 | + document = session.exec(select(SecretDocument).limit(1)).first() |
| 47 | + |
| 48 | + if not document: |
| 49 | + raise HTTPException(status_code=404, detail="Document not found") |
| 50 | + return document |
| 51 | + |
| 52 | + |
| 53 | +@router.get("/secure/documents", response_model=DocumentPublic) |
| 54 | +def get_document_secure( |
| 55 | + session: SessionDep, |
| 56 | + current_user: CurrentUser, |
| 57 | + doc_id: uuid.UUID | None = None, |
| 58 | +) -> Any: |
| 59 | + """ |
| 60 | + SECURE: Proper access control implementation with RBAC |
| 61 | +
|
| 62 | + Fix: |
| 63 | + 1. Requires authentication (CurrentUser dependency) |
| 64 | + 2. Verifies the user owns the document OR has documents:read permission OR is superuser |
| 65 | + """ |
| 66 | + if doc_id: |
| 67 | + document = session.get(SecretDocument, doc_id) |
| 68 | + else: |
| 69 | + # Auto-fetch first available document for easy testing |
| 70 | + document = session.exec(select(SecretDocument).limit(1)).first() |
| 71 | + |
| 72 | + if not document: |
| 73 | + raise HTTPException(status_code=404, detail="Document not found") |
| 74 | + |
| 75 | + if document.owner_id != current_user.id and not current_user.is_superuser: |
| 76 | + user_role_service = UserRoleService(session) |
| 77 | + if not user_role_service.user_has_permission(current_user.id, "documents:read"): |
| 78 | + raise HTTPException(status_code=403, detail="Access denied") |
| 79 | + |
| 80 | + return document |
| 81 | + |
| 82 | + |
| 83 | +@router.get("/vulnerable/notes", response_model=NotePublic) |
| 84 | +def get_note_idor_vulnerable( |
| 85 | + session: SessionDep, |
| 86 | + current_user: CurrentUser, # noqa: ARG001 |
| 87 | + note_id: int | None = None, |
| 88 | +) -> Any: |
| 89 | + """ |
| 90 | + VULNERABLE: IDOR with sequential IDs |
| 91 | +
|
| 92 | + Problem: Uses sequential integer IDs that are easy to enumerate. |
| 93 | + Even with authentication, there's no ownership check. |
| 94 | +
|
| 95 | + Attack: Try note_id=1, note_id=2, etc. to access other users' notes. |
| 96 | + """ |
| 97 | + if note_id: |
| 98 | + note = session.get(UserNote, note_id) |
| 99 | + else: |
| 100 | + # Auto-fetch first available note for easy testing |
| 101 | + note = session.exec(select(UserNote).limit(1)).first() |
| 102 | + |
| 103 | + if not note: |
| 104 | + raise HTTPException(status_code=404, detail="Note not found") |
| 105 | + return note |
| 106 | + |
| 107 | + |
| 108 | +@router.get("/secure/notes", response_model=NotePublic) |
| 109 | +def get_note_secure( |
| 110 | + session: SessionDep, |
| 111 | + current_user: CurrentUser, |
| 112 | + note_id: int | None = None, |
| 113 | +) -> Any: |
| 114 | + """ |
| 115 | + SECURE: Proper IDOR prevention with RBAC |
| 116 | +
|
| 117 | + Fix: Always verify ownership OR check RBAC permissions before returning data. |
| 118 | + """ |
| 119 | + if note_id: |
| 120 | + note = session.get(UserNote, note_id) |
| 121 | + else: |
| 122 | + # Auto-fetch first available note for easy testing |
| 123 | + note = session.exec(select(UserNote).limit(1)).first() |
| 124 | + |
| 125 | + if not note: |
| 126 | + raise HTTPException(status_code=404, detail="Note not found") |
| 127 | + |
| 128 | + if note.owner_id != current_user.id and not current_user.is_superuser: |
| 129 | + user_role_service = UserRoleService(session) |
| 130 | + if not user_role_service.user_has_permission(current_user.id, "notes:read"): |
| 131 | + raise HTTPException(status_code=403, detail="Access denied") |
| 132 | + |
| 133 | + return note |
| 134 | + |
| 135 | + |
| 136 | +@router.patch("/vulnerable/users/profile") |
| 137 | +def update_profile_mass_assignment( |
| 138 | + session: SessionDep, |
| 139 | + current_user: CurrentUser, |
| 140 | + profile: UserProfileUpdate, |
| 141 | +) -> dict[str, Any]: |
| 142 | + """ |
| 143 | + VULNERABLE: Mass Assignment / Privilege Escalation |
| 144 | +
|
| 145 | + Problem: Accepts is_superuser and is_active fields from user input, |
| 146 | + allowing users to grant themselves admin privileges. |
| 147 | +
|
| 148 | + Attack: Send {"is_superuser": true} in the request body. |
| 149 | + """ |
| 150 | + user = session.get(User, current_user.id) |
| 151 | + if not user: |
| 152 | + raise HTTPException(status_code=404, detail="User not found") |
| 153 | + |
| 154 | + update_data = profile.model_dump(exclude_unset=True) |
| 155 | + for field, value in update_data.items(): |
| 156 | + setattr(user, field, value) |
| 157 | + |
| 158 | + session.add(user) |
| 159 | + session.commit() |
| 160 | + session.refresh(user) |
| 161 | + |
| 162 | + return { |
| 163 | + "message": "Profile updated", |
| 164 | + "is_superuser": user.is_superuser, |
| 165 | + "is_active": user.is_active, |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | +@router.patch("/secure/users/profile") |
| 170 | +def update_profile_secure( |
| 171 | + session: SessionDep, |
| 172 | + current_user: CurrentUser, |
| 173 | + profile: UserProfileUpdate, |
| 174 | +) -> dict[str, Any]: |
| 175 | + """ |
| 176 | + SECURE: Protected against mass assignment |
| 177 | +
|
| 178 | + Fix: Explicitly whitelist which fields can be updated. |
| 179 | + Never trust user input for sensitive fields. |
| 180 | + """ |
| 181 | + user = session.get(User, current_user.id) |
| 182 | + if not user: |
| 183 | + raise HTTPException(status_code=404, detail="User not found") |
| 184 | + |
| 185 | + ALLOWED_FIELDS = {"full_name", "email"} |
| 186 | + |
| 187 | + update_data = profile.model_dump(exclude_unset=True) |
| 188 | + for field, value in update_data.items(): |
| 189 | + if field in ALLOWED_FIELDS: |
| 190 | + setattr(user, field, value) |
| 191 | + |
| 192 | + session.add(user) |
| 193 | + session.commit() |
| 194 | + session.refresh(user) |
| 195 | + |
| 196 | + return {"message": "Profile updated safely", "full_name": user.full_name} |
| 197 | + |
| 198 | + |
| 199 | +@router.get("/vulnerable/admin/users") |
| 200 | +def list_users_no_auth(session: SessionDep) -> dict[str, Any]: |
| 201 | + """ |
| 202 | + VULNERABLE: Missing Function Level Access Control |
| 203 | +
|
| 204 | + Problem: Admin endpoint accessible without authentication. |
| 205 | +
|
| 206 | + Attack: Anyone can access /admin/users to see all user data. |
| 207 | + """ |
| 208 | + statement = select(User) |
| 209 | + users = session.exec(statement).all() |
| 210 | + return { |
| 211 | + "count": len(users), |
| 212 | + "users": [ |
| 213 | + {"id": str(u.id), "email": u.email, "is_superuser": u.is_superuser} |
| 214 | + for u in users |
| 215 | + ], |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | +@router.get("/secure/admin/users") |
| 220 | +def list_users_secure( |
| 221 | + session: SessionDep, |
| 222 | + current_user: CurrentUser, |
| 223 | +) -> dict[str, Any]: |
| 224 | + """ |
| 225 | + SECURE: Proper admin access control with RBAC |
| 226 | +
|
| 227 | + Fix: Require authentication AND appropriate RBAC permission (users:read). |
| 228 | + Demonstrates proper function-level access control using role-based permissions. |
| 229 | + """ |
| 230 | + if not current_user.is_superuser: |
| 231 | + user_role_service = UserRoleService(session) |
| 232 | + if not user_role_service.user_has_permission(current_user.id, "users:read"): |
| 233 | + raise HTTPException(status_code=403, detail="Access denied") |
| 234 | + |
| 235 | + statement = select(User) |
| 236 | + users = session.exec(statement).all() |
| 237 | + return { |
| 238 | + "count": len(users), |
| 239 | + "users": [ |
| 240 | + {"id": str(u.id), "email": u.email, "is_superuser": u.is_superuser} |
| 241 | + for u in users |
| 242 | + ], |
| 243 | + } |
0 commit comments