flawopen.com/bola-idor/Python

● CWE-639 · अति गंभीर
सुरक्षा अनुसंधान · FlawOpen

Python में ऑब्जेक्ट लेवल ऑथराइजेशन की खामी (BOLA / IDOR) से बचाव

FastAPI और Python में BOLA (CWE-639) / IDOR को समाप्त करने की गाइड: क्लाइंट द्वारा भेजे गए आईडी पर भरोसा करने के बजाय डेटाबेस क्वेरी को प्रमाणित टेनेंट आईडी से बाध्य करना।

💡 आसान भाषा में (ELI5)

कल्पना कीजिए कि होटल में आपको कमरा 204 की चाबी मिली। लेकिन दरवाजे पर 204 मिटाकर 205 लिखने पर वह कमरा भी खुल जाता है। होटल ने यह तो जांचा कि आप मेहमान हैं (प्रमाणीकरण), लेकिन यह नहीं जांचा कि कमरा 205 आपका है या नहीं (ऑब्जेक्ट ऑथराइजेशन)।

इस पेज के मुख्य शब्द

Authentication vs. Authorization
सुरक्षा अवधारणा (Authentication vs. Authorization): Authentication confirms WHO the user is (valid login). Authorization confirms WHAT resources that specific user is permitted to view or modify.
BOLA (Broken Object Level Authorization)
सुरक्षा अवधारणा (BOLA (Broken Object Level Authorization)): The #1 OWASP API vulnerability where an endpoint exposes an object reference (e.g. /api/invoices/1042) without verifying tenant ownership.
IDOR (Insecure Direct Object Reference)
सुरक्षा अवधारणा (IDOR (Insecure Direct Object Reference)): The broader vulnerability class (CWE-639) where internal implementation objects are exposed directly to users without access control checks.
Tenancy Scope Injection
सुरक्षा अवधारणा (Tenancy Scope Injection): An architectural pattern where database queries automatically enforce WHERE organization_id = :auth_user_org_id at the ORM layer.
Non-Enumerable Identifiers (UUIDv4)
सुरक्षा अवधारणा (Non-Enumerable Identifiers (UUIDv4)): Using cryptographically random 128-bit identifiers instead of sequential auto-incrementing integers (1, 2, 3...) to prevent predictable enumeration.

हमले का चरण-दर-चरण प्रवाह

Step 1

Legitimate Sign-In

An attacker creates a legitimate account on the platform and receives a valid JWT authentication bearer token.

Step 2

Resource Inspection

The attacker accesses their own billing invoice via GET /api/invoices/1042 and observes sequential database identifiers in use.

Step 3

Identifier Perturbation

The attacker modifies the URL to request GET /api/invoices/1041 using their own valid authentication token.

Step 4

Flawed Controller Logic

The FastAPI backend confirms the token is valid, but queries the database solely by Invoice.id == 1041 without verifying tenant ownership.

Step 5

Cross-Tenant Exfiltration

The server returns confidential invoice records, billing details, and personal data belonging to another organization.

Step 6

Automated Harvest

The attacker scripts a loop iterating over IDs 1 through 100,000, draining the entire multi-tenant database.

सोर्स कोड: कमज़ोर बनाम सुरक्षित कार्यान्वयन

✕ कमज़ोर कार्यान्वयन
# VULNERABLE: Verifies user login, but queries object solely by client ID
from fastapi import FastAPI, Depends, HTTPException, status
from sqlalchemy.orm import Session

@app.get("/api/invoices/{invoice_id}")
def get_invoice(invoice_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
    # Flaw: Attacker passes invoice_id belonging to another tenant
    invoice = db.query(Invoice).filter(Invoice.id == invoice_id).first()
    
    if not invoice:
        raise HTTPException(status_code=404, detail="Invoice not found")
        
    # Leaks confidential billing records of competitor organizations!
    return invoice
✓ सुरक्षित और सुदृढ़ फ़िक्स
# HARDENED: Query is strictly bound to the authenticated tenant's organization ID
from fastapi import FastAPI, Depends, HTTPException, status
from sqlalchemy.orm import Session

@app.get("/api/invoices/{invoice_id}")
def get_invoice(invoice_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
    # Defense-in-depth: query filters by BOTH invoice_id AND authenticated organization_id
    invoice = db.query(Invoice).filter(
        Invoice.id == invoice_id,
        Invoice.organization_id == current_user.organization_id
    ).first()
    
    if not invoice:
        # Return 404 rather than 403 to prevent object existence enumeration
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Invoice not found")
        
    return invoice

इंजीनियरिंग और सिस्टम सुरक्षा चेकलिस्ट

References