<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>📁 File Management Vulnerabilities in AI-Generated Code</title>
<style>
  :root{
    --bg:#f9fafc;
    --card:#ffffff;
    --accent:#2563eb;
    --error:#ef4444;
    --safe:#10b981;
    --text:#333;
  }

  body{
    margin:0;
    font-family: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
    background: var(--bg);
    color: var(--text);
    line-height:1.5;
  }

  .file-guide{
    max-width:1400px;
    margin:0 auto;
    padding:20px;
  }

  .file-guide .title{
    font-size:2rem;
    font-weight:800;
    color:var(--accent);
    margin-bottom:10px;
  }

  .file-guide .intro{
    background:var(--card);
    border-radius:12px;
    padding:16px;
    margin-bottom:20px;
    box-shadow:0 4px 12px rgba(0,0,0,0.08);
    font-size:1rem;
  }

  .file-guide .vulnerability{
    background: var(--card);
    border-radius:12px;
    padding:14px;
    margin-bottom:16px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.07);
    transition: transform .18s ease, box-shadow .18s ease;
  }
  .file-guide .vulnerability:hover{
    transform: translateY(-4px);
    box-shadow: 0 10px 24px rgba(0,0,0,0.10);
  }

  .file-guide .subtitle{
    margin:0 0 8px 0;
    color:var(--accent);
    font-size:1.4rem;
    font-weight:800;
  }

  .file-guide .description{
    margin:0 0 6px 0;
    font-size:1rem;
  }

  .file-guide .detection{
    margin:6px 0 8px 0;
    font-size:1.05rem;
    color:#222;
    font-style:italic;
  }

  .file-guide .example{
    display:flex;
    gap:14px;
    flex-wrap:wrap;
    margin-top:10px;
  }

  .file-guide .code, .file-guide .solution{
    flex:1;
    min-width:320px;
    padding:10px;
    border-radius:8px;
    font-size:0.95rem;
  }

  .file-guide .code{
    background:#fff5f5;
    border:1px solid var(--error);
  }
  .file-guide .solution{
    background:#f0fff8;
    border:1px solid var(--safe);
  }

  .file-guide .example-title{
    margin:0 0 8px 0;
    font-size:1.05rem;
    font-weight:700;
  }

  .file-guide pre{
    margin:0;
    font-family:"Courier New", monospace;
    font-size:0.9rem;
    overflow-x:auto;
    white-space:pre-wrap;
    word-break:break-word;
  }

  .file-guide .services{
    margin-top:6px;
    font-size:0.92rem;
  }
  .file-guide .services span{
    display:inline-block;
    background:#eef2ff;
    color:var(--accent);
    padding:4px 8px;
    border-radius:6px;
    margin:4px 6px 0 0;
    font-weight:600;
    font-size:0.85rem;
  }

  .file-guide .links{
    margin-top:8px;
    font-size:1.05rem;
    font-weight:600;
  }
  .file-guide .links a{
    color:var(--accent);
    text-decoration:none;
  }
  .file-guide .links a:hover{ text-decoration:underline; }

  .file-guide .final-section{
    background:var(--card);
    border-radius:12px;
    padding:18px;
    margin-top:30px;
    box-shadow:0 4px 12px rgba(0,0,0,0.1);
  }
  .file-guide .final-section h2{
    color:var(--accent);
    font-size:1.5rem;
    margin-bottom:10px;
  }

  @media (max-width:760px){
    .file-guide .example{ flex-direction:column; }
    .file-guide .code, .file-guide .solution{ min-width:100%; }
  }
</style>
</head>
<body>
<section class="file-guide">

  <h1 class="title">📁 File Management Vulnerabilities in AI-Generated Code</h1>

  <div class="intro">
    <p>
      File management is a common area where insecure AI-generated code appears. While AI can speed up development, it often reproduces unsafe patterns from training data. 
      Attackers can exploit these flaws to access sensitive files, upload malicious content, or expose credentials.
    </p>
    <p>
      Below we outline major vulnerabilities, provide insecure vs. secure code examples, show how to detect them, and highlight which of our services can help mitigate the risk.
    </p>
  </div>

  <!-- Path Traversal -->
  <div class="vulnerability">
    <h2 class="subtitle">1. Path Traversal (CWE-22)</h2>
    <p class="description">
      Concatenating user input directly into file paths allows attackers to access files outside allowed directories. AI-generated code often naively uses string concatenation because it mirrors many examples in its training data without considering security implications. 
      This can unintentionally allow traversal sequences (like "../") that lead to sensitive files. AI may also fail to implement proper sanitization or validation, making exploitation easier.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Python Flask):</h3>
        <pre>
