Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add captcha handling in report submission endpoint
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent a1c12d4 commit 5581730
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion functions/api/reports/submit.ts
Expand Up @@ -10,7 +10,31 @@ function errorResponse(error: string, status: number): Response {
}

export async function onRequestPost(context: RequestContext) {
const { filename, filesize, usernames } = context.data.body;
const { filename, filesize, turnstileResponse, usernames } =
context.data.body;

if (!context.data.current_user) {
if (typeof turnstileResponse !== "string")
return errorResponse("You must complete the captcha", 401);

const turnstileAPIResponse = await fetch(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
{
body: JSON.stringify({
response: turnstileResponse,
secret: context.env.TURNSTILE_SECRETKEY,
}),
headers: {
"content-type": "application/json",
},
method: "POST",
}
);

const { success }: { success: boolean } = await turnstileAPIResponse.json();

if (!success) return errorResponse("Captcha test failed", 403);
}

if (!Array.isArray(usernames))
return errorResponse("Usernames must be type of array", 400);
Expand Down

0 comments on commit 5581730

Please sign in to comment.