ship-safe 2.0.0 → 3.1.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 +456 -357
- package/cli/bin/ship-safe.js +163 -104
- package/cli/commands/fix.js +216 -0
- package/cli/commands/guard.js +297 -0
- package/cli/commands/mcp.js +303 -0
- package/cli/commands/remediate.js +646 -0
- package/cli/commands/rotate.js +571 -0
- package/cli/commands/scan.js +231 -39
- package/cli/utils/entropy.js +126 -0
- package/cli/utils/output.js +10 -1
- package/cli/utils/patterns.js +32 -1
- package/configs/ship-safeignore-template +50 -0
- package/package.json +64 -63
package/README.md
CHANGED
|
@@ -1,357 +1,456 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
**
|
|
123
|
-
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
###
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
[
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
[
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src=".github/assets/logo%20ship%20safe.png" alt="Ship Safe Logo" width="180" />
|
|
3
|
+
</p>
|
|
4
|
+
<p align="center"><strong>Don't let vibe coding leak your API keys.</strong></p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://www.npmjs.com/package/ship-safe"><img src="https://badge.fury.io/js/ship-safe.svg" alt="npm version" /></a>
|
|
8
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT" /></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
You're shipping fast. You're using AI to write code. You're one `git push` away from exposing your database credentials to the world.
|
|
14
|
+
|
|
15
|
+
**Ship Safe** is a security toolkit for indie hackers and vibe coders who want to secure their MVP in 5 minutes, not 5 days.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Scan for leaked secrets (no install required!)
|
|
23
|
+
npx ship-safe scan .
|
|
24
|
+
|
|
25
|
+
# Auto-generate .env.example from found secrets
|
|
26
|
+
npx ship-safe fix
|
|
27
|
+
|
|
28
|
+
# Block git push if secrets are found
|
|
29
|
+
npx ship-safe guard
|
|
30
|
+
|
|
31
|
+
# Run the launch-day security checklist
|
|
32
|
+
npx ship-safe checklist
|
|
33
|
+
|
|
34
|
+
# Add security configs to your project
|
|
35
|
+
npx ship-safe init
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
That's it. Five commands to secure your MVP.
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
### Let AI Do It For You
|
|
43
|
+
|
|
44
|
+
Copy this prompt to your AI coding assistant:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
Run "npx ship-safe scan ." on my project and fix any secrets you find.
|
|
48
|
+
Then run "npx ship-safe init" to add security configs.
|
|
49
|
+
Explain what you're doing as you go.
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
[More AI prompts for specific frameworks](./AI_SECURITY_PROMPT.md)
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Why This Exists
|
|
57
|
+
|
|
58
|
+
Vibe coding is powerful. You can build a SaaS in a weekend. But speed creates blind spots:
|
|
59
|
+
|
|
60
|
+
- AI-generated code often hardcodes secrets
|
|
61
|
+
- Default configs ship with debug mode enabled
|
|
62
|
+
- "I'll fix it later" becomes "I got hacked"
|
|
63
|
+
|
|
64
|
+
This repo is your co-pilot for security. Copy, paste, ship safely.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## CLI Commands
|
|
69
|
+
|
|
70
|
+
### `npx ship-safe scan [path]`
|
|
71
|
+
|
|
72
|
+
Scans your codebase for leaked secrets: API keys, passwords, private keys, database URLs.
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
# Scan current directory
|
|
76
|
+
npx ship-safe scan .
|
|
77
|
+
|
|
78
|
+
# Scan a specific folder
|
|
79
|
+
npx ship-safe scan ./src
|
|
80
|
+
|
|
81
|
+
# Get JSON output (for CI pipelines)
|
|
82
|
+
npx ship-safe scan . --json
|
|
83
|
+
|
|
84
|
+
# Verbose mode (show files being scanned)
|
|
85
|
+
npx ship-safe scan . -v
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Exit codes:** Returns `1` if secrets found (useful for CI), `0` if clean.
|
|
89
|
+
|
|
90
|
+
**Flags:**
|
|
91
|
+
- `--json` — structured JSON output for CI pipelines
|
|
92
|
+
- `--sarif` — SARIF format for GitHub Code Scanning
|
|
93
|
+
- `--include-tests` — also scan test/spec/fixture files (excluded by default)
|
|
94
|
+
- `-v` — verbose mode
|
|
95
|
+
|
|
96
|
+
**Suppress false positives:**
|
|
97
|
+
```bash
|
|
98
|
+
const apiKey = 'example-key'; // ship-safe-ignore
|
|
99
|
+
```
|
|
100
|
+
Or exclude paths with `.ship-safeignore` (gitignore syntax).
|
|
101
|
+
|
|
102
|
+
**Custom patterns** — create `.ship-safe.json` in your project root:
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"patterns": [
|
|
106
|
+
{
|
|
107
|
+
"name": "My Internal API Key",
|
|
108
|
+
"pattern": "MYAPP_[A-Z0-9]{32}",
|
|
109
|
+
"severity": "high",
|
|
110
|
+
"description": "Internal key for myapp services."
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Detects 50+ secret patterns:**
|
|
117
|
+
- **AI/ML:** OpenAI, Anthropic, Google AI, Cohere, Replicate, Hugging Face
|
|
118
|
+
- **Auth:** Clerk, Auth0, Supabase Auth
|
|
119
|
+
- **Cloud:** AWS, Google Cloud, Azure
|
|
120
|
+
- **Database:** Supabase, PlanetScale, Neon, MongoDB, PostgreSQL, MySQL
|
|
121
|
+
- **Payment:** Stripe, PayPal
|
|
122
|
+
- **Messaging:** Twilio, SendGrid, Resend
|
|
123
|
+
- **And more:** GitHub tokens, private keys, JWTs, generic secrets
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
### `npx ship-safe checklist`
|
|
128
|
+
|
|
129
|
+
Interactive 10-point security checklist for launch day.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# Interactive mode (prompts for each item)
|
|
133
|
+
npx ship-safe checklist
|
|
134
|
+
|
|
135
|
+
# Print checklist without prompts
|
|
136
|
+
npx ship-safe checklist --no-interactive
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Covers: exposed .git folders, debug mode, RLS policies, hardcoded keys, HTTPS, security headers, rate limiting, and more.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
### `npx ship-safe init`
|
|
144
|
+
|
|
145
|
+
Initialize security configs in your project.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
# Add all security configs
|
|
149
|
+
npx ship-safe init
|
|
150
|
+
|
|
151
|
+
# Only add .gitignore patterns
|
|
152
|
+
npx ship-safe init --gitignore
|
|
153
|
+
|
|
154
|
+
# Only add security headers config
|
|
155
|
+
npx ship-safe init --headers
|
|
156
|
+
|
|
157
|
+
# Force overwrite existing files
|
|
158
|
+
npx ship-safe init -f
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**What it copies:**
|
|
162
|
+
- `.gitignore` - Patterns to prevent committing secrets
|
|
163
|
+
- `security-headers.config.js` - Drop-in Next.js security headers
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### `npx ship-safe fix`
|
|
168
|
+
|
|
169
|
+
Scan for secrets and auto-generate a `.env.example` file.
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
# Scan and generate .env.example
|
|
173
|
+
npx ship-safe fix
|
|
174
|
+
|
|
175
|
+
# Preview what would be generated without writing it
|
|
176
|
+
npx ship-safe fix --dry-run
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
### `npx ship-safe guard`
|
|
182
|
+
|
|
183
|
+
Install a git hook that blocks pushes if secrets are found. Works with or without Husky.
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# Install pre-push hook (runs scan before every git push)
|
|
187
|
+
npx ship-safe guard
|
|
188
|
+
|
|
189
|
+
# Install pre-commit hook instead
|
|
190
|
+
npx ship-safe guard --pre-commit
|
|
191
|
+
|
|
192
|
+
# Remove installed hooks
|
|
193
|
+
npx ship-safe guard remove
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Suppress false positives:**
|
|
197
|
+
- Add `# ship-safe-ignore` as a comment on a line to skip it
|
|
198
|
+
- Create `.ship-safeignore` (gitignore syntax) to exclude paths
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### `npx ship-safe mcp`
|
|
203
|
+
|
|
204
|
+
Start ship-safe as an MCP server so AI editors can call it directly.
|
|
205
|
+
|
|
206
|
+
**Setup (Claude Desktop)** — add to `claude_desktop_config.json`:
|
|
207
|
+
```json
|
|
208
|
+
{
|
|
209
|
+
"mcpServers": {
|
|
210
|
+
"ship-safe": {
|
|
211
|
+
"command": "npx",
|
|
212
|
+
"args": ["ship-safe", "mcp"]
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Works with Claude Desktop, Cursor, Windsurf, Zed, and any MCP-compatible editor.
|
|
219
|
+
|
|
220
|
+
**Available tools:**
|
|
221
|
+
- `scan_secrets` — scan a directory for leaked secrets
|
|
222
|
+
- `get_checklist` — return the security checklist as structured data
|
|
223
|
+
- `analyze_file` — analyze a single file for issues
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## What's Inside
|
|
228
|
+
|
|
229
|
+
### [`/checklists`](./checklists)
|
|
230
|
+
**Manual security audits you can do in 5 minutes.**
|
|
231
|
+
- [Launch Day Checklist](./checklists/launch-day.md) - 10 things to check before you go live
|
|
232
|
+
|
|
233
|
+
### [`/configs`](./configs)
|
|
234
|
+
**Secure defaults for popular stacks. Drop-in ready.**
|
|
235
|
+
|
|
236
|
+
| Stack | Files |
|
|
237
|
+
|-------|-------|
|
|
238
|
+
| **Next.js** | [Security Headers](./configs/nextjs-security-headers.js) - CSP, X-Frame-Options, HSTS |
|
|
239
|
+
| **Supabase** | [RLS Templates](./configs/supabase/rls-templates.sql) \| [Security Checklist](./configs/supabase/security-checklist.md) \| [Secure Client](./configs/supabase/secure-client.ts) |
|
|
240
|
+
| **Firebase** | [Firestore Rules](./configs/firebase/firestore-rules.txt) \| [Storage Rules](./configs/firebase/storage-rules.txt) \| [Security Checklist](./configs/firebase/security-checklist.md) |
|
|
241
|
+
|
|
242
|
+
### [`/snippets`](./snippets)
|
|
243
|
+
**Copy-paste code blocks for common security patterns.**
|
|
244
|
+
|
|
245
|
+
| Category | Files |
|
|
246
|
+
|----------|-------|
|
|
247
|
+
| **Rate Limiting** | [Upstash Redis](./snippets/rate-limiting/upstash-ratelimit.ts) \| [Next.js Middleware](./snippets/rate-limiting/nextjs-middleware.ts) |
|
|
248
|
+
| **Authentication** | [JWT Security Checklist](./snippets/auth/jwt-checklist.md) |
|
|
249
|
+
| **API Security** | [CORS Config](./snippets/api-security/cors-config.ts) \| [Input Validation](./snippets/api-security/input-validation.ts) \| [API Checklist](./snippets/api-security/api-security-checklist.md) |
|
|
250
|
+
|
|
251
|
+
### [`/ai-defense`](./ai-defense)
|
|
252
|
+
**Protect your AI features from abuse and cost explosions.**
|
|
253
|
+
|
|
254
|
+
| File | Description |
|
|
255
|
+
|------|-------------|
|
|
256
|
+
| [LLM Security Checklist](./ai-defense/llm-security-checklist.md) | Based on OWASP LLM Top 10 - prompt injection, data protection, scope control |
|
|
257
|
+
| [Prompt Injection Patterns](./ai-defense/prompt-injection-patterns.js) | Regex patterns to detect 25+ injection attempts |
|
|
258
|
+
| [Cost Protection Guide](./ai-defense/cost-protection.md) | Prevent $50k surprise bills - rate limits, budget caps, circuit breakers |
|
|
259
|
+
| [System Prompt Armor](./ai-defense/system-prompt-armor.md) | Template for hardened system prompts |
|
|
260
|
+
|
|
261
|
+
### [`/scripts`](./scripts)
|
|
262
|
+
**Automated scanning tools. Run them in CI or locally.**
|
|
263
|
+
- [Secret Scanner](./scripts/scan_secrets.py) - Python version of the secret scanner
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## AI/LLM Security
|
|
268
|
+
|
|
269
|
+
Building with AI? Don't let it bankrupt you or get hijacked.
|
|
270
|
+
|
|
271
|
+
### Quick Setup
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import { containsInjectionAttempt } from './ai-defense/prompt-injection-patterns';
|
|
275
|
+
|
|
276
|
+
async function handleChat(userInput: string) {
|
|
277
|
+
// 1. Check for injection attempts
|
|
278
|
+
const { detected } = containsInjectionAttempt(userInput);
|
|
279
|
+
if (detected) {
|
|
280
|
+
return "I can't process that request.";
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// 2. Rate limit per user
|
|
284
|
+
const { success } = await ratelimit.limit(userId);
|
|
285
|
+
if (!success) {
|
|
286
|
+
return "Too many requests. Please slow down.";
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// 3. Check budget before calling
|
|
290
|
+
await checkUserBudget(userId, estimatedCost);
|
|
291
|
+
|
|
292
|
+
// 4. Make the API call with token limits
|
|
293
|
+
const response = await openai.chat.completions.create({
|
|
294
|
+
model: 'gpt-4',
|
|
295
|
+
messages,
|
|
296
|
+
max_tokens: 500, // Hard cap
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
return response;
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Cost Protection Layers
|
|
304
|
+
|
|
305
|
+
1. **Token limits** - Cap input/output per request
|
|
306
|
+
2. **Rate limits** - Cap requests per user (10/min)
|
|
307
|
+
3. **Budget caps** - Daily ($1) and monthly ($10) per user
|
|
308
|
+
4. **Circuit breaker** - Disable AI when global budget hit
|
|
309
|
+
5. **Provider limits** - Set hard limits in OpenAI/Anthropic dashboard
|
|
310
|
+
|
|
311
|
+
[Full cost protection guide →](./ai-defense/cost-protection.md)
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## Database Security
|
|
316
|
+
|
|
317
|
+
### Supabase RLS Templates
|
|
318
|
+
|
|
319
|
+
```sql
|
|
320
|
+
-- Users can only see their own data
|
|
321
|
+
CREATE POLICY "Users own their data" ON items
|
|
322
|
+
FOR ALL USING (auth.uid() = user_id);
|
|
323
|
+
|
|
324
|
+
-- Read-only public data
|
|
325
|
+
CREATE POLICY "Public read access" ON public_items
|
|
326
|
+
FOR SELECT USING (true);
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
[6 more RLS patterns →](./configs/supabase/rls-templates.sql)
|
|
330
|
+
|
|
331
|
+
### Firebase Security Rules
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
// Users can only access their own documents
|
|
335
|
+
match /users/{userId} {
|
|
336
|
+
allow read, write: if request.auth != null
|
|
337
|
+
&& request.auth.uid == userId;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
[Full Firestore rules template →](./configs/firebase/firestore-rules.txt)
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## API Security
|
|
346
|
+
|
|
347
|
+
### CORS (Don't use `*` in production)
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
const ALLOWED_ORIGINS = [
|
|
351
|
+
'https://yourapp.com',
|
|
352
|
+
'https://www.yourapp.com',
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
// Only allow specific origins
|
|
356
|
+
if (origin && ALLOWED_ORIGINS.includes(origin)) {
|
|
357
|
+
headers['Access-Control-Allow-Origin'] = origin;
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
[CORS configs for Next.js, Express, Fastify, Hono →](./snippets/api-security/cors-config.ts)
|
|
362
|
+
|
|
363
|
+
### Input Validation (Zod)
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
const createUserSchema = z.object({
|
|
367
|
+
email: z.string().email().max(255),
|
|
368
|
+
password: z.string().min(8).max(128),
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const result = createUserSchema.safeParse(body);
|
|
372
|
+
if (!result.success) {
|
|
373
|
+
return Response.json({ error: result.error.issues }, { status: 400 });
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
[Full validation patterns →](./snippets/api-security/input-validation.ts)
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
381
|
+
## CI/CD Integration
|
|
382
|
+
|
|
383
|
+
Add to your GitHub Actions workflow:
|
|
384
|
+
|
|
385
|
+
```yaml
|
|
386
|
+
# .github/workflows/security.yml
|
|
387
|
+
name: Security Scan
|
|
388
|
+
|
|
389
|
+
on: [push, pull_request]
|
|
390
|
+
|
|
391
|
+
jobs:
|
|
392
|
+
scan-secrets:
|
|
393
|
+
runs-on: ubuntu-latest
|
|
394
|
+
steps:
|
|
395
|
+
- uses: actions/checkout@v4
|
|
396
|
+
- name: Scan for secrets
|
|
397
|
+
run: npx ship-safe scan . --json
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
The scan exits with code `1` if secrets are found, failing your build.
|
|
401
|
+
|
|
402
|
+
---
|
|
403
|
+
|
|
404
|
+
## The 5-Minute Security Checklist
|
|
405
|
+
|
|
406
|
+
1. ✅ Run `npx ship-safe scan .` on your project
|
|
407
|
+
2. ✅ Run `npx ship-safe init` to add security configs
|
|
408
|
+
3. ✅ Add security headers to your Next.js config
|
|
409
|
+
4. ✅ Run `npx ship-safe checklist` before launching
|
|
410
|
+
5. ✅ If using AI features, implement [cost protection](./ai-defense/cost-protection.md)
|
|
411
|
+
6. ✅ If using Supabase, check the [RLS checklist](./configs/supabase/security-checklist.md)
|
|
412
|
+
7. ✅ If using Firebase, check the [Firebase checklist](./configs/firebase/security-checklist.md)
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## Philosophy
|
|
417
|
+
|
|
418
|
+
- **Low friction** - If it takes more than 5 minutes, people won't do it
|
|
419
|
+
- **Educational** - Every config has comments explaining *why*
|
|
420
|
+
- **Modular** - Take what you need, ignore the rest
|
|
421
|
+
- **Copy-paste friendly** - No complex setup, just grab and go
|
|
422
|
+
|
|
423
|
+
---
|
|
424
|
+
|
|
425
|
+
## Contributing
|
|
426
|
+
|
|
427
|
+
Found a security pattern that saved your app? Share it!
|
|
428
|
+
|
|
429
|
+
1. Fork the repo
|
|
430
|
+
2. Add your checklist, config, or script
|
|
431
|
+
3. Include educational comments explaining *why* it matters
|
|
432
|
+
4. Open a PR
|
|
433
|
+
|
|
434
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
435
|
+
|
|
436
|
+
---
|
|
437
|
+
|
|
438
|
+
## Security Standards Reference
|
|
439
|
+
|
|
440
|
+
This toolkit is based on:
|
|
441
|
+
- [OWASP Top 10 Web 2025](https://owasp.org/Top10/)
|
|
442
|
+
- [OWASP Top 10 Mobile 2024](https://owasp.org/www-project-mobile-top-10/)
|
|
443
|
+
- [OWASP LLM Top 10 2025](https://genai.owasp.org/llm-top-10/)
|
|
444
|
+
- [OWASP API Security Top 10 2023](https://owasp.org/API-Security/)
|
|
445
|
+
|
|
446
|
+
---
|
|
447
|
+
|
|
448
|
+
## License
|
|
449
|
+
|
|
450
|
+
MIT - Use it, share it, secure your stuff.
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
**Remember: Security isn't about being paranoid. It's about being prepared.**
|
|
455
|
+
|
|
456
|
+
Ship fast. Ship safe.
|