feat: checking referrer request

This and CORS should at least filter out most potential errors. Plus
some cloudflare protections should be OK.

Could even add captcha
This commit is contained in:
2025-11-15 11:51:00 +00:00
parent a31d81dd3f
commit f84ec38af3
2 changed files with 17 additions and 4 deletions

View File

@ -14,6 +14,7 @@ const allowCors = async (_: Request): Promise<Response> => {
};
type Handler = (req: Request) => Promise<Response>;
type Middleware = (fn: Handler) => Handler;
const withCors = (fn: Handler): Handler => {
return async (req) => {
@ -57,14 +58,26 @@ const withLogger = (fn: Handler): Handler => {
};
};
const withFrontendReferrer: Middleware = (fn) => {
return async (req) => {
const referrer = req.headers.get("referrer");
if (referrer !== ENV.FRONTEND_URL) {
return new Response(undefined, { status: 403 });
}
return fn(req);
};
};
const server = Bun.serve({
port: ENV.PORT,
routes: {
"/health": new Response("alive!"),
"/sign": {
GET: withLogger(withCors(getPetitions)),
POST: withLogger(withCors(signPetition)),
OPTIONS: withLogger(allowCors),
GET: withFrontendReferrer(withLogger(withCors(getPetitions))),
POST: withFrontendReferrer(withLogger(withCors(signPetition))),
OPTIONS: withFrontendReferrer(withLogger(allowCors)),
},
},
});

View File

@ -12,7 +12,7 @@ const signedPetitionSignatures = z.array(signedPetitionWithParsedDate);
export const getSignatures = async (): Promise<
z.infer<typeof signedPetitionSignatures>
> => {
const res = await fetch(`${backendUrl}/sign`);
const res = await fetch(`${backendUrl}/sign`, { referrer: location.origin });
const body = await res.json();
const validatedBody = signedPetitionSignatures.parse(body);