Skip to content

Commit c496b11

Browse files
fix(api): update cors settings for public route
1 parent 46c7ad7 commit c496b11

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { updateTracker } from "./middleware/update-tracker.js";
2323
import { errorHandler, getAppVersion, getRoot, invalidEndpointHandler } from "./controllers/app-controller.js";
2424

2525
// allowed apps url
26-
var whitelist = [
26+
const whitelist = [
2727
"https://alpha-sws-pocket.web.app",
2828
"https://alpha-sws-pocket.firebaseapp.com",
2929
"https://sws-pocket.web.app",
@@ -36,15 +36,25 @@ var whitelist = [
3636
"https://sws2apps-tools.firebaseapp.com",
3737
];
3838

39-
var corsOptionsDelegate = function (req, callback) {
39+
const allowedUri = ["/app-version", "/api/public/source-material"];
40+
41+
const corsOptionsDelegate = function (req, callback) {
4042
var corsOptions;
43+
4144
if (process.env.NODE_ENV === "production") {
4245
const reqOrigin = req.header("Origin");
4346
if (reqOrigin) {
4447
if (whitelist.indexOf(reqOrigin) !== -1) {
4548
corsOptions = { origin: true }; // reflect (enable) the requested origin in the CORS response
4649
} else {
47-
corsOptions = { origin: false }; // disable CORS for this request
50+
const originalUri = req.headers["x-original-uri"];
51+
52+
if (originalUri === "/") {
53+
corsOptions = { origin: true }; // allow CORS for index route
54+
} else {
55+
const allowed = allowedUri.find((uri) => uri.startsWith(originalUri)) ? true : false;
56+
corsOptions = { origin: allowed };
57+
}
4858
}
4959
} else {
5060
corsOptions = { origin: false };
@@ -93,11 +103,10 @@ app.use("/api/admin", adminRoute);
93103
app.use("/api/sws-pocket", swsPocketRoute);
94104
app.use("/api/public", publicRoute);
95105

96-
app.get("/", getRoot);
97-
98-
// get app version for shields.io
99106
app.get("/app-version", getAppVersion);
100107

108+
app.get("/", getRoot);
109+
101110
// Handling invalid routes
102111
app.use(invalidEndpointHandler);
103112

0 commit comments

Comments
 (0)