Course contentsShow
← AI Engineering
Lesson 1502 of 1,88636. Security in AI SystemsFree lesson

Allowlisting Safe Libraries and APIs

Defining and enforcing lists of permitted libraries, modules, and system calls within sandboxed code.

Allowlisting Safe Libraries and APIs

What you'll learn: How to define and enforce strict lists of permitted libraries, modules, and system calls within sandboxed AI-generated code to prevent malicious or unsafe operations.

When your AI system generates or executes code in a sandbox (as introduced in earlier lessons), you've isolated the execution environment—but isolation alone isn't enough. Generated code could still import dangerous libraries like os, subprocess, or socket that enable file deletion, command execution, or network attacks. Allowlisting means explicitly defining what's allowed and blocking everything else.

The Core Idea

Think of allowlisting like airport security: rather than trying to list every dangerous item (blocklisting), you define what passengers can bring through and reject everything else by default. Similarly, you create a curated list of safe libraries (like math, datetime, pandas) and safe functions within those libraries, then configure your sandbox to reject any import or system call not on that list.

Why This Matters

AI-generated code is unpredictable. Even with prompt engineering and input validation, an LLM might generate import os; os.system('rm -rf /') if manipulated. By allowlisting:

  • You prevent access to file systems, network operations, and subprocess execution
  • You constrain the "attack surface" to only vetted, safe operations
  • You make security audits tractable—review the allowlist, not infinite possible exploits

Implementation Approach

Your sandbox runtime intercepts import statements and function calls, checking them against your allowlist before execution. Libraries like RestrictedPython or custom AST (Abstract Syntax Tree) parsers can enforce this at the Python level, while container configurations limit system-level calls.

Key Takeaway:
Allowlisting safe libraries and APIs creates a "secure by default" execution model where only explicitly permitted operations can run, dramatically reducing the risk of malicious AI-generated code exploiting your system.