Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add custom domains to CORS allowlist #2666

Merged
merged 3 commits into from
Jan 16, 2024
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
15 changes: 13 additions & 2 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { json, urlencoded } from "body-parser";
import assert from "assert";
import cookieParser from "cookie-parser";
import cookieSession from "cookie-session";
import cors from "cors";
import cors, { CorsOptions } from "cors";
import express, { ErrorRequestHandler } from "express";
import noir from "pino-noir";
import pinoLogger from "express-pino-logger";
Expand Down Expand Up @@ -38,11 +38,22 @@ useSwaggerDocs(app);

app.set("trust proxy", 1);

const checkAllowedOrigins: CorsOptions["origin"] = (origin, callback) => {
const isTest = process.env.NODE_ENV === "test";
const isDevelopment = process.env.APP_ENVIRONMENT === "development";
const allowList = process.env.CORS_ALLOWLIST?.split(", ") || [];
const isAllowed = Boolean(origin && allowList.includes(origin));

!origin || isTest || isDevelopment || isAllowed
? callback(null, true)
: callback(new Error("Not allowed by CORS"));
};

app.use(
cors({
credentials: true,
methods: "*",
origin: process.env.EDITOR_URL_EXT,
origin: checkAllowedOrigins,
allowedHeaders: [
"Accept",
"Authorization",
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ services:
SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL}
ORDNANCE_SURVEY_API_KEY: ${ORDNANCE_SURVEY_API_KEY}
MINIO_PORT: ${MINIO_PORT}
CORS_ALLOWLIST: ${EDITOR_URL_EXT}
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is just a "derived" env var, we actually don't need to add it to the .env files and we can just populate it here.

If this is unclear / unhelpful I can add it to the .env and .env.example files.

# Local authority config
# Lambeth
GOV_UK_PAY_TOKEN_LAMBETH: ${GOV_UK_PAY_TOKEN_LAMBETH}
Expand All @@ -153,7 +154,7 @@ services:
UNIFORM_CLIENT_AYLESBURY_VALE: ${UNIFORM_CLIENT_AYLESBURY_VALE}
UNIFORM_CLIENT_CHILTERN: ${UNIFORM_CLIENT_CHILTERN}
UNIFORM_CLIENT_WYCOMBE: ${UNIFORM_CLIENT_WYCOMBE}
#Medway
# Medway
GOV_UK_PAY_TOKEN_MEDWAY: ${GOV_UK_PAY_TOKEN_MEDWAY}

sharedb:
Expand Down
7 changes: 4 additions & 3 deletions infrastructure/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import * as mime from "mime";
import * as tldjs from "tldjs";
import * as url from "url";

import { generateTeamSecrets } from "./utils/generateTeamSecrets";
import { generateTeamSecrets, generateCORSAllowList, addRedirectToCloudFlareListenerRule } from "./utils";
import { createHasuraService } from "./services/hasura";
import { addRedirectToCloudFlareListenerRule } from "./utils/addListenerRule";
import { CustomDomains } from "../common/teams";

const config = new pulumi.Config();

Expand All @@ -25,7 +25,7 @@ const data = new pulumi.StackReference(`planx/data/${env}`);
// You can generate tokens here: https://dash.cloudflare.com/profile/api-tokens
new pulumi.Config("cloudflare").requireSecret("apiToken");

const CUSTOM_DOMAINS =
const CUSTOM_DOMAINS: CustomDomains =
env === "production"
? [
{
Expand Down Expand Up @@ -377,6 +377,7 @@ export = async () => {
name: "ORDNANCE_SURVEY_API_KEY",
value: config.requireSecret("ordnance-survey-api-key"),
},
generateCORSAllowList(CUSTOM_DOMAINS, DOMAIN),
...generateTeamSecrets(config, env),
],
},
Expand Down
16 changes: 16 additions & 0 deletions infrastructure/application/utils/generateCORSAllowList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as awsx from "@pulumi/awsx";

import { CustomDomains } from "../../common/teams";

export const generateCORSAllowList = (customDomains: CustomDomains, domain: string): awsx.ecs.KeyValuePair => {
const customDomainURLs = customDomains.map(team => `https://${team.domain}`);
const editorURL = `https://${domain}`;
const corsAllowList = [...customDomainURLs, editorURL];

const secret: awsx.ecs.KeyValuePair = {
name: "CORS_ALLOWLIST",
value: corsAllowList.join(", "),
};

return secret;
};
3 changes: 3 additions & 0 deletions infrastructure/application/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./addListenerRule";
export * from "./generateCORSAllowList";
export * from "./generateTeamSecrets";
5 changes: 5 additions & 0 deletions infrastructure/common/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ export const teams: Team[] = [
},
];

export type CustomDomains = Array<{
name: string,
domain: string
}>;

export default { teams }
Loading