Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@amplitude/analytics-browser": "2.6.1",
"@babel/runtime": "7.20.13",
"@babel/runtime-corejs3": "7.20.13",
"@back4app/back4app-settings": "1.6.7",
"@back4app/back4app-settings": "1.6.8",
"@back4app2/react-components": "1.0.0-beta.440.130",
"@back4app2/sdk": "^1.0.0-beta.440.180",
"@monaco-editor/react": "4.7.0",
Expand Down
17 changes: 16 additions & 1 deletion src/dashboard/Settings/Modals/cloneAppModal.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FormNote from 'components/FormNote/FormNote.react';
import Dropdown from 'components/Dropdown/Dropdown.react';
import Option from 'components/Dropdown/Option.react';
import PropTypes from 'prop-types';
import { getCaptchaToken, CREATE_APP_CAPTCHA_ACTION, CAPTCHA_FAILED_ERROR } from 'lib/turnstile';

export const CloneAppModal = ({ context, setParentState }) => {

Expand Down Expand Up @@ -53,10 +54,21 @@ export const CloneAppModal = ({ context, setParentState }) => {
await context.checkStorage();
}

// A token is good once: ask for a new one on every try.
setNote('Checking you are not a robot...');
setNoteColor('blue');

const captchaToken = await getCaptchaToken(CREATE_APP_CAPTCHA_ACTION);
if (!captchaToken) {
setNote('The captcha could not be completed. Please try again.');
setNoteColor('red');
return;
}

setNote('Creating a new parse app...');
setNoteColor('blue');

newApp = await context.createApp(cloneAppName, cloneParseVersion?.version, context.applicationId);
newApp = await context.createApp(cloneAppName, cloneParseVersion?.version, context.applicationId, captchaToken);

if (!newApp || Object.keys(newApp).length <= 0) {
throw new Error();
Expand Down Expand Up @@ -85,6 +97,9 @@ export const CloneAppModal = ({ context, setParentState }) => {
if (e.message) {
error = e.message;
}
if (e.message === CAPTCHA_FAILED_ERROR || e.error === CAPTCHA_FAILED_ERROR) {
error = 'The captcha failed. Please try again.';
}
setNote(error);
setNoteColor('red');
if (newApp && newApp._id) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ParseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,10 @@ export default class ParseApp {
return AJAX.post(path);
}

createApp(appName, parseVersion, originalAppId = null) {
// captchaToken: a one-use Cloudflare Turnstile token (lib/turnstile.js); the API creates no app without one.
createApp(appName, parseVersion, originalAppId = null, captchaToken = null) {
const path = '/create-parse-app';
return AJAX.post(path, { appDescription: '', parseVersion, originalAppId: originalAppId, appId: null, appName, isPublic: false })
return AJAX.post(path, { appDescription: '', parseVersion, originalAppId: originalAppId, appId: null, appName, isPublic: false, 'cf-turnstile-response': captchaToken || '' })
}

initializeDb(appId, parseVersion) {
Expand Down
110 changes: 110 additions & 0 deletions src/lib/turnstile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* global b4aSettings */
/**
* Cloudflare Turnstile token for app creation (action `create-backend-app`), sent as
* `cf-turnstile-response`.
*
* A token is good once, so each call renders a new widget and removes it when it answers. The
* widget shows nothing unless Cloudflare needs the person to do something; then it appears at the
* bottom of the page until it is solved.
*
* The site key comes from b4aSettings.TURNSTILE_SITE_KEY; without one, Cloudflare's test key.
*/

const SCRIPT_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
const TEST_SITE_KEY = '1x00000000000000000000AA';
const SITE_KEY = b4aSettings.TURNSTILE_SITE_KEY || TEST_SITE_KEY;
export const CREATE_APP_CAPTCHA_ACTION = 'create-backend-app';
// What the API answers for a missing or bad token.
export const CAPTCHA_FAILED_ERROR = 'Please fill the captcha correctly';
// Long enough for someone who has to click the widget.
const TIMEOUT_MS = 120000;

let loading = null;
let host = null;

const loadTurnstile = () => {
if (window.turnstile) {
return Promise.resolve(window.turnstile);
}
if (!loading) {
loading = new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = SCRIPT_URL;
script.async = true;
script.onload = () => (window.turnstile ? resolve(window.turnstile) : reject(new Error('Turnstile did not load')));
script.onerror = () => {
loading = null;
script.remove();
reject(new Error('Turnstile did not load'));
};
document.head.appendChild(script);
});
}
return loading;
};

const getHost = () => {
if (!host || !document.body.contains(host)) {
host = document.createElement('div');
host.setAttribute('data-turnstile-captcha', '');
Object.assign(host.style, {
position: 'fixed',
left: '50%',
bottom: '24px',
transform: 'translateX(-50%)',
zIndex: '2147483647',
});
document.body.appendChild(host);
}
return host;
};

/** A one-use Turnstile token for `action`, or null when the captcha could not be loaded or solved. */
export const getCaptchaToken = async (action = CREATE_APP_CAPTCHA_ACTION) => {
if (!SITE_KEY) {
return null;
}
let turnstile;
try {
turnstile = await loadTurnstile();
} catch (e) {
return null;
}
return new Promise((resolve) => {
const holder = document.createElement('div');
getHost().appendChild(holder);
let widgetId = null;
let done = false;
let timer = null;
const finish = (token) => {
if (done) {
return;
}
done = true;
clearTimeout(timer);
try {
if (widgetId !== null) {
turnstile.remove(widgetId);
}
} catch (e) {
// already gone
}
holder.remove();
resolve(token || null);
};
timer = setTimeout(() => finish(null), TIMEOUT_MS);
try {
widgetId = turnstile.render(holder, {
sitekey: SITE_KEY,
action,
appearance: 'interaction-only',
callback: (token) => finish(token),
'error-callback': () => finish(null),
'expired-callback': () => finish(null),
'timeout-callback': () => finish(null),
});
} catch (e) {
finish(null);
}
});
};
Loading