Skip to content

Commit

Permalink
fix: firefox browser extension CORS (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy authored Jun 22, 2024
2 parents 4846fd5 + 65ad0ef commit ce563b2
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,24 @@ const defaultCorsAllowlist = [
"https://ios.recipesage.com",
"https://localhost",
"capacitor://localhost",
"moz-extension://5d18e06b-c0ef-4566-8c94-8ec47d7776e5",
"moz-extension://*",
"chrome-extension://oepplnnfceidfaaacjpdpobnjkcpgcpo",
];

const hostMatch = (pattern: string, origin: string) => {
if (pattern.endsWith("*")) {
return origin.startsWith(pattern.substring(0, pattern.length - 1));
}

return origin === pattern;
};

const corsOptions = {
origin: (origin, callback) => {
if (process.env.CORS_ALLOWLIST) {
const allowList = process.env.CORS_ALLOWLIST.split(",");
const allowCors = origin && allowList.indexOf(origin) !== -1;
const allowCors =
origin && allowList.some((pattern) => hostMatch(pattern, origin));
callback(null, allowCors);
return;
}
Expand All @@ -64,7 +74,9 @@ const corsOptions = {
return;
}

const allowCors = origin && defaultCorsAllowlist.indexOf(origin) !== -1;
const allowCors =
origin &&
defaultCorsAllowlist.some((pattern) => hostMatch(pattern, origin));
callback(null, allowCors);
},
} satisfies cors.CorsOptions;
Expand Down

0 comments on commit ce563b2

Please sign in to comment.