APILOT: Navigating Large Language Models to Generate Secure Code by Sidestepping Outdated API Pitfalls

Weiheng Bai, Keyang Xuan, Pengxiang Huang, Qiushi Wu, Jianing Wen, Jingjing Wu, Kangjie LuarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) have revolutionized automated code generation but are hampered by a "knowledge cutoff," leading them to suggest deprecated or insecure APIs that were prevalent during their training. This paper introduces APILOT, a navigation system designed to mitigate these risks. APILOT maintains a real-time database of outdated APIs via GitHub commit analysis and employs an iterative, version-aware prompting strategy. Our evaluation shows that APILOT reduces outdated API recommendations by an average of 89.42% and improves code usability by 27.54% by steering models toward modern, functional alternatives.

Introduction

The rapid evolution of software libraries creates a significant challenge for LLMs like GPT-4 or Llama-3. Because these models are trained on static datasets, their internal knowledge of "best practices" quickly becomes obsolete. This leads to the recommendation of deprecated APIs, patched APIs with known vulnerabilities (CVEs), and usage-modified APIs where security fixes have changed function signatures. Existing mitigation strategies, such as simple prompt engineering, often fail to capture the structural nuances of code or are too costly to update frequently.

Methodology: The APILOT Framework

APILOT operates as an intermediary layer between the user and the LLM, following a three-phase process to ensure code security and validity.

The three-phase workflow of APILOT: Collection, Sanitization, and Augmented Generation.
  1. Outdated API Collection: To bypass the latency of official documentation, APILOT performs differential analysis on GitHub commits for popular packages. This allows the system to identify API removals, deprecations, and security-related modifications in real-time.
  2. AST-Based Sanitization: Rather than relying on error-prone keyword matching, APILOT converts generated code into an Abstract Syntax Tree (AST). This structural analysis ensures that only actual function calls are flagged, avoiding false positives in comments or strings.
  3. Iterative Augmented Generation: When an outdated API is detected, APILOT appends it to a "ban list" and re-prompts the LLM with specific instructions to avoid those functions. This process iterates until the code is sanitized or a maximum iteration count is reached.

Evaluation and Results

The researchers evaluated APILOT against a benchmark of 221 deprecated, 100 CVE-patched, and 402 usage-modified APIs across 15 major Python packages.

Baseline failure rates ($F_{API}$) for ChatGPT and Copilot across various Python packages for deprecated and usage-modified APIs.

Key Findings

  • Risk Mitigation: APILOT achieved an average reduction of 89.42% in outdated API recommendations. For models like Llama-3, the reduction reached 100% in several categories.
  • Usability Improvements: By steering LLMs away from removed or modified APIs, the "compilability" and overall usability of the code increased by 27.54%. This suggests that security-aware generation also yields more functional code.
  • The Alternative Pitfall: A critical observation was that LLMs often attempt to substitute one banned outdated API with another. APILOT’s iterative approach is essential to "exhaust" these insecure options until the model identifies a modern solution.
  • Performance Trade-off: The safety benefits come with a latency cost; iterative re-prompting results in an execution time approximately 1.97x longer than standard requests.

Conclusion

APILOT demonstrates that structural analysis combined with real-time API tracking can effectively bridge the gap created by LLM knowledge cutoffs. While currently focused on Python and GitHub-hosted projects, the framework provides a scalable blueprint for secure code generation. Future work will focus on expanding language support and reducing the latency of the iterative prompting cycle to facilitate integration into real-time development environments.

Summary updated

BibTeX
@misc{bai2024apilot,
  author = {Weiheng Bai and Keyang Xuan and Pengxiang Huang and Qiushi Wu and Jianing Wen and Jingjing Wu and Kangjie Lu},
  title = {{APILOT: Navigating Large Language Models to Generate Secure Code by Sidestepping Outdated API Pitfalls}},
  year = {2024},
  eprint = {2409.16526},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.16526},
}
Figure