vibesuite 1.3.2 → 2.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/README.md +8 -1
- package/assets/.agent/skills/avoid-feature-creep/SKILL.md +307 -0
- package/assets/.agent/skills/avoid-feature-creep/agents/openai.yaml +3 -0
- package/assets/.agent/skills/avoid-feature-creep/assets/large-logo.png +0 -0
- package/assets/.agent/skills/avoid-feature-creep/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex/SKILL.md +62 -0
- package/assets/.agent/skills/convex/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-agents/SKILL.md +516 -0
- package/assets/.agent/skills/convex-agents/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-agents/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-agents/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-best-practices/SKILL.md +369 -0
- package/assets/.agent/skills/convex-best-practices/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-best-practices/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-best-practices/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-component-authoring/SKILL.md +457 -0
- package/assets/.agent/skills/convex-component-authoring/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-component-authoring/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-component-authoring/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-cron-jobs/SKILL.md +604 -0
- package/assets/.agent/skills/convex-cron-jobs/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-cron-jobs/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-cron-jobs/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-file-storage/SKILL.md +467 -0
- package/assets/.agent/skills/convex-file-storage/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-file-storage/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-file-storage/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-functions/SKILL.md +458 -0
- package/assets/.agent/skills/convex-functions/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-functions/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-functions/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-http-actions/SKILL.md +733 -0
- package/assets/.agent/skills/convex-http-actions/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-http-actions/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-http-actions/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-migrations/SKILL.md +712 -0
- package/assets/.agent/skills/convex-migrations/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-migrations/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-migrations/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-realtime/SKILL.md +443 -0
- package/assets/.agent/skills/convex-realtime/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-realtime/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-realtime/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-schema-validator/SKILL.md +400 -0
- package/assets/.agent/skills/convex-schema-validator/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-schema-validator/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-schema-validator/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-security-audit/SKILL.md +539 -0
- package/assets/.agent/skills/convex-security-audit/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-security-audit/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-security-audit/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/convex-security-check/SKILL.md +378 -0
- package/assets/.agent/skills/convex-security-check/agents/openai.yaml +3 -0
- package/assets/.agent/skills/convex-security-check/assets/large-logo.png +0 -0
- package/assets/.agent/skills/convex-security-check/assets/small-logo.svg +17 -0
- package/assets/.agent/skills/github-ops/SKILL.md +4 -4
- package/assets/.agent/skills/google-trends/SKILL.md +7 -7
- package/assets/.agent/skills/optimize-agent-context/SKILL.md +97 -0
- package/assets/.agent/skills/youtube-pipeline/SKILL.md +10 -10
- package/assets/.agent/workflows/LEGACY/init_smart_ops.md +2 -2
- package/assets/.agent/workflows/agent_reset.md +2 -2
- package/assets/.agent/workflows/mode-orchestrator.md +114 -640
- package/assets/.agent/workflows/mode-visionary.md +192 -0
- package/assets/.agent/workflows/optimize-agent-context.md +54 -0
- package/assets/.agent/workflows/remotion-build.md +17 -17
- package/assets/.agent/workflows/stitch.md +4 -4
- package/assets/VibeCode-Agents/custom_modes.yaml +1257 -0
- package/assets/VibeCode-Agents/vibe-orchestrator.yaml +427 -145
- package/assets/VibeCode-Agents/vibe-visionary.yaml +617 -0
- package/package.json +2 -2
- package/src/cli.js +416 -20
- package/src/harness.js +281 -0
- package/src/store.js +239 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: convex-security-audit
|
|
3
|
+
displayName: Convex Security Audit
|
|
4
|
+
description: Deep security review patterns for authorization logic, data access boundaries, action isolation, rate limiting, and protecting sensitive operations
|
|
5
|
+
version: 1.0.0
|
|
6
|
+
author: Convex
|
|
7
|
+
tags: [convex, security, audit, authorization, rate-limiting, protection]
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Convex Security Audit
|
|
11
|
+
|
|
12
|
+
Comprehensive security review patterns for Convex applications including authorization logic, data access boundaries, action isolation, rate limiting, and protecting sensitive operations.
|
|
13
|
+
|
|
14
|
+
## Documentation Sources
|
|
15
|
+
|
|
16
|
+
Before implementing, do not assume; fetch the latest documentation:
|
|
17
|
+
|
|
18
|
+
- Primary: https://docs.convex.dev/auth/functions-auth
|
|
19
|
+
- Production Security: https://docs.convex.dev/production
|
|
20
|
+
- For broader context: https://docs.convex.dev/llms.txt
|
|
21
|
+
|
|
22
|
+
## Instructions
|
|
23
|
+
|
|
24
|
+
### Security Audit Areas
|
|
25
|
+
|
|
26
|
+
1. **Authorization Logic** - Who can do what
|
|
27
|
+
2. **Data Access Boundaries** - What data users can see
|
|
28
|
+
3. **Action Isolation** - Protecting external API calls
|
|
29
|
+
4. **Rate Limiting** - Preventing abuse
|
|
30
|
+
5. **Sensitive Operations** - Protecting critical functions
|
|
31
|
+
|
|
32
|
+
### Authorization Logic Audit
|
|
33
|
+
|
|
34
|
+
#### Role-Based Access Control (RBAC)
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// convex/lib/auth.ts
|
|
38
|
+
import { QueryCtx, MutationCtx } from "./_generated/server";
|
|
39
|
+
import { ConvexError } from "convex/values";
|
|
40
|
+
import { Doc } from "./_generated/dataModel";
|
|
41
|
+
|
|
42
|
+
type UserRole = "user" | "moderator" | "admin" | "superadmin";
|
|
43
|
+
|
|
44
|
+
const roleHierarchy: Record<UserRole, number> = {
|
|
45
|
+
user: 0,
|
|
46
|
+
moderator: 1,
|
|
47
|
+
admin: 2,
|
|
48
|
+
superadmin: 3,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export async function getUser(ctx: QueryCtx | MutationCtx): Promise<Doc<"users"> | null> {
|
|
52
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
53
|
+
if (!identity) return null;
|
|
54
|
+
|
|
55
|
+
return await ctx.db
|
|
56
|
+
.query("users")
|
|
57
|
+
.withIndex("by_tokenIdentifier", (q) =>
|
|
58
|
+
q.eq("tokenIdentifier", identity.tokenIdentifier)
|
|
59
|
+
)
|
|
60
|
+
.unique();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function requireRole(
|
|
64
|
+
ctx: QueryCtx | MutationCtx,
|
|
65
|
+
minRole: UserRole
|
|
66
|
+
): Promise<Doc<"users">> {
|
|
67
|
+
const user = await getUser(ctx);
|
|
68
|
+
|
|
69
|
+
if (!user) {
|
|
70
|
+
throw new ConvexError({
|
|
71
|
+
code: "UNAUTHENTICATED",
|
|
72
|
+
message: "Authentication required",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const userRoleLevel = roleHierarchy[user.role as UserRole] ?? 0;
|
|
77
|
+
const requiredLevel = roleHierarchy[minRole];
|
|
78
|
+
|
|
79
|
+
if (userRoleLevel < requiredLevel) {
|
|
80
|
+
throw new ConvexError({
|
|
81
|
+
code: "FORBIDDEN",
|
|
82
|
+
message: `Role '${minRole}' or higher required`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return user;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Permission-based check
|
|
90
|
+
type Permission = "read:users" | "write:users" | "delete:users" | "admin:system";
|
|
91
|
+
|
|
92
|
+
const rolePermissions: Record<UserRole, Permission[]> = {
|
|
93
|
+
user: ["read:users"],
|
|
94
|
+
moderator: ["read:users", "write:users"],
|
|
95
|
+
admin: ["read:users", "write:users", "delete:users"],
|
|
96
|
+
superadmin: ["read:users", "write:users", "delete:users", "admin:system"],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export async function requirePermission(
|
|
100
|
+
ctx: QueryCtx | MutationCtx,
|
|
101
|
+
permission: Permission
|
|
102
|
+
): Promise<Doc<"users">> {
|
|
103
|
+
const user = await getUser(ctx);
|
|
104
|
+
|
|
105
|
+
if (!user) {
|
|
106
|
+
throw new ConvexError({ code: "UNAUTHENTICATED", message: "Authentication required" });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const userRole = user.role as UserRole;
|
|
110
|
+
const permissions = rolePermissions[userRole] ?? [];
|
|
111
|
+
|
|
112
|
+
if (!permissions.includes(permission)) {
|
|
113
|
+
throw new ConvexError({
|
|
114
|
+
code: "FORBIDDEN",
|
|
115
|
+
message: `Permission '${permission}' required`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return user;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Data Access Boundaries Audit
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// convex/data.ts
|
|
127
|
+
import { query, mutation } from "./_generated/server";
|
|
128
|
+
import { v } from "convex/values";
|
|
129
|
+
import { getUser, requireRole } from "./lib/auth";
|
|
130
|
+
import { ConvexError } from "convex/values";
|
|
131
|
+
|
|
132
|
+
// Audit: Users can only see their own data
|
|
133
|
+
export const getMyData = query({
|
|
134
|
+
args: {},
|
|
135
|
+
returns: v.array(v.object({
|
|
136
|
+
_id: v.id("userData"),
|
|
137
|
+
content: v.string(),
|
|
138
|
+
})),
|
|
139
|
+
handler: async (ctx) => {
|
|
140
|
+
const user = await getUser(ctx);
|
|
141
|
+
if (!user) return [];
|
|
142
|
+
|
|
143
|
+
// SECURITY: Filter by userId
|
|
144
|
+
return await ctx.db
|
|
145
|
+
.query("userData")
|
|
146
|
+
.withIndex("by_user", (q) => q.eq("userId", user._id))
|
|
147
|
+
.collect();
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Audit: Verify ownership before returning sensitive data
|
|
152
|
+
export const getSensitiveItem = query({
|
|
153
|
+
args: { itemId: v.id("sensitiveItems") },
|
|
154
|
+
returns: v.union(v.object({
|
|
155
|
+
_id: v.id("sensitiveItems"),
|
|
156
|
+
secret: v.string(),
|
|
157
|
+
}), v.null()),
|
|
158
|
+
handler: async (ctx, args) => {
|
|
159
|
+
const user = await getUser(ctx);
|
|
160
|
+
if (!user) return null;
|
|
161
|
+
|
|
162
|
+
const item = await ctx.db.get(args.itemId);
|
|
163
|
+
|
|
164
|
+
// SECURITY: Verify ownership
|
|
165
|
+
if (!item || item.ownerId !== user._id) {
|
|
166
|
+
return null; // Don't reveal if item exists
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return item;
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Audit: Shared resources with access list
|
|
174
|
+
export const getSharedDocument = query({
|
|
175
|
+
args: { docId: v.id("documents") },
|
|
176
|
+
returns: v.union(v.object({
|
|
177
|
+
_id: v.id("documents"),
|
|
178
|
+
content: v.string(),
|
|
179
|
+
accessLevel: v.string(),
|
|
180
|
+
}), v.null()),
|
|
181
|
+
handler: async (ctx, args) => {
|
|
182
|
+
const user = await getUser(ctx);
|
|
183
|
+
const doc = await ctx.db.get(args.docId);
|
|
184
|
+
|
|
185
|
+
if (!doc) return null;
|
|
186
|
+
|
|
187
|
+
// Public documents
|
|
188
|
+
if (doc.visibility === "public") {
|
|
189
|
+
return { ...doc, accessLevel: "public" };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Must be authenticated for non-public
|
|
193
|
+
if (!user) return null;
|
|
194
|
+
|
|
195
|
+
// Owner has full access
|
|
196
|
+
if (doc.ownerId === user._id) {
|
|
197
|
+
return { ...doc, accessLevel: "owner" };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Check shared access
|
|
201
|
+
const access = await ctx.db
|
|
202
|
+
.query("documentAccess")
|
|
203
|
+
.withIndex("by_doc_and_user", (q) =>
|
|
204
|
+
q.eq("documentId", args.docId).eq("userId", user._id)
|
|
205
|
+
)
|
|
206
|
+
.unique();
|
|
207
|
+
|
|
208
|
+
if (!access) return null;
|
|
209
|
+
|
|
210
|
+
return { ...doc, accessLevel: access.level };
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Action Isolation Audit
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// convex/actions.ts
|
|
219
|
+
"use node";
|
|
220
|
+
|
|
221
|
+
import { action, internalAction } from "./_generated/server";
|
|
222
|
+
import { v } from "convex/values";
|
|
223
|
+
import { api, internal } from "./_generated/api";
|
|
224
|
+
import { ConvexError } from "convex/values";
|
|
225
|
+
|
|
226
|
+
// SECURITY: Never expose API keys in responses
|
|
227
|
+
export const callExternalAPI = action({
|
|
228
|
+
args: { query: v.string() },
|
|
229
|
+
returns: v.object({ result: v.string() }),
|
|
230
|
+
handler: async (ctx, args) => {
|
|
231
|
+
// Verify user is authenticated
|
|
232
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
233
|
+
if (!identity) {
|
|
234
|
+
throw new ConvexError("Authentication required");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Get API key from environment (not hardcoded)
|
|
238
|
+
const apiKey = process.env.EXTERNAL_API_KEY;
|
|
239
|
+
if (!apiKey) {
|
|
240
|
+
throw new Error("API key not configured");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Log usage for audit trail
|
|
244
|
+
await ctx.runMutation(internal.audit.logAPICall, {
|
|
245
|
+
userId: identity.tokenIdentifier,
|
|
246
|
+
endpoint: "external-api",
|
|
247
|
+
timestamp: Date.now(),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const response = await fetch("https://api.example.com/query", {
|
|
251
|
+
method: "POST",
|
|
252
|
+
headers: {
|
|
253
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
254
|
+
"Content-Type": "application/json",
|
|
255
|
+
},
|
|
256
|
+
body: JSON.stringify({ query: args.query }),
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
if (!response.ok) {
|
|
260
|
+
// Don't expose external API error details
|
|
261
|
+
throw new ConvexError("External service unavailable");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const data = await response.json();
|
|
265
|
+
|
|
266
|
+
// Sanitize response before returning
|
|
267
|
+
return { result: sanitizeResponse(data) };
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Internal action - not exposed to clients
|
|
272
|
+
export const _processPayment = internalAction({
|
|
273
|
+
args: {
|
|
274
|
+
userId: v.id("users"),
|
|
275
|
+
amount: v.number(),
|
|
276
|
+
paymentMethodId: v.string(),
|
|
277
|
+
},
|
|
278
|
+
returns: v.object({ success: v.boolean(), transactionId: v.optional(v.string()) }),
|
|
279
|
+
handler: async (ctx, args) => {
|
|
280
|
+
const stripeKey = process.env.STRIPE_SECRET_KEY;
|
|
281
|
+
|
|
282
|
+
// Process payment with Stripe
|
|
283
|
+
// This should NEVER be exposed as a public action
|
|
284
|
+
|
|
285
|
+
return { success: true, transactionId: "txn_xxx" };
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Rate Limiting Audit
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
// convex/rateLimit.ts
|
|
294
|
+
import { mutation, query } from "./_generated/server";
|
|
295
|
+
import { v } from "convex/values";
|
|
296
|
+
import { ConvexError } from "convex/values";
|
|
297
|
+
|
|
298
|
+
const RATE_LIMITS = {
|
|
299
|
+
message: { requests: 10, windowMs: 60000 }, // 10 per minute
|
|
300
|
+
upload: { requests: 5, windowMs: 300000 }, // 5 per 5 minutes
|
|
301
|
+
api: { requests: 100, windowMs: 3600000 }, // 100 per hour
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export const checkRateLimit = mutation({
|
|
305
|
+
args: {
|
|
306
|
+
userId: v.string(),
|
|
307
|
+
action: v.union(v.literal("message"), v.literal("upload"), v.literal("api")),
|
|
308
|
+
},
|
|
309
|
+
returns: v.object({ allowed: v.boolean(), retryAfter: v.optional(v.number()) }),
|
|
310
|
+
handler: async (ctx, args) => {
|
|
311
|
+
const limit = RATE_LIMITS[args.action];
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
const windowStart = now - limit.windowMs;
|
|
314
|
+
|
|
315
|
+
// Count requests in window
|
|
316
|
+
const requests = await ctx.db
|
|
317
|
+
.query("rateLimits")
|
|
318
|
+
.withIndex("by_user_and_action", (q) =>
|
|
319
|
+
q.eq("userId", args.userId).eq("action", args.action)
|
|
320
|
+
)
|
|
321
|
+
.filter((q) => q.gt(q.field("timestamp"), windowStart))
|
|
322
|
+
.collect();
|
|
323
|
+
|
|
324
|
+
if (requests.length >= limit.requests) {
|
|
325
|
+
const oldestRequest = requests[0];
|
|
326
|
+
const retryAfter = oldestRequest.timestamp + limit.windowMs - now;
|
|
327
|
+
|
|
328
|
+
return { allowed: false, retryAfter };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Record this request
|
|
332
|
+
await ctx.db.insert("rateLimits", {
|
|
333
|
+
userId: args.userId,
|
|
334
|
+
action: args.action,
|
|
335
|
+
timestamp: now,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
return { allowed: true };
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// Use in mutations
|
|
343
|
+
export const sendMessage = mutation({
|
|
344
|
+
args: { content: v.string() },
|
|
345
|
+
returns: v.id("messages"),
|
|
346
|
+
handler: async (ctx, args) => {
|
|
347
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
348
|
+
if (!identity) throw new ConvexError("Authentication required");
|
|
349
|
+
|
|
350
|
+
// Check rate limit
|
|
351
|
+
const rateCheck = await checkRateLimit(ctx, {
|
|
352
|
+
userId: identity.tokenIdentifier,
|
|
353
|
+
action: "message",
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
if (!rateCheck.allowed) {
|
|
357
|
+
throw new ConvexError({
|
|
358
|
+
code: "RATE_LIMITED",
|
|
359
|
+
message: `Too many requests. Try again in ${Math.ceil(rateCheck.retryAfter! / 1000)} seconds`,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return await ctx.db.insert("messages", {
|
|
364
|
+
content: args.content,
|
|
365
|
+
authorId: identity.tokenIdentifier,
|
|
366
|
+
createdAt: Date.now(),
|
|
367
|
+
});
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### Sensitive Operations Protection
|
|
373
|
+
|
|
374
|
+
```typescript
|
|
375
|
+
// convex/admin.ts
|
|
376
|
+
import { mutation, internalMutation } from "./_generated/server";
|
|
377
|
+
import { v } from "convex/values";
|
|
378
|
+
import { requireRole, requirePermission } from "./lib/auth";
|
|
379
|
+
import { internal } from "./_generated/api";
|
|
380
|
+
|
|
381
|
+
// Two-factor confirmation for dangerous operations
|
|
382
|
+
export const deleteAllUserData = mutation({
|
|
383
|
+
args: {
|
|
384
|
+
userId: v.id("users"),
|
|
385
|
+
confirmationCode: v.string(),
|
|
386
|
+
},
|
|
387
|
+
returns: v.null(),
|
|
388
|
+
handler: async (ctx, args) => {
|
|
389
|
+
// Require superadmin
|
|
390
|
+
const admin = await requireRole(ctx, "superadmin");
|
|
391
|
+
|
|
392
|
+
// Verify confirmation code
|
|
393
|
+
const confirmation = await ctx.db
|
|
394
|
+
.query("confirmations")
|
|
395
|
+
.withIndex("by_admin_and_code", (q) =>
|
|
396
|
+
q.eq("adminId", admin._id).eq("code", args.confirmationCode)
|
|
397
|
+
)
|
|
398
|
+
.filter((q) => q.gt(q.field("expiresAt"), Date.now()))
|
|
399
|
+
.unique();
|
|
400
|
+
|
|
401
|
+
if (!confirmation || confirmation.action !== "delete_user_data") {
|
|
402
|
+
throw new ConvexError("Invalid or expired confirmation code");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Delete confirmation to prevent reuse
|
|
406
|
+
await ctx.db.delete(confirmation._id);
|
|
407
|
+
|
|
408
|
+
// Schedule deletion (don't do it inline)
|
|
409
|
+
await ctx.scheduler.runAfter(0, internal.admin._performDeletion, {
|
|
410
|
+
userId: args.userId,
|
|
411
|
+
requestedBy: admin._id,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// Audit log
|
|
415
|
+
await ctx.db.insert("auditLogs", {
|
|
416
|
+
action: "delete_user_data",
|
|
417
|
+
targetUserId: args.userId,
|
|
418
|
+
performedBy: admin._id,
|
|
419
|
+
timestamp: Date.now(),
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return null;
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// Generate confirmation code for sensitive action
|
|
427
|
+
export const requestDeletionConfirmation = mutation({
|
|
428
|
+
args: { userId: v.id("users") },
|
|
429
|
+
returns: v.string(),
|
|
430
|
+
handler: async (ctx, args) => {
|
|
431
|
+
const admin = await requireRole(ctx, "superadmin");
|
|
432
|
+
|
|
433
|
+
const code = generateSecureCode();
|
|
434
|
+
|
|
435
|
+
await ctx.db.insert("confirmations", {
|
|
436
|
+
adminId: admin._id,
|
|
437
|
+
code,
|
|
438
|
+
action: "delete_user_data",
|
|
439
|
+
targetUserId: args.userId,
|
|
440
|
+
expiresAt: Date.now() + 5 * 60 * 1000, // 5 minutes
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// In production, send code via secure channel (email, SMS)
|
|
444
|
+
return code;
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Examples
|
|
450
|
+
|
|
451
|
+
### Complete Audit Trail System
|
|
452
|
+
|
|
453
|
+
```typescript
|
|
454
|
+
// convex/audit.ts
|
|
455
|
+
import { mutation, query, internalMutation } from "./_generated/server";
|
|
456
|
+
import { v } from "convex/values";
|
|
457
|
+
import { getUser, requireRole } from "./lib/auth";
|
|
458
|
+
|
|
459
|
+
const auditEventValidator = v.object({
|
|
460
|
+
_id: v.id("auditLogs"),
|
|
461
|
+
_creationTime: v.number(),
|
|
462
|
+
action: v.string(),
|
|
463
|
+
userId: v.optional(v.string()),
|
|
464
|
+
resourceType: v.string(),
|
|
465
|
+
resourceId: v.string(),
|
|
466
|
+
details: v.optional(v.any()),
|
|
467
|
+
ipAddress: v.optional(v.string()),
|
|
468
|
+
timestamp: v.number(),
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// Internal: Log audit event
|
|
472
|
+
export const logEvent = internalMutation({
|
|
473
|
+
args: {
|
|
474
|
+
action: v.string(),
|
|
475
|
+
userId: v.optional(v.string()),
|
|
476
|
+
resourceType: v.string(),
|
|
477
|
+
resourceId: v.string(),
|
|
478
|
+
details: v.optional(v.any()),
|
|
479
|
+
},
|
|
480
|
+
returns: v.id("auditLogs"),
|
|
481
|
+
handler: async (ctx, args) => {
|
|
482
|
+
return await ctx.db.insert("auditLogs", {
|
|
483
|
+
...args,
|
|
484
|
+
timestamp: Date.now(),
|
|
485
|
+
});
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// Admin: View audit logs
|
|
490
|
+
export const getAuditLogs = query({
|
|
491
|
+
args: {
|
|
492
|
+
resourceType: v.optional(v.string()),
|
|
493
|
+
userId: v.optional(v.string()),
|
|
494
|
+
limit: v.optional(v.number()),
|
|
495
|
+
},
|
|
496
|
+
returns: v.array(auditEventValidator),
|
|
497
|
+
handler: async (ctx, args) => {
|
|
498
|
+
await requireRole(ctx, "admin");
|
|
499
|
+
|
|
500
|
+
let query = ctx.db.query("auditLogs");
|
|
501
|
+
|
|
502
|
+
if (args.resourceType) {
|
|
503
|
+
query = query.withIndex("by_resource_type", (q) =>
|
|
504
|
+
q.eq("resourceType", args.resourceType)
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return await query
|
|
509
|
+
.order("desc")
|
|
510
|
+
.take(args.limit ?? 100);
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
## Best Practices
|
|
516
|
+
|
|
517
|
+
- Never run `npx convex deploy` unless explicitly instructed
|
|
518
|
+
- Never run any git commands unless explicitly instructed
|
|
519
|
+
- Implement defense in depth (multiple security layers)
|
|
520
|
+
- Log all sensitive operations for audit trails
|
|
521
|
+
- Use confirmation codes for destructive actions
|
|
522
|
+
- Rate limit all user-facing endpoints
|
|
523
|
+
- Never expose internal API keys or errors
|
|
524
|
+
- Review access patterns regularly
|
|
525
|
+
|
|
526
|
+
## Common Pitfalls
|
|
527
|
+
|
|
528
|
+
1. **Single point of failure** - Implement multiple auth checks
|
|
529
|
+
2. **Missing audit logs** - Log all sensitive operations
|
|
530
|
+
3. **Trusting client data** - Always validate server-side
|
|
531
|
+
4. **Exposing error details** - Sanitize error messages
|
|
532
|
+
5. **No rate limiting** - Always implement rate limits
|
|
533
|
+
|
|
534
|
+
## References
|
|
535
|
+
|
|
536
|
+
- Convex Documentation: https://docs.convex.dev/
|
|
537
|
+
- Convex LLMs.txt: https://docs.convex.dev/llms.txt
|
|
538
|
+
- Functions Auth: https://docs.convex.dev/auth/functions-auth
|
|
539
|
+
- Production Security: https://docs.convex.dev/production
|
|
Binary file
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<g clip-path="url(#clip0_3_23)">
|
|
3
|
+
<g clip-path="url(#clip1_3_23)">
|
|
4
|
+
<path d="M10.0643 12.5735C12.3769 12.3166 14.5572 11.0843 15.7577 9.02756C15.1892 14.1148 9.62646 17.3302 5.08583 15.356C4.66743 15.1746 4.30728 14.8728 4.06013 14.4848C3.03973 12.8825 2.7043 10.8437 3.18626 8.99344C4.56327 11.37 7.3632 12.8267 10.0643 12.5735Z" fill="#F3B01C"/>
|
|
5
|
+
<path d="M3.1018 7.50072C2.16436 9.66714 2.12376 12.2034 3.27303 14.2907C-0.771507 11.2479 -0.72737 4.7362 3.2236 1.72378C3.58904 1.44535 4.02333 1.2801 4.47881 1.25494C6.3519 1.15614 8.25501 1.88006 9.58963 3.22909C6.87799 3.25604 4.23695 4.99308 3.1018 7.50072Z" fill="#8D2676"/>
|
|
6
|
+
<path d="M10.8974 3.89562C9.52924 1.98794 7.38779 0.68921 5.04156 0.649695C9.57686 -1.40888 15.1555 1.92867 15.7629 6.86314C15.8194 7.32119 15.7452 7.78824 15.5421 8.20138C14.6948 9.92223 13.1236 11.2569 11.2876 11.7508C12.6328 9.25579 12.4668 6.20748 10.8974 3.89562Z" fill="#EE342F"/>
|
|
7
|
+
</g>
|
|
8
|
+
</g>
|
|
9
|
+
<defs>
|
|
10
|
+
<clipPath id="clip0_3_23">
|
|
11
|
+
<rect width="16" height="16" fill="white"/>
|
|
12
|
+
</clipPath>
|
|
13
|
+
<clipPath id="clip1_3_23">
|
|
14
|
+
<rect width="16" height="16" fill="white"/>
|
|
15
|
+
</clipPath>
|
|
16
|
+
</defs>
|
|
17
|
+
</svg>
|