Skip to content

Commit b74e0f0

Browse files
authored
ENG-758 Edge function stopped working, now needs CORS handling (#353)
* Add cors handling to test function * better check for vercel preview
1 parent e5c79f1 commit b74e0f0

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

apps/website/app/utils/llm/cors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextRequest } from "next/server";
33
const allowedOrigins = ["https://roamresearch.com", "http://localhost:3000"];
44

55
const isVercelPreviewUrl = (origin: string): boolean =>
6-
origin.includes(".vercel.app") || origin.includes("discourse-graph");
6+
/^https:\/\/.*-discourse-graph-[a-z0-9]+\.vercel\.app$/.test(origin);
77

88
const isAllowedOrigin = (origin: string): boolean =>
99
allowedOrigins.some((allowed) => origin.startsWith(allowed)) ||

package-lock.json

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

packages/database/supabase/functions/create-space/index.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,34 @@ const processAndGetOrCreateSpace = async (
181181
return result;
182182
};
183183

184+
// The following lines are duplicated from apps/website/app/utils/llm/cors.ts
185+
const allowedOrigins = ["https://roamresearch.com", "http://localhost:3000"];
186+
187+
const isVercelPreviewUrl = (origin: string): boolean =>
188+
/^https:\/\/.*-discourse-graph-[a-z0-9]+\.vercel\.app$/.test(origin)
189+
190+
const isAllowedOrigin = (origin: string): boolean =>
191+
allowedOrigins.some((allowed) => origin.startsWith(allowed)) ||
192+
isVercelPreviewUrl(origin);
193+
184194
// @ts-ignore Deno is not visible to the IDE
185195
Deno.serve(async (req) => {
196+
const origin = req.headers.get("origin");
197+
const originIsAllowed = origin && isAllowedOrigin(origin);
198+
if (req.method === "OPTIONS") {
199+
return new Response(null, {
200+
status: 204,
201+
headers: {
202+
...(originIsAllowed ? { "Access-Control-Allow-Origin": origin } : {}),
203+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
204+
"Access-Control-Allow-Headers":
205+
"Content-Type, Authorization, x-vercel-protection-bypass, x-client-info, apikey",
206+
"Access-Control-Max-Age": "86400",
207+
},
208+
});
209+
}
210+
186211
const input = await req.json();
187-
// TODO: We should check whether the request comes from a vetted source, like
188-
// the roam or obsidian plugin. A combination of CSRF, headers, etc.
189212
// @ts-ignore Deno is not visible to the IDE
190213
const url = Deno.env.get("SUPABASE_URL");
191214
// @ts-ignore Deno is not visible to the IDE
@@ -207,9 +230,20 @@ Deno.serve(async (req) => {
207230
});
208231
}
209232

210-
return new Response(JSON.stringify(data), {
233+
const res = new Response(JSON.stringify(data), {
211234
headers: { "Content-Type": "application/json" },
212235
});
236+
237+
if (originIsAllowed) {
238+
res.headers.set("Access-Control-Allow-Origin", origin as string);
239+
res.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
240+
res.headers.set(
241+
"Access-Control-Allow-Headers",
242+
"Content-Type, Authorization, x-vercel-protection-bypass, x-client-info, apikey",
243+
);
244+
}
245+
246+
return res;
213247
});
214248

215249
/* To invoke locally:

packages/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"typescript": "5.5.4"
3636
},
3737
"dependencies": {
38+
"@supabase/functions-js": "^2.4.5",
3839
"@supabase/supabase-js": "^2.52.0",
3940
"class-variance-authority": "^0.7.1",
4041
"clsx": "^2.1.1",

0 commit comments

Comments
 (0)