Skip to content

Commit 7c6adea

Browse files
authored
CORS Origin : Regex Support (#378)
* santinized origins config * fixed test case with exporting func
1 parent ee44fbd commit 7c6adea

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

src/server/middleware/cors/cors.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,32 @@ const defaultOptions = {
102102
strictPreflight: true,
103103
};
104104

105+
export const sanitizeOrigin = (data: string): string | RegExp => {
106+
if (data.startsWith("/") && data.endsWith("/")) {
107+
return new RegExp(data.slice(1, -1));
108+
}
109+
110+
if (data.startsWith("*.")) {
111+
const regex = data.replace("*.", ".*.");
112+
return new RegExp(regex);
113+
}
114+
115+
if (data.includes("thirdweb-preview.com")) {
116+
return new RegExp(/^https?:\/\/.*\.thirdweb-preview\.com$/);
117+
}
118+
if (data.includes("thirdweb-dev.com")) {
119+
return new RegExp(/^https?:\/\/.*\.thirdweb-dev\.com$/);
120+
}
121+
122+
// Remove trailing slashes.
123+
// The origin header does not include a trailing slash.
124+
if (data.endsWith("/")) {
125+
return data.slice(0, -1);
126+
}
127+
128+
return data;
129+
};
130+
105131
export const fastifyCors = async (
106132
fastify: FastifyInstance,
107133
req: FastifyRequest,
@@ -112,7 +138,7 @@ export const fastifyCors = async (
112138
const config = await getConfig();
113139

114140
const originArray = config.accessControlAllowOrigin.split(",") as string[];
115-
opts.origin = originArray;
141+
opts.origin = originArray.map(sanitizeOrigin);
116142

117143
let hideOptionsRoute = true;
118144
if (opts.hideOptionsRoute !== undefined) {

src/server/middleware/cors/index.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,3 @@ export const withCors = async (server: FastifyInstance) => {
1414
);
1515
});
1616
};
17-
18-
export const sanitizeOrigin = (data: string): string | RegExp => {
19-
if (data.startsWith("/") && data.endsWith("/")) {
20-
return new RegExp(data.slice(1, -1));
21-
}
22-
23-
if (data.startsWith("*.")) {
24-
const regex = data.replace("*.", ".*.");
25-
return new RegExp(regex);
26-
}
27-
28-
if (data.includes("thirdweb-preview.com")) {
29-
return new RegExp(/^https?:\/\/.*\.thirdweb-preview\.com$/);
30-
}
31-
if (data.includes("thirdweb-dev.com")) {
32-
return new RegExp(/^https?:\/\/.*\.thirdweb-dev\.com$/);
33-
}
34-
35-
// Remove trailing slashes.
36-
// The origin header does not include a trailing slash.
37-
if (data.endsWith("/")) {
38-
return data.slice(0, -1);
39-
}
40-
41-
return data;
42-
};

src/server/routes/configuration/cors/remove.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function removeUrlToCorsConfiguration(fastify: FastifyInstance) {
4848
);
4949
if (containsMandatoryUrl) {
5050
throw new Error(
51-
`Cannot remove mandatory URLs: ${mandatoryAllowedCorsUrls.join(",")}`,
51+
`Cannot remove URLs: ${mandatoryAllowedCorsUrls.join(",")}`,
5252
);
5353
}
5454

src/tests/cors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sanitizeOrigin } from "../server/middleware/cors";
1+
import { sanitizeOrigin } from "../server/middleware/cors/cors";
22

33
describe("sanitizeOrigin", () => {
44
it("with leading and trailing slashes", () => {

0 commit comments

Comments
 (0)