agcel 1.0.1
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/.agent/workflows/api-gen.md +59 -0
- package/.agent/workflows/architect.md +44 -0
- package/.agent/workflows/brainstorm.md +223 -0
- package/.agent/workflows/build.md +38 -0
- package/.agent/workflows/changelog.md +51 -0
- package/.agent/workflows/checkpoint.md +138 -0
- package/.agent/workflows/commit.md +223 -0
- package/.agent/workflows/debug.md +57 -0
- package/.agent/workflows/deploy.md +76 -0
- package/.agent/workflows/doc.md +247 -0
- package/.agent/workflows/execute-plan.md +225 -0
- package/.agent/workflows/feature.md +255 -0
- package/.agent/workflows/fix.md +323 -0
- package/.agent/workflows/help.md +63 -0
- package/.agent/workflows/index.md +148 -0
- package/.agent/workflows/load.md +112 -0
- package/.agent/workflows/mode.md +170 -0
- package/.agent/workflows/optimize.md +53 -0
- package/.agent/workflows/plan.md +337 -0
- package/.agent/workflows/pr.md +74 -0
- package/.agent/workflows/product-plan.md +36 -0
- package/.agent/workflows/production-deploy.md +39 -0
- package/.agent/workflows/refactor.md +63 -0
- package/.agent/workflows/research.md +116 -0
- package/.agent/workflows/review.md +344 -0
- package/.agent/workflows/security-scan.md +56 -0
- package/.agent/workflows/ship.md +221 -0
- package/.agent/workflows/spawn.md +177 -0
- package/.agent/workflows/status.md +59 -0
- package/.agent/workflows/tdd.md +139 -0
- package/.agent/workflows/test.md +340 -0
- package/.agent/workflows/verify.md +35 -0
- package/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/commands/init.js +142 -0
- package/dist/commands/install.js +98 -0
- package/dist/commands/list.js +49 -0
- package/dist/commands/restart.js +17 -0
- package/dist/commands/start.js +41 -0
- package/dist/commands/status.js +24 -0
- package/dist/commands/stop.js +29 -0
- package/dist/commands/uninstall.js +78 -0
- package/dist/index.js +58 -0
- package/dist/server/index.js +174 -0
- package/dist/utils/index.js +63 -0
- package/package.json +54 -0
- package/skills/api-security-best-practices/SKILL.md +291 -0
- package/skills/api-security-best-practices/references/examples.md +617 -0
- package/skills/architecture/SKILL.md +59 -0
- package/skills/architecture/context-discovery.md +43 -0
- package/skills/architecture/examples.md +94 -0
- package/skills/architecture/pattern-selection.md +68 -0
- package/skills/architecture/patterns-reference.md +50 -0
- package/skills/architecture/trade-off-analysis.md +77 -0
- package/skills/aws-serverless/SKILL.md +327 -0
- package/skills/brainstorming/SKILL.md +234 -0
- package/skills/c4-context/SKILL.md +154 -0
- package/skills/ci-cd-engineer/SKILL.md +50 -0
- package/skills/code-auditing/SKILL.md +55 -0
- package/skills/copywriting/SKILL.md +248 -0
- package/skills/database-engineer/SKILL.md +47 -0
- package/skills/doc-coauthoring/SKILL.md +379 -0
- package/skills/docker-expert/SKILL.md +412 -0
- package/skills/langgraph/SKILL.md +291 -0
- package/skills/postgresql/SKILL.md +73 -0
- package/skills/pricing-strategy/SKILL.md +360 -0
- package/skills/product-manager/SKILL.md +57 -0
- package/skills/prompt-engineer/README.md +659 -0
- package/skills/prompt-engineer/SKILL.md +256 -0
- package/skills/python-patterns/SKILL.md +445 -0
- package/skills/qa-engineer/SKILL.md +42 -0
- package/skills/rag-engineer/SKILL.md +94 -0
- package/skills/react-patterns/SKILL.md +202 -0
- package/skills/secure-refactoring/SKILL.md +54 -0
- package/skills/security-documentation/SKILL.md +53 -0
- package/skills/senior-architect/SKILL.md +213 -0
- package/skills/senior-architect/references/architecture_patterns.md +103 -0
- package/skills/senior-architect/references/system_design_workflows.md +103 -0
- package/skills/senior-architect/references/tech_decision_guide.md +103 -0
- package/skills/senior-architect/scripts/architecture_diagram_generator.py +114 -0
- package/skills/senior-architect/scripts/dependency_analyzer.py +114 -0
- package/skills/senior-architect/scripts/project_architect.py +114 -0
- package/skills/seo-audit/SKILL.md +491 -0
- package/skills/sql-injection-testing/SKILL.md +452 -0
- package/skills/test-driven-development/SKILL.md +375 -0
- package/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/skills/test-fixing/SKILL.md +123 -0
- package/skills/testing-patterns/SKILL.md +263 -0
- package/skills/typescript-expert/SKILL.md +202 -0
- package/skills/typescript-expert/references/advanced-topics.md +252 -0
- package/skills/typescript-expert/references/tsconfig-strict.json +92 -0
- package/skills/typescript-expert/references/typescript-cheatsheet.md +383 -0
- package/skills/typescript-expert/references/utility-types.ts +335 -0
- package/skills/typescript-expert/scripts/ts_diagnostic.py +203 -0
- package/skills/ui-ux-designer/SKILL.md +46 -0
- package/skills/vercel-deployment/SKILL.md +83 -0
- package/skills/vulnerability-scanner/SKILL.md +280 -0
- package/skills/vulnerability-scanner/checklists.md +121 -0
- package/skills/vulnerability-scanner/scripts/security_scan.py +458 -0
- package/skills/writing-plans/SKILL.md +120 -0
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
# API Security Examples
|
|
2
|
+
|
|
3
|
+
## Example 1: Implementing JWT Authentication
|
|
4
|
+
|
|
5
|
+
### Secure JWT Authentication Implementation
|
|
6
|
+
|
|
7
|
+
#### Authentication Flow
|
|
8
|
+
|
|
9
|
+
1. User logs in with credentials
|
|
10
|
+
2. Server validates credentials
|
|
11
|
+
3. Server generates JWT token
|
|
12
|
+
4. Client stores token securely
|
|
13
|
+
5. Client sends token with each request
|
|
14
|
+
6. Server validates token
|
|
15
|
+
|
|
16
|
+
#### Implementation
|
|
17
|
+
|
|
18
|
+
**1. Generate Secure JWT Tokens**
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
// auth.js
|
|
22
|
+
const jwt = require('jsonwebtoken');
|
|
23
|
+
const bcrypt = require('bcrypt');
|
|
24
|
+
|
|
25
|
+
// Login endpoint
|
|
26
|
+
app.post('/api/auth/login', async (req, res) => {
|
|
27
|
+
try {
|
|
28
|
+
const { email, password } = req.body;
|
|
29
|
+
|
|
30
|
+
// Validate input
|
|
31
|
+
if (!email || !password) {
|
|
32
|
+
return res.status(400).json({
|
|
33
|
+
error: 'Email and password are required'
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Find user
|
|
38
|
+
const user = await db.user.findUnique({
|
|
39
|
+
where: { email }
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (!user) {
|
|
43
|
+
// Don't reveal if user exists
|
|
44
|
+
return res.status(401).json({
|
|
45
|
+
error: 'Invalid credentials'
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Verify password
|
|
50
|
+
const validPassword = await bcrypt.compare(
|
|
51
|
+
password,
|
|
52
|
+
user.passwordHash
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (!validPassword) {
|
|
56
|
+
return res.status(401).json({
|
|
57
|
+
error: 'Invalid credentials'
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Generate JWT token
|
|
62
|
+
const token = jwt.sign(
|
|
63
|
+
{
|
|
64
|
+
userId: user.id,
|
|
65
|
+
email: user.email,
|
|
66
|
+
role: user.role
|
|
67
|
+
},
|
|
68
|
+
process.env.JWT_SECRET,
|
|
69
|
+
{
|
|
70
|
+
expiresIn: '1h',
|
|
71
|
+
issuer: 'your-app',
|
|
72
|
+
audience: 'your-app-users'
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// Generate refresh token
|
|
77
|
+
const refreshToken = jwt.sign(
|
|
78
|
+
{ userId: user.id },
|
|
79
|
+
process.env.JWT_REFRESH_SECRET,
|
|
80
|
+
{ expiresIn: '7d' }
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Store refresh token in database
|
|
84
|
+
await db.refreshToken.create({
|
|
85
|
+
data: {
|
|
86
|
+
token: refreshToken,
|
|
87
|
+
userId: user.id,
|
|
88
|
+
expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
res.json({
|
|
93
|
+
token,
|
|
94
|
+
refreshToken,
|
|
95
|
+
expiresIn: 3600
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('Login error:', error);
|
|
100
|
+
res.status(500).json({
|
|
101
|
+
error: 'An error occurred during login'
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**2. Verify JWT Tokens (Middleware)**
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
// middleware/auth.js
|
|
111
|
+
const jwt = require('jsonwebtoken');
|
|
112
|
+
|
|
113
|
+
function authenticateToken(req, res, next) {
|
|
114
|
+
// Get token from header
|
|
115
|
+
const authHeader = req.headers['authorization'];
|
|
116
|
+
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
117
|
+
|
|
118
|
+
if (!token) {
|
|
119
|
+
return res.status(401).json({
|
|
120
|
+
error: 'Access token required'
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Verify token
|
|
125
|
+
jwt.verify(
|
|
126
|
+
token,
|
|
127
|
+
process.env.JWT_SECRET,
|
|
128
|
+
{
|
|
129
|
+
issuer: 'your-app',
|
|
130
|
+
audience: 'your-app-users'
|
|
131
|
+
},
|
|
132
|
+
(err, user) => {
|
|
133
|
+
if (err) {
|
|
134
|
+
if (err.name === 'TokenExpiredError') {
|
|
135
|
+
return res.status(401).json({
|
|
136
|
+
error: 'Token expired'
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return res.status(403).json({
|
|
140
|
+
error: 'Invalid token'
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Attach user to request
|
|
145
|
+
req.user = user;
|
|
146
|
+
next();
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
module.exports = { authenticateToken };
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**3. Protect Routes**
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
const { authenticateToken } = require('./middleware/auth');
|
|
158
|
+
|
|
159
|
+
// Protected route
|
|
160
|
+
app.get('/api/user/profile', authenticateToken, async (req, res) => {
|
|
161
|
+
try {
|
|
162
|
+
const user = await db.user.findUnique({
|
|
163
|
+
where: { id: req.user.userId },
|
|
164
|
+
select: {
|
|
165
|
+
id: true,
|
|
166
|
+
email: true,
|
|
167
|
+
name: true,
|
|
168
|
+
// Don't return passwordHash
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
res.json(user);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
res.status(500).json({ error: 'Server error' });
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**4. Implement Token Refresh**
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
app.post('/api/auth/refresh', async (req, res) => {
|
|
183
|
+
const { refreshToken } = req.body;
|
|
184
|
+
|
|
185
|
+
if (!refreshToken) {
|
|
186
|
+
return res.status(401).json({
|
|
187
|
+
error: 'Refresh token required'
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
// Verify refresh token
|
|
193
|
+
const decoded = jwt.verify(
|
|
194
|
+
refreshToken,
|
|
195
|
+
process.env.JWT_REFRESH_SECRET
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
// Check if refresh token exists in database
|
|
199
|
+
const storedToken = await db.refreshToken.findFirst({
|
|
200
|
+
where: {
|
|
201
|
+
token: refreshToken,
|
|
202
|
+
userId: decoded.userId,
|
|
203
|
+
expiresAt: { gt: new Date() }
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (!storedToken) {
|
|
208
|
+
return res.status(403).json({
|
|
209
|
+
error: 'Invalid refresh token'
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Generate new access token
|
|
214
|
+
const user = await db.user.findUnique({
|
|
215
|
+
where: { id: decoded.userId }
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const newToken = jwt.sign(
|
|
219
|
+
{
|
|
220
|
+
userId: user.id,
|
|
221
|
+
email: user.email,
|
|
222
|
+
role: user.role
|
|
223
|
+
},
|
|
224
|
+
process.env.JWT_SECRET,
|
|
225
|
+
{ expiresIn: '1h' }
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
res.json({
|
|
229
|
+
token: newToken,
|
|
230
|
+
expiresIn: 3600
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
} catch (error) {
|
|
234
|
+
res.status(403).json({
|
|
235
|
+
error: 'Invalid refresh token'
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Security Best Practices
|
|
242
|
+
|
|
243
|
+
- β
Use strong JWT secrets (256-bit minimum)
|
|
244
|
+
- β
Set short expiration times (1 hour for access tokens)
|
|
245
|
+
- β
Implement refresh tokens for long-lived sessions
|
|
246
|
+
- β
Store refresh tokens in database (can be revoked)
|
|
247
|
+
- β
Use HTTPS only
|
|
248
|
+
- β
Don't store sensitive data in JWT payload
|
|
249
|
+
- β
Validate token issuer and audience
|
|
250
|
+
- β
Implement token blacklisting for logout
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
## Example 2: Input Validation and SQL Injection Prevention
|
|
254
|
+
|
|
255
|
+
### Preventing SQL Injection and Input Validation
|
|
256
|
+
|
|
257
|
+
#### The Problem
|
|
258
|
+
|
|
259
|
+
**β Vulnerable Code:**
|
|
260
|
+
```javascript
|
|
261
|
+
// NEVER DO THIS - SQL Injection vulnerability
|
|
262
|
+
app.get('/api/users/:id', async (req, res) => {
|
|
263
|
+
const userId = req.params.id;
|
|
264
|
+
|
|
265
|
+
// Dangerous: User input directly in query
|
|
266
|
+
const query = `SELECT * FROM users WHERE id = '${userId}'`;
|
|
267
|
+
const user = await db.query(query);
|
|
268
|
+
|
|
269
|
+
res.json(user);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Attack example:
|
|
273
|
+
// GET /api/users/1' OR '1'='1
|
|
274
|
+
// Returns all users!
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### The Solution
|
|
278
|
+
|
|
279
|
+
**1. Use Parameterized Queries**
|
|
280
|
+
|
|
281
|
+
```javascript
|
|
282
|
+
// β
Safe: Parameterized query
|
|
283
|
+
app.get('/api/users/:id', async (req, res) => {
|
|
284
|
+
const userId = req.params.id;
|
|
285
|
+
|
|
286
|
+
// Validate input first
|
|
287
|
+
if (!userId || /^\d+$/.test(userId)) {
|
|
288
|
+
return res.status(400).json({
|
|
289
|
+
error: 'Invalid user ID'
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Use parameterized query
|
|
294
|
+
const user = await db.query(
|
|
295
|
+
'SELECT id, email, name FROM users WHERE id = $1',
|
|
296
|
+
[userId]
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
if (!user) {
|
|
300
|
+
return res.status(404).json({
|
|
301
|
+
error: 'User not found'
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
res.json(user);
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**2. Use ORM with Proper Escaping**
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
// β
Safe: Using Prisma ORM
|
|
313
|
+
app.get('/api/users/:id', async (req, res) => {
|
|
314
|
+
const userId = parseInt(req.params.id);
|
|
315
|
+
|
|
316
|
+
if (isNaN(userId)) {
|
|
317
|
+
return res.status(400).json({
|
|
318
|
+
error: 'Invalid user ID'
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const user = await prisma.user.findUnique({
|
|
323
|
+
where: { id: userId },
|
|
324
|
+
select: {
|
|
325
|
+
id: true,
|
|
326
|
+
email: true,
|
|
327
|
+
name: true,
|
|
328
|
+
// Don't select sensitive fields
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
if (!user) {
|
|
333
|
+
return res.status(404).json({
|
|
334
|
+
error: 'User not found'
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
res.json(user);
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
**3. Implement Request Validation with Zod**
|
|
343
|
+
|
|
344
|
+
```javascript
|
|
345
|
+
const { z } = require('zod');
|
|
346
|
+
|
|
347
|
+
// Define validation schema
|
|
348
|
+
const createUserSchema = z.object({
|
|
349
|
+
email: z.string().email('Invalid email format'),
|
|
350
|
+
password: z.string()
|
|
351
|
+
.min(8, 'Password must be at least 8 characters')
|
|
352
|
+
.regex(/[A-Z]/, 'Password must contain uppercase letter')
|
|
353
|
+
.regex(/[a-z]/, 'Password must contain lowercase letter')
|
|
354
|
+
.regex(/[0-9]/, 'Password must contain number'),
|
|
355
|
+
name: z.string()
|
|
356
|
+
.min(2, 'Name must be at least 2 characters')
|
|
357
|
+
.max(100, 'Name too long'),
|
|
358
|
+
age: z.number()
|
|
359
|
+
.int('Age must be an integer')
|
|
360
|
+
.min(18, 'Must be 18 or older')
|
|
361
|
+
.max(120, 'Invalid age')
|
|
362
|
+
.optional()
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
// Validation middleware
|
|
366
|
+
function validateRequest(schema) {
|
|
367
|
+
return (req, res, next) => {
|
|
368
|
+
try {
|
|
369
|
+
schema.parse(req.body);
|
|
370
|
+
next();
|
|
371
|
+
} catch (error) {
|
|
372
|
+
res.status(400).json({
|
|
373
|
+
error: 'Validation failed',
|
|
374
|
+
details: error.errors
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Use validation
|
|
381
|
+
app.post('/api/users',
|
|
382
|
+
validateRequest(createUserSchema),
|
|
383
|
+
async (req, res) => {
|
|
384
|
+
// Input is validated at this point
|
|
385
|
+
const { email, password, name, age } = req.body;
|
|
386
|
+
|
|
387
|
+
// Hash password
|
|
388
|
+
const passwordHash = await bcrypt.hash(password, 10);
|
|
389
|
+
|
|
390
|
+
// Create user
|
|
391
|
+
const user = await prisma.user.create({
|
|
392
|
+
data: {
|
|
393
|
+
email,
|
|
394
|
+
passwordHash,
|
|
395
|
+
name,
|
|
396
|
+
age
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// Don't return password hash
|
|
401
|
+
const { passwordHash: _, ...userWithoutPassword } = user;
|
|
402
|
+
res.status(201).json(userWithoutPassword);
|
|
403
|
+
}
|
|
404
|
+
);
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
**4. Sanitize Output to Prevent XSS**
|
|
408
|
+
|
|
409
|
+
```javascript
|
|
410
|
+
const DOMPurify = require('isomorphic-dompurify');
|
|
411
|
+
|
|
412
|
+
app.post('/api/comments', authenticateToken, async (req, res) => {
|
|
413
|
+
const { content } = req.body;
|
|
414
|
+
|
|
415
|
+
// Validate
|
|
416
|
+
if (!content || content.length > 1000) {
|
|
417
|
+
return res.status(400).json({
|
|
418
|
+
error: 'Invalid comment content'
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Sanitize HTML to prevent XSS
|
|
423
|
+
const sanitizedContent = DOMPurify.sanitize(content, {
|
|
424
|
+
ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a'],
|
|
425
|
+
ALLOWED_ATTR: ['href']
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
const comment = await prisma.comment.create({
|
|
429
|
+
data: {
|
|
430
|
+
content: sanitizedContent,
|
|
431
|
+
userId: req.user.userId
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
res.status(201).json(comment);
|
|
436
|
+
});
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### Validation Checklist
|
|
440
|
+
|
|
441
|
+
- [ ] Validate all user inputs
|
|
442
|
+
- [ ] Use parameterized queries or ORM
|
|
443
|
+
- [ ] Validate data types (string, number, email, etc.)
|
|
444
|
+
- [ ] Validate data ranges (min/max length, value ranges)
|
|
445
|
+
- [ ] Sanitize HTML content
|
|
446
|
+
- [ ] Escape special characters
|
|
447
|
+
- [ ] Validate file uploads (type, size, content)
|
|
448
|
+
- [ ] Use allowlists, not blocklists
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
## Example 3: Rate Limiting and DDoS Protection
|
|
452
|
+
|
|
453
|
+
### Implementing Rate Limiting
|
|
454
|
+
|
|
455
|
+
#### Why Rate Limiting?
|
|
456
|
+
|
|
457
|
+
- Prevent brute force attacks
|
|
458
|
+
- Protect against DDoS
|
|
459
|
+
- Prevent API abuse
|
|
460
|
+
- Ensure fair usage
|
|
461
|
+
- Reduce server costs
|
|
462
|
+
|
|
463
|
+
#### Implementation with Express Rate Limit
|
|
464
|
+
|
|
465
|
+
```javascript
|
|
466
|
+
const rateLimit = require('express-rate-limit');
|
|
467
|
+
const RedisStore = require('rate-limit-redis');
|
|
468
|
+
const Redis = require('ioredis');
|
|
469
|
+
|
|
470
|
+
// Create Redis client
|
|
471
|
+
const redis = new Redis({
|
|
472
|
+
host: process.env.REDIS_HOST,
|
|
473
|
+
port: process.env.REDIS_PORT
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
// General API rate limit
|
|
477
|
+
const apiLimiter = rateLimit({
|
|
478
|
+
store: new RedisStore({
|
|
479
|
+
client: redis,
|
|
480
|
+
prefix: 'rl:api:'
|
|
481
|
+
}),
|
|
482
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
483
|
+
max: 100, // 100 requests per window
|
|
484
|
+
message: {
|
|
485
|
+
error: 'Too many requests, please try again later',
|
|
486
|
+
retryAfter: 900 // seconds
|
|
487
|
+
},
|
|
488
|
+
standardHeaders: true, // Return rate limit info in headers
|
|
489
|
+
legacyHeaders: false,
|
|
490
|
+
// Custom key generator (by user ID or IP)
|
|
491
|
+
keyGenerator: (req) => {
|
|
492
|
+
return req.user?.userId || req.ip;
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
// Strict rate limit for authentication endpoints
|
|
497
|
+
const authLimiter = rateLimit({
|
|
498
|
+
store: new RedisStore({
|
|
499
|
+
client: redis,
|
|
500
|
+
prefix: 'rl:auth:'
|
|
501
|
+
}),
|
|
502
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
503
|
+
max: 5, // Only 5 login attempts per 15 minutes
|
|
504
|
+
skipSuccessfulRequests: true, // Don't count successful logins
|
|
505
|
+
message: {
|
|
506
|
+
error: 'Too many login attempts, please try again later',
|
|
507
|
+
retryAfter: 900
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
// Apply rate limiters
|
|
512
|
+
app.use('/api/', apiLimiter);
|
|
513
|
+
app.use('/api/auth/login', authLimiter);
|
|
514
|
+
app.use('/api/auth/register', authLimiter);
|
|
515
|
+
|
|
516
|
+
// Custom rate limiter for expensive operations
|
|
517
|
+
const expensiveLimiter = rateLimit({
|
|
518
|
+
windowMs: 60 * 60 * 1000, // 1 hour
|
|
519
|
+
max: 10, // 10 requests per hour
|
|
520
|
+
message: {
|
|
521
|
+
error: 'Rate limit exceeded for this operation'
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
app.post('/api/reports/generate',
|
|
526
|
+
authenticateToken,
|
|
527
|
+
expensiveLimiter,
|
|
528
|
+
async (req, res) => {
|
|
529
|
+
// Expensive operation
|
|
530
|
+
}
|
|
531
|
+
);
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
#### Advanced: Per-User Rate Limiting
|
|
535
|
+
|
|
536
|
+
```javascript
|
|
537
|
+
// Different limits based on user tier
|
|
538
|
+
function createTieredRateLimiter() {
|
|
539
|
+
const limits = {
|
|
540
|
+
free: { windowMs: 60 * 60 * 1000, max: 100 },
|
|
541
|
+
pro: { windowMs: 60 * 60 * 1000, max: 1000 },
|
|
542
|
+
enterprise: { windowMs: 60 * 60 * 1000, max: 10000 }
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
return async (req, res, next) => {
|
|
546
|
+
const user = req.user;
|
|
547
|
+
const tier = user?.tier || 'free';
|
|
548
|
+
const limit = limits[tier];
|
|
549
|
+
|
|
550
|
+
const key = `rl:user:${user.userId}`;
|
|
551
|
+
const current = await redis.incr(key);
|
|
552
|
+
|
|
553
|
+
if (current === 1) {
|
|
554
|
+
await redis.expire(key, limit.windowMs / 1000);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if (current > limit.max) {
|
|
558
|
+
return res.status(429).json({
|
|
559
|
+
error: 'Rate limit exceeded',
|
|
560
|
+
limit: limit.max,
|
|
561
|
+
remaining: 0,
|
|
562
|
+
reset: await redis.ttl(key)
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Set rate limit headers
|
|
567
|
+
res.set({
|
|
568
|
+
'X-RateLimit-Limit': limit.max,
|
|
569
|
+
'X-RateLimit-Remaining': limit.max - current,
|
|
570
|
+
'X-RateLimit-Reset': await redis.ttl(key)
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
next();
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
app.use('/api/', authenticateToken, createTieredRateLimiter());
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
#### DDoS Protection with Helmet
|
|
581
|
+
|
|
582
|
+
```javascript
|
|
583
|
+
const helmet = require('helmet');
|
|
584
|
+
|
|
585
|
+
app.use(helmet({
|
|
586
|
+
// Content Security Policy
|
|
587
|
+
contentSecurityPolicy: {
|
|
588
|
+
directives: {
|
|
589
|
+
defaultSrc: ["'self'"],
|
|
590
|
+
styleSrc: ["'self'", "'unsafe-inline'"],
|
|
591
|
+
scriptSrc: ["'self'"],
|
|
592
|
+
imgSrc: ["'self'", 'data:', 'https:']
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
// Prevent clickjacking
|
|
596
|
+
frameguard: { action: 'deny' },
|
|
597
|
+
// Hide X-Powered-By header
|
|
598
|
+
hidePoweredBy: true,
|
|
599
|
+
// Prevent MIME type sniffing
|
|
600
|
+
noSniff: true,
|
|
601
|
+
// Enable HSTS
|
|
602
|
+
hsts: {
|
|
603
|
+
maxAge: 31536000,
|
|
604
|
+
includeSubDomains: true,
|
|
605
|
+
preload: true
|
|
606
|
+
}
|
|
607
|
+
}));
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
#### Rate Limit Response Headers
|
|
611
|
+
|
|
612
|
+
```
|
|
613
|
+
X-RateLimit-Limit: 100
|
|
614
|
+
X-RateLimit-Remaining: 87
|
|
615
|
+
X-RateLimit-Reset: 1640000000
|
|
616
|
+
Retry-After: 900
|
|
617
|
+
```
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: architecture
|
|
3
|
+
description: Architectural decision-making framework. Requirements analysis, trade-off evaluation, ADR documentation. Use when making architecture decisions or analyzing system design.
|
|
4
|
+
allowed-tools: Read, Glob, Grep
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Architecture Decision Framework
|
|
8
|
+
|
|
9
|
+
> "Requirements drive architecture. Trade-offs inform decisions. ADRs capture rationale."
|
|
10
|
+
|
|
11
|
+
## π― Selective Reading Rule
|
|
12
|
+
|
|
13
|
+
**Read ONLY files relevant to the request!** Check the content map, find what you need.
|
|
14
|
+
|
|
15
|
+
| File | Description | When to Read |
|
|
16
|
+
|------|-------------|--------------|
|
|
17
|
+
| `context-discovery.md` | Questions to ask, project classification | Starting architecture design |
|
|
18
|
+
| `trade-off-analysis.md` | ADR templates, trade-off framework | Documenting decisions |
|
|
19
|
+
| `pattern-selection.md` | Decision trees, anti-patterns | Choosing patterns |
|
|
20
|
+
| `examples.md` | MVP, SaaS, Enterprise examples | Reference implementations |
|
|
21
|
+
| `patterns-reference.md` | Quick lookup for patterns | Pattern comparison |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## π Related Skills
|
|
26
|
+
|
|
27
|
+
| Skill | Use For |
|
|
28
|
+
|-------|---------|
|
|
29
|
+
| `@[skills/database-design]` | Database schema design |
|
|
30
|
+
| `@[skills/api-patterns]` | API design patterns |
|
|
31
|
+
| `@[skills/deployment-procedures]` | Deployment architecture |
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Core Principle
|
|
36
|
+
|
|
37
|
+
**"Simplicity is the ultimate sophistication."**
|
|
38
|
+
|
|
39
|
+
- Start simple
|
|
40
|
+
- Add complexity ONLY when proven necessary
|
|
41
|
+
- You can always add patterns later
|
|
42
|
+
- Removing complexity is MUCH harder than adding it
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Validation Checklist
|
|
47
|
+
|
|
48
|
+
Before finalizing architecture:
|
|
49
|
+
|
|
50
|
+
- [ ] Requirements clearly understood
|
|
51
|
+
- [ ] Constraints identified
|
|
52
|
+
- [ ] Each decision has trade-off analysis
|
|
53
|
+
- [ ] Simpler alternatives considered
|
|
54
|
+
- [ ] ADRs written for significant decisions
|
|
55
|
+
- [ ] Team expertise matches chosen patterns
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Gap Analysis Rule
|
|
59
|
+
Always identify gaps and suggest next steps to users. In case there is no gaps anymore, then AI should clearly state that there is no gap left.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Context Discovery
|
|
2
|
+
|
|
3
|
+
> Before suggesting any architecture, gather context.
|
|
4
|
+
|
|
5
|
+
## Question Hierarchy (Ask User FIRST)
|
|
6
|
+
|
|
7
|
+
1. **Scale**
|
|
8
|
+
- How many users? (10, 1K, 100K, 1M+)
|
|
9
|
+
- Data volume? (MB, GB, TB)
|
|
10
|
+
- Transaction rate? (per second/minute)
|
|
11
|
+
|
|
12
|
+
2. **Team**
|
|
13
|
+
- Solo developer or team?
|
|
14
|
+
- Team size and expertise?
|
|
15
|
+
- Distributed or co-located?
|
|
16
|
+
|
|
17
|
+
3. **Timeline**
|
|
18
|
+
- MVP/Prototype or long-term product?
|
|
19
|
+
- Time to market pressure?
|
|
20
|
+
|
|
21
|
+
4. **Domain**
|
|
22
|
+
- CRUD-heavy or business logic complex?
|
|
23
|
+
- Real-time requirements?
|
|
24
|
+
- Compliance/regulations?
|
|
25
|
+
|
|
26
|
+
5. **Constraints**
|
|
27
|
+
- Budget limitations?
|
|
28
|
+
- Legacy systems to integrate?
|
|
29
|
+
- Technology stack preferences?
|
|
30
|
+
|
|
31
|
+
## Project Classification Matrix
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
MVP SaaS Enterprise
|
|
35
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
36
|
+
β Scale β <1K β 1K-100K β 100K+ β
|
|
37
|
+
β Team β Solo β 2-10 β 10+ β
|
|
38
|
+
β Timeline β Fast (weeks) β Medium (months)β Long (years)β
|
|
39
|
+
β Architecture β Simple β Modular β Distributed β
|
|
40
|
+
β Patterns β Minimal β Selective β Comprehensiveβ
|
|
41
|
+
β Example β Next.js API β NestJS β Microservicesβ
|
|
42
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
43
|
+
```
|