From fbd86a35202bb85bd6904d0c4f3240e860056068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nat=C3=A1lia=20Cond=C3=AA?= Date: Fri, 20 Nov 2026 10:26:20 -0300 Subject: [PATCH 1/3] Send a Turnstile answer when cloning an app The back4app API now creates a Parse app (POST /create-parse-app) only with a Cloudflare Turnstile answer for 'create-backend-app', and the clone flow calls that route. The clone modal gets a fresh token (they are good once) right before creating the app and sends it as cf-turnstile-response. No token: the API is not called. A refused captcha shows "The captcha failed" and the next click gets a new token. TURNSTILE_SITE_KEY (the same widget key as the back4app2 dashboards) reaches the bundle through DefinePlugin; the deploy scripts stop without it. Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 4 + deploy-homolog.sh | 3 +- deploy-homolog2.sh | 3 +- deploy-prod.sh | 3 +- deploy-prod2.sh | 3 +- .../Settings/Modals/cloneAppModal.react.js | 17 ++- src/lib/ParseApp.js | 5 +- src/lib/turnstile.js | 110 ++++++++++++++++++ webpack/base.config.js | 10 ++ 9 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 src/lib/turnstile.js diff --git a/Dockerfile b/Dockerfile index 26eddd4a89..ac6e2ecc5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,10 @@ COPY . /src # Install remaining dev dependencies RUN npm ci +# Cloudflare Turnstile site key of the app creation captcha (webpack/base.config.js): +# docker build --build-arg TURNSTILE_SITE_KEY=... +ARG TURNSTILE_SITE_KEY + # Run all webpack build steps RUN npm run build diff --git a/deploy-homolog.sh b/deploy-homolog.sh index d9cc0d6774..b788c82fe8 100755 --- a/deploy-homolog.sh +++ b/deploy-homolog.sh @@ -1,6 +1,7 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi +if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=34.192.186.60 user=ubuntu @@ -9,4 +10,4 @@ pem=AppContainers.pem branch=$(git symbolic-ref --short HEAD) git='~/bin/git-parse-dashboard' -ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build-homolog'" \ No newline at end of file +ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build-homolog'" \ No newline at end of file diff --git a/deploy-homolog2.sh b/deploy-homolog2.sh index be9267f59c..a16165566e 100755 --- a/deploy-homolog2.sh +++ b/deploy-homolog2.sh @@ -1,6 +1,7 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi +if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=34.192.186.60 user=ubuntu @@ -9,4 +10,4 @@ pem=AppContainers.pem branch=$(git symbolic-ref --short HEAD) git='~/bin/git-parse-dashboard' -ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build-homolog'" \ No newline at end of file +ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build-homolog'" \ No newline at end of file diff --git a/deploy-prod.sh b/deploy-prod.sh index 89a91db1da..f54dd8aac4 100755 --- a/deploy-prod.sh +++ b/deploy-prod.sh @@ -1,6 +1,7 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi +if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=52.0.232.94 user=ubuntu @@ -10,7 +11,7 @@ now=`date '+%Y%m%d%H%M%S'` branch=production1 git='~/bin/git-parse-dashboard' -ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard ~/scm/parse-dashboard-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build'" +ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard ~/scm/parse-dashboard-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build'" # curl -X DELETE "https://api.cloudflare.com/client/v4/zones/69ace06909c83213745231d2c6d0fd27/purge_cache" -H "X-Auth-Email: $CF_EMAIL" -H "X-Auth-Key: $CF_KEY" -H "Content-Type: application/json" --data '{"files":[ # "https://parse-dashboard.back4app.com/bundles/dashboard.bundle.js", diff --git a/deploy-prod2.sh b/deploy-prod2.sh index 9bbf7afc8d..252d455aec 100755 --- a/deploy-prod2.sh +++ b/deploy-prod2.sh @@ -1,6 +1,7 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi +if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=52.0.232.94 user=ubuntu @@ -10,7 +11,7 @@ now=`date '+%Y%m%d%H%M%S'` branch=production2 git='~/bin/git-parse-dashboard' -ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard2 ~/scm/parse-dashboard2-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build'" +ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard2 ~/scm/parse-dashboard2-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build'" # curl -X DELETE "https://api.cloudflare.com/client/v4/zones/69ace06909c83213745231d2c6d0fd27/purge_cache" -H "X-Auth-Email: $CF_EMAIL" -H "X-Auth-Key: $CF_KEY" -H "Content-Type: application/json" --data '{"files":[ # "https://parse-dashboard.back4app.com/bundles/dashboard.bundle.js", diff --git a/src/dashboard/Settings/Modals/cloneAppModal.react.js b/src/dashboard/Settings/Modals/cloneAppModal.react.js index 67f2dfee00..16a671724e 100644 --- a/src/dashboard/Settings/Modals/cloneAppModal.react.js +++ b/src/dashboard/Settings/Modals/cloneAppModal.react.js @@ -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 }) => { @@ -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(); @@ -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) { diff --git a/src/lib/ParseApp.js b/src/lib/ParseApp.js index 74d024d614..259b8ccb07 100644 --- a/src/lib/ParseApp.js +++ b/src/lib/ParseApp.js @@ -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) { diff --git a/src/lib/turnstile.js b/src/lib/turnstile.js new file mode 100644 index 0000000000..8c6de72874 --- /dev/null +++ b/src/lib/turnstile.js @@ -0,0 +1,110 @@ +/** + * Cloudflare Turnstile, the captcha in front of app creation. The API creates an app + * (POST /create-parse-app) only with a Turnstile answer solved on one of our dashboards for the + * 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. Same as the site's signup captcha + * (back4app-site2 lib/signupCaptcha.js) and the back4app2 dashboards' Turnstile component. + * + * The site key is set at build time (TURNSTILE_SITE_KEY, see webpack/base.config.js). + */ + +const SCRIPT_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'; +const SITE_KEY = process.env.TURNSTILE_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); + } + }); +}; diff --git a/webpack/base.config.js b/webpack/base.config.js index 211da24583..1ad1486eb3 100644 --- a/webpack/base.config.js +++ b/webpack/base.config.js @@ -17,6 +17,15 @@ const json = JSON.parse(fs.readFileSync('package.json', 'utf8')); const version = json.version; const settings = require('@back4app/back4app-settings'); const BACK4APP_API_PATH = process.env.ENVIRONMENT == 'local' ? settings.BACK4APP_API_PATH : undefined +// Cloudflare Turnstile site key for the app creation captcha (src/lib/turnstile.js). The same widget +// as the back4app2 dashboards (their REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY): its hostnames +// must include this dashboard's. Set TURNSTILE_SITE_KEY when building (deploy*.sh pass it); a local +// or dev build falls back to Cloudflare's always-pass test key. +const TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY + || (['production', 'homolog'].includes(process.env.NODE_ENV) ? '' : '1x00000000000000000000AA'); +if (!TURNSTILE_SITE_KEY) { + console.warn('TURNSTILE_SITE_KEY is not set: the dashboard will not be able to create or clone apps.'); +} module.exports = { context: path.join(__dirname, '../src'), @@ -104,6 +113,7 @@ module.exports = { 'version' : JSON.stringify(version), 'BACKEND_URL': JSON.stringify(BACK4APP_API_PATH), 'BUILD_TIMESTAMP': JSON.stringify(new Date().toISOString()), + 'TURNSTILE_SITE_KEY': JSON.stringify(TURNSTILE_SITE_KEY), }, b4aSettings: JSON.stringify(settings) }), From c6f90d9965c78ab979eb2781e2414535f6fa1808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nat=C3=A1lia=20Cond=C3=AA?= Date: Fri, 20 Nov 2026 10:40:41 -0300 Subject: [PATCH 2/3] Read the Turnstile site key from back4app-settings 1.6.8 @back4app/back4app-settings 1.6.8 carries TURNSTILE_SITE_KEY for production and homolog, and the bundle already embeds those settings (b4aSettings). The key now comes from there, so a deploy needs no extra environment variable: the env-var plumbing in webpack, the Dockerfile and the deploy scripts is dropped. dev.json has no key, so a local build uses Cloudflare's always-pass test key. Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 4 ---- deploy-homolog.sh | 3 +-- deploy-homolog2.sh | 3 +-- deploy-prod.sh | 3 +-- deploy-prod2.sh | 3 +-- package.json | 2 +- src/lib/turnstile.js | 8 ++++++-- webpack/base.config.js | 10 ---------- 8 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac6e2ecc5c..26eddd4a89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,10 +21,6 @@ COPY . /src # Install remaining dev dependencies RUN npm ci -# Cloudflare Turnstile site key of the app creation captcha (webpack/base.config.js): -# docker build --build-arg TURNSTILE_SITE_KEY=... -ARG TURNSTILE_SITE_KEY - # Run all webpack build steps RUN npm run build diff --git a/deploy-homolog.sh b/deploy-homolog.sh index b788c82fe8..d9cc0d6774 100755 --- a/deploy-homolog.sh +++ b/deploy-homolog.sh @@ -1,7 +1,6 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi -if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=34.192.186.60 user=ubuntu @@ -10,4 +9,4 @@ pem=AppContainers.pem branch=$(git symbolic-ref --short HEAD) git='~/bin/git-parse-dashboard' -ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build-homolog'" \ No newline at end of file +ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build-homolog'" \ No newline at end of file diff --git a/deploy-homolog2.sh b/deploy-homolog2.sh index a16165566e..be9267f59c 100755 --- a/deploy-homolog2.sh +++ b/deploy-homolog2.sh @@ -1,7 +1,6 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi -if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=34.192.186.60 user=ubuntu @@ -10,4 +9,4 @@ pem=AppContainers.pem branch=$(git symbolic-ref --short HEAD) git='~/bin/git-parse-dashboard' -ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build-homolog'" \ No newline at end of file +ssh -t -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c '. ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard-homolog.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build-homolog'" \ No newline at end of file diff --git a/deploy-prod.sh b/deploy-prod.sh index f54dd8aac4..89a91db1da 100755 --- a/deploy-prod.sh +++ b/deploy-prod.sh @@ -1,7 +1,6 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi -if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=52.0.232.94 user=ubuntu @@ -11,7 +10,7 @@ now=`date '+%Y%m%d%H%M%S'` branch=production1 git='~/bin/git-parse-dashboard' -ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard ~/scm/parse-dashboard-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build'" +ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard ~/scm/parse-dashboard-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build'" # curl -X DELETE "https://api.cloudflare.com/client/v4/zones/69ace06909c83213745231d2c6d0fd27/purge_cache" -H "X-Auth-Email: $CF_EMAIL" -H "X-Auth-Key: $CF_KEY" -H "Content-Type: application/json" --data '{"files":[ # "https://parse-dashboard.back4app.com/bundles/dashboard.bundle.js", diff --git a/deploy-prod2.sh b/deploy-prod2.sh index 252d455aec..9bbf7afc8d 100755 --- a/deploy-prod2.sh +++ b/deploy-prod2.sh @@ -1,7 +1,6 @@ #!/bin/bash if [[ ! $b4a_certs_path ]]; then echo 'Set b4a_certs_path environment variable, please!' && exit; fi -if [[ ! $TURNSTILE_SITE_KEY ]]; then echo 'Set TURNSTILE_SITE_KEY environment variable (the back4app2 dashboards REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY), please!' && exit; fi host=52.0.232.94 user=ubuntu @@ -11,7 +10,7 @@ now=`date '+%Y%m%d%H%M%S'` branch=production2 git='~/bin/git-parse-dashboard' -ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard2 ~/scm/parse-dashboard2-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && TURNSTILE_SITE_KEY=$TURNSTILE_SITE_KEY npm run build'" +ssh -t -o IdentitiesOnly=yes -i $b4a_certs_path/$pem $user@$host "sudo su back4app -c 'cp -r ~/scm/parse-dashboard2 ~/scm/parse-dashboard2-$now && . ~/.nvm/nvm.sh && nvm use 14 && cd ~/scm/parse-dashboard2 && rm -rf node_modules && $git reset --hard && $git remote update && $git checkout $branch && $git merge origin/$branch && npm install --production=false && sed -i \"s/http:\/\/localhost:4000\/parseapi/https:\/\/dashboard.back4app.com\/parseapi/\" node_modules/parse/lib/browser/settings.js && npm run build'" # curl -X DELETE "https://api.cloudflare.com/client/v4/zones/69ace06909c83213745231d2c6d0fd27/purge_cache" -H "X-Auth-Email: $CF_EMAIL" -H "X-Auth-Key: $CF_KEY" -H "Content-Type: application/json" --data '{"files":[ # "https://parse-dashboard.back4app.com/bundles/dashboard.bundle.js", diff --git a/package.json b/package.json index 446bcea769..66c22551a6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/turnstile.js b/src/lib/turnstile.js index 8c6de72874..88725c3f0f 100644 --- a/src/lib/turnstile.js +++ b/src/lib/turnstile.js @@ -1,3 +1,4 @@ +/* global b4aSettings */ /** * Cloudflare Turnstile, the captcha in front of app creation. The API creates an app * (POST /create-parse-app) only with a Turnstile answer solved on one of our dashboards for the @@ -8,11 +9,14 @@ * bottom of the page until it is solved. Same as the site's signup captcha * (back4app-site2 lib/signupCaptcha.js) and the back4app2 dashboards' Turnstile component. * - * The site key is set at build time (TURNSTILE_SITE_KEY, see webpack/base.config.js). + * The site key comes from @back4app/back4app-settings (TURNSTILE_SITE_KEY in production.json and + * homolog.json, the same widget as the site and the back4app2 dashboards). dev.json has none, so a + * local build uses Cloudflare's always-pass test key. */ const SCRIPT_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'; -const SITE_KEY = process.env.TURNSTILE_SITE_KEY || ''; +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'; diff --git a/webpack/base.config.js b/webpack/base.config.js index 1ad1486eb3..211da24583 100644 --- a/webpack/base.config.js +++ b/webpack/base.config.js @@ -17,15 +17,6 @@ const json = JSON.parse(fs.readFileSync('package.json', 'utf8')); const version = json.version; const settings = require('@back4app/back4app-settings'); const BACK4APP_API_PATH = process.env.ENVIRONMENT == 'local' ? settings.BACK4APP_API_PATH : undefined -// Cloudflare Turnstile site key for the app creation captcha (src/lib/turnstile.js). The same widget -// as the back4app2 dashboards (their REACT_APP_BACK4APP2_DASHBOARD_TURNSTILE_SITE_KEY): its hostnames -// must include this dashboard's. Set TURNSTILE_SITE_KEY when building (deploy*.sh pass it); a local -// or dev build falls back to Cloudflare's always-pass test key. -const TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY - || (['production', 'homolog'].includes(process.env.NODE_ENV) ? '' : '1x00000000000000000000AA'); -if (!TURNSTILE_SITE_KEY) { - console.warn('TURNSTILE_SITE_KEY is not set: the dashboard will not be able to create or clone apps.'); -} module.exports = { context: path.join(__dirname, '../src'), @@ -113,7 +104,6 @@ module.exports = { 'version' : JSON.stringify(version), 'BACKEND_URL': JSON.stringify(BACK4APP_API_PATH), 'BUILD_TIMESTAMP': JSON.stringify(new Date().toISOString()), - 'TURNSTILE_SITE_KEY': JSON.stringify(TURNSTILE_SITE_KEY), }, b4aSettings: JSON.stringify(settings) }), From 91c59b9bd23eeea30f7cb75784d494098e4c3f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nat=C3=A1lia=20Cond=C3=AA?= Date: Fri, 20 Nov 2026 10:49:26 -0300 Subject: [PATCH 3/3] Simplify Turnstile helper comments --- src/lib/turnstile.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib/turnstile.js b/src/lib/turnstile.js index 88725c3f0f..ecfdfa5d7a 100644 --- a/src/lib/turnstile.js +++ b/src/lib/turnstile.js @@ -1,17 +1,13 @@ /* global b4aSettings */ /** - * Cloudflare Turnstile, the captcha in front of app creation. The API creates an app - * (POST /create-parse-app) only with a Turnstile answer solved on one of our dashboards for the - * action `create-backend-app`, sent as `cf-turnstile-response`. + * 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. Same as the site's signup captcha - * (back4app-site2 lib/signupCaptcha.js) and the back4app2 dashboards' Turnstile component. + * bottom of the page until it is solved. * - * The site key comes from @back4app/back4app-settings (TURNSTILE_SITE_KEY in production.json and - * homolog.json, the same widget as the site and the back4app2 dashboards). dev.json has none, so a - * local build uses Cloudflare's always-pass test key. + * 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';