[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7AS1TPF6WGYFQK432X05NNG":3,"skill-same:01M7AS1TPF6WGYFQK432X05NNG":48},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":29,"ownerHtmlUrl":30,"ownerBlogUrl":31,"ownerDescription":32,"updatedAt":33,"install":34,"resources":47},"01M7AS1TPF6WGYFQK432X05NNG","ai-security-practices",null,"AI security and cybersecurity best practices for software development (Python and TypeScript\u002FNext.js) covering prompt injection defense, OWASP LLM Top 10 2025, agentic AI risks (OWASP ASI Top 10), MCP security, RAG attack vectors, output sanitization, guardrail architectures (NeMo Guardrails, LLM Guard), secrets management for AI APIs, Denial of Wallet prevention, supply-chain security (SLSA\u002FSigstore\u002FSBOMs), critical Next.js\u002FReact CVEs, and NIST AI RMF. Use when advising on securing AI systems, LLM applications, agent architectures, or web application security in Python\u002FTypeScript stacks.","the-vibey-project\u002Fvibey","the-vibey-project","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F329298758?v=4","01M7AJHGZ9722CTJ4JNQZVRH0Q","the vibey project","01M7AJHGZVMG2K7TGXQ6XFHVG3",5,"MIT","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fsecurity-principles\u002Fskills\u002Fai-security-practices\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: ai-security-practices\ndescription: \"AI security and cybersecurity best practices for software development (Python and TypeScript\u002FNext.js) covering prompt injection defense, OWASP LLM Top 10 2025, agentic AI risks (OWASP ASI Top 10), MCP security, RAG attack vectors, output sanitization, guardrail architectures (NeMo Guardrails, LLM Guard), secrets management for AI APIs, Denial of Wallet prevention, supply-chain security (SLSA\u002FSigstore\u002FSBOMs), critical Next.js\u002FReact CVEs, and NIST AI RMF. Use when advising on securing AI systems, LLM applications, agent architectures, or web application security in Python\u002FTypeScript stacks.\"\n---\n\n# AI Security Practices: 2025–2026 Reference for Python & TypeScript\u002FNext.js\n\n## The Three Core Truths\n\n1. **Treat the LLM as an untrusted, internet-connected user.** Defense-in-depth (input validation + output sanitization at every boundary, least-privilege tool access, human-in-the-loop for high-impact actions) is the only viable posture, because prompt injection cannot be fully patched away.\n2. **The biggest 2025–2026 attack-surface shifts are agentic AI and the framework deserialization layer.** The Next.js\u002FReact Server Components RCE (CVE-2025-55182 \u002F CVE-2025-66478, CVSS 10.0, actively exploited within days) and the middleware auth-bypass (CVE-2025-29927, CVSS 9.1) show that \"secure-by-default\" frameworks still require continuous patching.\n3. **AI-generated code is shipping security debt at scale.** Veracode (July 2025): 45% of AI-generated code samples failed security tests; XSS failing 86%, log injection 88%. Apiiro: 10,000+ new security findings\u002Fmonth (10× spike), privilege-escalation paths up 322%. GitGuardian: AI-assisted commits leak secrets at 3.2% vs 1.5% baseline.\n\n---\n\n## OWASP LLM Top 10 (2025 Edition)\n\n| Rank | Risk | Key Point |\n|---|---|---|\n| LLM01 | **Prompt Injection** | #1 for second consecutive edition; both direct and indirect; cannot be fully patched at input layer |\n| LLM02 | Sensitive Information Disclosure | — |\n| LLM03 | Supply Chain (model provenance) | Pickle-based RCE, malicious weights, poisoned training data |\n| LLM04 | Data and Model Poisoning | — |\n| LLM05 | **Improper Output Handling** | Treating model output as trusted → XSS, SQLi, SSRF, RCE |\n| LLM06 | **Excessive Agency** | Over-permissioned tools, agentic misuse |\n| LLM07 | System Prompt Leakage | — |\n| LLM08 | **Vector and Embedding Weaknesses** | RAG security — new in 2025 edition |\n| LLM09 | Misinformation (renamed Overreliance) | — |\n| LLM10 | **Unbounded Consumption** | \"Denial of Wallet\" — new in 2025 edition |\n\n---\n\n## Prompt Injection (LLM01:2025) — The Unfixable Risk\n\n### Why It Cannot Be Patched Away\nLLMs process instructions and data in the same channel without clear separation. No input filter is fully reliable — adaptive attacks evade classifiers.\n\n### Direct vs Indirect Injection\n- **Direct:** \"ignore previous instructions\"\n- **Indirect (harder):** malicious instructions embedded in a webpage, RAG document, email, or tool result the model later reads — NOT caught by input-only classifiers\n- **Multimodal:** instructions hidden in images\n\n### Defensible Architectural Patterns (More Robust Than Detection)\n1. **Action-selector:** constrain the set of actions an LLM can take\n2. **Plan-then-execute:** LLM produces a plan; deterministic code validates and executes it\n3. **Dual-LLM** (Simon Willison): one privileged LLM for user instructions, one quarantined for external content\n4. **CaMeL** (Google DeepMind, March 2025): privileged planner + quarantined LLM executor with enforced information-flow controls\n5. **IsolateGPT** (NDSS 2025): sandboxed execution of external content\n\n### Detection Tooling (Treat as One Layer, Not the Solution)\n- **Meta Prompt-Guard \u002F Llama Guard 3** (open-weight, 8B): outperforms GPT-4 on injection detection with ~1\u002F3 the false-positive rate\n- **NVIDIA NeMo** jailbreak heuristics\n- **LLM Guard** (Protect AI): input scanners\n- **DataSentinel**, **PIShield**\n\n### Implementation\n- **Python:** run Llama Guard via vLLM or LLM Guard input scanners before the model call; enforce a Pydantic output schema after\n- **TypeScript:** validate model output with **Zod** before it touches SQL\u002FHTML\u002Fshell; never pass raw LLM text to `dangerouslySetInnerHTML`\n\n---\n\n## Output Sanitization (LLM05:2025 Improper Output Handling)\n\n**Threat:** treating model output as trusted → XSS, SQLi, SSRF, or RCE when output flows into downstream interpreters.\n\n**Pattern:** validate\u002Fencode output by context:\n- Schema validation: Pydantic (Python) \u002F Zod (TypeScript)\n- Context-aware output encoding\n- Parameterized queries (never string-concatenate LLM output into SQL)\n- PII redaction: Presidio-based (built into NeMo Guardrails and Guardrails AI Hub)\n\n---\n\n## Guardrail Architectures\n\n### Layer Model\n```\ninput rail (jailbreak\u002FPII) \n  → retrieval rail (RAG chunks) \n  → execution rail (tool-call gating) \n  → output rail (moderation\u002FPII\u002Fformat)\n```\nNo single framework covers all ten OWASP items — layer them.\n\n### NVIDIA NeMo Guardrails\n- Open-source, Apache 2.0; v0.17.0 (Oct 2025); **NVIDIA labels it beta \u002F \"additional hardening required for production\"**\n- Five rail types: input, dialog, retrieval, execution, output\n- Uses the Colang DSL; uniquely models multi-turn dialog\n- Integrates: Llama Guard, Presidio PII detection, NemoGuard-8b content-safety\u002Ftopic-control NIMs\n- Supports streaming output checks\n- Integrates with LangChain\u002FLangGraph\n\n### Other Options\n| Tool | Model | Best For |\n|---|---|---|\n| **Guardrails AI** | RAIL spec + validator Hub, `num_reasks` for auto re-prompting | Python-centric apps |\n| **LLM Guard** (Protect AI) | Input + output scanners | Composable pipeline |\n| **Lakera Guard** | Managed API | Minimal integration effort |\n| **Azure AI Content Safety** | Managed Azure | Azure-native teams |\n| **AWS Bedrock Guardrails** | Managed AWS | AWS-native teams |\n\n### Red-Teaming Tools\nGarak, PyRIT, Promptfoo, DeepTeam\n\n---\n\n## RAG Security (LLM08:2025)\n\n### Attack Vectors\n- **PoisonedRAG** (USENIX Security 2025): 5 malicious documents → 90%+ attack success rate on databases with millions of entries (97% on NQ, 99% on HotpotQA, 91% on MS-MARCO in black-box settings)\n- **Embedding-inversion attacks:** recover 50–70% of source text from stolen vectors (ALGEN 2025: ~1,000 samples, transfers across black-box encoders)\n- **Cross-tenant leakage** through shared vector stores\n- **Hidden-text injection:** white-on-white resume text ingested as legitimate content\n\n### Defense-in-Depth Across Ingest \u002F Retrieve \u002F Generate\n**Ingest:**\n- Validate and authenticate all document sources\n- Strip hidden\u002Fzero-width text and ignore formatting on ingestion\n- Detect and reject suspicious instruction patterns in documents\n\n**Store:**\n- Per-tenant physical isolation or DB-layer namespace filtering (not app-layer)\n- Treat the vector DB as a sensitive data store: encrypt, access-control\n- Embedding anomaly detection\n\n**Retrieve\u002FGenerate:**\n- Retrieval rails\n- Per OWASP\u002FSnyk LLM Security Verification Standard (Aug 2025): full verification checklist\n\n---\n\n## Agentic AI Security (LLM06:2025 + OWASP ASI Top 10)\n\n### Threat Classes\n- Tool misuse, identity\u002Fprivilege abuse, rogue agents\n- Unexpected code execution (ASI05)\n- Memory poisoning, resource overload, cascading hallucination\n- Real CVEs: Claude Code data exfiltration via DNS (CVE-2025-55284); Cursor \"AgentFlayer\" via malicious Jira ticket\n\n### Mandated Controls\n1. **Least-privilege tool scoping** — the planner often needs no tools; scope each tool to only what it needs\n2. **Sandboxing** — OWASP ASI: \"Never execute agent-generated code without strict sandboxing, input validation, and allowlisting\"\n   - **Firecracker microVMs:** strongest isolation\n   - **gVisor:** syscall-level isolation\n   - **V8 isolates:** JS, latency-critical\n3. **Default-deny reads** of `.env`\u002Fsecrets\n4. **Egress allow-lists** — not egress defaults\n5. **Human approval gates** for high-stakes or irreversible actions\n6. **Emergency kill switches** and circuit breakers\n7. **Continuous behavioral monitoring**\n\n**Note:** CVE-2025-59528 (CVSS 10.0) and the Google Antigravity sandbox escape confirm that app-level controls fail without isolated execution.\n\n### Frameworks\n- Progent (programmable privilege control)\n- Microsoft Agent Governance Toolkit (April 2026)\n\n---\n\n## MCP Security (New, Under-Secured Layer)\n\n**Current state (Bitsight Research, December 2025):** ~1,000 MCP servers exposed on the public internet with no authorization controls — some allowing Kubernetes cluster command execution, CRM access, and arbitrary shell commands.\n\n### Documented Threats\n- **Tool poisoning:** malicious MCP server returns harmful tool definitions\n- **Rug pulls:** tool behavior changes after trust is established\n- **Tool\u002Fghost shadowing:** malicious tool impersonates legitimate one\n- **Command injection:** CVE-2025-49596 (MCP-Inspector, fixed in 0.14.1)\n- **Confused-deputy attacks**\n- First malicious MCP package appeared September 2025\n\n### Controls\n- OAuth-enhanced tool definitions + policy-based access control (ETDI)\n- Gateway\u002Fproxy with authentication\n- Audit logging of all tool calls\n- Secret management for MCP server credentials\n- Human-in-the-loop for tool calls accessing sensitive systems\n- References: NSA MCP guidance (May 2026), OWASP MCP Top 10, CISA joint guidance (May 22, 2025)\n\n---\n\n## Secrets Management for AI Services\n\n### The Scale of the Problem\nGitGuardian State of Secrets Sprawl 2026 (March 2026):\n- 28.65 million new hardcoded secrets added to public GitHub commits in 2025 (+34% YoY, largest single-year jump)\n- AI-assisted commits leak secrets at **3.2%** vs 1.5% GitHub-wide baseline\n- 1,275,105 AI service secrets detected in 2025 (+81% over 2024)\n- 8 of 10 fastest-growing detector categories tied to AI services\n- ~113,000 leaked DeepSeek API keys as one example\n\n### OpenAI Key Management\n- Use **project-scoped keys** (`sk-proj-` prefix, replaces legacy org-wide `sk-` keys since April 2024)\n- Use **service account keys** for CI\u002Fagents\n- Separate **Admin key** for the management API\n- RBAC at org\u002Fproject level; IP allowlisting\n- **Ephemeral Realtime client secrets** for browser sessions (never expose long-lived keys client-side)\n- On compromise: rotate immediately from the API Keys page\n\n### Anthropic Key Management\n- Keys (`sk-ant-`) are **workspace-scoped**\n- **Admin API keys** (`ANTHROPIC_ADMIN_KEY`) for management — org-admin only\n- **Workload Identity Federation:** exchanges OIDC tokens for short-lived `sk-ant-oat01-` tokens (default 3,600s, min 60s) — no `ANTHROPIC_API_KEY` secret to create, store, or rotate\n- Official guidance: rotate keys ~every 90 days; set usage\u002Fspend limits as a safeguard\n\n### Secrets Managers\n| Tool | Approach | Notes |\n|---|---|---|\n| **HashiCorp Vault** | True dynamic\u002Fshort-lived secrets | BSL license; acquired by IBM 2025 |\n| **AWS Secrets Manager \u002F Azure Key Vault \u002F GCP Secret Manager** | Managed, scheduled rotation | Cloud-native |\n| **Doppler** | SaaS secrets sync | Developer-friendly |\n| **Infisical** | MIT, self-hostable | Open-source option |\n\nNever store AI keys client-side — route through a server-side proxy.\n\n### Python Implementation\n- **pydantic-settings** (`BaseSettings` + `SettingsConfigDict(env_file=...)` with `SecretStr` and `.get_secret_value()`; supports `secrets_dir` for Docker secrets)\n- **python-dotenv** (does not override existing env vars by default)\n- Add `.env` to `.gitignore` on day one; ship `.env.example`\n- Pre-commit hooks: **detect-secrets** \u002F **Gitleaks** \u002F **TruffleHog**\n\n### Next.js Implementation\n- Only `NEXT_PUBLIC_`-prefixed vars reach the browser (inlined at build time) — never put secrets there\n- Access `process.env` only in a server-only Data Access Layer (`import 'server-only'`)\n- React **taint APIs** (`experimental_taintObjectReference`\u002F`taintUniqueValue`) prevent accidental client exposure\n- Post-build: grep `.next\u002Fstatic\u002Fchunks` for leaked secrets\n\n---\n\n## Denial of Wallet (LLM10:2025 Unbounded Consumption)\n\n**Threat:** \"excessive and uncontrolled inferences, leading to denial of service (DoS), economic losses, model theft, and service degradation.\" Bills can be business-ending.\n\n### Controls\n- Set explicit `max_tokens` on **every** call\n- **Token-based limiting** (input+output tokens) AND **cost-based caps**, not just request counts\n- Pre-count tokens with `tiktoken` before sending\n- Per-user\u002Fper-API-key\u002Fper-endpoint limits\n- Timeouts and throttling\n- Provider dashboard hard\u002Fsoft spend caps\n  - OpenAI: billing limits → 429 on hard limit\n  - Anthropic: monthly cap + alerts (essential — billing is post-usage)\n- Continuous logging\u002Fmonitoring; graceful degradation\n\n### Python Rate Limiting\n- **slowapi** (Starlette\u002FFastAPI, v0.1.9): Redis\u002FMemcached backends; custom `key_func` for per-user\u002Fper-key limits; token-bucket\u002Fsliding-window via `limits`\n- **fastapi-limiter** (Redis async)\n- Atomic Redis+Lua token buckets for distributed limiting\n\n### Next.js Rate Limiting\n- **@upstash\u002Fratelimit** (connectionless HTTP, v2.0.8): `fixedWindow`\u002F`slidingWindow`\u002F`tokenBucket`; multi-region; ephemeral in-memory DDoS cache\n- **Arcjet:** rate limiting + bot detection + shield, `aj.protect(req)`\n\n---\n\n## Critical Next.js \u002F React CVEs (2025–2026)\n\n### React2Shell RCE — CVE-2025-55182 \u002F CVE-2025-66478 (CVSS 10.0)\n- **Unauthenticated RCE** exploitable on a default `create-next-app`\n- React Server Components deserialization vulnerability\n- **Actively exploited in the wild within days of disclosure**\n- Follow-on CVEs landed through April 2026: CVE-2025-55184, CVE-2025-55183, CVE-2025-67779, CVE-2026-23864, CVE-2026-23869\n\n**Action:** upgrade to patched Next.js (15.x patched line \u002F 16.1.2+ with React 19.1+); validate inputs at the data layer; never trust middleware alone for security.\n\n### Middleware Auth-Bypass — CVE-2025-29927 (CVSS 9.1)\n- Spoofed `x-middleware-subrequest` header bypassed middleware-based authorization entirely\n- **Fix:** upgrade (≥15.2.3, ≥14.2.25, ≥13.5.9, ≥12.3.5) AND strip the header at the reverse proxy\u002FWAF\n\n**Architectural lesson (applies beyond this CVE):** never rely on middleware alone for auth — do \"optimistic\" cookie checks in middleware but full session validation in the Server Component\u002FRoute Handler\u002FData Access Layer. In Next.js 16, `middleware.ts` is renamed `proxy.ts`.\n\n---\n\n## Python Security Patterns\n\n### Type Safety and Validation (Pydantic v2)\n- Use Pydantic models as request\u002Fresponse contracts in FastAPI\n- `Field` constraints: `min_length`, `ge`\u002F`le`, `EmailStr`\n- Custom `@field_validator`\u002F`@model_validator`\n- Separate Create\u002FUpdate\u002FResponse models — secrets (password hashes, internal fields) never serialize out\n- `response_model` to filter output\n- Validation is the first line of defense but not a substitute for authorization\n\n### Dependency Security\n- **pip-audit** (PyPA official): run in CI and as a pytest gate against the PyPA advisory DB\n- Pin deps with hashes (`uv lock` \u002F `uv pip compile --generate-hashes`)\n- Add cool-off (`--exclude-newer \"1 week\"`) to dodge fresh malicious releases\n- Behavioral scanners: **Socket.dev** \u002F **GuardDog** \u002F **Phylum**\n- **Trusted Publishing (OIDC)** instead of long-lived PyPI tokens\n- Real campaigns to know: Shai-Hulud worm (Nov 2025), GhostAction (Sept 2025, 570+ repos \u002F 3,300+ secrets), PyPI phishing (pypj.org, July 2025)\n\n### Static Analysis\n- **Bandit** (~80 plugins: weak crypto, `shell=True`, `eval`\u002F`exec`, `assert` for security [B101 — stripped under `-O`], `random` for secrets, bind-all-interfaces) — run in pre-commit at MEDIUM severity\u002FHIGH confidence\n- **Semgrep** (taint tracking, OWASP rulesets, reachability in paid Supply Chain)\n- **CodeQL**, **Ruff** security rules, **Pysa**\n\n### Async Pitfalls\n- Never block the event loop with sync I\u002FO — use `run_in_threadpool` (Starlette) for sync libs\n- Avoid shared mutable state across coroutines\n- Set timeouts on all awaits to bound resource use\n\n### Authentication and Authorization\n- OAuth2 + JWT via `fastapi.security` (`OAuth2PasswordBearer`)\n- Hash passwords with **bcrypt** (passlib) or **Argon2**\n- Store JWT\u002Frefresh config in pydantic-settings with `SecretStr`\n- Short-lived access tokens + rotating refresh tokens\n- Never use `assert` for authorization (stripped under `-O`)\n- RBAC and BOLA checks on every object access (OWASP API Top 10)\n- Never log auth headers\u002Fcookies\u002Ftokens — use a logging filter to scrub PII\n\n---\n\n## TypeScript \u002F Next.js Security Patterns\n\n### TypeScript as a Security Mechanism\n- Strict types + Zod runtime validation at every trust boundary\n- Strict schema validation also blunts prototype-pollution (reject `__proto__`\u002F`constructor` keys) — the exact class CVE-2025-55182 abused\n\n### Server Actions and RSC\n- Server Actions deserialize client input — validate with Zod, treat as untrusted\n- Never hardcode secrets in Server Action code (source-exposure CVE risk)\n- CSRF protection for Server Actions invoked from forms: Next.js encrypts action IDs; set a persistent `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` across instances\n\n### Auth.js v5 \u002F NextAuth\n- All env vars prefixed `AUTH_` (auto-inferred)\n- `AUTH_SECRET` is mandatory\n- **JWT sessions:** encrypted (JWE) in HttpOnly cookies; can't be revoked pre-expiry but Edge-compatible\n- **Database sessions:** allow revocation\u002F\"sign out everywhere\" but not Edge-compatible\n- Split config: `auth.config.ts` (edge-safe) vs `auth.ts` (with adapter) so middleware stays Edge-compatible\n- Always validate sessions server-side for sensitive ops, not just in middleware\n\n### Cookie Security Flags\nHttpOnly + Secure + SameSite + `__Host-` prefix\n\n### Content Security Policy\n- Start with `default-src 'self'`\n- Prefer **nonce-based** CSP over `'unsafe-inline'`\n- Libraries: **Nosecone** (Arcjet) \u002F set headers in `proxy.ts`\n- Also add: HSTS, X-Content-Type-Options, Permissions-Policy, X-Frame-Options\u002F`frame-ancestors 'none'`\n- Validate with Google CSP Evaluator\n\n### Server vs Client Data Exposure\nMost dangerous anti-pattern: passing whole DB rows from Server to Client Components.\n- Pass only needed, sanitized fields\n- Use `import 'server-only'`\n- Taint APIs (`experimental_taintObjectReference`\u002F`taintUniqueValue`)\n- With Supabase\u002FPostgres: Row-Level Security as a DB-layer backstop\n\n### API Route Security\n- Rate limiting (@upstash\u002Fratelimit, Arcjet)\n- Explicit CORS allow-lists\n- Validate all inputs server-side with Zod\n\n### Dependency Security\n- `npm audit` as a blocking CI step; lockfile committed\n- **Socket** for behavioral analysis on PRs\n- Pin GitHub Actions to commit SHA, not tag (GhostAction lesson)\n- Renovate\u002FDependabot auto-merge patch releases for next\u002Freact\u002Freact-dom\n\n---\n\n## AI Model Supply Chain (LLM03:2025)\n\n### Threats\n- Malicious weights, pickle-based RCE (PickleScan bypasses: CVE-2025-10156 CRC differential, CVE-2025-10157 subclass substitution — single scanners insufficient)\n- Poisoned training data\n- Real incidents: Ultralytics compromise (Dec 2024, ~80M downloads\u002Fmo), LiteLLM supply-chain compromise, Langflow code injection (CVE-2025-3248)\n\n### Controls\n- Prefer **safetensors** over pickle\n- Allow-lists not block-lists for model file types\n- Hash verification before loading\n- Sandboxed model loading\n- Update PickleScan ≥0.0.31\n- Generate **AI-BOMs** (OWASP AIBOM generator)\n\n---\n\n## Supply-Chain Security (SBOM\u002FSigstore\u002FProvenance)\n\n- Generate SBOMs: **Syft** (broad coverage) or **cyclonedx-py** (build-tool integrated, most accurate at capture time)\n- Sign artifacts: **cosign** (keyless via Sigstore\u002FFulcio\u002FRekor + OIDC)\n- Target **SLSA Level 2→3** provenance (SLSA GitHub Generator gives L3 on GitHub Actions)\n- **in-toto attestations**\n- Verify at deploy time via admission policy (Kubernetes admission controller)\n- Sonatype: 454,600+ new malicious packages reported in 2025\n- **SBOM alone is insufficient** — pair with provenance + signature verification (SolarWinds\u002FGhostAction lesson)\n\n### CI\u002FCD Security Gates\nSAST (Bandit\u002FSemgrep\u002FCodeQL) + SCA (pip-audit\u002Fnpm audit\u002FSnyk\u002FTrivy\u002FOSV-Scanner) + secret scanning (Gitleaks\u002Fdetect-secrets\u002FTruffleHog) + DAST (OWASP ZAP) as **blocking** pipeline gates.\n\n**Note on SAST limitations:** a 2026 benchmark found 78% of confirmed vulnerabilities detected by only 1 of 5 SAST tools — SAST is structurally insufficient for semantic flaws.\n\n---\n\n## Vibe Coding \u002F AI-Generated Code Risks\n\n**The data (multiple independent sources, 2025–2026):**\n- **Veracode (July 2025):** 45% failure rate on security tests; XSS failing 86%, log injection 88%; rate \"virtually identical to where it stood two years ago\" as of Spring 2026\n- **Apiiro (Sept 2025):** 10× spike in security findings (10,000+\u002Fmonth by June 2025); 3–4× more commits; privilege-escalation paths +322%, architectural design flaws +153%\n- **CodeRabbit:** AI-co-authored PRs had ~1.7× more major issues, 2.74× more security flaws\n- **Tenzai (Dec 2025):** 5\u002F5 AI agents introduced SSRF; 0\u002F15 apps had CSRF protection or security headers\n- **IEEE-ISTAS:** +37.6% critical vulns after 5 rounds of AI refinement (iteration compounds flaws)\n- **Slopsquatting:** attackers pre-register hallucinated package names\n\n### Mitigations\n1. Mandatory human review gates for AI-generated PRs\n2. Strict AI \"rule files\" (ban `eval`, require env vars + parameterized queries)\n3. Secret-scanning pre-commit hooks\n4. Enforce security at the infra layer (WAF\u002FZero Trust gateway) — SAST alone is insufficient\n5. AI code review as a quality gate on AI-generated code (CodeRabbit, Greptile, etc.)\n\n---\n\n## AI Threat-Modeling Frameworks\n\n| Framework | Focus |\n|---|---|\n| **MITRE ATLAS** | Adversarial ML tactics\u002Ftechniques |\n| **NIST AI RMF 1.0** (Jan 2023) | Govern\u002FMap\u002FMeasure\u002FManage functions |\n| **NIST GenAI Profile (AI 600-1)** (July 2024) | Generative AI-specific risks |\n| **Draft NIST Cyber AI Profile (IR 8596)** (Dec 2025, comment period closed Jan 2026) | Overlays Secure\u002FDetect\u002FThwart focus areas on CSF 2.0; High\u002FModerate\u002FFoundational priorities |\n| **NIST COSAiS** (concept paper Aug 2025) | SP 800-53 control overlays for AI systems |\n| **Databricks AI Security Framework v2.0** | Data\u002FML platform focus |\n| **ISO\u002FIEC 42001** | AI management system standard |\n\n---\n\n## Zero-Trust Principles for AI Systems\n\n- Least-privilege for every actor including **non-human\u002Fagent identities**\n- Verify explicitly at each layer (don't trust middleware\u002Fnetwork position)\n- Short-lived scoped credentials for all service-to-service calls\n- Per-request authorization at the data layer\n- OIDC for CI\u002FCD to cloud (no long-lived secrets)\n\n---\n\n## Container and Kubernetes Security\n\n- Run as non-root; read-only filesystem\n- Minimal base images (distroless\u002FChainguard\u002FAlpine)\n- Trivy\u002FCheckov scanning in CI\n- Egress firewall\u002Fallow-list; block cloud-metadata access (IMDSv2, hop limit 1)\n- Admission controllers (Kyverno) to require signed images\n\n---\n\n## Observability for Security\n\n- Structured logging to a SIEM\n- Log security events (authn failures, authz denials) at a distinct level\n- Anomaly detection on LLM usage\u002Fcost (unusual spend = potential credential compromise or DoS)\n- For LLMs: trace each guardrail step (e.g., Langfuse `@observe`) and monitor risk scores\n- OpenTelemetry tracing\n- Data privacy: Presidio-based PII detection\u002Fredaction; data minimization; never log request bodies\u002Ftokens\n\n---\n\n## Staged Implementation Roadmap\n\n### Stage 1 — This Week (Stop Active Bleeding)\n1. **Patch Next.js\u002FReact** to current patched versions; strip `x-middleware-subrequest` at proxy\u002FWAF; confirm no auth relies solely on middleware — verify with CVE-2025-29927 \u002F CVE-2025-66478 detection templates\n2. **Turn on secret scanning** (Gitleaks\u002Fdetect-secrets) as pre-commit + CI gate; rotate any exposed AI keys; move to project\u002Fworkspace-scoped keys; set provider spend caps\n3. **Add token-aware rate limiting + `max_tokens` + spend alerts** to every LLM endpoint\n\n### Stage 2 — This Quarter (Build the Baseline)\n4. Wire **SAST + SCA + DAST** into CI as blocking gates; pin deps with hashes and Actions to SHA; generate SBOMs; sign artifacts with cosign (SLSA L2)\n5. Stand up a **guardrail layer** (NeMo Guardrails or LLM Guard + Llama Guard 3) with input\u002Fretrieval\u002Fexecution\u002Foutput rails; enforce Pydantic\u002FZod schemas on all model output\n6. For **RAG:** authenticate document sources, strip hidden text on ingest, enforce per-tenant isolation at the DB layer, encrypt the vector store\n7. **Mandate human review gates** and \"rule files\" for AI-assisted PRs\n\n### Stage 3 — This Year (Mature the Program)\n8. Adopt **NIST AI RMF** + map to the Cyber AI Profile and ISO 42001; maintain an AI\u002FAgent inventory with named owners\n9. **Sandbox all agents** (Firecracker\u002FgVisor), enforce least-privilege tool scoping, default-deny secret reads, egress allow-lists, human approval for high-impact actions\n10. Move secrets to a manager with dynamic\u002Fshort-lived credentials (Vault) or OIDC federation (Anthropic WIF, OpenAI service accounts); rotate on 90-day cadence\n\n### Thresholds That Change the Plan\n- Any agent with write\u002Ffinancial\u002FPII access → require sandbox + human-in-the-loop before launch\n- LLM spend variance >X% week-over-week → tighten token quotas\n- Any unauthenticated public endpoint → WAF + rate limit mandatory\n- SAST\u002FSCA HIGH\u002FCRITICAL finding → block deploy\n\n---\n\n## The First Rule\n\n\"The first sentence of any LLM defense is: don't grant the capability you can't afford to have misused.\" Capability minimization is more reliable than any filter.\n","# AI Security Practices: 2025–2026 Reference for Python & TypeScript\u002FNext.js\n\n## The Three Core Truths\n\n1. **Treat the LLM as an untrusted, internet-connected user.** Defense-in-depth (input validation + output sanitization at every boundary, least-privilege tool access, human-in-the-loop for high-impact actions) is the only viable posture, because prompt injection cannot be fully patched away.\n2. **The biggest 2025–2026 attack-surface shifts are agentic AI and the framework deserialization layer.** The Next.js\u002FReact Server Components RCE (CVE-2025-55182 \u002F CVE-2025-66478, CVSS 10.0, actively exploited within days) and the middleware auth-bypass (CVE-2025-29927, CVSS 9.1) show that \"secure-by-default\" frameworks still require continuous patching.\n3. **AI-generated code is shipping security debt at scale.** Veracode (July 2025): 45% of AI-generated code samples failed security tests; XSS failing 86%, log injection 88%. Apiiro: 10,000+ new security findings\u002Fmonth (10× spike), privilege-escalation paths up 322%. GitGuardian: AI-assisted commits leak secrets at 3.2% vs 1.5% baseline.\n\n---\n\n## OWASP LLM Top 10 (2025 Edition)\n\n| Rank | Risk | Key Point |\n|---|---|---|\n| LLM01 | **Prompt Injection** | #1 for second consecutive edition; both direct and indirect; cannot be fully patched at input layer |\n| LLM02 | Sensitive Information Disclosure | — |\n| LLM03 | Supply Chain (model provenance) | Pickle-based RCE, malicious weights, poisoned training data |\n| LLM04 | Data and Model Poisoning | — |\n| LLM05 | **Improper Output Handling** | Treating model output as trusted → XSS, SQLi, SSRF, RCE |\n| LLM06 | **Excessive Agency** | Over-permissioned tools, agentic misuse |\n| LLM07 | System Prompt Leakage | — |\n| LLM08 | **Vector and Embedding Weaknesses** | RAG security — new in 2025 edition |\n| LLM09 | Misinformation (renamed Overreliance) | — |\n| LLM10 | **Unbounded Consumption** | \"Denial of Wallet\" — new in 2025 edition |\n\n---\n\n## Prompt Injection (LLM01:2025) — The Unfixable Risk\n\n### Why It Cannot Be Patched Away\nLLMs process instructions and data in the same channel without clear separation. No input filter is fully reliable — adaptive attacks evade classifiers.\n\n### Direct vs Indirect Injection\n- **Direct:** \"ignore previous instructions\"\n- **Indirect (harder):** malicious instructions embedded in a webpage, RAG document, email, or tool result the model later reads — NOT caught by input-only classifiers\n- **Multimodal:** instructions hidden in images\n\n### Defensible Architectural Patterns (More Robust Than Detection)\n1. **Action-selector:** constrain the set of actions an LLM can take\n2. **Plan-then-execute:** LLM produces a plan; deterministic code validates and executes it\n3. **Dual-LLM** (Simon Willison): one privileged LLM for user instructions, one quarantined for external content\n4. **CaMeL** (Google DeepMind, March 2025): privileged planner + quarantined LLM executor with enforced information-flow controls\n5. **IsolateGPT** (NDSS 2025): sandboxed execution of external content\n\n### Detection Tooling (Treat as One Layer, Not the Solution)\n- **Meta Prompt-Guard \u002F Llama Guard 3** (open-weight, 8B): outperforms GPT-4 on injection detection with ~1\u002F3 the false-positive rate\n- **NVIDIA NeMo** jailbreak heuristics\n- **LLM Guard** (Protect AI): input scanners\n- **DataSentinel**, **PIShield**\n\n### Implementation\n- **Python:** run Llama Guard via vLLM or LLM Guard input scanners before the model call; enforce a Pydantic output schema after\n- **TypeScript:** validate model output with **Zod** before it touches SQL\u002FHTML\u002Fshell; never pass raw LLM text to `dangerouslySetInnerHTML`\n\n---\n\n## Output Sanitization (LLM05:2025 Improper Output Handling)\n\n**Threat:** treating model output as trusted → XSS, SQLi, SSRF, or RCE when output flows into downstream interpreters.\n\n**Pattern:** validate\u002Fencode output by context:\n- Schema validation: Pydantic (Python) \u002F Zod (TypeScript)\n- Context-aware output encoding\n- Parameterized queries (never string-concatenate LLM output into SQL)\n- PII redaction: Presidio-based (built into NeMo Guardrails and Guardrails AI Hub)\n\n---\n\n## Guardrail Architectures\n\n### Layer Model\n```\ninput rail (jailbreak\u002FPII) \n  → retrieval rail (RAG chunks) \n  → execution rail (tool-call gating) \n  → output rail (moderation\u002FPII\u002Fformat)\n```\nNo single framework covers all ten OWASP items — layer them.\n\n### NVIDIA NeMo Guardrails\n- Open-source, Apache 2.0; v0.17.0 (Oct 2025); **NVIDIA labels it beta \u002F \"additional hardening required for production\"**\n- Five rail types: input, dialog, retrieval, execution, output\n- Uses the Colang DSL; uniquely models multi-turn dialog\n- Integrates: Llama Guard, Presidio PII detection, NemoGuard-8b content-safety\u002Ftopic-control NIMs\n- Supports streaming output checks\n- Integrates with LangChain\u002FLangGraph\n\n### Other Options\n| Tool | Model | Best For |\n|---|---|---|\n| **Guardrails AI** | RAIL spec + validator Hub, `num_reasks` for auto re-prompting | Python-centric apps |\n| **LLM Guard** (Protect AI) | Input + output scanners | Composable pipeline |\n| **Lakera Guard** | Managed API | Minimal integration effort |\n| **Azure AI Content Safety** | Managed Azure | Azure-native teams |\n| **AWS Bedrock Guardrails** | Managed AWS | AWS-native teams |\n\n### Red-Teaming Tools\nGarak, PyRIT, Promptfoo, DeepTeam\n\n---\n\n## RAG Security (LLM08:2025)\n\n### Attack Vectors\n- **PoisonedRAG** (USENIX Security 2025): 5 malicious documents → 90%+ attack success rate on databases with millions of entries (97% on NQ, 99% on HotpotQA, 91% on MS-MARCO in black-box settings)\n- **Embedding-inversion attacks:** recover 50–70% of source text from stolen vectors (ALGEN 2025: ~1,000 samples, transfers across black-box encoders)\n- **Cross-tenant leakage** through shared vector stores\n- **Hidden-text injection:** white-on-white resume text ingested as legitimate content\n\n### Defense-in-Depth Across Ingest \u002F Retrieve \u002F Generate\n**Ingest:**\n- Validate and authenticate all document sources\n- Strip hidden\u002Fzero-width text and ignore formatting on ingestion\n- Detect and reject suspicious instruction patterns in documents\n\n**Store:**\n- Per-tenant physical isolation or DB-layer namespace filtering (not app-layer)\n- Treat the vector DB as a sensitive data store: encrypt, access-control\n- Embedding anomaly detection\n\n**Retrieve\u002FGenerate:**\n- Retrieval rails\n- Per OWASP\u002FSnyk LLM Security Verification Standard (Aug 2025): full verification checklist\n\n---\n\n## Agentic AI Security (LLM06:2025 + OWASP ASI Top 10)\n\n### Threat Classes\n- Tool misuse, identity\u002Fprivilege abuse, rogue agents\n- Unexpected code execution (ASI05)\n- Memory poisoning, resource overload, cascading hallucination\n- Real CVEs: Claude Code data exfiltration via DNS (CVE-2025-55284); Cursor \"AgentFlayer\" via malicious Jira ticket\n\n### Mandated Controls\n1. **Least-privilege tool scoping** — the planner often needs no tools; scope each tool to only what it needs\n2. **Sandboxing** — OWASP ASI: \"Never execute agent-generated code without strict sandboxing, input validation, and allowlisting\"\n   - **Firecracker microVMs:** strongest isolation\n   - **gVisor:** syscall-level isolation\n   - **V8 isolates:** JS, latency-critical\n3. **Default-deny reads** of `.env`\u002Fsecrets\n4. **Egress allow-lists** — not egress defaults\n5. **Human approval gates** for high-stakes or irreversible actions\n6. **Emergency kill switches** and circuit breakers\n7. **Continuous behavioral monitoring**\n\n**Note:** CVE-2025-59528 (CVSS 10.0) and the Google Antigravity sandbox escape confirm that app-level controls fail without isolated execution.\n\n### Frameworks\n- Progent (programmable privilege control)\n- Microsoft Agent Governance Toolkit (April 2026)\n\n---\n\n## MCP Security (New, Under-Secured Layer)\n\n**Current state (Bitsight Research, December 2025):** ~1,000 MCP servers exposed on the public internet with no authorization controls — some allowing Kubernetes cluster command execution, CRM access, and arbitrary shell commands.\n\n### Documented Threats\n- **Tool poisoning:** malicious MCP server returns harmful tool definitions\n- **Rug pulls:** tool behavior changes after trust is established\n- **Tool\u002Fghost shadowing:** malicious tool impersonates legitimate one\n- **Command injection:** CVE-2025-49596 (MCP-Inspector, fixed in 0.14.1)\n- **Confused-deputy attacks**\n- First malicious MCP package appeared September 2025\n\n### Controls\n- OAuth-enhanced tool definitions + policy-based access control (ETDI)\n- Gateway\u002Fproxy with authentication\n- Audit logging of all tool calls\n- Secret management for MCP server credentials\n- Human-in-the-loop for tool calls accessing sensitive systems\n- References: NSA MCP guidance (May 2026), OWASP MCP Top 10, CISA joint guidance (May 22, 2025)\n\n---\n\n## Secrets Management for AI Services\n\n### The Scale of the Problem\nGitGuardian State of Secrets Sprawl 2026 (March 2026):\n- 28.65 million new hardcoded secrets added to public GitHub commits in 2025 (+34% YoY, largest single-year jump)\n- AI-assisted commits leak secrets at **3.2%** vs 1.5% GitHub-wide baseline\n- 1,275,105 AI service secrets detected in 2025 (+81% over 2024)\n- 8 of 10 fastest-growing detector categories tied to AI services\n- ~113,000 leaked DeepSeek API keys as one example\n\n### OpenAI Key Management\n- Use **project-scoped keys** (`sk-proj-` prefix, replaces legacy org-wide `sk-` keys since April 2024)\n- Use **service account keys** for CI\u002Fagents\n- Separate **Admin key** for the management API\n- RBAC at org\u002Fproject level; IP allowlisting\n- **Ephemeral Realtime client secrets** for browser sessions (never expose long-lived keys client-side)\n- On compromise: rotate immediately from the API Keys page\n\n### Anthropic Key Management\n- Keys (`sk-ant-`) are **workspace-scoped**\n- **Admin API keys** (`ANTHROPIC_ADMIN_KEY`) for management — org-admin only\n- **Workload Identity Federation:** exchanges OIDC tokens for short-lived `sk-ant-oat01-` tokens (default 3,600s, min 60s) — no `ANTHROPIC_API_KEY` secret to create, store, or rotate\n- Official guidance: rotate keys ~every 90 days; set usage\u002Fspend limits as a safeguard\n\n### Secrets Managers\n| Tool | Approach | Notes |\n|---|---|---|\n| **HashiCorp Vault** | True dynamic\u002Fshort-lived secrets | BSL license; acquired by IBM 2025 |\n| **AWS Secrets Manager \u002F Azure Key Vault \u002F GCP Secret Manager** | Managed, scheduled rotation | Cloud-native |\n| **Doppler** | SaaS secrets sync | Developer-friendly |\n| **Infisical** | MIT, self-hostable | Open-source option |\n\nNever store AI keys client-side — route through a server-side proxy.\n\n### Python Implementation\n- **pydantic-settings** (`BaseSettings` + `SettingsConfigDict(env_file=...)` with `SecretStr` and `.get_secret_value()`; supports `secrets_dir` for Docker secrets)\n- **python-dotenv** (does not override existing env vars by default)\n- Add `.env` to `.gitignore` on day one; ship `.env.example`\n- Pre-commit hooks: **detect-secrets** \u002F **Gitleaks** \u002F **TruffleHog**\n\n### Next.js Implementation\n- Only `NEXT_PUBLIC_`-prefixed vars reach the browser (inlined at build time) — never put secrets there\n- Access `process.env` only in a server-only Data Access Layer (`import 'server-only'`)\n- React **taint APIs** (`experimental_taintObjectReference`\u002F`taintUniqueValue`) prevent accidental client exposure\n- Post-build: grep `.next\u002Fstatic\u002Fchunks` for leaked secrets\n\n---\n\n## Denial of Wallet (LLM10:2025 Unbounded Consumption)\n\n**Threat:** \"excessive and uncontrolled inferences, leading to denial of service (DoS), economic losses, model theft, and service degradation.\" Bills can be business-ending.\n\n### Controls\n- Set explicit `max_tokens` on **every** call\n- **Token-based limiting** (input+output tokens) AND **cost-based caps**, not just request counts\n- Pre-count tokens with `tiktoken` before sending\n- Per-user\u002Fper-API-key\u002Fper-endpoint limits\n- Timeouts and throttling\n- Provider dashboard hard\u002Fsoft spend caps\n  - OpenAI: billing limits → 429 on hard limit\n  - Anthropic: monthly cap + alerts (essential — billing is post-usage)\n- Continuous logging\u002Fmonitoring; graceful degradation\n\n### Python Rate Limiting\n- **slowapi** (Starlette\u002FFastAPI, v0.1.9): Redis\u002FMemcached backends; custom `key_func` for per-user\u002Fper-key limits; token-bucket\u002Fsliding-window via `limits`\n- **fastapi-limiter** (Redis async)\n- Atomic Redis+Lua token buckets for distributed limiting\n\n### Next.js Rate Limiting\n- **@upstash\u002Fratelimit** (connectionless HTTP, v2.0.8): `fixedWindow`\u002F`slidingWindow`\u002F`tokenBucket`; multi-region; ephemeral in-memory DDoS cache\n- **Arcjet:** rate limiting + bot detection + shield, `aj.protect(req)`\n\n---\n\n## Critical Next.js \u002F React CVEs (2025–2026)\n\n### React2Shell RCE — CVE-2025-55182 \u002F CVE-2025-66478 (CVSS 10.0)\n- **Unauthenticated RCE** exploitable on a default `create-next-app`\n- React Server Components deserialization vulnerability\n- **Actively exploited in the wild within days of disclosure**\n- Follow-on CVEs landed through April 2026: CVE-2025-55184, CVE-2025-55183, CVE-2025-67779, CVE-2026-23864, CVE-2026-23869\n\n**Action:** upgrade to patched Next.js (15.x patched line \u002F 16.1.2+ with React 19.1+); validate inputs at the data layer; never trust middleware alone for security.\n\n### Middleware Auth-Bypass — CVE-2025-29927 (CVSS 9.1)\n- Spoofed `x-middleware-subrequest` header bypassed middleware-based authorization entirely\n- **Fix:** upgrade (≥15.2.3, ≥14.2.25, ≥13.5.9, ≥12.3.5) AND strip the header at the reverse proxy\u002FWAF\n\n**Architectural lesson (applies beyond this CVE):** never rely on middleware alone for auth — do \"optimistic\" cookie checks in middleware but full session validation in the Server Component\u002FRoute Handler\u002FData Access Layer. In Next.js 16, `middleware.ts` is renamed `proxy.ts`.\n\n---\n\n## Python Security Patterns\n\n### Type Safety and Validation (Pydantic v2)\n- Use Pydantic models as request\u002Fresponse contracts in FastAPI\n- `Field` constraints: `min_length`, `ge`\u002F`le`, `EmailStr`\n- Custom `@field_validator`\u002F`@model_validator`\n- Separate Create\u002FUpdate\u002FResponse models — secrets (password hashes, internal fields) never serialize out\n- `response_model` to filter output\n- Validation is the first line of defense but not a substitute for authorization\n\n### Dependency Security\n- **pip-audit** (PyPA official): run in CI and as a pytest gate against the PyPA advisory DB\n- Pin deps with hashes (`uv lock` \u002F `uv pip compile --generate-hashes`)\n- Add cool-off (`--exclude-newer \"1 week\"`) to dodge fresh malicious releases\n- Behavioral scanners: **Socket.dev** \u002F **GuardDog** \u002F **Phylum**\n- **Trusted Publishing (OIDC)** instead of long-lived PyPI tokens\n- Real campaigns to know: Shai-Hulud worm (Nov 2025), GhostAction (Sept 2025, 570+ repos \u002F 3,300+ secrets), PyPI phishing (pypj.org, July 2025)\n\n### Static Analysis\n- **Bandit** (~80 plugins: weak crypto, `shell=True`, `eval`\u002F`exec`, `assert` for security [B101 — stripped under `-O`], `random` for secrets, bind-all-interfaces) — run in pre-commit at MEDIUM severity\u002FHIGH confidence\n- **Semgrep** (taint tracking, OWASP rulesets, reachability in paid Supply Chain)\n- **CodeQL**, **Ruff** security rules, **Pysa**\n\n### Async Pitfalls\n- Never block the event loop with sync I\u002FO — use `run_in_threadpool` (Starlette) for sync libs\n- Avoid shared mutable state across coroutines\n- Set timeouts on all awaits to bound resource use\n\n### Authentication and Authorization\n- OAuth2 + JWT via `fastapi.security` (`OAuth2PasswordBearer`)\n- Hash passwords with **bcrypt** (passlib) or **Argon2**\n- Store JWT\u002Frefresh config in pydantic-settings with `SecretStr`\n- Short-lived access tokens + rotating refresh tokens\n- Never use `assert` for authorization (stripped under `-O`)\n- RBAC and BOLA checks on every object access (OWASP API Top 10)\n- Never log auth headers\u002Fcookies\u002Ftokens — use a logging filter to scrub PII\n\n---\n\n## TypeScript \u002F Next.js Security Patterns\n\n### TypeScript as a Security Mechanism\n- Strict types + Zod runtime validation at every trust boundary\n- Strict schema validation also blunts prototype-pollution (reject `__proto__`\u002F`constructor` keys) — the exact class CVE-2025-55182 abused\n\n### Server Actions and RSC\n- Server Actions deserialize client input — validate with Zod, treat as untrusted\n- Never hardcode secrets in Server Action code (source-exposure CVE risk)\n- CSRF protection for Server Actions invoked from forms: Next.js encrypts action IDs; set a persistent `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` across instances\n\n### Auth.js v5 \u002F NextAuth\n- All env vars prefixed `AUTH_` (auto-inferred)\n- `AUTH_SECRET` is mandatory\n- **JWT sessions:** encrypted (JWE) in HttpOnly cookies; can't be revoked pre-expiry but Edge-compatible\n- **Database sessions:** allow revocation\u002F\"sign out everywhere\" but not Edge-compatible\n- Split config: `auth.config.ts` (edge-safe) vs `auth.ts` (with adapter) so middleware stays Edge-compatible\n- Always validate sessions server-side for sensitive ops, not just in middleware\n\n### Cookie Security Flags\nHttpOnly + Secure + SameSite + `__Host-` prefix\n\n### Content Security Policy\n- Start with `default-src 'self'`\n- Prefer **nonce-based** CSP over `'unsafe-inline'`\n- Libraries: **Nosecone** (Arcjet) \u002F set headers in `proxy.ts`\n- Also add: HSTS, X-Content-Type-Options, Permissions-Policy, X-Frame-Options\u002F`frame-ancestors 'none'`\n- Validate with Google CSP Evaluator\n\n### Server vs Client Data Exposure\nMost dangerous anti-pattern: passing whole DB rows from Server to Client Components.\n- Pass only needed, sanitized fields\n- Use `import 'server-only'`\n- Taint APIs (`experimental_taintObjectReference`\u002F`taintUniqueValue`)\n- With Supabase\u002FPostgres: Row-Level Security as a DB-layer backstop\n\n### API Route Security\n- Rate limiting (@upstash\u002Fratelimit, Arcjet)\n- Explicit CORS allow-lists\n- Validate all inputs server-side with Zod\n\n### Dependency Security\n- `npm audit` as a blocking CI step; lockfile committed\n- **Socket** for behavioral analysis on PRs\n- Pin GitHub Actions to commit SHA, not tag (GhostAction lesson)\n- Renovate\u002FDependabot auto-merge patch releases for next\u002Freact\u002Freact-dom\n\n---\n\n## AI Model Supply Chain (LLM03:2025)\n\n### Threats\n- Malicious weights, pickle-based RCE (PickleScan bypasses: CVE-2025-10156 CRC differential, CVE-2025-10157 subclass substitution — single scanners insufficient)\n- Poisoned training data\n- Real incidents: Ultralytics compromise (Dec 2024, ~80M downloads\u002Fmo), LiteLLM supply-chain compromise, Langflow code injection (CVE-2025-3248)\n\n### Controls\n- Prefer **safetensors** over pickle\n- Allow-lists not block-lists for model file types\n- Hash verification before loading\n- Sandboxed model loading\n- Update PickleScan ≥0.0.31\n- Generate **AI-BOMs** (OWASP AIBOM generator)\n\n---\n\n## Supply-Chain Security (SBOM\u002FSigstore\u002FProvenance)\n\n- Generate SBOMs: **Syft** (broad coverage) or **cyclonedx-py** (build-tool integrated, most accurate at capture time)\n- Sign artifacts: **cosign** (keyless via Sigstore\u002FFulcio\u002FRekor + OIDC)\n- Target **SLSA Level 2→3** provenance (SLSA GitHub Generator gives L3 on GitHub Actions)\n- **in-toto attestations**\n- Verify at deploy time via admission policy (Kubernetes admission controller)\n- Sonatype: 454,600+ new malicious packages reported in 2025\n- **SBOM alone is insufficient** — pair with provenance + signature verification (SolarWinds\u002FGhostAction lesson)\n\n### CI\u002FCD Security Gates\nSAST (Bandit\u002FSemgrep\u002FCodeQL) + SCA (pip-audit\u002Fnpm audit\u002FSnyk\u002FTrivy\u002FOSV-Scanner) + secret scanning (Gitleaks\u002Fdetect-secrets\u002FTruffleHog) + DAST (OWASP ZAP) as **blocking** pipeline gates.\n\n**Note on SAST limitations:** a 2026 benchmark found 78% of confirmed vulnerabilities detected by only 1 of 5 SAST tools — SAST is structurally insufficient for semantic flaws.\n\n---\n\n## Vibe Coding \u002F AI-Generated Code Risks\n\n**The data (multiple independent sources, 2025–2026):**\n- **Veracode (July 2025):** 45% failure rate on security tests; XSS failing 86%, log injection 88%; rate \"virtually identical to where it stood two years ago\" as of Spring 2026\n- **Apiiro (Sept 2025):** 10× spike in security findings (10,000+\u002Fmonth by June 2025); 3–4× more commits; privilege-escalation paths +322%, architectural design flaws +153%\n- **CodeRabbit:** AI-co-authored PRs had ~1.7× more major issues, 2.74× more security flaws\n- **Tenzai (Dec 2025):** 5\u002F5 AI agents introduced SSRF; 0\u002F15 apps had CSRF protection or security headers\n- **IEEE-ISTAS:** +37.6% critical vulns after 5 rounds of AI refinement (iteration compounds flaws)\n- **Slopsquatting:** attackers pre-register hallucinated package names\n\n### Mitigations\n1. Mandatory human review gates for AI-generated PRs\n2. Strict AI \"rule files\" (ban `eval`, require env vars + parameterized queries)\n3. Secret-scanning pre-commit hooks\n4. Enforce security at the infra layer (WAF\u002FZero Trust gateway) — SAST alone is insufficient\n5. AI code review as a quality gate on AI-generated code (CodeRabbit, Greptile, etc.)\n\n---\n\n## AI Threat-Modeling Frameworks\n\n| Framework | Focus |\n|---|---|\n| **MITRE ATLAS** | Adversarial ML tactics\u002Ftechniques |\n| **NIST AI RMF 1.0** (Jan 2023) | Govern\u002FMap\u002FMeasure\u002FManage functions |\n| **NIST GenAI Profile (AI 600-1)** (July 2024) | Generative AI-specific risks |\n| **Draft NIST Cyber AI Profile (IR 8596)** (Dec 2025, comment period closed Jan 2026) | Overlays Secure\u002FDetect\u002FThwart focus areas on CSF 2.0; High\u002FModerate\u002FFoundational priorities |\n| **NIST COSAiS** (concept paper Aug 2025) | SP 800-53 control overlays for AI systems |\n| **Databricks AI Security Framework v2.0** | Data\u002FML platform focus |\n| **ISO\u002FIEC 42001** | AI management system standard |\n\n---\n\n## Zero-Trust Principles for AI Systems\n\n- Least-privilege for every actor including **non-human\u002Fagent identities**\n- Verify explicitly at each layer (don't trust middleware\u002Fnetwork position)\n- Short-lived scoped credentials for all service-to-service calls\n- Per-request authorization at the data layer\n- OIDC for CI\u002FCD to cloud (no long-lived secrets)\n\n---\n\n## Container and Kubernetes Security\n\n- Run as non-root; read-only filesystem\n- Minimal base images (distroless\u002FChainguard\u002FAlpine)\n- Trivy\u002FCheckov scanning in CI\n- Egress firewall\u002Fallow-list; block cloud-metadata access (IMDSv2, hop limit 1)\n- Admission controllers (Kyverno) to require signed images\n\n---\n\n## Observability for Security\n\n- Structured logging to a SIEM\n- Log security events (authn failures, authz denials) at a distinct level\n- Anomaly detection on LLM usage\u002Fcost (unusual spend = potential credential compromise or DoS)\n- For LLMs: trace each guardrail step (e.g., Langfuse `@observe`) and monitor risk scores\n- OpenTelemetry tracing\n- Data privacy: Presidio-based PII detection\u002Fredaction; data minimization; never log request bodies\u002Ftokens\n\n---\n\n## Staged Implementation Roadmap\n\n### Stage 1 — This Week (Stop Active Bleeding)\n1. **Patch Next.js\u002FReact** to current patched versions; strip `x-middleware-subrequest` at proxy\u002FWAF; confirm no auth relies solely on middleware — verify with CVE-2025-29927 \u002F CVE-2025-66478 detection templates\n2. **Turn on secret scanning** (Gitleaks\u002Fdetect-secrets) as pre-commit + CI gate; rotate any exposed AI keys; move to project\u002Fworkspace-scoped keys; set provider spend caps\n3. **Add token-aware rate limiting + `max_tokens` + spend alerts** to every LLM endpoint\n\n### Stage 2 — This Quarter (Build the Baseline)\n4. Wire **SAST + SCA + DAST** into CI as blocking gates; pin deps with hashes and Actions to SHA; generate SBOMs; sign artifacts with cosign (SLSA L2)\n5. Stand up a **guardrail layer** (NeMo Guardrails or LLM Guard + Llama Guard 3) with input\u002Fretrieval\u002Fexecution\u002Foutput rails; enforce Pydantic\u002FZod schemas on all model output\n6. For **RAG:** authenticate document sources, strip hidden text on ingest, enforce per-tenant isolation at the DB layer, encrypt the vector store\n7. **Mandate human review gates** and \"rule files\" for AI-assisted PRs\n\n### Stage 3 — This Year (Mature the Program)\n8. Adopt **NIST AI RMF** + map to the Cyber AI Profile and ISO 42001; maintain an AI\u002FAgent inventory with named owners\n9. **Sandbox all agents** (Firecracker\u002FgVisor), enforce least-privilege tool scoping, default-deny secret reads, egress allow-lists, human approval for high-impact actions\n10. Move secrets to a manager with dynamic\u002Fshort-lived credentials (Vault) or OIDC federation (Anthropic WIF, OpenAI service accounts); rotate on 90-day cadence\n\n### Thresholds That Change the Plan\n- Any agent with write\u002Ffinancial\u002FPII access → require sandbox + human-in-the-loop before launch\n- LLM spend variance >X% week-over-week → tighten token quotas\n- Any unauthenticated public endpoint → WAF + rate limit mandatory\n- SAST\u002FSCA HIGH\u002FCRITICAL finding → block deploy\n\n---\n\n## The First Rule\n\n\"The first sentence of any LLM defense is: don't grant the capability you can't afford to have misused.\" Capability minimization is more reliable than any filter.\n",{"name":5,"description":7},"develop","ae32736a1aabddabca5d2c96d665aa0ebc7d8700","src\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fsecurity-principles\u002Fskills\u002Fai-security-practices",3,"https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey","https:\u002F\u002Fthe-vibey-project.github.io\u002Fvibey\u002F","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project","https:\u002F\u002Fvibewithadam.matthewsteinberger.com","freedom for software engineers","2026-11-15 05:47:42",{"source":8,"skillName":5,"commands":35,"prompt":45,"zipAvailable":46},[36,39,42],{"tool":37,"command":38},"npx","npx skills add the-vibey-project\u002Fvibey --skill ai-security-practices",{"tool":40,"command":41},"bunx","bunx skills add the-vibey-project\u002Fvibey --skill ai-security-practices",{"tool":43,"command":44},"pnpm","pnpm dlx skills add the-vibey-project\u002Fvibey --skill ai-security-practices","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fsecurity-principles\u002Fskills\u002Fai-security-practices\u002FSKILL.md",true,[],[49,56,63,70,77,84],{"skillNo":50,"name":51,"nameZh":6,"description":52,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":19},"01M7AT1FQJAE1FQYAMDTF490Y4","white-paper-writing","Use when writing white papers, technical reports, policy documents, thought leadership pieces, or any long-form authoritative document. Triggers on requests to write, structure, outline, or review white papers, technical reports, position papers, policy briefs, or evidence-driven arguments. Also use for B2B content marketing strategy, technical documentation requiring authority, academic-style reports, or any document that must educate while persuading. Use when someone asks \"how should I structure this\" for a long-form document.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Fwhite-paper-writing\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":59,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":60,"categories":61,"keywords":62,"publishedAt":19},"01M7AT16BJRJJKK2MASXRZS76G","prose-technical-mechanics","Use when editing or writing prose at a technical\u002Fmechanical level: choosing between Anglo-Saxon and Latinate diction, placing information in topic vs stress positions, writing periodic vs cumulative sentences, handling sentence rhythm, punctuation as logic, transitions, paragraph structure, or phonesthetics. Use when someone wants to understand WHY prose works or doesn't, not just surface-level style advice. Triggers on word choice, sentence structure, emphasis, rhythm, diction, or any deep writing mechanics question.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Fprose-technical-mechanics\u002FSKILL.md",[],[],{"skillNo":64,"name":65,"nameZh":6,"description":66,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":67,"categories":68,"keywords":69,"publishedAt":19},"01M7AT0W9HT0ZZ9Y287NW99MR3","prose-mechanics","Use when writing, editing, or analyzing prose style; when trying to avoid generic AI-sounding writing; when applying specific prose styles such as journalistic, academic, narrative, KJV biblical, Hemingway plain, Latinate\u002FJamesian, Orwellian, Augustan, Victorian, Early Modern (Bacon, Browne, Milton), or free indirect discourse; or when coaching someone on writing voice and mechanics. Triggers on any request to improve, analyze, or write in a specific prose style, to fix \"AI-sounding\" writing, to make prose more vivid or concrete, to vary sentence rhythm, or to understand how a particular writer achieves their effects.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Fprose-mechanics\u002FSKILL.md",[],[],{"skillNo":71,"name":72,"nameZh":6,"description":73,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":74,"categories":75,"keywords":76,"publishedAt":19},"01M7AT0KR635JX66X2X6F3E2CP","narrative-structure","Use when structuring stories, pitches, presentations, white papers, or any persuasive narrative; analyzing the hero's journey in fiction or non-fiction; applying StoryBrand for business communication; designing narrative arcs for thought leadership; or understanding Vogler, Snyder, Propp, or Campbell frameworks. Triggers on any narrative structure, storytelling, pitch structure, StoryBrand, hero's journey, three-act structure, Freytag, Save the Cat, narrative arc, or \"how should I structure this story\u002Fpitch\u002Fpresentation\" request.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Fnarrative-structure\u002FSKILL.md",[],[],{"skillNo":78,"name":79,"nameZh":6,"description":80,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":81,"categories":82,"keywords":83,"publishedAt":19},"01M7AT0AKGW09NKKZDYBT8VRDE","legal-brief-writing","Use when writing legal briefs, motions, or any court filing; when analyzing judicial cognition and persuasion; when applying elite brief-writing techniques from Roberts, Clement, Kagan, Garner; or when structuring legal arguments for maximum persuasion. Also use for autistic\u002Fneurodivergent advocates who need to understand how their natural communication style maps to elite legal advocacy. Triggers on any legal writing, brief drafting, motion preparation, or legal argument structuring request.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Flegal-brief-writing\u002FSKILL.md",[],[],{"skillNo":85,"name":86,"nameZh":6,"description":87,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":88,"categories":89,"keywords":90,"publishedAt":19},"01M7AT00VWPS4Q8B5VEMXX9NQT","journal-publishability-criteria","Use whenever a research paper, article or preprint is being written, revised, reviewed or published, and specifically before every publication of docs\u002Fpaper.md. Covers what legitimate journals actually gate on, in order of gating — the editor's desk screen (scope, novelty, package problems), what reviewers judge (methodological soundness, claims the evidence can carry, statistical and qualitative reporting), EQUATOR reporting guidelines, the ICMJE authorship criteria, COPE's view of text recycling, the near-universal generative-AI policy (no AI author, disclosure, author responsibility), TOP 2025 transparency standards, how to verify a journal is legitimate, submitting without an institution, and a pre-submission checklist. Triggers on requests to evaluate, review, prepare or submit a paper, to judge publishability, to write a declarations or data-availability section, to disclose AI use, or to check a journal.","https:\u002F\u002Fgithub.com\u002Fthe-vibey-project\u002Fvibey\u002Fblob\u002Fdevelop\u002Fsrc\u002Fvibey_tools\u002Fskills\u002Fplugins\u002Fwriting-craft\u002Fskills\u002Fjournal-publishability-criteria\u002FSKILL.md",[],[]]