Missing Authentication Anti-Pattern
Severity: Critical
Summary
Missing or broken authentication occurs when applications fail to verify user identity, allowing unauthorized access to protected data and functionality. This manifests as unprotected endpoints, missing session checks, or weak credential verification vulnerable to bypass or brute-force. AI-generated code frequently produces insecure boilerplate with stubbed or missing authentication checks.
The Anti-Pattern
Never create endpoints accessing sensitive data or functionality without verifying user identity and validating active sessions.
BAD Code Example
# VULNERABLE: Critical API endpoint without authentication check
from flask import request, jsonify
from db import User, session
@app.route("/api/users/<int:user_id>/profile")
def get_user_profile(user_id):
# Takes user ID and returns profile data
# CRITICAL FLAW: Never checks who makes the request
# Any user can access any profile by changing user_id in URL
user = session.query(User).filter_by(id=user_id).first()
if not user:
return jsonify({"error": "User not found"}), 404
# Returns sensitive profile information without verification
return jsonify({
"id": user.id,
"username": user.username,
"email": user.email,
"signed_up_at": user.created_at
})
GOOD Code Example
# SECURE: Endpoint protected by authentication and authorization
from flask import request, jsonify
from db import User, session
from auth import require_authentication # Decorator for auth
@app.route("/api/users/<int:user_id>/profile")
@require_authentication # Ensures valid user session exists
def get_user_profile_secure(current_user, user_id):
# `require_authentication` decorator decodes session token (JWT)
# and passes authenticated user object to function
# AUTHORIZATION CHECK:
# Verify user can access this data
# Users see only their own profile unless admin
if current_user.id != user_id and not current_user.is_admin:
return jsonify({"error": "Access denied. You are not authorized to view this profile."}), 403
user = session.query(User).filter_by(id=user_id).first()
if not user:
return jsonify({"error": "User not found"}), 404
# Safe to return data after authentication and authorization
return jsonify({
"id": user.id,
"username": user.username,
"email": user.email,
"signed_up_at": user.created_at
})
Detection
- Audit all endpoints for authentication: Grep for routes without auth:
rg '@app\.route|@router\.(get|post)' --type py -A 5 | rg -v '@require|@login|@auth'
rg 'app\.(get|post|put|delete)\(' --type js -A 3 | rg -v 'authenticate|isAuth'
rg '@GetMapping|@PostMapping' --type java -A 3 | rg -v '@PreAuthorize|@Secured'
- Find sensitive endpoints: Identify admin, profile, financial routes:
rg '/admin|/api/users|/profile|/account|/payment' -i
- Check each for authentication decorators/middleware
- Check for fail-open logic: Find default permit patterns:
rg 'if.*not.*authenticated.*return|except.*pass' --type py
rg 'catch.*\{\s*\}|if.*!auth.*continue' --type js
- Test unauthenticated access: Direct endpoint testing:
curl -X GET https://api.example.com/api/users/me (no auth header)
curl -X DELETE https://api.example.com/api/admin/users/1 (no token)
- If these succeed without 401/403, endpoints are vulnerable
Prevention
Related Security Patterns & Anti-Patterns
References
1---2name: missing-authentication-anti-pattern3description: Security anti-pattern for missing or broken authentication (CWE-287). Use when generating or reviewing code for login systems, API endpoints, protected routes, or access control. Detects unprotected endpoints, weak password policies, and missing rate limiting on authentication.4---56# Missing Authentication Anti-Pattern78**Severity:** Critical910## Summary1112Missing or broken authentication occurs when applications fail to verify user identity, allowing unauthorized access to protected data and functionality. This manifests as unprotected endpoints, missing session checks, or weak credential verification vulnerable to bypass or brute-force. AI-generated code frequently produces insecure boilerplate with stubbed or missing authentication checks.1314## The Anti-Pattern1516Never create endpoints accessing sensitive data or functionality without verifying user identity and validating active sessions.1718### BAD Code Example1920```python21# VULNERABLE: Critical API endpoint without authentication check22from flask import request, jsonify23from db import User, session2425@app.route("/api/users/<int:user_id>/profile")26def get_user_profile(user_id):27 # Takes user ID and returns profile data28 # CRITICAL FLAW: Never checks who makes the request29 # Any user can access any profile by changing user_id in URL30 user = session.query(User).filter_by(id=user_id).first()3132 if not user:33 return jsonify({"error": "User not found"}), 4043435 # Returns sensitive profile information without verification36 return jsonify({37 "id": user.id,38 "username": user.username,39 "email": user.email,40 "signed_up_at": user.created_at41 })42```4344### GOOD Code Example4546```python47# SECURE: Endpoint protected by authentication and authorization48from flask import request, jsonify49from db import User, session50from auth import require_authentication # Decorator for auth5152@app.route("/api/users/<int:user_id>/profile")53@require_authentication # Ensures valid user session exists54def get_user_profile_secure(current_user, user_id):55 # `require_authentication` decorator decodes session token (JWT)56 # and passes authenticated user object to function5758 # AUTHORIZATION CHECK:59 # Verify user can access this data60 # Users see only their own profile unless admin61 if current_user.id != user_id and not current_user.is_admin:62 return jsonify({"error": "Access denied. You are not authorized to view this profile."}), 4036364 user = session.query(User).filter_by(id=user_id).first()6566 if not user:67 return jsonify({"error": "User not found"}), 4046869 # Safe to return data after authentication and authorization70 return jsonify({71 "id": user.id,72 "username": user.username,73 "email": user.email,74 "signed_up_at": user.created_at75 })76```7778## Detection7980- **Audit all endpoints for authentication:** Grep for routes without auth:81 - `rg '@app\.route|@router\.(get|post)' --type py -A 5 | rg -v '@require|@login|@auth'`82 - `rg 'app\.(get|post|put|delete)\(' --type js -A 3 | rg -v 'authenticate|isAuth'`83 - `rg '@GetMapping|@PostMapping' --type java -A 3 | rg -v '@PreAuthorize|@Secured'`84- **Find sensitive endpoints:** Identify admin, profile, financial routes:85 - `rg '/admin|/api/users|/profile|/account|/payment' -i`86 - Check each for authentication decorators/middleware87- **Check for fail-open logic:** Find default permit patterns:88 - `rg 'if.*not.*authenticated.*return|except.*pass' --type py`89 - `rg 'catch.*\{\s*\}|if.*!auth.*continue' --type js`90- **Test unauthenticated access:** Direct endpoint testing:91 - `curl -X GET https://api.example.com/api/users/me` (no auth header)92 - `curl -X DELETE https://api.example.com/api/admin/users/1` (no token)93 - If these succeed without 401/403, endpoints are vulnerable9495## Prevention9697- [ ] **Default to deny:** Require authentication for all endpoints by default. Explicitly mark public endpoints (login, registration) as exempt98- [ ] **Centralize authentication logic:** Use middleware (Express), decorators (Flask/Django), or filters (Java) for authentication. Avoid repeating logic across functions99- [ ] **Distinguish authentication from authorization:**100 - **Authentication:** Verify user identity101 - **Authorization:** Verify user permissions for action102 - Endpoints must perform both103- [ ] **Use robust authentication:** Implement JWTs, OAuth2, or secure session management. Never roll your own authentication104105## Related Security Patterns & Anti-Patterns106107- [Session Fixation Anti-Pattern](../session-fixation/): Session identifier management after login108- [JWT Misuse Anti-Pattern](../jwt-misuse/): Common token-based authentication mistakes109- [Missing Rate Limiting Anti-Pattern](../missing-rate-limiting/): Login brute-force protection110111## References112113- [OWASP Top 10 A07:2025 - Authentication Failures](https://owasp.org/Top10/2025/A07_2025-Authentication_Failures/)114- [OWASP GenAI LLM06:2025 - Excessive Agency](https://genai.owasp.org/llmrisk/llm06-excessive-agency/)115- [OWASP API Security API2:2023 - Broken Authentication](https://owasp.org/API-Security/editions/2023/en/0xa2-broken-authentication/)116- [OWASP Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)117- [CWE-287: Improper Authentication](https://cwe.mitre.org/data/definitions/287.html)118- [CAPEC-115: Authentication Bypass](https://capec.mitre.org/data/definitions/115.html)119- [PortSwigger: Authentication](https://portswigger.net/web-security/authentication)120- Source: [sec-context](https://github.com/Arcanum-Sec/sec-context)