middlewares.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // config/middlewares.ts
  2. // Helper function to get allowed origins based on environment
  3. // Ensures required domains are always included
  4. const getAllowedOrigins = () => {
  5. const allowedOrigins = [
  6. 'https://genomii.ai', // Your production frontend **REQUIRED**
  7. // Add other domains if needed, e.g., staging environments
  8. ];
  9. // Optionally add localhost for development environments
  10. if (process.env.NODE_ENV !== 'production') {
  11. allowedOrigins.push('http://localhost:3000'); // Common React dev port
  12. allowedOrigins.push('http://localhost:1337'); // Strapi default admin port
  13. }
  14. // Include the Strapi server URL itself if necessary for admin panel or previews
  15. if (process.env.URL) {
  16. allowedOrigins.push(process.env.URL); // URL Strapi is running on (e.g., https://strapi.genomii.ai)
  17. } else {
  18. // Fallback if URL env var isn't set (adjust if needed)
  19. allowedOrigins.push('https://strapi.genomii.ai');
  20. }
  21. return allowedOrigins;
  22. };
  23. export default [
  24. 'strapi::logger',
  25. 'strapi::errors',
  26. {
  27. name: 'strapi::security',
  28. config: {
  29. contentSecurityPolicy: {
  30. useDefaults: true,
  31. directives: {
  32. // Your existing CSP directives... make sure CLOUDFRONT_URL is properly set in your environment
  33. 'connect-src': ["'self'", 'https:'],
  34. 'img-src': [
  35. "'self'",
  36. 'data:',
  37. 'blob:',
  38. 'https://market-assets.strapi.io',
  39. `https://${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`, // Use env vars for bucket
  40. '*.s3.amazonaws.com', // More general S3 pattern if needed
  41. process.env.CLOUDFRONT_URL || 'https://blog-media.genomii.ai', // CloudFront domain
  42. ],
  43. 'media-src': [
  44. "'self'",
  45. 'data:',
  46. 'blob:',
  47. 'https://market-assets.strapi.io',
  48. `https://${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`, // Use env vars for bucket
  49. '*.s3.amazonaws.com', // More general S3 pattern if needed
  50. process.env.CLOUDFRONT_URL || 'https://blog-media.genomii.ai', // CloudFront domain
  51. ],
  52. upgradeInsecureRequests: null,
  53. },
  54. },
  55. },
  56. },
  57. // == Replace 'strapi::cors' with this configuration object ==
  58. {
  59. name: 'strapi::cors',
  60. config: {
  61. enabled: true,
  62. headers: '*', // Allow all headers, or specify ['Content-Type', 'Authorization', ...]
  63. origin: getAllowedOrigins(), // Dynamically set allowed origins
  64. methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'], // Common methods needed
  65. credentials: false // Set to true if you need cookies/auth headers across origins
  66. }
  67. },
  68. // ==========================================================
  69. 'strapi::poweredBy',
  70. 'strapi::query',
  71. 'strapi::body',
  72. 'strapi::session',
  73. 'strapi::favicon',
  74. 'strapi::public',
  75. ];