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.

referencessecrets-lifecycle.md

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

Secrets Lifecycle — Dev to Prod

A secret that reaches os.environ can be read by anyone with docker exec, kubectl exec, or a crashed-process dump (P37). This reference walks the full secret lifecycle: where it lives per environment, how it's loaded into the app, and how pydantic.SecretStr keeps it from leaking through logs.

Environment matrix

Environment Secret source Loaded via Exported to os.environ?
Local dev .env (gitignored) python-dotenv Yes (accepted risk — no PII, dev keys only)
CI GitHub secrets / CI vault Action injects env var Yes (job-scoped, rotated on each run)
Staging Secret Manager In-process fetch → SecretStr No
Production Secret Manager + scheduled rotation In-process fetch → SecretStr No

The critical rule: in staging and production, secrets never touch os.environ. They are fetched into memory and wrapped immediately.

.env in dev — the rules that keep it safe

  1. .env in .gitignore, always. Verify with git check-ignore .env.
  2. .env.example committed with placeholder values so new devs know which keys are required.
  3. Use dev-tier provider keys (separate from prod) — rotate if a dev machine is lost.
  4. Never COPY .env in a Dockerfile. Use build args or runtime injection.
# dev_config.py — only imported when APP_ENV=dev
from dotenv import load_dotenv
import os

load_dotenv(".env")
anthropic_key = os.getenv("ANTHROPIC_API_KEY")

Staging and production — secret manager flow

Three common providers. Pick one per infra.

GCP Secret Manager

from google.cloud import secretmanager
from pydantic import SecretStr

def fetch_secret(project: str, name: str, version: str = "latest") -> SecretStr:
    client = secretmanager.SecretManagerServiceClient()
    path = f"projects/{project}/secrets/{name}/versions/{version}"
    resp = client.access_secret_version(name=path)
    return SecretStr(resp.payload.data.decode("utf-8"))

anthropic_key = fetch_secret("my-project", "anthropic-api-key")

IAM: grant roles/secretmanager.secretAccessor to the service account the app runs as. Do not grant to humans unless they're debugging a specific incident — use break-glass access logs.

AWS Secrets Manager

import boto3
from pydantic import SecretStr

def fetch_secret(name: str, region: str = "us-east-1") -> SecretStr:
    client = boto3.client("secretsmanager", region_name=region)
    resp = client.get_secret_value(SecretId=name)
    return SecretStr(resp["SecretString"])

openai_key = fetch_secret("prod/openai-api-key")

IAM: least-privilege policy granting secretsmanager:GetSecretValue on the specific secret ARN, not *.

HashiCorp Vault

import hvac
from pydantic import SecretStr

def fetch_secret(path: str, field: str) -> SecretStr:
    client = hvac.Client(url="https://vault.corp.example.com", token=VAULT_TOKEN)
    resp = client.secrets.kv.v2.read_secret_version(path=path)
    return SecretStr(resp["data"]["data"][field])

gemini_key = fetch_secret("langchain-prod", "gemini_api_key")

Vault: prefer AppRole or Kubernetes auth over static tokens. Rotate tokens on every deploy.

pydantic.SecretStr end to end

SecretStr exists to defeat accidental print() / repr() / JSON serialization:

>>> from pydantic import BaseModel, SecretStr
>>> class Settings(BaseModel):
...     api_key: SecretStr
...
>>> s = Settings(api_key=SecretStr("sk-super-secret"))
>>> print(s)
api_key=SecretStr('**********')
>>> s.model_dump_json()
'{"api_key":"**********"}'
>>> s.api_key.get_secret_value()  # explicit opt-in to read
'sk-super-secret'

LangChain 1.0 provider integrations accept SecretStr directly:

from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(
    model="claude-sonnet-4-6",
    api_key=settings.api_key,  # SecretStr — no .get_secret_value() needed
)

Never .get_secret_value() except when passing to a provider that requires a string, and never log the return value.

Rotation

Secret type Rotation cadence Method
Provider API key (Anthropic/OpenAI/Gemini) 90 days Generate new key → deploy → revoke old
Database password 30 days Secret Manager auto-rotation where supported
Vault token Per deploy New token on each pod start
Webhook signing secret 90 days or on compromise Accept both old + new for grace period

Rotation implementation: the fetch helper re-reads the secret on each process start. For long-lived processes, add a scheduled background refresh every 15 minutes so a rotated secret propagates without a redeploy.

Leakage scenarios and mitigations

Scenario Mitigation
docker exec <pod> env shows ANTHROPIC_API_KEY=... (P37) Don't export to env. Fetch into SecretStr in-process only.
Crash dump uploaded to Sentry contains the secret SecretStr masks in repr; set Sentry's before_send to strip any string matching sk-...
Git history includes .env from early commit git filter-repo to rewrite history; rotate every leaked secret
CI logs echo secret Use ::add-mask:: in GitHub Actions, or CI-vault-provided masking
kubectl describe pod shows env Use Kubernetes Secret resource mounted as file, not env var; read into SecretStr

What a reviewer will check

  • .env in .gitignore
  • No COPY .env in Dockerfiles
  • Process env in prod pod does not contain API keys (kubectl exec <pod> env | grep -iE 'key|secret|token' returns nothing)
  • Secret Manager IAM grants least privilege to the service account
  • Rotation schedule documented and audit-logged
  • Settings classes use SecretStr, not str, for every credential field
  • Sentry / error-tracker has a scrubber for provider key patterns

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