agentic-team-templates 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +280 -0
- package/bin/cli.js +5 -0
- package/package.json +47 -0
- package/src/index.js +521 -0
- package/templates/_shared/code-quality.md +162 -0
- package/templates/_shared/communication.md +114 -0
- package/templates/_shared/core-principles.md +62 -0
- package/templates/_shared/git-workflow.md +165 -0
- package/templates/_shared/security-fundamentals.md +173 -0
- package/templates/blockchain/.cursorrules/defi-patterns.md +520 -0
- package/templates/blockchain/.cursorrules/gas-optimization.md +339 -0
- package/templates/blockchain/.cursorrules/overview.md +130 -0
- package/templates/blockchain/.cursorrules/security.md +318 -0
- package/templates/blockchain/.cursorrules/smart-contracts.md +364 -0
- package/templates/blockchain/.cursorrules/testing.md +415 -0
- package/templates/blockchain/.cursorrules/web3-integration.md +538 -0
- package/templates/blockchain/CLAUDE.md +389 -0
- package/templates/cli-tools/.cursorrules/architecture.md +412 -0
- package/templates/cli-tools/.cursorrules/arguments.md +406 -0
- package/templates/cli-tools/.cursorrules/distribution.md +546 -0
- package/templates/cli-tools/.cursorrules/error-handling.md +455 -0
- package/templates/cli-tools/.cursorrules/overview.md +136 -0
- package/templates/cli-tools/.cursorrules/testing.md +537 -0
- package/templates/cli-tools/.cursorrules/user-experience.md +545 -0
- package/templates/cli-tools/CLAUDE.md +356 -0
- package/templates/data-engineering/.cursorrules/data-modeling.md +367 -0
- package/templates/data-engineering/.cursorrules/data-quality.md +455 -0
- package/templates/data-engineering/.cursorrules/overview.md +85 -0
- package/templates/data-engineering/.cursorrules/performance.md +339 -0
- package/templates/data-engineering/.cursorrules/pipeline-design.md +280 -0
- package/templates/data-engineering/.cursorrules/security.md +460 -0
- package/templates/data-engineering/.cursorrules/testing.md +452 -0
- package/templates/data-engineering/CLAUDE.md +974 -0
- package/templates/devops-sre/.cursorrules/capacity-planning.md +653 -0
- package/templates/devops-sre/.cursorrules/change-management.md +584 -0
- package/templates/devops-sre/.cursorrules/chaos-engineering.md +651 -0
- package/templates/devops-sre/.cursorrules/disaster-recovery.md +641 -0
- package/templates/devops-sre/.cursorrules/incident-management.md +565 -0
- package/templates/devops-sre/.cursorrules/observability.md +714 -0
- package/templates/devops-sre/.cursorrules/overview.md +230 -0
- package/templates/devops-sre/.cursorrules/postmortems.md +588 -0
- package/templates/devops-sre/.cursorrules/runbooks.md +760 -0
- package/templates/devops-sre/.cursorrules/slo-sli.md +617 -0
- package/templates/devops-sre/.cursorrules/toil-reduction.md +567 -0
- package/templates/devops-sre/CLAUDE.md +1007 -0
- package/templates/documentation/.cursorrules/adr.md +277 -0
- package/templates/documentation/.cursorrules/api-documentation.md +411 -0
- package/templates/documentation/.cursorrules/code-comments.md +253 -0
- package/templates/documentation/.cursorrules/maintenance.md +260 -0
- package/templates/documentation/.cursorrules/overview.md +82 -0
- package/templates/documentation/.cursorrules/readme-standards.md +306 -0
- package/templates/documentation/CLAUDE.md +120 -0
- package/templates/fullstack/.cursorrules/api-contracts.md +331 -0
- package/templates/fullstack/.cursorrules/architecture.md +298 -0
- package/templates/fullstack/.cursorrules/overview.md +109 -0
- package/templates/fullstack/.cursorrules/shared-types.md +348 -0
- package/templates/fullstack/.cursorrules/testing.md +386 -0
- package/templates/fullstack/CLAUDE.md +349 -0
- package/templates/ml-ai/.cursorrules/data-engineering.md +483 -0
- package/templates/ml-ai/.cursorrules/deployment.md +601 -0
- package/templates/ml-ai/.cursorrules/model-development.md +538 -0
- package/templates/ml-ai/.cursorrules/monitoring.md +658 -0
- package/templates/ml-ai/.cursorrules/overview.md +131 -0
- package/templates/ml-ai/.cursorrules/security.md +637 -0
- package/templates/ml-ai/.cursorrules/testing.md +678 -0
- package/templates/ml-ai/CLAUDE.md +1136 -0
- package/templates/mobile/.cursorrules/navigation.md +246 -0
- package/templates/mobile/.cursorrules/offline-first.md +302 -0
- package/templates/mobile/.cursorrules/overview.md +71 -0
- package/templates/mobile/.cursorrules/performance.md +345 -0
- package/templates/mobile/.cursorrules/testing.md +339 -0
- package/templates/mobile/CLAUDE.md +233 -0
- package/templates/platform-engineering/.cursorrules/ci-cd.md +778 -0
- package/templates/platform-engineering/.cursorrules/developer-experience.md +632 -0
- package/templates/platform-engineering/.cursorrules/infrastructure-as-code.md +600 -0
- package/templates/platform-engineering/.cursorrules/kubernetes.md +710 -0
- package/templates/platform-engineering/.cursorrules/observability.md +747 -0
- package/templates/platform-engineering/.cursorrules/overview.md +215 -0
- package/templates/platform-engineering/.cursorrules/security.md +855 -0
- package/templates/platform-engineering/.cursorrules/testing.md +878 -0
- package/templates/platform-engineering/CLAUDE.md +850 -0
- package/templates/utility-agent/.cursorrules/action-control.md +284 -0
- package/templates/utility-agent/.cursorrules/context-management.md +186 -0
- package/templates/utility-agent/.cursorrules/hallucination-prevention.md +253 -0
- package/templates/utility-agent/.cursorrules/overview.md +78 -0
- package/templates/utility-agent/.cursorrules/token-optimization.md +369 -0
- package/templates/utility-agent/CLAUDE.md +513 -0
- package/templates/web-backend/.cursorrules/api-design.md +255 -0
- package/templates/web-backend/.cursorrules/authentication.md +309 -0
- package/templates/web-backend/.cursorrules/database-patterns.md +298 -0
- package/templates/web-backend/.cursorrules/error-handling.md +366 -0
- package/templates/web-backend/.cursorrules/overview.md +69 -0
- package/templates/web-backend/.cursorrules/security.md +358 -0
- package/templates/web-backend/.cursorrules/testing.md +395 -0
- package/templates/web-backend/CLAUDE.md +366 -0
- package/templates/web-frontend/.cursorrules/accessibility.md +296 -0
- package/templates/web-frontend/.cursorrules/component-patterns.md +204 -0
- package/templates/web-frontend/.cursorrules/overview.md +72 -0
- package/templates/web-frontend/.cursorrules/performance.md +325 -0
- package/templates/web-frontend/.cursorrules/state-management.md +227 -0
- package/templates/web-frontend/.cursorrules/styling.md +271 -0
- package/templates/web-frontend/.cursorrules/testing.md +311 -0
- package/templates/web-frontend/CLAUDE.md +399 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Communication Guidelines
|
|
2
|
+
|
|
3
|
+
How to communicate effectively when assisting with development tasks.
|
|
4
|
+
|
|
5
|
+
## Tone and Style
|
|
6
|
+
|
|
7
|
+
### Be Direct and Concise
|
|
8
|
+
- Get to the point quickly
|
|
9
|
+
- Avoid unnecessary filler words
|
|
10
|
+
- Use clear, simple language
|
|
11
|
+
- Prefer short paragraphs
|
|
12
|
+
|
|
13
|
+
### Be Honest
|
|
14
|
+
- If something doesn't work, say it doesn't work
|
|
15
|
+
- If you don't know something, say you don't know
|
|
16
|
+
- If there's a better approach, suggest it
|
|
17
|
+
- Don't hide problems or risks
|
|
18
|
+
|
|
19
|
+
### Be Objective
|
|
20
|
+
- Focus on facts, not opinions
|
|
21
|
+
- Provide evidence for claims
|
|
22
|
+
- Acknowledge trade-offs
|
|
23
|
+
- Present alternatives when relevant
|
|
24
|
+
|
|
25
|
+
## Code Communication
|
|
26
|
+
|
|
27
|
+
### When Showing Code Examples
|
|
28
|
+
|
|
29
|
+
Always show both good and bad examples when illustrating patterns:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
// Good: Clear intention
|
|
33
|
+
const isActive = user.status === 'active';
|
|
34
|
+
|
|
35
|
+
// Bad: Unclear
|
|
36
|
+
const x = user.status === 'active';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### When Explaining Code
|
|
40
|
+
|
|
41
|
+
- Start with what the code does at a high level
|
|
42
|
+
- Then explain the details
|
|
43
|
+
- Use code references with file paths and line numbers
|
|
44
|
+
- Example: "The authentication check happens in `src/auth/middleware.ts:45`"
|
|
45
|
+
|
|
46
|
+
### When Suggesting Changes
|
|
47
|
+
|
|
48
|
+
- Explain *why* the change is needed
|
|
49
|
+
- Show the current state and proposed state
|
|
50
|
+
- Note any risks or considerations
|
|
51
|
+
- Be specific about file locations
|
|
52
|
+
|
|
53
|
+
## Avoiding Common Communication Issues
|
|
54
|
+
|
|
55
|
+
### Don't Over-Explain
|
|
56
|
+
❌ "I'm going to read the file to understand the code structure so I can make the changes you requested."
|
|
57
|
+
✅ "Let me check the file." [then read it]
|
|
58
|
+
|
|
59
|
+
### Don't Be Sycophantic
|
|
60
|
+
❌ "That's a great question! You're absolutely right that..."
|
|
61
|
+
✅ "The answer is..."
|
|
62
|
+
|
|
63
|
+
### Don't Use Unnecessary Superlatives
|
|
64
|
+
❌ "This is an excellent implementation!"
|
|
65
|
+
✅ "This implementation handles the edge cases correctly."
|
|
66
|
+
|
|
67
|
+
### Don't Hedge Excessively
|
|
68
|
+
❌ "I think maybe this could potentially be an issue perhaps..."
|
|
69
|
+
✅ "This may cause a race condition. Here's why..."
|
|
70
|
+
|
|
71
|
+
## Asking Clarifying Questions
|
|
72
|
+
|
|
73
|
+
When requirements are unclear:
|
|
74
|
+
- Ask specific, focused questions
|
|
75
|
+
- Provide options when possible
|
|
76
|
+
- Explain why the clarification matters
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
Good: "Should the retry logic use exponential backoff or fixed intervals? Exponential is better for rate-limited APIs, fixed is simpler."
|
|
80
|
+
|
|
81
|
+
Bad: "How do you want me to implement the retry logic?"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Reporting Errors
|
|
85
|
+
|
|
86
|
+
When something fails:
|
|
87
|
+
1. State what went wrong clearly
|
|
88
|
+
2. Provide the relevant error message
|
|
89
|
+
3. Explain the likely cause
|
|
90
|
+
4. Suggest next steps
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
The build failed with a TypeScript error in `src/api/users.ts:23`:
|
|
94
|
+
|
|
95
|
+
Type 'string' is not assignable to type 'number'.
|
|
96
|
+
|
|
97
|
+
The `userId` parameter expects a number but is receiving a string from the URL params. We need to parse it: `const userId = parseInt(params.id, 10);`
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Progress Updates
|
|
101
|
+
|
|
102
|
+
For longer tasks:
|
|
103
|
+
- Indicate what you're doing at major steps
|
|
104
|
+
- Report completion of significant milestones
|
|
105
|
+
- Surface blockers immediately
|
|
106
|
+
- Don't narrate every small action
|
|
107
|
+
|
|
108
|
+
## Documentation
|
|
109
|
+
|
|
110
|
+
When writing documentation:
|
|
111
|
+
- Lead with the most important information
|
|
112
|
+
- Use clear headings and structure
|
|
113
|
+
- Include working code examples
|
|
114
|
+
- Keep it up-to-date or delete it
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Core Principles
|
|
2
|
+
|
|
3
|
+
Universal principles that guide all development work, regardless of technology stack or project type.
|
|
4
|
+
|
|
5
|
+
## 1. Honesty Over Output
|
|
6
|
+
|
|
7
|
+
- If something doesn't work, say it doesn't work
|
|
8
|
+
- If you don't know, say you don't know
|
|
9
|
+
- If a pattern is wrong, refactor it immediately
|
|
10
|
+
- Never hide errors or suppress warnings without justification
|
|
11
|
+
- Admit mistakes early—they're cheaper to fix
|
|
12
|
+
|
|
13
|
+
## 2. Simplicity Over Cleverness
|
|
14
|
+
|
|
15
|
+
- Write code that is easy to read and understand
|
|
16
|
+
- Avoid premature optimization
|
|
17
|
+
- Prefer explicit over implicit
|
|
18
|
+
- The best code is code you don't have to write
|
|
19
|
+
- If it needs a comment to explain *what* it does, simplify it
|
|
20
|
+
|
|
21
|
+
## 3. Tests Are Required
|
|
22
|
+
|
|
23
|
+
- No feature is complete without tests
|
|
24
|
+
- Tests document expected behavior
|
|
25
|
+
- Green CI or it didn't happen
|
|
26
|
+
- Test behavior, not implementation details
|
|
27
|
+
- A failing test is better than no test
|
|
28
|
+
|
|
29
|
+
## 4. Professional Objectivity
|
|
30
|
+
|
|
31
|
+
- Focus on facts and problem-solving
|
|
32
|
+
- Provide direct, objective technical information
|
|
33
|
+
- Respectful correction is more valuable than false agreement
|
|
34
|
+
- Investigate to find the truth rather than confirming assumptions
|
|
35
|
+
- Disagree when necessary, even if it's not what the user wants to hear
|
|
36
|
+
|
|
37
|
+
## 5. Incremental Progress
|
|
38
|
+
|
|
39
|
+
- Ship small, working increments
|
|
40
|
+
- A working prototype beats a perfect design document
|
|
41
|
+
- Get feedback early and often
|
|
42
|
+
- Progress > perfection
|
|
43
|
+
|
|
44
|
+
## Anti-Patterns to Avoid
|
|
45
|
+
|
|
46
|
+
### "It Works on My Machine"
|
|
47
|
+
|
|
48
|
+
❌ **Wrong**: Ship code without considering different environments
|
|
49
|
+
|
|
50
|
+
✅ **Right**: Test in environments that match production
|
|
51
|
+
|
|
52
|
+
### "We'll Fix It Later"
|
|
53
|
+
|
|
54
|
+
❌ **Wrong**: Knowingly ship broken or incomplete code
|
|
55
|
+
|
|
56
|
+
✅ **Right**: Either fix it now or explicitly track it as tech debt
|
|
57
|
+
|
|
58
|
+
### "The User Will Figure It Out"
|
|
59
|
+
|
|
60
|
+
❌ **Wrong**: Unclear interfaces, cryptic errors, missing documentation
|
|
61
|
+
|
|
62
|
+
✅ **Right**: Clear error messages, intuitive interfaces, helpful docs
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Git Workflow
|
|
2
|
+
|
|
3
|
+
Universal git practices for maintaining clean, traceable project history.
|
|
4
|
+
|
|
5
|
+
**Note**: For comprehensive commit and PR standards including semantic versioning, see the main `.cursorrules` directory:
|
|
6
|
+
- [Commit Standards](../../.cursorrules/commit-standards.md)
|
|
7
|
+
- [PR Standards](../../.cursorrules/pr-standards.md)
|
|
8
|
+
|
|
9
|
+
## Commit Practices
|
|
10
|
+
|
|
11
|
+
### Small, Meaningful Commits
|
|
12
|
+
|
|
13
|
+
Commits should be atomic and focused:
|
|
14
|
+
- One logical change per commit
|
|
15
|
+
- Each commit should compile and pass tests
|
|
16
|
+
- Separate refactoring from feature changes
|
|
17
|
+
- Separate tests from implementation
|
|
18
|
+
|
|
19
|
+
**Good commit examples:**
|
|
20
|
+
- `feat(auth): add login button component`
|
|
21
|
+
- `test(auth): add login button tests`
|
|
22
|
+
- `fix(api): handle rate limit errors`
|
|
23
|
+
- `refactor(utils): extract date formatting`
|
|
24
|
+
|
|
25
|
+
**Bad commit examples:**
|
|
26
|
+
- `WIP` or `fix stuff`
|
|
27
|
+
- `add feature and fix bugs and update tests`
|
|
28
|
+
- Giant commits with 50+ file changes
|
|
29
|
+
|
|
30
|
+
### Commit Message Format (Conventional Commits)
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
<type>(<scope>): <subject>
|
|
34
|
+
|
|
35
|
+
<body>
|
|
36
|
+
|
|
37
|
+
<footer>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Types:**
|
|
41
|
+
- `feat`: New feature
|
|
42
|
+
- `fix`: Bug fix
|
|
43
|
+
- `docs`: Documentation
|
|
44
|
+
- `test`: Tests
|
|
45
|
+
- `refactor`: Code change that neither fixes a bug nor adds a feature
|
|
46
|
+
- `perf`: Performance improvement
|
|
47
|
+
- `chore`: Build process or auxiliary tool changes
|
|
48
|
+
- `style`: Formatting, missing semicolons, etc. (no code change)
|
|
49
|
+
- `ci`: CI configuration changes
|
|
50
|
+
|
|
51
|
+
**Example:**
|
|
52
|
+
```
|
|
53
|
+
feat(auth): add OAuth integration
|
|
54
|
+
|
|
55
|
+
- Implement OAuth callback handler
|
|
56
|
+
- Add token validation
|
|
57
|
+
- Set secure HTTP-only cookies
|
|
58
|
+
|
|
59
|
+
Closes #123
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Branch Strategy
|
|
63
|
+
|
|
64
|
+
### Feature Branches
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# Create feature branch from main
|
|
68
|
+
git checkout main
|
|
69
|
+
git pull origin main
|
|
70
|
+
git checkout -b feat/user-authentication
|
|
71
|
+
|
|
72
|
+
# Work in small commits
|
|
73
|
+
git add src/auth/login.ts
|
|
74
|
+
git commit -m "feat(auth): add login component"
|
|
75
|
+
|
|
76
|
+
git add tests/auth/login.test.ts
|
|
77
|
+
git commit -m "test(auth): add login component tests"
|
|
78
|
+
|
|
79
|
+
# Push and create PR
|
|
80
|
+
git push origin feat/user-authentication
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Branch Naming
|
|
84
|
+
|
|
85
|
+
- `feat/description` - New features
|
|
86
|
+
- `fix/description` - Bug fixes
|
|
87
|
+
- `refactor/description` - Code refactoring
|
|
88
|
+
- `docs/description` - Documentation updates
|
|
89
|
+
- `chore/description` - Maintenance tasks
|
|
90
|
+
|
|
91
|
+
## Pull Request Guidelines
|
|
92
|
+
|
|
93
|
+
### Before Creating PR
|
|
94
|
+
|
|
95
|
+
- [ ] All tests pass locally
|
|
96
|
+
- [ ] Code compiles without errors
|
|
97
|
+
- [ ] Linting passes
|
|
98
|
+
- [ ] Code is formatted
|
|
99
|
+
- [ ] Self-review completed
|
|
100
|
+
|
|
101
|
+
### PR Description Template
|
|
102
|
+
|
|
103
|
+
```markdown
|
|
104
|
+
## Summary
|
|
105
|
+
Brief description of changes
|
|
106
|
+
|
|
107
|
+
## Changes
|
|
108
|
+
- Bullet points of specific changes
|
|
109
|
+
|
|
110
|
+
## Testing
|
|
111
|
+
How were these changes tested?
|
|
112
|
+
|
|
113
|
+
## Checklist
|
|
114
|
+
- [ ] Tests added/updated
|
|
115
|
+
- [ ] Documentation updated
|
|
116
|
+
- [ ] No breaking changes (or documented)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Safety Rules
|
|
120
|
+
|
|
121
|
+
### Never Do These Without Explicit Permission
|
|
122
|
+
|
|
123
|
+
- `git push --force` (especially to main/master)
|
|
124
|
+
- `git reset --hard`
|
|
125
|
+
- `git checkout .` or `git restore .`
|
|
126
|
+
- `git clean -f`
|
|
127
|
+
- `git branch -D`
|
|
128
|
+
- Skip hooks (`--no-verify`)
|
|
129
|
+
|
|
130
|
+
### Staging Best Practices
|
|
131
|
+
|
|
132
|
+
- Stage specific files by name rather than `git add -A` or `git add .`
|
|
133
|
+
- Review staged changes before committing: `git diff --staged`
|
|
134
|
+
- Avoid accidentally staging sensitive files (.env, credentials, etc.)
|
|
135
|
+
|
|
136
|
+
## Handling Mistakes
|
|
137
|
+
|
|
138
|
+
### Amending Commits
|
|
139
|
+
|
|
140
|
+
Only amend the most recent unpushed commit:
|
|
141
|
+
```bash
|
|
142
|
+
git add forgotten-file.ts
|
|
143
|
+
git commit --amend --no-edit
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Fixing Pre-Commit Hook Failures
|
|
147
|
+
|
|
148
|
+
When a pre-commit hook fails:
|
|
149
|
+
1. The commit did NOT happen
|
|
150
|
+
2. Fix the issue identified by the hook
|
|
151
|
+
3. Re-stage the changes
|
|
152
|
+
4. Create a NEW commit (don't amend the previous commit)
|
|
153
|
+
|
|
154
|
+
### Undoing Local Changes
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# Discard changes to a specific file
|
|
158
|
+
git checkout -- path/to/file
|
|
159
|
+
|
|
160
|
+
# Unstage a file (keep changes)
|
|
161
|
+
git reset HEAD path/to/file
|
|
162
|
+
|
|
163
|
+
# Undo last commit (keep changes)
|
|
164
|
+
git reset --soft HEAD~1
|
|
165
|
+
```
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# Security Fundamentals
|
|
2
|
+
|
|
3
|
+
Universal security practices applicable to all software development.
|
|
4
|
+
|
|
5
|
+
## Core Security Principles
|
|
6
|
+
|
|
7
|
+
### 1. Zero Trust
|
|
8
|
+
Every input is hostile until proven otherwise.
|
|
9
|
+
|
|
10
|
+
### 2. Defense in Depth
|
|
11
|
+
Multiple layers of security, never rely on a single control.
|
|
12
|
+
|
|
13
|
+
### 3. Least Privilege
|
|
14
|
+
Grant minimum permissions necessary for the task.
|
|
15
|
+
|
|
16
|
+
### 4. Fail Secure
|
|
17
|
+
When something fails, fail to a secure state.
|
|
18
|
+
|
|
19
|
+
## Input Validation
|
|
20
|
+
|
|
21
|
+
### Validate Everything
|
|
22
|
+
|
|
23
|
+
All external input must be validated:
|
|
24
|
+
- User input (forms, URL parameters, headers)
|
|
25
|
+
- API requests
|
|
26
|
+
- File uploads
|
|
27
|
+
- Environment variables at startup
|
|
28
|
+
- Configuration files
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
// Good: Validate at system boundaries
|
|
32
|
+
function createUser(input: unknown): Result<User, ValidationError> {
|
|
33
|
+
const parsed = UserSchema.safeParse(input);
|
|
34
|
+
if (!parsed.success) {
|
|
35
|
+
return err(new ValidationError(parsed.error));
|
|
36
|
+
}
|
|
37
|
+
return ok(processUser(parsed.data));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Bad: Trust input
|
|
41
|
+
function createUser(input: any): User {
|
|
42
|
+
return processUser(input); // What if input is malicious?
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Sanitize Output
|
|
47
|
+
|
|
48
|
+
Always sanitize data before rendering or returning:
|
|
49
|
+
- HTML encode for web display
|
|
50
|
+
- Parameterize database queries
|
|
51
|
+
- Encode for the target context (URL, JSON, XML, etc.)
|
|
52
|
+
|
|
53
|
+
## Common Vulnerabilities (OWASP Top 10)
|
|
54
|
+
|
|
55
|
+
### Injection (SQL, Command, etc.)
|
|
56
|
+
|
|
57
|
+
❌ **Vulnerable:**
|
|
58
|
+
```
|
|
59
|
+
const query = `SELECT * FROM users WHERE id = ${userId}`;
|
|
60
|
+
exec(`convert ${filename} output.pdf`);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
✅ **Secure:**
|
|
64
|
+
```
|
|
65
|
+
const query = 'SELECT * FROM users WHERE id = ?';
|
|
66
|
+
db.query(query, [userId]);
|
|
67
|
+
|
|
68
|
+
// Use libraries that handle escaping
|
|
69
|
+
execFile('convert', [sanitizedFilename, 'output.pdf']);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Cross-Site Scripting (XSS)
|
|
73
|
+
|
|
74
|
+
❌ **Vulnerable:**
|
|
75
|
+
```
|
|
76
|
+
element.innerHTML = userInput;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
✅ **Secure:**
|
|
80
|
+
```
|
|
81
|
+
element.textContent = userInput;
|
|
82
|
+
// Or use a sanitization library for HTML
|
|
83
|
+
element.innerHTML = DOMPurify.sanitize(userInput);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Broken Authentication
|
|
87
|
+
|
|
88
|
+
- Use established authentication libraries
|
|
89
|
+
- Implement proper session management
|
|
90
|
+
- Use secure, HTTP-only, SameSite cookies
|
|
91
|
+
- Implement rate limiting on auth endpoints
|
|
92
|
+
- Use CSRF tokens for state-changing operations
|
|
93
|
+
|
|
94
|
+
### Sensitive Data Exposure
|
|
95
|
+
|
|
96
|
+
- Never log sensitive data (passwords, tokens, PII)
|
|
97
|
+
- Use HTTPS everywhere
|
|
98
|
+
- Encrypt sensitive data at rest
|
|
99
|
+
- Use secure password hashing (bcrypt, argon2)
|
|
100
|
+
|
|
101
|
+
## Secrets Management
|
|
102
|
+
|
|
103
|
+
### Never Commit Secrets
|
|
104
|
+
|
|
105
|
+
- No API keys in code
|
|
106
|
+
- No passwords in configuration files
|
|
107
|
+
- No tokens in version control
|
|
108
|
+
- Use `.gitignore` for sensitive files
|
|
109
|
+
|
|
110
|
+
### Environment Variables
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
# .env.local (never committed)
|
|
114
|
+
DATABASE_URL=postgres://...
|
|
115
|
+
API_KEY=sk_live_...
|
|
116
|
+
|
|
117
|
+
# .env.example (committed, no real values)
|
|
118
|
+
DATABASE_URL=postgres://user:pass@localhost/db
|
|
119
|
+
API_KEY=your_api_key_here
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Secret Rotation
|
|
123
|
+
|
|
124
|
+
- Support secret rotation without downtime
|
|
125
|
+
- Have a plan for compromised credentials
|
|
126
|
+
- Audit secret access
|
|
127
|
+
|
|
128
|
+
## Error Handling for Security
|
|
129
|
+
|
|
130
|
+
### Don't Leak Information
|
|
131
|
+
|
|
132
|
+
❌ **Leaky:**
|
|
133
|
+
```
|
|
134
|
+
catch (e) {
|
|
135
|
+
res.status(500).json({ error: e.stack }); // Reveals internals
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Login error
|
|
139
|
+
res.json({ error: 'Password incorrect for user admin' }); // Confirms username exists
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
✅ **Secure:**
|
|
143
|
+
```
|
|
144
|
+
catch (e) {
|
|
145
|
+
logger.error('Internal error', { error: e, requestId });
|
|
146
|
+
res.status(500).json({ error: 'Internal server error', requestId });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Login error
|
|
150
|
+
res.json({ error: 'Invalid credentials' }); // Generic message
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Dependency Security
|
|
154
|
+
|
|
155
|
+
- Keep dependencies updated
|
|
156
|
+
- Audit dependencies regularly (`npm audit`, `pip-audit`, etc.)
|
|
157
|
+
- Review new dependencies before adding
|
|
158
|
+
- Prefer well-maintained, widely-used packages
|
|
159
|
+
- Lock dependency versions
|
|
160
|
+
|
|
161
|
+
## Security Checklist
|
|
162
|
+
|
|
163
|
+
Before deployment:
|
|
164
|
+
- [ ] All inputs validated
|
|
165
|
+
- [ ] Outputs properly encoded
|
|
166
|
+
- [ ] Authentication implemented correctly
|
|
167
|
+
- [ ] Authorization checked on all protected resources
|
|
168
|
+
- [ ] Secrets not in code or logs
|
|
169
|
+
- [ ] HTTPS enforced
|
|
170
|
+
- [ ] Security headers set (CSP, HSTS, etc.)
|
|
171
|
+
- [ ] Dependencies audited
|
|
172
|
+
- [ ] Rate limiting in place
|
|
173
|
+
- [ ] Logging (without sensitive data) enabled
|