Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/langchain-security-basics

@0d57af8
Run it yourself

Harden a LangChain 1.0 chain or LangGraph agent against prompt injection, tool abuse, PII leakage in traces, and secrets exfiltration — wrap user content in XML tags, enforce the tool allowlist via provider-native tool calling, redact PII in middleware upstream of cache and tracing, validate outputs with Pydantic, and lock down secrets behind a secret manager. Use when prepping for a security review, responding to an incident, building a multi-tenant SaaS, or writing a threat model. Trigger with "langchain security", "prompt injection defense", "langchain tool allowlist", "langchain PII redaction", "langchain secrets management".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/langchain-security-basics

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencestool-allowlist-enforcement.md

≈1.7k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Tool Allowlist Enforcement

The tool allowlist — the list of tools an agent is allowed to call — is a security boundary, not a suggestion. This reference covers how to make it actually enforceable, the free-text ReAct trap that makes it advisory, and the arg-level deny-list that stops the allowed tools from being weaponized.

The free-text ReAct trap (P32)

The legacy ReAct pattern prompted the model with text like:

You have access to: lookup_order, lookup_shipment
Respond in this format:
Action: <tool_name>
Action Input: <json>

The agent parsed Action: <name> lines out of the response text. If the model hallucinated Action: shell_exec, a permissive parser tried to call tools_by_name["shell_exec"] — KeyError, or in a looser implementation, fell through to a default handler. The allowlist was only as strong as the parser's exception handling.

Provider-native tool calling (Anthropic's tool_use, OpenAI's function_calling, Gemini's function_calling) replaces text parsing with a structured API contract: the model returns a tool call object whose name field is constrained by the server to the tool schema sent in the request. The model physically cannot emit a tool name outside the bound list.

The enforcement primitive — create_react_agent

LangGraph's prebuilt create_react_agent (1.0+) uses provider-native tool calling. Bind the tools you want, and the provider enforces the allowlist:

from langchain_anthropic import ChatAnthropic
from langgraph.prebuilt import create_react_agent
from langchain_core.tools import tool

@tool
def lookup_order(order_id: str) -> str:
    """Look up an order by ID."""
    return db.fetch_order(order_id)

model = ChatAnthropic(model="claude-sonnet-4-6", temperature=0)
agent = create_react_agent(model, tools=[lookup_order])

No parser, no string matching, no room for shell_exec to leak through. If someone asks you to add a free-text ReAct agent to a production codebase, treat it as a security review item — it is.

Per-request allowlist for multi-tenant

In a multi-tenant SaaS, Tenant A may call lookup_order while Tenant B may not. Bind tools per request:

def agent_for_tenant(tenant_id: str):
    perms = permissions_for(tenant_id)
    tools = [t for t in ALL_TOOLS if t.name in perms.allowed_tools]
    return create_react_agent(model, tools=tools)

# Per-request
agent = agent_for_tenant(request.tenant_id)
result = agent.invoke({"messages": [("user", request.query)]})

Constructing a fresh agent per request is cheap (< 1ms) compared to the model call. Do not cache by tenant — a deploy could leak tenants (see P33 in the pack pain catalog).

Tool-arg deny-list

The allowlist covers tool names. Tool arguments are still free text the model chooses. A WebBaseLoader tool called with a link-local URL probes internal services:

URL What it probes
http://169.254.169.254/latest/meta-data/ AWS instance metadata (IAM creds)
http://metadata.google.internal/ GCP instance metadata
http://169.254.169.254/metadata/instance?api-version=2021-02-01 Azure instance metadata
http://127.0.0.1:6379/ Local Redis
` Local filesystem
ftp://internal-share.corp/ Internal FTP

The defense is a Pydantic validator on every URL-accepting tool:

from pydantic import BaseModel, HttpUrl, field_validator
from urllib.parse import urlparse

ALLOWED_DOMAINS = {"docs.example.com", "api.example.com"}
BLOCKED_HOSTS = {
    "169.254.169.254", "127.0.0.1", "0.0.0.0", "::1",
    "localhost", "metadata.google.internal",
}
BLOCKED_CIDRS = [  # link-local + loopback
    "127.0.0.0/8", "169.254.0.0/16", "10.0.0.0/8",
    "172.16.0.0/12", "192.168.0.0/16",
]

import ipaddress

def _is_blocked_ip(host: str) -> bool:
    try:
        ip = ipaddress.ip_address(host)
    except ValueError:
        return False
    return any(ip in ipaddress.ip_network(c) for c in BLOCKED_CIDRS)

class FetchArgs(BaseModel):
    url: HttpUrl

    @field_validator("url")
    @classmethod
    def _check_host(cls, v):
        host = urlparse(str(v)).hostname or ""
        if host in BLOCKED_HOSTS or _is_blocked_ip(host):
            raise ValueError(f"blocked host: {host}")
        if host not in ALLOWED_DOMAINS:
            raise ValueError(f"host not in allowlist: {host}")
        return v

@tool
def fetch_url(args: FetchArgs) -> str:
    """Fetch a web page. Only allowlisted domains permitted."""
    return requests.get(str(args.url), timeout=5).text

Tool-arg patterns to reject

Pattern Reject because
Shell metacharacters in a command-like arg: ;, |, &&, backticks Arg is building a shell command — never let the model build shell commands
../ in file paths Path traversal
file://, ftp://, gopher:// in URL args SSRF / exfil
Raw SQL fragments in a "query" arg SQL injection — use parameterized queries, not model-generated SQL
Arg length > 10KB Prompt-injection via arg — inspect before use

Never: shell tools in production

A run_shell(command: str) tool is a security incident waiting to happen. If you need command execution, use a sandboxed executor (gVisor, Firecracker, a Docker container with read-only filesystem and no network) and an arg allowlist so the model can only run predefined commands with parameterized arguments:

ALLOWED_COMMANDS = {
    "list_files": ["ls", "-la"],
    "check_disk": ["df", "-h"],
}

@tool
def sandboxed_command(name: str) -> str:
    """Run a preapproved read-only diagnostic command. Name must be one of: list_files, check_disk."""
    if name not in ALLOWED_COMMANDS:
        raise ValueError(f"unknown command: {name}")
    return subprocess.run(ALLOWED_COMMANDS[name], capture_output=True, text=True).stdout

The model chooses a command name, never builds a command string.

Auditability

Every tool call should emit a structured log event: {tool, args, tenant_id, request_id, result_summary, duration_ms}. In multi-tenant, partition logs per tenant so an auditor can reconstruct the exact tool-call chain. Tie back to the OTEL trace via request_id (see langchain-observability).

Resources

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides a comprehensive set of security best practices for LangChain 1.0 and LangGraph applications. It covers prompt injection defenses, secret management, PII redaction, tool allowlisting, and compliance posture. The identified prompt injection patterns are educational examples used to illustrate threats and demonstrate defensive coding techniques, posing no security risk to the user or system.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: MEDIUM · 1 issue

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 2 months ago
What it can do
Reads files Edits files Runs commands
Modelinherit
model
inherit
argument-hint
<chain-or-agent-path>
user-invocable
true
All 5 allowed tools
ReadWriteEditGrepBash(grep:*)
Other metadata
disable-model-invocation
false
version
2.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "security",
  "prompt-injection",
  "pii",
  "owasp-llm"
]
compatibility
Designed for Claude Code

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-security-basics