Skip to main content
This guide explains how to use TrueFoundry’s built-in Code Safety Linter guardrail to detect and block potentially dangerous code patterns in LLM outputs.

What is Code Safety Linter?

Code Safety Linter is a built-in TrueFoundry guardrail that analyzes code in LLM responses to detect unsafe patterns that could pose security risks when executed. It runs directly within the AI Gateway without requiring external API calls.

Key Features

  1. Dangerous Function Detection: Identifies potentially dangerous code patterns including:
    • eval() and exec() calls that execute arbitrary code
    • os.system() and subprocess calls (Popen, call, run) for shell command execution
    • rm -rf and recursive delete commands
    • curl | bash and wget | bash remote code execution patterns
    • File system operations like unlink(), rmdir(), and shutil.rmtree()
  2. Multi-Language Support: Detects unsafe patterns across common programming languages:
    • Python: eval, exec, os.system, subprocess, shutil.rmtree, unlink, rmdir
    • JavaScript/Node.js: child_process.exec, child_process.spawn, require(‘child_process’)
    • Shell/Bash: rm -rf, rm -r, curl | bash, wget | sh
    • SQL: DROP TABLE, DELETE FROM, TRUNCATE TABLE
  3. Validation-Only Mode: Code Safety Linter operates in validation mode, detecting and blocking unsafe code rather than modifying it—ensuring code integrity while maintaining security.

Adding Code Safety Linter Guardrail

To add Code Safety Linter to your TrueFoundry setup, follow these steps:
1

Navigate to Guardrails

Go to the AI Gateway dashboard and navigate to the Guardrails section.
2

Create or Select a Guardrails Group

Create a new guardrails group or select an existing one where you want to add the Code Safety Linter guardrail.
3

Add Code Safety Linter Integration

Click on Add Guardrail and select Code Safety Linter from the TrueFoundry Guardrails section.
TrueFoundry guardrail selection interface showing Code Safety Linter option

Select Code Safety Linter from TrueFoundry Guardrails

4

Configure the Guardrail

Fill in the configuration form:
  • Name: Enter a unique name for this guardrail configuration (e.g., code-safety-linter)
  • Description: Optional description for this guardrail (default: “Detects unsafe code patterns in tool outputs (eval, exec, os.system, subprocess, rm -rf)”)
  • Operation: validate (Code Safety Linter only supports validation mode)
5

Save the Configuration

Click Save to add the guardrail to your group.

Configuration Options

Code Safety Linter only supports validate mode—modifying code could break functionality. See Guardrails Overview for details on Enforcing Strategy.
Start with Audit mode to monitor detected patterns in Request Traces before switching to Enforce.

Detected Unsafe Patterns

The Code Safety Linter detects the following categories of unsafe code patterns. Each pattern is designed to identify potentially dangerous code constructs that could pose security risks.

Python Dangerous Functions

Python File System Operations

Shell/Bash Dangerous Commands

JavaScript/Node.js Dangerous Functions

SQL Dangerous Statements

For more comprehensive SQL protection, consider using the SQL Sanitizer guardrail which provides configurable options for SQL-specific patterns.

How It Works

The guardrail scans message content using regex-based detection:
  1. Extracts messages from request/response
  2. Scans against all blocked patterns
  3. Returns findings with pattern name and matched text (truncated to 20 chars)
  4. Returns verdict: true (pass) or false (block), limited to first 10 findings

Example - Blocked Responses

Result: Blocked - Detected subprocess.Popen() call
Result: Blocked - Detected eval() call
Result: Blocked - Detected rm -rf command
Result: Blocked - Detected curl | bash pattern
Result: Blocked - Detected require('child_process') and child_process.exec() calls
Result: Blocked - Detected DROP TABLE statement

Example - Allowed Responses

Result: Allowed - No unsafe patterns detected
Result: Allowed - No unsafe patterns detected
Result: Allowed - No unsafe patterns detected

Use Cases

Agent Tool Output Validation

When using AI agents that execute code via MCP tools, apply Code Safety Linter to validate tool outputs. Configure via guardrail rules:

Code Generation Applications

For LLM responses that generate code, validate via the X-TFY-GUARDRAILS header:
Regex-based detection: Patterns match exact syntax. Obfuscated code may not be detected. Use as part of defense-in-depth, not sole protection.