axiom-coding-agent-setup 1.1.0 → 1.1.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/.agents/CONTEXT-MANAGEMENT.md +155 -155
- package/.agents/DEBUGGING.md +124 -124
- package/.agents/ENGINEERING.md +180 -180
- package/.agents/PERFORMANCE.md +164 -164
- package/.agents/SECURITY.md +109 -109
- package/.agents/WORKFLOW.md +143 -143
- package/.agents/skills/agent-browser/SKILL.md +55 -55
- package/.agents/skills/project-design/SKILL.md +207 -207
- package/.agents/skills/project-design/references/ARCHITECTURE.md +641 -641
- package/.agents/skills/project-design/references/PROJECT_PLAN.md +315 -315
- package/.env.axiom +8 -8
- package/AGENTS.md +104 -104
- package/README.md +145 -145
- package/bin/cli.js +0 -1
- package/error/error.md +57 -0
- package/opencode.json +64 -64
- package/package.json +1 -1
- package/plugin/oh-my-openagent.json +198 -198
- package/skills-lock.json +57 -57
package/.agents/PERFORMANCE.md
CHANGED
|
@@ -1,164 +1,164 @@
|
|
|
1
|
-
# Performance Awareness
|
|
2
|
-
|
|
3
|
-
## Mindset: Measure First, Optimize Second
|
|
4
|
-
|
|
5
|
-
Premature optimization is the root of all evil — but so is shipping code that falls over at scale. The key is knowing when to care and what to measure.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## The Performance Hierarchy
|
|
10
|
-
|
|
11
|
-
Optimize in this order:
|
|
12
|
-
|
|
13
|
-
1. **Algorithmic complexity** — O(n²) will always beat O(n) eventually, regardless of micro-optimizations
|
|
14
|
-
2. **Database queries** — N+1 queries, missing indexes, and full table scans kill performance
|
|
15
|
-
3. **I/O bottlenecks** — Network calls, disk reads, file system operations
|
|
16
|
-
4. **Memory allocation** — Excessive object creation, memory leaks, large data structures
|
|
17
|
-
5. **Micro-optimizations** — Loop unrolling, cache locality (last resort, measure impact)
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
## Key Metrics
|
|
22
|
-
|
|
23
|
-
### Response Time
|
|
24
|
-
| Target | Acceptable | Warning | Critical |
|
|
25
|
-
|---|---|---|---|
|
|
26
|
-
| API response | < 100ms | < 500ms | > 1s |
|
|
27
|
-
| Page load | < 1s | < 3s | > 5s |
|
|
28
|
-
| Database query | < 10ms | < 100ms | > 500ms |
|
|
29
|
-
|
|
30
|
-
### Throughput
|
|
31
|
-
- Requests per second (RPS) your system can handle
|
|
32
|
-
- Concurrent connections supported
|
|
33
|
-
- Queue depth and wait times
|
|
34
|
-
|
|
35
|
-
### Resource Utilization
|
|
36
|
-
- CPU: sustained > 70% is a warning
|
|
37
|
-
- Memory: monitor for leaks and growth over time
|
|
38
|
-
- Disk I/O: random reads are expensive; sequential is cheap
|
|
39
|
-
- Network: bandwidth and latency to dependent services
|
|
40
|
-
|
|
41
|
-
---
|
|
42
|
-
|
|
43
|
-
## Caching Strategy
|
|
44
|
-
|
|
45
|
-
### When to Cache
|
|
46
|
-
- **Static assets** — Cache aggressively (CDN, browser cache)
|
|
47
|
-
- **Database queries** — Cache results that change infrequently
|
|
48
|
-
- **Computed values** — Cache expensive calculations
|
|
49
|
-
- **API responses** — Cache external API calls with appropriate TTL
|
|
50
|
-
|
|
51
|
-
### When NOT to Cache
|
|
52
|
-
- **User-specific data** — unless scoped to that user
|
|
53
|
-
- **Real-time data** — stock prices, live feeds
|
|
54
|
-
- **Small, cheap computations** — cache overhead > computation cost
|
|
55
|
-
- **Data that must be consistent** — cache invalidation is hard
|
|
56
|
-
|
|
57
|
-
### Cache Invalidation
|
|
58
|
-
```
|
|
59
|
-
"There are only two hard things in Computer Science:
|
|
60
|
-
cache invalidation and naming things."
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
- Prefer **TTL-based** expiration for simplicity
|
|
64
|
-
- Use **write-through** or **write-behind** for consistency-critical data
|
|
65
|
-
- **Never** cache without an invalidation strategy
|
|
66
|
-
|
|
67
|
-
---
|
|
68
|
-
|
|
69
|
-
## Database Performance
|
|
70
|
-
|
|
71
|
-
### Query Optimization
|
|
72
|
-
- **Indexing** — Index columns used in WHERE, JOIN, ORDER BY. Don't over-index (writes slow down).
|
|
73
|
-
- **N+1 Problem** — Fetch related data in a single query (JOIN) or batch queries
|
|
74
|
-
- **Pagination** — Never `SELECT *` without LIMIT on large tables
|
|
75
|
-
- **Explain plans** — Use `EXPLAIN` (PostgreSQL) or `EXPLAIN ANALYZE` to understand query execution
|
|
76
|
-
|
|
77
|
-
### Connection Management
|
|
78
|
-
- Use connection pooling (PgBouncer, SQLAlchemy pool, Prisma connection pool)
|
|
79
|
-
- Close connections promptly; don't leak them
|
|
80
|
-
- Monitor pool exhaustion
|
|
81
|
-
|
|
82
|
-
---
|
|
83
|
-
|
|
84
|
-
## Async & Concurrency
|
|
85
|
-
|
|
86
|
-
### When to Use Async
|
|
87
|
-
- **I/O-bound work** — API calls, database queries, file reads
|
|
88
|
-
- **Many concurrent operations** — handling thousands of connections
|
|
89
|
-
- **Not for CPU-bound work** — use threads/processes instead
|
|
90
|
-
|
|
91
|
-
### Concurrency Patterns
|
|
92
|
-
- **Batching** — Group small operations into larger ones
|
|
93
|
-
- **Parallelization** — Run independent operations concurrently
|
|
94
|
-
- **Streaming** — Process data as it arrives, not all at once
|
|
95
|
-
- **Backpressure** — Slow down producers when consumers can't keep up
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## Frontend Performance
|
|
100
|
-
|
|
101
|
-
### Core Web Vitals
|
|
102
|
-
- **LCP (Largest Contentful Paint)** — < 2.5s (main content loaded)
|
|
103
|
-
- **FID (First Input Delay)** — < 100ms (page is interactive)
|
|
104
|
-
- **CLS (Cumulative Layout Shift)** — < 0.1 (visual stability)
|
|
105
|
-
|
|
106
|
-
### Techniques
|
|
107
|
-
- **Code splitting** — Load only what the user needs
|
|
108
|
-
- **Lazy loading** — Images, components, routes load on demand
|
|
109
|
-
- **Tree shaking** — Remove unused code at build time
|
|
110
|
-
- **Image optimization** — WebP, responsive sizes, lazy loading
|
|
111
|
-
- **Minimize main thread work** — Offload to web workers where possible
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
## Profiling & Measurement
|
|
116
|
-
|
|
117
|
-
### Before Optimizing
|
|
118
|
-
1. **Profile** — Find the actual bottleneck, not the suspected one
|
|
119
|
-
2. **Benchmark** — Measure current performance as a baseline
|
|
120
|
-
3. **Hypothesize** — Predict the impact of the optimization
|
|
121
|
-
4. **Implement** — Make the change
|
|
122
|
-
5. **Verify** — Did it actually help? If not, revert.
|
|
123
|
-
|
|
124
|
-
### Tools
|
|
125
|
-
- **Node.js** — `node --prof`, clinic.js, 0x
|
|
126
|
-
- **Python** — cProfile, py-spot, line_profiler
|
|
127
|
-
- **Browser** — Chrome DevTools Performance tab, Lighthouse
|
|
128
|
-
- **Database** — `EXPLAIN ANALYZE`, `pg_stat_statements`, slow query log
|
|
129
|
-
- **System** — `htop`, `iotop`, `netstat`, Prometheus + Grafana
|
|
130
|
-
|
|
131
|
-
---
|
|
132
|
-
|
|
133
|
-
## Performance Anti-Patterns
|
|
134
|
-
|
|
135
|
-
| Anti-Pattern | Why It Hurts |
|
|
136
|
-
|---|---|
|
|
137
|
-
| **Optimizing without measuring** | You optimize the wrong thing. The real bottleneck remains. |
|
|
138
|
-
| **Caching everything** | Cache invalidation nightmares, stale data, memory bloat |
|
|
139
|
-
| **Ignoring Big O** | O(n²) with n=1000 is 1M operations. It will always be slow. |
|
|
140
|
-
| **Blocking the main thread** | In async systems, synchronous I/O blocks everything |
|
|
141
|
-
| **Loading everything at once** | Memory exhaustion, slow startup, poor UX |
|
|
142
|
-
| **No connection pooling** | Connection overhead dominates; database chokes |
|
|
143
|
-
| **Giant transactions** | Lock contention, timeouts, rollback nightmares |
|
|
144
|
-
|
|
145
|
-
---
|
|
146
|
-
|
|
147
|
-
## When Performance Matters
|
|
148
|
-
|
|
149
|
-
**Always:**
|
|
150
|
-
- User-facing response times
|
|
151
|
-
- APIs that serve mobile clients
|
|
152
|
-
- Batch processing that runs on a schedule
|
|
153
|
-
|
|
154
|
-
**Sometimes:**
|
|
155
|
-
- Internal tools (if used by many people)
|
|
156
|
-
- Build times (developer productivity)
|
|
157
|
-
- Test suite speed (feedback loop)
|
|
158
|
-
|
|
159
|
-
**Rarely:**
|
|
160
|
-
- One-off scripts
|
|
161
|
-
- Prototypes and MVPs (but document the debt)
|
|
162
|
-
- Code that's not on the critical path
|
|
163
|
-
|
|
164
|
-
**Measure. Then optimize. Then measure again.**
|
|
1
|
+
# Performance Awareness
|
|
2
|
+
|
|
3
|
+
## Mindset: Measure First, Optimize Second
|
|
4
|
+
|
|
5
|
+
Premature optimization is the root of all evil — but so is shipping code that falls over at scale. The key is knowing when to care and what to measure.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## The Performance Hierarchy
|
|
10
|
+
|
|
11
|
+
Optimize in this order:
|
|
12
|
+
|
|
13
|
+
1. **Algorithmic complexity** — O(n²) will always beat O(n) eventually, regardless of micro-optimizations
|
|
14
|
+
2. **Database queries** — N+1 queries, missing indexes, and full table scans kill performance
|
|
15
|
+
3. **I/O bottlenecks** — Network calls, disk reads, file system operations
|
|
16
|
+
4. **Memory allocation** — Excessive object creation, memory leaks, large data structures
|
|
17
|
+
5. **Micro-optimizations** — Loop unrolling, cache locality (last resort, measure impact)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Key Metrics
|
|
22
|
+
|
|
23
|
+
### Response Time
|
|
24
|
+
| Target | Acceptable | Warning | Critical |
|
|
25
|
+
|---|---|---|---|
|
|
26
|
+
| API response | < 100ms | < 500ms | > 1s |
|
|
27
|
+
| Page load | < 1s | < 3s | > 5s |
|
|
28
|
+
| Database query | < 10ms | < 100ms | > 500ms |
|
|
29
|
+
|
|
30
|
+
### Throughput
|
|
31
|
+
- Requests per second (RPS) your system can handle
|
|
32
|
+
- Concurrent connections supported
|
|
33
|
+
- Queue depth and wait times
|
|
34
|
+
|
|
35
|
+
### Resource Utilization
|
|
36
|
+
- CPU: sustained > 70% is a warning
|
|
37
|
+
- Memory: monitor for leaks and growth over time
|
|
38
|
+
- Disk I/O: random reads are expensive; sequential is cheap
|
|
39
|
+
- Network: bandwidth and latency to dependent services
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Caching Strategy
|
|
44
|
+
|
|
45
|
+
### When to Cache
|
|
46
|
+
- **Static assets** — Cache aggressively (CDN, browser cache)
|
|
47
|
+
- **Database queries** — Cache results that change infrequently
|
|
48
|
+
- **Computed values** — Cache expensive calculations
|
|
49
|
+
- **API responses** — Cache external API calls with appropriate TTL
|
|
50
|
+
|
|
51
|
+
### When NOT to Cache
|
|
52
|
+
- **User-specific data** — unless scoped to that user
|
|
53
|
+
- **Real-time data** — stock prices, live feeds
|
|
54
|
+
- **Small, cheap computations** — cache overhead > computation cost
|
|
55
|
+
- **Data that must be consistent** — cache invalidation is hard
|
|
56
|
+
|
|
57
|
+
### Cache Invalidation
|
|
58
|
+
```
|
|
59
|
+
"There are only two hard things in Computer Science:
|
|
60
|
+
cache invalidation and naming things."
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
- Prefer **TTL-based** expiration for simplicity
|
|
64
|
+
- Use **write-through** or **write-behind** for consistency-critical data
|
|
65
|
+
- **Never** cache without an invalidation strategy
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Database Performance
|
|
70
|
+
|
|
71
|
+
### Query Optimization
|
|
72
|
+
- **Indexing** — Index columns used in WHERE, JOIN, ORDER BY. Don't over-index (writes slow down).
|
|
73
|
+
- **N+1 Problem** — Fetch related data in a single query (JOIN) or batch queries
|
|
74
|
+
- **Pagination** — Never `SELECT *` without LIMIT on large tables
|
|
75
|
+
- **Explain plans** — Use `EXPLAIN` (PostgreSQL) or `EXPLAIN ANALYZE` to understand query execution
|
|
76
|
+
|
|
77
|
+
### Connection Management
|
|
78
|
+
- Use connection pooling (PgBouncer, SQLAlchemy pool, Prisma connection pool)
|
|
79
|
+
- Close connections promptly; don't leak them
|
|
80
|
+
- Monitor pool exhaustion
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Async & Concurrency
|
|
85
|
+
|
|
86
|
+
### When to Use Async
|
|
87
|
+
- **I/O-bound work** — API calls, database queries, file reads
|
|
88
|
+
- **Many concurrent operations** — handling thousands of connections
|
|
89
|
+
- **Not for CPU-bound work** — use threads/processes instead
|
|
90
|
+
|
|
91
|
+
### Concurrency Patterns
|
|
92
|
+
- **Batching** — Group small operations into larger ones
|
|
93
|
+
- **Parallelization** — Run independent operations concurrently
|
|
94
|
+
- **Streaming** — Process data as it arrives, not all at once
|
|
95
|
+
- **Backpressure** — Slow down producers when consumers can't keep up
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Frontend Performance
|
|
100
|
+
|
|
101
|
+
### Core Web Vitals
|
|
102
|
+
- **LCP (Largest Contentful Paint)** — < 2.5s (main content loaded)
|
|
103
|
+
- **FID (First Input Delay)** — < 100ms (page is interactive)
|
|
104
|
+
- **CLS (Cumulative Layout Shift)** — < 0.1 (visual stability)
|
|
105
|
+
|
|
106
|
+
### Techniques
|
|
107
|
+
- **Code splitting** — Load only what the user needs
|
|
108
|
+
- **Lazy loading** — Images, components, routes load on demand
|
|
109
|
+
- **Tree shaking** — Remove unused code at build time
|
|
110
|
+
- **Image optimization** — WebP, responsive sizes, lazy loading
|
|
111
|
+
- **Minimize main thread work** — Offload to web workers where possible
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Profiling & Measurement
|
|
116
|
+
|
|
117
|
+
### Before Optimizing
|
|
118
|
+
1. **Profile** — Find the actual bottleneck, not the suspected one
|
|
119
|
+
2. **Benchmark** — Measure current performance as a baseline
|
|
120
|
+
3. **Hypothesize** — Predict the impact of the optimization
|
|
121
|
+
4. **Implement** — Make the change
|
|
122
|
+
5. **Verify** — Did it actually help? If not, revert.
|
|
123
|
+
|
|
124
|
+
### Tools
|
|
125
|
+
- **Node.js** — `node --prof`, clinic.js, 0x
|
|
126
|
+
- **Python** — cProfile, py-spot, line_profiler
|
|
127
|
+
- **Browser** — Chrome DevTools Performance tab, Lighthouse
|
|
128
|
+
- **Database** — `EXPLAIN ANALYZE`, `pg_stat_statements`, slow query log
|
|
129
|
+
- **System** — `htop`, `iotop`, `netstat`, Prometheus + Grafana
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Performance Anti-Patterns
|
|
134
|
+
|
|
135
|
+
| Anti-Pattern | Why It Hurts |
|
|
136
|
+
|---|---|
|
|
137
|
+
| **Optimizing without measuring** | You optimize the wrong thing. The real bottleneck remains. |
|
|
138
|
+
| **Caching everything** | Cache invalidation nightmares, stale data, memory bloat |
|
|
139
|
+
| **Ignoring Big O** | O(n²) with n=1000 is 1M operations. It will always be slow. |
|
|
140
|
+
| **Blocking the main thread** | In async systems, synchronous I/O blocks everything |
|
|
141
|
+
| **Loading everything at once** | Memory exhaustion, slow startup, poor UX |
|
|
142
|
+
| **No connection pooling** | Connection overhead dominates; database chokes |
|
|
143
|
+
| **Giant transactions** | Lock contention, timeouts, rollback nightmares |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## When Performance Matters
|
|
148
|
+
|
|
149
|
+
**Always:**
|
|
150
|
+
- User-facing response times
|
|
151
|
+
- APIs that serve mobile clients
|
|
152
|
+
- Batch processing that runs on a schedule
|
|
153
|
+
|
|
154
|
+
**Sometimes:**
|
|
155
|
+
- Internal tools (if used by many people)
|
|
156
|
+
- Build times (developer productivity)
|
|
157
|
+
- Test suite speed (feedback loop)
|
|
158
|
+
|
|
159
|
+
**Rarely:**
|
|
160
|
+
- One-off scripts
|
|
161
|
+
- Prototypes and MVPs (but document the debt)
|
|
162
|
+
- Code that's not on the critical path
|
|
163
|
+
|
|
164
|
+
**Measure. Then optimize. Then measure again.**
|
package/.agents/SECURITY.md
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
# Security Principles
|
|
2
|
-
|
|
3
|
-
## Mindset: Security Is Not a Feature
|
|
4
|
-
|
|
5
|
-
Security is a property of every decision, not a checkbox at the end. Treat every input as malicious until proven otherwise. Treat every dependency as compromised until audited. Treat every secret as leaked until rotated.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## The Security Checklist
|
|
10
|
-
|
|
11
|
-
Before shipping any code, verify these:
|
|
12
|
-
|
|
13
|
-
### Input Validation
|
|
14
|
-
- [ ] All user inputs are validated at trust boundaries (API, CLI, form, file upload)
|
|
15
|
-
- [ ] Validation happens before any processing — not as an afterthought
|
|
16
|
-
- [ ] Whitelist over blacklist: define what's allowed, not what's forbidden
|
|
17
|
-
- [ ] Type, length, format, and range are all constrained
|
|
18
|
-
- [ ] File uploads: check MIME type, extension, size, and scan for malicious content
|
|
19
|
-
- [ ] Deserialization: never blindly deserialize untrusted data (pickle, YAML, XML, JSON with custom decoders)
|
|
20
|
-
|
|
21
|
-
### Secrets Management
|
|
22
|
-
- [ ] No hardcoded secrets in source code (API keys, passwords, tokens)
|
|
23
|
-
- [ ] Environment variables or secret managers only
|
|
24
|
-
- [ ] `.env` files are in `.gitignore`
|
|
25
|
-
- [ ] No secrets in logs, error messages, or stack traces
|
|
26
|
-
- [ ] Rotate secrets when team members leave or when compromise is suspected
|
|
27
|
-
- [ ] Different secrets for dev/staging/prod — never share production keys with dev environments
|
|
28
|
-
|
|
29
|
-
### Authentication & Authorization
|
|
30
|
-
- [ ] Authentication on every endpoint that isn't explicitly public
|
|
31
|
-
- [ ] Principle of least privilege: users get the minimum access they need
|
|
32
|
-
- [ ] Never trust client-side authorization checks — verify on the server
|
|
33
|
-
- [ ] Session tokens: secure, httpOnly, sameSite cookies; short expiry with refresh tokens
|
|
34
|
-
- [ ] Rate limiting on auth endpoints (login, register, password reset)
|
|
35
|
-
|
|
36
|
-
### Data Protection
|
|
37
|
-
- [ ] Sensitive data encrypted at rest (database, files, backups)
|
|
38
|
-
- [ ] Sensitive data encrypted in transit (TLS 1.2+, no downgrade)
|
|
39
|
-
- [ ] PII minimized: collect only what's necessary, delete when no longer needed
|
|
40
|
-
- [ ] Database queries parameterized — never string-concatenate SQL
|
|
41
|
-
- [ ] No sensitive data in URL parameters or client-side storage
|
|
42
|
-
|
|
43
|
-
### Dependency Security
|
|
44
|
-
- [ ] Audit dependencies before adding them: maintenance status, known vulnerabilities, supply chain risk
|
|
45
|
-
- [ ] Pin versions in lock files; use `npm audit`, `pip-audit`, `cargo audit`
|
|
46
|
-
- [ ] Review what a dependency does before trusting it with secrets or user data
|
|
47
|
-
- [ ] Prefer well-maintained, widely-used libraries over obscure ones
|
|
48
|
-
|
|
49
|
-
---
|
|
50
|
-
|
|
51
|
-
## Common Attack Vectors
|
|
52
|
-
|
|
53
|
-
| Attack | Prevention |
|
|
54
|
-
|---|---|
|
|
55
|
-
| **Injection** (SQL, NoSQL, Command, LDAP) | Parameterized queries, input sanitization, avoid `eval`/`exec` |
|
|
56
|
-
| **XSS** (Cross-Site Scripting) | Escape output, Content-Security-Policy, sanitize HTML |
|
|
57
|
-
| **CSRF** (Cross-Site Request Forgery) | CSRF tokens, SameSite cookies, verify Origin header |
|
|
58
|
-
| **Path Traversal** | Canonicalize paths, whitelist allowed directories, no user-controlled paths |
|
|
59
|
-
| **SSRF** (Server-Side Request Forgery) | Whitelist outbound URLs, no user-controlled destinations |
|
|
60
|
-
| **Deserialization** | Avoid deserializing untrusted data; use safe formats (JSON with schema validation) |
|
|
61
|
-
| **IDOR** (Insecure Direct Object Reference) | Verify ownership on every resource access; use UUIDs not sequential IDs |
|
|
62
|
-
| **Race Conditions** | Atomic operations, proper locking, idempotent endpoints |
|
|
63
|
-
|
|
64
|
-
---
|
|
65
|
-
|
|
66
|
-
## Security-First Development Patterns
|
|
67
|
-
|
|
68
|
-
### Defensive Coding
|
|
69
|
-
```python
|
|
70
|
-
# BAD: Trusting user input
|
|
71
|
-
query = f"SELECT * FROM users WHERE id = {user_id}"
|
|
72
|
-
|
|
73
|
-
# GOOD: Parameterized query
|
|
74
|
-
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Fail Securely
|
|
78
|
-
```python
|
|
79
|
-
# BAD: Default allow
|
|
80
|
-
if user.is_admin:
|
|
81
|
-
allow_access()
|
|
82
|
-
else:
|
|
83
|
-
deny_access()
|
|
84
|
-
|
|
85
|
-
# GOOD: Default deny
|
|
86
|
-
if not user.is_authenticated or not user.is_admin:
|
|
87
|
-
raise PermissionDenied()
|
|
88
|
-
allow_access()
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Secure Defaults
|
|
92
|
-
- Framework security features enabled by default (CSRF protection, XSS filters)
|
|
93
|
-
- No debug mode in production
|
|
94
|
-
- No open CORS (`*`) in production
|
|
95
|
-
- No stack traces exposed to end users
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## When to Escalate
|
|
100
|
-
|
|
101
|
-
Stop and ask the human when:
|
|
102
|
-
- Handling production secrets or credentials
|
|
103
|
-
- Modifying authentication/authorization logic
|
|
104
|
-
- Adding new external dependencies with network access
|
|
105
|
-
- Processing sensitive user data (PII, health, financial)
|
|
106
|
-
- Deploying to production environments
|
|
107
|
-
- Anything involving encryption key management
|
|
108
|
-
|
|
109
|
-
**Security is not "I'll fix it later." Security is now.**
|
|
1
|
+
# Security Principles
|
|
2
|
+
|
|
3
|
+
## Mindset: Security Is Not a Feature
|
|
4
|
+
|
|
5
|
+
Security is a property of every decision, not a checkbox at the end. Treat every input as malicious until proven otherwise. Treat every dependency as compromised until audited. Treat every secret as leaked until rotated.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## The Security Checklist
|
|
10
|
+
|
|
11
|
+
Before shipping any code, verify these:
|
|
12
|
+
|
|
13
|
+
### Input Validation
|
|
14
|
+
- [ ] All user inputs are validated at trust boundaries (API, CLI, form, file upload)
|
|
15
|
+
- [ ] Validation happens before any processing — not as an afterthought
|
|
16
|
+
- [ ] Whitelist over blacklist: define what's allowed, not what's forbidden
|
|
17
|
+
- [ ] Type, length, format, and range are all constrained
|
|
18
|
+
- [ ] File uploads: check MIME type, extension, size, and scan for malicious content
|
|
19
|
+
- [ ] Deserialization: never blindly deserialize untrusted data (pickle, YAML, XML, JSON with custom decoders)
|
|
20
|
+
|
|
21
|
+
### Secrets Management
|
|
22
|
+
- [ ] No hardcoded secrets in source code (API keys, passwords, tokens)
|
|
23
|
+
- [ ] Environment variables or secret managers only
|
|
24
|
+
- [ ] `.env` files are in `.gitignore`
|
|
25
|
+
- [ ] No secrets in logs, error messages, or stack traces
|
|
26
|
+
- [ ] Rotate secrets when team members leave or when compromise is suspected
|
|
27
|
+
- [ ] Different secrets for dev/staging/prod — never share production keys with dev environments
|
|
28
|
+
|
|
29
|
+
### Authentication & Authorization
|
|
30
|
+
- [ ] Authentication on every endpoint that isn't explicitly public
|
|
31
|
+
- [ ] Principle of least privilege: users get the minimum access they need
|
|
32
|
+
- [ ] Never trust client-side authorization checks — verify on the server
|
|
33
|
+
- [ ] Session tokens: secure, httpOnly, sameSite cookies; short expiry with refresh tokens
|
|
34
|
+
- [ ] Rate limiting on auth endpoints (login, register, password reset)
|
|
35
|
+
|
|
36
|
+
### Data Protection
|
|
37
|
+
- [ ] Sensitive data encrypted at rest (database, files, backups)
|
|
38
|
+
- [ ] Sensitive data encrypted in transit (TLS 1.2+, no downgrade)
|
|
39
|
+
- [ ] PII minimized: collect only what's necessary, delete when no longer needed
|
|
40
|
+
- [ ] Database queries parameterized — never string-concatenate SQL
|
|
41
|
+
- [ ] No sensitive data in URL parameters or client-side storage
|
|
42
|
+
|
|
43
|
+
### Dependency Security
|
|
44
|
+
- [ ] Audit dependencies before adding them: maintenance status, known vulnerabilities, supply chain risk
|
|
45
|
+
- [ ] Pin versions in lock files; use `npm audit`, `pip-audit`, `cargo audit`
|
|
46
|
+
- [ ] Review what a dependency does before trusting it with secrets or user data
|
|
47
|
+
- [ ] Prefer well-maintained, widely-used libraries over obscure ones
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Common Attack Vectors
|
|
52
|
+
|
|
53
|
+
| Attack | Prevention |
|
|
54
|
+
|---|---|
|
|
55
|
+
| **Injection** (SQL, NoSQL, Command, LDAP) | Parameterized queries, input sanitization, avoid `eval`/`exec` |
|
|
56
|
+
| **XSS** (Cross-Site Scripting) | Escape output, Content-Security-Policy, sanitize HTML |
|
|
57
|
+
| **CSRF** (Cross-Site Request Forgery) | CSRF tokens, SameSite cookies, verify Origin header |
|
|
58
|
+
| **Path Traversal** | Canonicalize paths, whitelist allowed directories, no user-controlled paths |
|
|
59
|
+
| **SSRF** (Server-Side Request Forgery) | Whitelist outbound URLs, no user-controlled destinations |
|
|
60
|
+
| **Deserialization** | Avoid deserializing untrusted data; use safe formats (JSON with schema validation) |
|
|
61
|
+
| **IDOR** (Insecure Direct Object Reference) | Verify ownership on every resource access; use UUIDs not sequential IDs |
|
|
62
|
+
| **Race Conditions** | Atomic operations, proper locking, idempotent endpoints |
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Security-First Development Patterns
|
|
67
|
+
|
|
68
|
+
### Defensive Coding
|
|
69
|
+
```python
|
|
70
|
+
# BAD: Trusting user input
|
|
71
|
+
query = f"SELECT * FROM users WHERE id = {user_id}"
|
|
72
|
+
|
|
73
|
+
# GOOD: Parameterized query
|
|
74
|
+
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Fail Securely
|
|
78
|
+
```python
|
|
79
|
+
# BAD: Default allow
|
|
80
|
+
if user.is_admin:
|
|
81
|
+
allow_access()
|
|
82
|
+
else:
|
|
83
|
+
deny_access()
|
|
84
|
+
|
|
85
|
+
# GOOD: Default deny
|
|
86
|
+
if not user.is_authenticated or not user.is_admin:
|
|
87
|
+
raise PermissionDenied()
|
|
88
|
+
allow_access()
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Secure Defaults
|
|
92
|
+
- Framework security features enabled by default (CSRF protection, XSS filters)
|
|
93
|
+
- No debug mode in production
|
|
94
|
+
- No open CORS (`*`) in production
|
|
95
|
+
- No stack traces exposed to end users
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## When to Escalate
|
|
100
|
+
|
|
101
|
+
Stop and ask the human when:
|
|
102
|
+
- Handling production secrets or credentials
|
|
103
|
+
- Modifying authentication/authorization logic
|
|
104
|
+
- Adding new external dependencies with network access
|
|
105
|
+
- Processing sensitive user data (PII, health, financial)
|
|
106
|
+
- Deploying to production environments
|
|
107
|
+
- Anything involving encryption key management
|
|
108
|
+
|
|
109
|
+
**Security is not "I'll fix it later." Security is now.**
|