Skip to content

Commit a91c531

Browse files
authored
SERVER_ORIGIN, WEB_ORIGIN, WEB_ORIGIN_BUILD etc の代わりに CORS_ALLOW_ORIGINS を使う (#516)
## 影響範囲 環境変数に影響がある。 ## 動作要件 <!-- 動作に必要な 環境変数 / 依存関係 / DBの更新 など --> ## 補足 <!-- レビューをする際に見てほしい点、ローカル環境で試す際の注意点、など --> ## レビューリクエストを出す前にチェック! - [x] 改めてセルフレビューしたか - [ ] 手動での動作検証を行ったか - [ ] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [ ] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [x] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 -->
1 parent a9e3b78 commit a91c531

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test: export DATABASE_URL=$(LOCAL_DB)
3636
test: export NEVER_LOAD_DOTENV=1
3737
test: export UNSAFE_SKIP_AUTH=1
3838
test: export FIREBASE_PROJECT_ID=mock-proj
39+
test: export CORS_ALLOW_ORIGINS=http://localhost:3000,https://localhost:5173
3940
test: dev-db
4041
cd server/src; ENV_FILE=../.env.dev bun test
4142
cd ./test; ENV_FILE=../server/.env.dev bun test

server/.env.sample

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
# below can be used for docker db created via `make dev-db`
44
DATABASE_URL=postgres://user:password@localhost:5432/database
55

6-
# Application origins
7-
SERVER_ORIGIN=http://localhost:3000
8-
WEB_ORIGIN=http://localhost:5173
9-
MOBILE_ORIGIN=http://localhost:8081
6+
# CORS allow origins, separated by "," | no space is allowed before/after ","
7+
CORS_ALLOW_ORIGINS=http://localhost:3000,http://localhost:5173
108

119
# Firebase
1210
FIREBASE_PROJECT_ID=project-id

server/src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express from "express";
33
import csrf from "./lib/cross-origin/block-unknown-origin";
44
import cors from "./lib/cross-origin/multi-origin-cors";
55
import { initializeSocket } from "./lib/socket/socket";
6+
import { allUrlMustBeValid, panic } from "./lib/utils";
67
import chatRoutes from "./router/chat";
78
import coursesRoutes from "./router/courses";
89
import matchesRoutes from "./router/matches";
@@ -17,14 +18,15 @@ const app = express();
1718
app.set("query parser", "simple");
1819

1920
const port = 3000;
20-
const allowedOrigins = [
21-
process.env.SERVER_ORIGIN ?? "http://localhost:3000", // delete this fallback when you think everyone has updated their .env
22-
process.env.WEB_ORIGIN,
23-
process.env.MOBILE_ORIGIN,
24-
process.env.WEB_ORIGIN_BUILD,
25-
];
21+
const allowedOrigins = (
22+
process.env.CORS_ALLOW_ORIGINS || panic("env CORS_ALLOW_ORIGINS is missing")
23+
)
24+
.split(",")
25+
.filter((s) => s); // ignore empty string (trailing comma?)
26+
allUrlMustBeValid(allowedOrigins);
27+
2628
export const corsOptions = {
27-
origins: allowedOrigins.filter((s) => s != null).filter((s) => s), // ignore empty string too
29+
origins: allowedOrigins,
2830
methods: ["GET", "HEAD", "POST", "PUT", "DELETE"],
2931
credentials: true,
3032
};

server/src/lib/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function panic(reason: string): never {
2+
throw new Error(`function panic() called for reason: "${reason}"`);
3+
}
4+
5+
export function allUrlMustBeValid(urls: string[]) {
6+
for (const url of urls) {
7+
try {
8+
new URL(url);
9+
} catch (err) {
10+
console.error(err);
11+
throw err;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)