logo

Database

Python Basic Auth Header Used

Description

Detects insecure usage of HTTP Basic Authentication headers in Python applications. Basic Authentication transmits credentials in a weakly encoded format that can expose usernames and passwords if not used over HTTPS/TLS, creating a risk of credential theft.

Weakness:

015 - Insecure authentication method - Basic

Category: Protocol Manipulation

Detection Strategy

    Identifies function calls that set or manipulate HTTP Basic Authentication headers

    Checks if the authentication headers are being used in potentially insecure contexts (e.g. non-HTTPS connections)

    Verifies if the code explicitly enforces secure transport requirements when using basic authentication

    Reports a vulnerability when basic auth headers are used without proper security controls

Vulnerable code example

import os
import aiohttp

async def vulnerable_request():
    # Insecure: Direct use of environment variable in auth header without validation
    auth_header = f"Basic {os.environ['API_TOKEN']}"  
    
    async with aiohttp.ClientSession() as session:...

✅ Secure code example

import os
import aiohttp
from urllib.parse import urlparse
from typing import Optional

async def secure_request() -> Optional[str]:
    # Validate API token exists and is non-empty
    api_token = os.getenv('API_TOKEN')...