-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
396 lines (350 loc) Β· 12.6 KB
/
server.js
File metadata and controls
396 lines (350 loc) Β· 12.6 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// AgentShield - Production-ready AI Agent Security Scanner
// Main Express server application
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const compression = require('compression');
const path = require('path');
require('dotenv').config();
// Import local modules
const { AgentShieldDB } = require('./db');
const { authenticateAPI } = require('./middleware/auth');
const { createRateLimiter, ipRateLimit, dailyQuotaCheck, abuseDetection } = require('./middleware/rateLimit');
const { trackUsage, analyticsMiddleware, securityMonitoring } = require('./middleware/usage');
// Import routes
const scanRoutes = require('./routes/scan');
const reportRoutes = require('./routes/reports');
const badgeRoutes = require('./routes/badges');
const registerRoutes = require('./routes/register');
const billingRoutes = require('./routes/billing');
const monitorRoutes = require('./routes/monitors');
const shieldScoreRoutes = require('./routes/shield-score');
const reputationRoutes = require('./routes/reputation');
const { startScheduler } = require('./monitoring/scheduler');
async function startServer() {
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3000;
const NODE_ENV = process.env.NODE_ENV || 'development';
// Initialize database (async)
const db = new AgentShieldDB({ dbPath: process.env.DATABASE_PATH || './agent-shield.db' });
await db.init();
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com", "https://cdnjs.cloudflare.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
imgSrc: ["'self'", "data:", "https:"],
scriptSrc: ["'self'", "'unsafe-inline'", "https://cdnjs.cloudflare.com"],
scriptSrcAttr: ["'unsafe-inline'"],
connectSrc: ["'self'"]
}
},
crossOriginEmbedderPolicy: false
}));
// CORS configuration
const corsOptions = {
origin: process.env.ALLOWED_ORIGINS ?
process.env.ALLOWED_ORIGINS.split(',').map(origin => origin.trim()) :
true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-API-Key'],
credentials: true,
maxAge: 86400 // 24 hours
};
app.use(cors(corsOptions));
// Compression
app.use(compression());
// Stripe webhook needs raw body β mount BEFORE json parser
// The webhook route handler uses express.raw() internally
app.use('/billing/webhook', express.raw({ type: 'application/json' }));
app.use('/api/billing/webhook', express.raw({ type: 'application/json' }));
// Body parsing middleware (for all other routes)
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Trust proxy for rate limiting behind reverse proxy
app.set('trust proxy', 1);
// Attach database to request object
app.use((req, res, next) => {
req.db = db;
next();
});
// Usage tracking and analytics
app.use(trackUsage(db));
app.use(analyticsMiddleware(db));
// Security monitoring
app.use(securityMonitoring(db));
// Serve static files (before auth β public content)
app.use(express.static(path.join(__dirname, 'public')));
// Blog routes - serve static HTML articles (before auth β public content)
app.get('/blog', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'blog', 'index.html'));
});
app.get('/blog/:slug', (req, res) => {
const slug = req.params.slug.replace(/[^a-z0-9-]/gi, '');
const filePath = path.join(__dirname, 'public', 'blog', slug + '.html');
res.sendFile(filePath, (err) => {
if (err) {
res.status(404).json({ error: 'Article not found' });
}
});
});
// IP-based rate limiting for unauthenticated requests
app.use(ipRateLimit);
// Authentication middleware
app.use(authenticateAPI(db));
// API rate limiting based on plan
app.use(createRateLimiter(db));
// Daily quota checking
app.use(dailyQuotaCheck(db));
// Abuse detection
app.use(abuseDetection(db));
// API Routes (both /scan and /api/scan work β we link /api/scan in marketing)
app.use('/scan', scanRoutes);
app.use('/api/scan', scanRoutes);
app.use('/report', reportRoutes);
app.use('/api/report', reportRoutes);
app.use('/badges', badgeRoutes);
app.use('/api/badges', badgeRoutes);
app.use('/register', registerRoutes);
app.use('/api/register', registerRoutes);
app.use('/billing', billingRoutes);
app.use('/api/billing', billingRoutes);
app.use('/monitors', monitorRoutes);
app.use('/api/monitors', monitorRoutes);
app.use('/shield-score', shieldScoreRoutes);
app.use('/api/shield-score', shieldScoreRoutes);
app.use('/reputation', reputationRoutes);
app.use('/api/reputation', reputationRoutes);
// Root endpoint - Landing page
app.get('/', async (req, res) => {
try {
const stats = await db.getStats();
// If requesting JSON, return stats
if (req.headers.accept?.includes('application/json')) {
return res.json({
name: 'AgentShield',
version: '1.0.0',
description: 'Production-ready security scanner for AI agent skills and plugins',
status: 'operational',
stats: stats.allTime,
endpoints: {
scan: '/scan',
reports: '/report/:scanId',
badges: '/badges/:scanId',
health: '/health',
discovery: '/discovery'
}
});
}
// Serve HTML landing page
res.sendFile(path.join(__dirname, 'public', 'index.html'));
} catch (error) {
console.error('Root endpoint error:', error);
res.status(500).json({
error: 'Server error',
message: 'Failed to load landing page'
});
}
});
// Health check endpoint
app.get('/health', async (req, res) => {
try {
const stats = await db.getStats();
const health = {
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0',
uptime: process.uptime(),
memory: process.memoryUsage(),
environment: NODE_ENV,
database: {
connected: !!db.db,
stats: stats
}
};
res.json(health);
} catch (error) {
console.error('Health check error:', error);
res.status(503).json({
status: 'unhealthy',
timestamp: new Date().toISOString(),
error: error.message
});
}
});
// Platform statistics endpoint
app.get('/stats', async (req, res) => {
try {
const stats = await db.getStats();
res.json({
totalScans: stats.allTime.total_scans || 0,
threatsDetected: stats.allTime.threats_detected || 0,
cleanSkills: stats.allTime.clean_skills || 0,
cleanPercentage: stats.allTime.total_scans > 0 ?
Math.round((stats.allTime.clean_skills / stats.allTime.total_scans) * 100) : 0,
avgTrustScore: Math.round(stats.allTime.avg_trust_score || 0),
avgScanTime: Math.round(stats.allTime.avg_scan_duration_ms || 0),
today: stats.today,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Stats endpoint error:', error);
res.status(500).json({
error: 'Failed to retrieve statistics',
message: error.message
});
}
});
// x402 discovery endpoint for AI agent marketplaces
app.get('/discovery', (req, res) => {
try {
const baseUrl = `${req.protocol}://${req.get('host')}`;
res.json({
name: 'AgentShield Security Scanner',
description: 'Comprehensive security scanning for AI agent skills and plugins',
version: '1.0.0',
provider: 'AgentShield',
capabilities: [
'skill-scanning',
'vulnerability-detection',
'security-reporting',
'badge-generation',
'batch-processing'
],
endpoints: {
scan: {
url: `${baseUrl}/scan`,
methods: ['POST'],
description: 'Scan individual skills for security vulnerabilities',
pricing: {
free: { price: 0, limit: '10/day' },
pro: { price: 0.05, limit: '1000/day' },
enterprise: { price: 'contact', limit: 'unlimited' }
}
},
batchScan: {
url: `${baseUrl}/scan/batch`,
methods: ['POST'],
description: 'Scan multiple skills in a single request',
pricing: {
pro: { price: 0.04, limit: '25/batch' },
enterprise: { price: 'contact', limit: '100/batch' }
}
},
badges: {
url: `${baseUrl}/badges/:scanId`,
methods: ['GET'],
description: 'Generate embeddable security badges',
pricing: { free: { price: 0, limit: '30/min' } }
}
},
authentication: {
type: 'api-key',
header: 'X-API-Key',
registration: `${baseUrl}#pricing`
},
documentation: `${baseUrl}#documentation`,
support: 'https://github.com/SiamakSafari/agent-shield/issues'
});
} catch (error) {
console.error('Discovery endpoint error:', error);
res.status(500).json({
error: 'Discovery failed',
message: error.message
});
}
});
// Verification endpoint (placeholder for future manual verification)
app.post('/verify', (req, res) => {
res.json({
message: 'Manual verification feature coming soon',
description: 'Submit skills for manual security review by our experts',
status: 'not_implemented',
estimatedLaunch: '2024-Q2',
notification: 'Sign up for updates at https://agentshield.dev'
});
});
// Global error handler
app.use((error, req, res, next) => {
console.error('Global error handler:', error);
// Don't leak error details in production
const isDevelopment = NODE_ENV === 'development';
res.status(error.status || 500).json({
error: 'Internal server error',
message: isDevelopment ? error.message : 'Something went wrong',
timestamp: new Date().toISOString(),
...(isDevelopment && { stack: error.stack })
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
error: 'Not found',
message: `Endpoint ${req.method} ${req.path} does not exist`,
availableEndpoints: [
'POST /scan',
'POST /scan/batch',
'GET /report/:scanId',
'GET /badges/:scanId',
'POST /register',
'GET /register/plans',
'POST /billing/checkout',
'GET /billing/status',
'GET /billing/portal',
'POST /api/monitors',
'GET /api/monitors',
'GET /api/monitors/:id',
'DELETE /api/monitors/:id',
'POST /api/monitors/:id/scan',
'GET /api/monitors/:id/alerts',
'POST /api/monitors/:id/webhook',
'GET /api/shield-score?skills=url1,url2',
'GET /api/shield-score/badge/:score',
'GET /api/shield-score/leaderboard',
'GET /api/reputation?agent_id=OWNER/REPO',
'GET /health',
'GET /stats',
'GET /discovery'
]
});
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
if (db) db.close();
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
if (db) db.close();
process.exit(0);
});
// Start server
app.listen(PORT, () => {
console.log(`π‘οΈ AgentShield Server running on port ${PORT}`);
console.log(`π Environment: ${NODE_ENV}`);
console.log(`π API Base URL: http://localhost:${PORT}`);
console.log(`π Health Check: http://localhost:${PORT}/health`);
console.log(`π Statistics: http://localhost:${PORT}/stats`);
if (NODE_ENV === 'development') {
console.log(`π Landing Page: http://localhost:${PORT}`);
console.log(`π Discovery: http://localhost:${PORT}/discovery`);
}
// Start background monitoring scheduler
const schedulerId = startScheduler(db);
console.log(`π‘ Continuous Monitoring scheduler active`);
// Clean up scheduler on shutdown
process.on('SIGTERM', () => clearInterval(schedulerId));
process.on('SIGINT', () => clearInterval(schedulerId));
});
return app;
}
// Start the server
const appPromise = startServer().catch(err => {
console.error('Failed to start server:', err);
process.exit(1);
});
module.exports = appPromise;