Skip to content

Commit

Permalink
Converted static CORS into middleware, and added to all routers (#161)
Browse files Browse the repository at this point in the history
* Added CORS stuff

* Format + Lint
  • Loading branch information
AydanPirani authored Jan 21, 2024
1 parent 9f3b44c commit faa7bf5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { ErrorHandler } from "./middleware/error-handler.js";
import { StatusCode } from "status-code-enum";
import Config from "./config.js";
import database from "./middleware/database.js";
import corsSelector from "./middleware/cors-selector.js";

const app: Application = express();

// Utility packages (detailed in the readme)
// app.use(helmet({ crossOriginResourcePolicy: false }));
app.use(corsSelector);

// Enable request output when not a test
if (!Config.TEST) {
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const Config = {

JWT_SECRET: requireEnv("JWT_SECRET"),

NEWSLETTER_CORS: {
CORS: {
PROD_REGEX: requireEnv("PROD_REGEX"),
DEPLOY_REGEX: requireEnv("DEPLOY_REGEX"),
},
Expand Down
24 changes: 24 additions & 0 deletions src/middleware/cors-selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cors, { CorsOptions } from "cors";
import Config from "../config.js";

// Only allow a certain set of regexes to be allowed via CORS
const allowedOrigins: RegExp[] = [new RegExp(Config.CORS.PROD_REGEX), new RegExp(Config.CORS.DEPLOY_REGEX)];

function regexPasses(target: string, patterns: RegExp[]): boolean {
return patterns.some((pattern: RegExp) => {
return pattern.test(target);
});
}

// CORS options configuration
const corsOptions: CorsOptions = {
origin: (origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void) => {
if (!origin || regexPasses(origin, allowedOrigins)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};

export default cors(corsOptions);
11 changes: 0 additions & 11 deletions src/services/newsletter/newsletter-lib.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/services/newsletter/newsletter-router.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
import { Request, Response, Router } from "express";
import { regexPasses } from "./newsletter-lib.js";
import cors, { CorsOptions } from "cors";

import { SubscribeRequest } from "./newsletter-formats.js";
import { NewsletterSubscription } from "../../database/newsletter-db.js";
import Models from "../../database/models.js";
import { UpdateQuery } from "mongoose";
import { StatusCode } from "status-code-enum";
import Config from "../../config.js";
import { RouterError } from "../../middleware/error-handler.js";
import { NextFunction } from "express-serve-static-core";

const newsletterRouter: Router = Router();

// Only allow a certain set of regexes to be allowed via CORS
const allowedOrigins: RegExp[] = [new RegExp(Config.NEWSLETTER_CORS.PROD_REGEX), new RegExp(Config.NEWSLETTER_CORS.DEPLOY_REGEX)];

// CORS options configuration
const corsOptions: CorsOptions = {
origin: (origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void) => {
if (!origin || regexPasses(origin, allowedOrigins)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};

// Use CORS for exclusively the newsletter - public access
newsletterRouter.use(cors(corsOptions));

/**
* @api {post} /newsletter/subscribe/ POST /newsletter/subscribe/
* @apiGroup Newsletter
Expand Down

0 comments on commit faa7bf5

Please sign in to comment.