Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { connect } from './services/client';
import { initDb } from './services/database';
import { initPoolDb } from './services/pool';
import { createMessage, initiateLogger } from './services/logger';
import { getRequestId } from './utils';
import { getRequestId, isPermittedOrigin } from './utils';

interface AppSettings {
mongoClient?: MongoClient;
Expand All @@ -33,13 +33,19 @@ const errorHandler: ErrorRequestHandler = (err, req, res, _next) => {
}
};

const reqHandler: RequestHandler = (req, _res, next) => {
const reqHandler: RequestHandler = (req, res, next) => {
const reqId = new ObjectId().toString();
// Custom header specifically for a request ID. This ID will be used to track
// logs related to the same request
req.headers['req-id'] = reqId;
const message = `Request for: ${req.url}`;
logger.info(createMessage(message, reqId));

// allow cross origin requests from our web servers
const origin = req.headers.origin;
if (origin && isPermittedOrigin(origin)) {
res.append('Access-Control-Allow-Origin', origin);
}
next();
};

Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ export const assertTrailingSlash = (str: string) => {
}
return `${str}/`;
};

const STAGING_HOSTNAME = 'netlify.app';
const PROD_HOSTNAME = 'mongodb.com';

export const isPermittedOrigin = (origin: string | undefined) => {
if (!origin) return false;
let url;
try {
url = new URL(origin);
} catch (err) {
return false;
}
return (
url.protocol === 'https:' &&
(url.hostname.split('.').slice(-2).join('.') === STAGING_HOSTNAME ||
url.hostname.split('.').slice(-2).join('.') === PROD_HOSTNAME)
);
};