<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>⚡ Resource 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">⚡ Resource Management Vulnerabilities in AI-Generated Code</h1>

  <div class="intro">
    <p>
      Resource Management vulnerabilities occur when AI-generated code fails to properly allocate, manage, or release system resources such as memory, file handles, network sockets, or database connections. Mismanagement can lead to leaks, crashes, denial of service, or unexpected behavior.
    </p>
    <p>
      AI-generated code often magnifies these problems because AI models may prioritize completing tasks over best practices, replicate unsafe patterns from training data, or fail to consider the lifecycle of system resources. Below we outline key vulnerabilities with detailed explanations, examples, detection methods, and our services that mitigate these risks.
    </p>
  </div>

  <!-- Improper resource shutdown -->
  <div class="vulnerability">
    <h2 class="subtitle">1. Improper Resource Shutdown (CWE-404)</h2>
    <p class="description">
      AI-generated code can forget to close files, sockets, or database connections. This often happens because AI tools focus on generating functional code snippets without understanding the resource lifecycle. Over time, leaving resources open accumulates, causing memory leaks, file descriptor exhaustion, and potential service crashes.
    </p>
    <p class="description">
      For example, AI might replicate a common snippet from tutorials that reads files but does not use context managers or finally blocks. In large systems, repeated improper shutdown can cause unpredictable failures, especially under high load.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Python):</h3>
        <pre>
file = open("data.txt", "r")
data = file.read()
# forgot to close the file
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution:</h3>
        <pre>
with open("data.txt", "r") as file:
    data = file.read()
# automatically closes file
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Code review, static analysis, resource monitoring.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/404.html" target="_blank">CWE-404</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Missing release of resources -->
  <div class="vulnerability">
    <h2 class="subtitle">2. Missing Release of Resources (CWE-772)</h2>
    <p class="description">
      AI-generated code may allocate resources like threads, memory, or database connections but fail to release them. This can occur because AI models often generate isolated snippets without global context, missing cleanup logic.
    </p>
    <p class="description">
      This leads to resource leaks, degraded performance, and potential denial-of-service over time. In multi-user or multi-threaded environments, unreleased resources can bottleneck the system, making it fragile and difficult to debug.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Java):</h3>
        <pre>
Connection conn = DriverManager.getConnection(dbURL);
// do some operations
// forgot conn.close();
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution:</h3>
        <pre>
try (Connection conn = DriverManager.getConnection(dbURL)) {
    // perform operations
} // conn automatically closed
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Resource tracking, static analysis, unit tests.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/772.html" target="_blank">CWE-772</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>

  <!-- Multiple binds to the same port -->
  <div class="vulnerability">
    <h2 class="subtitle">3. Multiple Binds to the Same Port (CWE-605)</h2>
    <p class="description">
      AI-generated code can suggest binding multiple sockets or services to the same port because it lacks context about runtime constraints. This can happen when copying network setup examples from different sources without checking for conflicts.
    </p>
    <p class="description">
      Multiple binds on the same port can lead to runtime errors, service crashes, or undefined behavior. AI-generated code can worsen this if it duplicates snippets without checking system state, especially in containerized or concurrent environments.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Node.js):</h3>
        <pre>
const net = require('net');
net.createServer().listen(3000);
net.createServer().listen(3000); // AI suggested duplicate
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution:</h3>
        <pre>
const net = require('net');
const server = net.createServer();
server.listen(3000);
// Ensure only one bind per port
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Runtime monitoring, static analysis.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/605.html" target="_blank">CWE-605</a>
    </div>
    <div class="services">
      <strong>🔧 Services we offer:</strong>
      <span>SonarQube Setup Assistance</span>
      <span>Source Code Review</span>
    </div>
  </div>

  <!-- Improper resource control -->
  <div class="vulnerability">
    <h2 class="subtitle">4. Improper Resource Control (CWE-664)</h2>
    <p class="description">
      AI-generated code may allocate resources without proper limits because it often tries to generate “working” code rather than robust, scalable code. This can allow a single user or process to consume excessive CPU, memory, or file handles.
    </p>
    <p class="description">
      Improper resource control can lead to crashes, degraded performance, or service denial. AI models may not account for system constraints, concurrency, or multi-user scenarios, which worsens the impact in production environments.
    </p>
    <div class="example">
      <div class="code">
        <h3 class="example-title">AI Insecure Example (Python):</h3>
        <pre>
def create_threads(n):
    threads = []
    for i in range(n):
        t = threading.Thread(target=do_work)
        threads.append(t)
        t.start()
# no limit on n, may crash system
        </pre>
      </div>
      <div class="solution">
        <h3 class="example-title">Safe Solution:</h3>
        <pre>
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor(max_workers=10) as executor:
    for i in range(10):
        executor.submit(do_work)
# limit resources to prevent exhaustion
        </pre>
      </div>
    </div>
    <p class="detection"><strong>Detection:</strong> Load testing, code review, monitoring.</p>
    <div class="links">
      Reference: <a href="https://cwe.mitre.org/data/definitions/664.html" target="_blank">CWE-664</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>

  <!-- Final Section -->
  <div class="final-section">
    <h2>🔧 How Our Services Help</h2>
    <ul>
      <li><strong>SonarQube Setup Assistance:</strong> Detects improper resource shutdown, missing resource releases, duplicate port bindings, and improper resource allocation patterns.</li>
      <li><strong>Source Code Review:</strong> Expert review of AI-generated code for all resource management vulnerabilities.</li>
      <li><strong>Software Composition Analysis:</strong> Detects third-party modules that may mismanage resources or introduce leaks.</li>
      <li><strong>Software Licence Analysis:</strong> Ensures compliance for third-party components affecting resource handling.</li>
    </ul>
  </div>

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