1---2name: cybersecurity-23description: Security rules, architecture decisions, and hardening guidelines for building secure systems. Use this skill whenever designing or reviewing a Next.js frontend, FastAPI backend, authentication flow, Docker environment, AI agent, RAG pipeline, admin tool, or any feature that handles user input, secrets, file uploads, external integrations, or sensitive business data. Also use when reviewing pull requests for security, preparing production deployments, or building any system that processes untrusted content. Covers OWASP top 10, container hardening, prompt injection defense, and supply-chain security.4---5
6## 10 Core Principles
7
81. Validate everything.
92. Authorize everything.
103. Trust nothing from the client.
114. Trust nothing from the model.
125. Minimize privileges everywhere.
136. Keep secrets out of code and logs.
147. Harden containers and networks by default.
158. Restrict tools, files, and outbound access.
169. Log security-relevant actions.
1710. Design so compromise has limited blast radius.
18
19## When to Use
20
21- Designing or reviewing a Next.js frontend or FastAPI backend
22- Creating authentication or authorization flows
23- Building Dockerfiles or Docker Compose environments
24- Adding file upload, search, scraping, or external integrations
25- Creating admin dashboards or internal tools
26- Building AI chat, agent, RAG, tool-calling, or automation systems
27- Handling user-generated content or third-party content
28- Preparing production deployments
29- Reviewing pull requests for security-sensitive changes
30- Implementing APIs that expose sensitive business data
31
32## Core Security Philosophy
33
34- Security must be designed into the architecture, not added as a final step
35- Every external input is untrusted until validated
36- Every internal component may eventually be compromised — design accordingly
37- Prefer secure defaults, least privilege, narrow permissions, and explicit allowlists
38- Prevention matters, but limiting blast radius after compromise matters equally
39- Simplicity is a security feature — avoid unnecessary complexity and hidden magic
40- Never trust the frontend for security decisions
41- Never trust LLM output as safe, correct, or authorized
42- If a feature is security-sensitive and unclear, choose the more restrictive behavior
43
44## Golden Rules
45
461. **Validate all inputs** at every boundary
472. **Escape or sanitize all outputs** based on rendering context
483. **Authenticate every sensitive action**
494. **Authorize every resource access** — authentication ≠ authorization
505. **Never expose secrets** in code, logs, images, or client bundles
516. **Never execute user-controlled input** in shells, SQL, templates, or interpreters
527. **Never assume internal traffic is trustworthy**
538. **Never mount dangerous host resources** into containers
549. **Never let AI outputs directly perform privileged actions** without validation and authorization
5510. **Every sensitive action must be observable**, auditable, and attributable
56
57## Secure Architecture Rules
58
59- Separate domain logic, application logic, infrastructure, and API layers — sensitive logic must not be scattered across routers or middleware
60- Enforce authentication and authorization separately — being authenticated does not imply permission
61- Perform object-level authorization on every resource access (IDOR prevention)
62- Centralize auth rules — inconsistent enforcement across routes causes privilege escalation
63- Apply least privilege by default to users, service accounts, tools, and background jobs
64- Treat background jobs, workers, cron tasks, and internal endpoints as production attack surfaces
65- Use allowlists instead of blocklists for input validation
66- Validate at the boundary, then work with trusted internal types
67
68## AI & Agent Security Rules
69
70These rules are critical when building any AI-powered feature:
71
72- **Treat all model inputs as untrusted** — user instructions, retrieved documents, web content, OCR, tool outputs can all contain prompt injection
73- **Never let the model decide authorization** — LLMs are not security boundaries and can be manipulated
74- **Separate trust layers explicitly** — system instructions, developer rules, user input, retrieved content, and tool results are NOT equally authoritative
75- **Tell the model that retrieved content is data, not instructions** — hostile text can imitate system guidance
76- **Use strict tool allowlists** — every available tool is part of the attack surface
77- **Validate all tool arguments server-side** before execution — even aligned models can generate dangerous parameters
78- **Require authorization checks outside the model** for sensitive tool calls (file access, email, payments, shell, data export, admin)
79- **Treat model-generated code, SQL, shell commands, URLs as untrusted** — prompt injection and hallucinations produce harmful actions
80- **Sandbox tool execution** with timeouts, memory limits, network restrictions, filesystem isolation
81- **Prevent unrestricted browsing/retrieval** from feeding the model with high-trust privileges — malicious websites contain hidden prompt injection
82- **Never give the model raw secrets or broad credentials** — assume they may appear in logs, outputs, or downstream prompts
83- **Protect memory/context stores** from untrusted writes — persistent prompt injection survives across sessions
84- **Require human approval for high-risk actions** — sending messages, writing files, purchases, production changes
85- **Log every tool call** — validated arguments, acting identity, policy decision, execution result
86- **Use typed output schemas** for planning and tool execution — structure reduces attack opportunities
87- **Model proposes, policy layer decides** — separate reasoning from enforcement
88- **Never let the model self-modify its security rules or tool permissions**
89- **Test for prompt injection explicitly** — "ignore previous instructions," hidden HTML, fake tool responses, poisoned PDFs
90
91## Quick Security Checklist
92
93Before shipping any feature, verify:
94
95- [ ] All external inputs validated with schemas (Pydantic/Zod)
96- [ ] Authentication required for sensitive endpoints
97- [ ] Authorization checked per resource (not just per route)
98- [ ] No secrets in code, config, logs, or Docker images
99- [ ] SQL uses parameterized queries (no string concatenation)
100- [ ] No unsafe shell execution with user input
101- [ ] Error responses do not leak internals
102- [ ] Rate limits and request size limits applied
103- [ ] Containers run as non-root with minimal capabilities
104- [ ] AI tool calls validated and authorized outside the model
105- [ ] Security-relevant events logged with request IDs
106- [ ] Dependencies scanned for known vulnerabilities
107
108## Security Review Decision Tree
109
110```
111Does the feature handle...
112|
113+-- Auth, secrets, uploads, external URLs, HTML rendering, admin actions, AI tool execution
114| --> Requires explicit security review
115|
116+-- New dependency, third-party API, browser script, Docker image, background worker
117| --> Requires supply-chain and privilege review
118|
119+-- User files, prompts, markdown, URLs, or documents that influence model behavior
120| --> Requires prompt injection and content safety review
121|
122+-- Payments, account data, exports, or internal tooling
123| --> Requires authorization and auditability review
124|
125+-- Shell execution, filesystem access, or code execution
126 --> Must be sandboxed and reviewed as high risk
127```
128
129## Deep Dives
130
131| File | Content |
132|------|---------|
133| `references/web-security.md` | Frontend (Next.js) + Backend (FastAPI) security rules, auth, secrets, input validation, API design |
134| `references/docker-security.md` | Container hardening, infrastructure, network isolation, supply chain |
135| `references/ai-agent-security.md` | AI/agent security, RAG, prompt injection, tool safety, admin tools |
136
137## Practical Examples
138
139| File | Content |
140|------|---------|
141| `examples/secure-fastapi-endpoint.py` | FastAPI endpoint with validation, auth, authorization, error handling |
142| `examples/secure-nextjs-auth-flow.md` | Next.js auth with CSP, cookies, CSRF, redirect validation |
143| `examples/secure-docker-compose.yml` | Hardened docker-compose with all security measures |
144
145## Checklists
146
147| File | Content |
148|------|---------|
149| `checklists/pull-request-security-checklist.md` | Security review checklist for PRs |
150| `checklists/production-security-checklist.md` | Pre-deploy production readiness checklist |