nexusbert commited on
Commit
b2cd2a3
Β·
1 Parent(s): f5035b9
Files changed (1) hide show
  1. src/app.ts +20 -35
src/app.ts CHANGED
@@ -4,7 +4,7 @@ import helmet from 'helmet';
4
  import rateLimit from 'express-rate-limit';
5
  import swaggerUi from 'swagger-ui-express';
6
  import { swaggerSpec } from './docs/swagger';
7
- import { requestLogger, sanitizeRequestBody, validateRequestSize, createStrictRateLimiter } from './middlewares/security';
8
 
9
  // Routes
10
  import authRoutes from './routes/authRoutes';
@@ -23,24 +23,10 @@ const app: Application = express();
23
  // Only trust the first proxy (Hugging Face Spaces proxy)
24
  app.set('trust proxy', 1);
25
 
 
26
  app.use(helmet({
27
- contentSecurityPolicy: {
28
- directives: {
29
- defaultSrc: ["'self'"],
30
- styleSrc: ["'self'", "'unsafe-inline'"],
31
- scriptSrc: ["'self'"],
32
- imgSrc: ["'self'", "data:", "https:"],
33
- },
34
- },
35
  crossOriginEmbedderPolicy: false,
36
- hsts: {
37
- maxAge: 31536000,
38
- includeSubDomains: true,
39
- preload: true,
40
- },
41
- noSniff: true,
42
- xssFilter: true,
43
- frameguard: { action: 'deny' },
44
  }));
45
 
46
  // CORS configuration - simplified
@@ -75,10 +61,8 @@ app.use(cors({
75
  app.use(express.json({ limit: '10mb' }));
76
  app.use(express.urlencoded({ extended: true, limit: '10mb' }));
77
 
78
- // Security middleware
79
- app.use(validateRequestSize(10 * 1024 * 1024));
80
- app.use(sanitizeRequestBody);
81
- app.use(requestLogger);
82
 
83
  const generalLimiter = createStrictRateLimiter(15 * 60 * 1000, 100);
84
  app.use('/api/', generalLimiter);
@@ -123,26 +107,27 @@ app.use('/api/chat', chatRoutes);
123
  app.use('/api/subscriptions', subscriptionRoutes);
124
  app.use('/api/wallet', walletRoutes);
125
 
 
126
  app.use((req, res) => {
127
- res.status(404).json({ error: 'Route not found' });
 
 
 
 
 
128
  });
129
 
 
130
  app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
131
- console.error('Error:', {
132
- message: err.message,
133
- stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
134
- path: req.path,
135
- method: req.method,
136
- ip: req.ip,
137
- });
138
-
139
- if (err.message === 'Not allowed by CORS') {
140
- return res.status(403).json({ error: 'CORS policy violation' });
141
  }
142
 
143
- res.status(500).json({
144
- error: process.env.NODE_ENV === 'production' ? 'Internal server error' : err.message,
145
- });
 
 
146
  });
147
 
148
  export default app;
 
4
  import rateLimit from 'express-rate-limit';
5
  import swaggerUi from 'swagger-ui-express';
6
  import { swaggerSpec } from './docs/swagger';
7
+ import { sanitizeRequestBody, createStrictRateLimiter } from './middlewares/security';
8
 
9
  // Routes
10
  import authRoutes from './routes/authRoutes';
 
23
  // Only trust the first proxy (Hugging Face Spaces proxy)
24
  app.set('trust proxy', 1);
25
 
26
+ // Simplified Helmet - basic security only
27
  app.use(helmet({
28
+ contentSecurityPolicy: false, // Disable CSP for simplicity
 
 
 
 
 
 
 
29
  crossOriginEmbedderPolicy: false,
 
 
 
 
 
 
 
 
30
  }));
31
 
32
  // CORS configuration - simplified
 
61
  app.use(express.json({ limit: '10mb' }));
62
  app.use(express.urlencoded({ extended: true, limit: '10mb' }));
63
 
64
+ // Simplified security - only sanitize API routes
65
+ app.use('/api', sanitizeRequestBody);
 
 
66
 
67
  const generalLimiter = createStrictRateLimiter(15 * 60 * 1000, 100);
68
  app.use('/api/', generalLimiter);
 
107
  app.use('/api/subscriptions', subscriptionRoutes);
108
  app.use('/api/wallet', walletRoutes);
109
 
110
+ // Simple 404 handler
111
  app.use((req, res) => {
112
+ // Only return JSON for API routes, otherwise just 404
113
+ if (req.path.startsWith('/api')) {
114
+ res.status(404).json({ error: 'Route not found' });
115
+ } else {
116
+ res.status(404).send('Not found');
117
+ }
118
  });
119
 
120
+ // Simplified error handler
121
  app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
122
+ if (process.env.NODE_ENV === 'development') {
123
+ console.error('Error:', err.message);
 
 
 
 
 
 
 
 
124
  }
125
 
126
+ if (req.path.startsWith('/api')) {
127
+ res.status(500).json({ error: 'Server error' });
128
+ } else {
129
+ res.status(500).send('Server error');
130
+ }
131
  });
132
 
133
  export default app;