Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reCAPTCHA improvements #1087

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/toolpad-app/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ServerConfig = {
encryptionKeys: string[];
basicAuthUser?: string;
basicAuthPassword?: string;
recaptchaSecretKey?: string;
} & BasicAuthConfig;

function readConfig(): ServerConfig & typeof sharedConfig {
Expand Down Expand Up @@ -52,6 +53,7 @@ function readConfig(): ServerConfig & typeof sharedConfig {
googleSheetsClientId: process.env.TOOLPAD_DATASOURCE_GOOGLESHEETS_CLIENT_ID,
googleSheetsClientSecret: process.env.TOOLPAD_DATASOURCE_GOOGLESHEETS_CLIENT_SECRET,
externalUrl: process.env.TOOLPAD_EXTERNAL_URL || `http://localhost:${process.env.PORT || 3000}`,
recaptchaSecretKey: process.env.TOOLPAD_RECAPTCHA_SECRET_KEY,
encryptionKeys,
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/toolpad-app/src/server/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { decryptSecret, encryptSecret } from './secrets';
import applyTransform from './applyTransform';
import { excludeFields } from '../utils/prisma';
import { getAppTemplateDom } from './appTemplateDoms/doms';
import { validateRecaptchaToken } from '../utils/recaptcha';
import { validateRecaptchaToken } from './validateRecaptchaToken';
import config from './config';

const SELECT_RELEASE_META = excludeFields(prisma.Prisma.ReleaseScalarFieldEnum, ['snapshot']);
const SELECT_APP_META = excludeFields(prisma.Prisma.AppScalarFieldEnum, ['dom']);
Expand Down Expand Up @@ -197,7 +198,7 @@ export type CreateAppOptions = {
export async function createApp(name: string, opts: CreateAppOptions = {}): Promise<prisma.App> {
const { from } = opts;

const recaptchaSecretKey = process.env.TOOLPAD_RECAPTCHA_SECRET_KEY;
const recaptchaSecretKey = config.recaptchaSecretKey;
if (recaptchaSecretKey) {
const isRecaptchaTokenValid = await validateRecaptchaToken(
recaptchaSecretKey,
Expand Down
22 changes: 22 additions & 0 deletions packages/toolpad-app/src/server/validateRecaptchaToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
const SCORE_THRESHOLD = 0.5;

export const validateRecaptchaToken = async (
secretKey: string,
token: string,
): Promise<boolean> => {
const siteVerifyUrl = new URL(SITE_VERIFY_URL);
siteVerifyUrl.searchParams.set('secret', secretKey);
siteVerifyUrl.searchParams.set('response', token);

const recaptchaResponse = await fetch(siteVerifyUrl, {
method: 'POST',
});

const { success, score } = await recaptchaResponse.json();
if (!success || score < SCORE_THRESHOLD) {
return false;
}

return true;
};
6 changes: 4 additions & 2 deletions packages/toolpad-app/src/toolpad/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { ConfirmDialog } from '../../components/SystemDialogs';
import config from '../../config';
import { AppTemplateId } from '../../types';
import { errorFrom } from '../../utils/errors';
import { getRecaptchaToken } from '../../utils/recaptcha';

export const APP_TEMPLATE_OPTIONS = {
blank: {
Expand Down Expand Up @@ -112,7 +111,10 @@ function CreateAppDialog({ onClose, ...props }: CreateAppDialogProps) {

let recaptchaToken;
if (config.recaptchaSiteKey) {
recaptchaToken = await getRecaptchaToken(config.recaptchaSiteKey);
await new Promise<void>((resolve) => grecaptcha.ready(resolve));
recaptchaToken = await grecaptcha.execute(config.recaptchaSiteKey, {
action: 'submit',
});
}

const appDom = dom.trim() ? JSON.parse(dom) : null;
Expand Down
31 changes: 0 additions & 31 deletions packages/toolpad-app/src/utils/recaptcha.ts

This file was deleted.