🔐 Authentication & Authorization Vulnerabilities in AI-Generated Code
Authentication and authorization are fundamental for application security. AI-generated code can introduce subtle but serious vulnerabilities, allowing unauthorized access, credential leaks, or bypassing role-based restrictions.
The following sections describe the most common issues, explain why AI makes them more likely, provide insecure vs. secure code examples, and list services that can detect and mitigate them.
1. Missing or Flawed Access Control (CWE-284)
Access control failures occur when software does not correctly enforce permissions, or omits checks entirely. AI-generated code can replicate simplistic tutorials, neglecting edge cases and hierarchical roles. This results in sensitive endpoints being exposed to unauthorized users.
Insecure Example (Python Flask):
@app.route("/admin")
def admin_panel():
return render_template("admin.html")
Secure Solution (Python Flask):
from flask_login import login_required, current_user
@app.route("/admin")
@login_required
def admin_panel():
if not current_user.is_admin:
abort(403)
return render_template("admin.html")
Detection: Role-based access audits, SAST, penetration tests.
2. Weak or Hard-Coded Credentials (CWE-798)
Hard-coded passwords or weak credentials are easily compromised. AI-generated code may insert example passwords, default tokens, or weak schemes without verifying security. This exposes the system to brute-force, credential stuffing, or code-leak attacks.
Insecure Example (Node.js):
const adminPassword = "12345";
if (req.body.password === adminPassword) grantAccess();
Secure Solution (Node.js):
const adminPassword = process.env.ADMIN_PASSWORD;
if (req.body.password === adminPassword) grantAccess();
Insecure Example (Python):
PASSWORD = "password123" # Hard-coded
Secure Solution:
import os
PASSWORD = os.environ.get("APP_PASSWORD")
Detection: Secret scanning, static code analysis, and source code review for embedded credentials.
3. Broken Access Control (OWASP A5)
Even when access control mechanisms exist, logic errors or misconfigurations can allow privilege escalation. AI-generated code may overlook subtle conditions, enabling attackers to access admin resources, modify sensitive data, or bypass checks.
Insecure Example (PHP):
if($_SESSION["role"] == "user") {
include("user_page.php");
} else {
include("admin_page.php");
}
Secure Solution (PHP):
if($_SESSION["role"] === "admin") {
include("admin_page.php");
} else if($_SESSION["role"] === "user") {
include("user_page.php");
} else {
http_response_code(403);
echo "Access denied";
}
Detection: Access control testing, session manipulation tests, code review, and SAST.
🔧 How Our Services Help
- SonarQube Setup Assistance: Detects missing access checks, weak credentials, and broken role enforcement.
- Source Code Review: Expert evaluation of AI-generated authentication and authorization logic.
- Software Composition Analysis: Finds vulnerable dependencies affecting authentication modules.
- Software Licence Analysis: Ensures compliance for third-party components in AI-generated projects.