@app.route("/view")
def view_file():
    filename = request.args.get("file")
    return send_file("/var/www/uploads/" + filename)
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution (Python Flask):</h3>
        <pre>
from werkzeug.utils import secure_filename
@app.route("/view")
def view_file():
    filename = secure_filename(request.args.get("file"))
    return send_from_directory("/var/www/uploads", filename)
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Static analysis, code review, fuzzing for traversal payloads.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/22.html" target="_blank">CWE-22</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Improper File Permissions -->
  <div class="vulnerability">
    <h2 class="subtitle">2. Improper File Permissions</h2>
    <p class="description">
      Using overly permissive file permissions exposes files to unauthorized access. AI-generated code frequently sets default permissions without understanding the security impact, often mirroring permissive examples. 
      As a result, sensitive configuration or data files may become world-readable or writable, increasing risk of data leakage or tampering. AI lacks contextual understanding of security principles, so it may even recommend 777 permissions as a "quick solution."
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example:</h3>
        <pre>
with open("config.json", "w") as f:
    f.write(data)
os.chmod("config.json", 0o777)
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution:</h3>
        <pre>
with open("config.json", "w") as f:
    f.write(data)
os.chmod("config.json", 0o600)
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Permission audits, SAST, manual review.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/732.html" target="_blank">CWE-732</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Unrestricted File Uploads -->
  <div class="vulnerability">
    <h2 class="subtitle">3. Unrestricted File Uploads</h2>
    <p class="description">
      Failing to validate file type or size can allow attackers to upload malicious files. AI-generated code may blindly implement file upload features without enforcing restrictions, simply copying patterns from examples. 
      This can result in attackers uploading scripts, executables, or malware that run on the server. AI rarely adds sufficient checks for file extension, MIME type, or scanning, making systems highly vulnerable.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (PHP):</h3>
        <pre>
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution (PHP):</h3>
        <pre>
$filename = basename($_FILES["file"]["name"]);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(in_array($ext, ["jpg","png","txt"])) {
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $filename);
}
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> SAST tools, penetration testing of upload endpoints.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/434.html" target="_blank">CWE-434</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
      <span>Software Composition Analysis</span>
    </div>
  </div>

  <!-- Insecure Temporary Files -->
  <div class="vulnerability">
    <h2 class="subtitle">4. Insecure Temporary Files</h2>
    <p class="description">
      Predictable temp file names or unsafe directories can expose sensitive data. AI-generated code may use common or simple temporary file paths without randomness, reflecting patterns seen in training examples. 
      This can allow attackers to predict file locations, read secrets, or perform race condition attacks. AI does not account for secure handling, leading to unintentional leaks of sensitive information.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Python):</h3>
        <pre>
tmp_path = "/tmp/data.tmp"
with open(tmp_path, "w") as f:
    f.write(secret)
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution (Python):</h3>
        <pre>
import tempfile
with tempfile.NamedTemporaryFile(delete=False) as f:
    f.write(secret.encode())
    tmp_path = f.name
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Code review, SAST, temp file analysis.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/377.html" target="_blank">CWE-377</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Logging Sensitive Data -->
  <div class="vulnerability">
    <h2 class="subtitle">5. Logging Sensitive Data</h2>
    <p class="description">
      Logging passwords, tokens, or API keys can leak secrets to logs. AI-generated code may include debug statements from examples it has seen, or automatically log sensitive variables to assist developers. 
      Without understanding the sensitivity, it can output credentials or keys in plain text, creating an easy attack vector for anyone with log access. AI does not differentiate between safe and sensitive data, increasing risk.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Node.js):</h3>
        <pre>
console.log("User password: " + password);
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution (Node.js):</h3>
        <pre>
console.log("User logged in: " + username);
// Do not log passwords or sensitive tokens
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Secret scanning, manual review, logging audits.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/532.html" target="_blank">CWE-532</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Final Section -->
  <div class="final-section">
    <h2>🔧 How Our Services Help</h2>
    <ul>
      <li><strong>SonarQube Setup Assistance:</strong> Detects insecure file handling, unsafe permissions, risky uploads, temp file issues, and sensitive logging.</li>
      <li><strong>Source Code Review:</strong> Expert review of AI-generated code for all file management vulnerabilities.</li>
      <li><strong>Software Composition Analysis:</strong> Detects vulnerable dependencies affecting file handling and uploads.</li>
      <li><strong>Software Licence Analysis:</strong> Ensures compliance for third-party components in AI-generated projects.</li>
    </ul>
  </div>

</section>
</body>
</html>