diff --git a/assets/js/backend/create-form.js b/assets/js/backend/create-form.js
index cde9793f..00d857b5 100644
--- a/assets/js/backend/create-form.js
+++ b/assets/js/backend/create-form.js
@@ -937,6 +937,7 @@
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
var params = {
action: 'super_get_element_builder_html',
+ nonce: super_create_form_i18n.admin_nonce,
tag: obj.$el.data('shortcode'),
group: obj.$el.data('group'),
predefined: $predefined,
@@ -957,6 +958,14 @@
});
};
+ // Returns true (and informs the user) when the server or a firewall rejected a builder request
+ SUPER.builder_request_rejected = function (xhr) {
+ var body = (typeof xhr.responseText === 'string' ? xhr.responseText.trim() : '');
+ if (xhr.status == 200 && body !== '-1' && body !== '0') return false;
+ alert(super_create_form_i18n.request_rejected.replace('%s', xhr.status));
+ return true;
+ };
+
SUPER.save_form = function ($this, $method, $button, $initial_i18n, callback, updatingRawFormCode) {
var i,ii,
form = document.querySelector('.super-preview-elements'),
@@ -1008,6 +1017,13 @@
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
+ if (SUPER.builder_request_rejected(this)) {
+ $('.super-create-form .super-actions .super-save').html('Save');
+ if ($method == 1) $('.super-create-form .super-actions .super-preview:eq(3)').html('Preview');
+ if ($method == 3) $('.super-preview-elements, .super-form-settings').removeClass('super-loading');
+ if ($method == 2 && typeof callback === "function") callback();
+ return false;
+ }
// Success:
if (this.status == 200) {
$('.super-create-form .super-actions .super-save').html('Save');
@@ -1110,6 +1126,12 @@
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
+ if (SUPER.builder_request_rejected(this)) {
+ $('.super-live-preview').removeClass('super-loading').css('display', 'none');
+ $('.super-tabs-content').css('display', '');
+ $this.html('Preview').removeClass('super-active');
+ return false;
+ }
// Success:
if (this.status == 200) {
$('.super-live-preview').removeClass('super-loading');
@@ -1133,6 +1155,7 @@
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
var params = {
action: 'super_load_preview',
+ nonce: super_create_form_i18n.admin_nonce,
form_id: $form_id
};
params = $.param(params);
@@ -1743,6 +1766,7 @@
url: ajaxurl,
data: {
action: 'super_switch_language',
+ nonce: super_create_form_i18n.admin_nonce,
form_id: $('.super-create-form input[name="form_id"]').val(),
i18n: $i18n
},
@@ -1949,6 +1973,7 @@
url: ajaxurl,
data: {
action: 'super_undo_redo',
+ nonce: super_create_form_i18n.admin_nonce,
form_id: $('.super-create-form input[name="form_id"]').val(),
elements: JSON.stringify($history[$index])
},
@@ -2545,6 +2570,7 @@
}
var params = {
action: 'super_get_element_builder_html',
+ nonce: super_create_form_i18n.admin_nonce,
tag: $tag,
group: $group,
builder: ($tag == 'tabs' ? $builder : 0),
@@ -2738,6 +2764,7 @@
}
var params = {
action: 'super_load_element_settings',
+ nonce: super_create_form_i18n.admin_nonce,
form_id: $('.super-create-form input[name="form_id"]').val(),
tag: $tag,
group: $group,
diff --git a/docs/changelog.md b/docs/changelog.md
index 52275d06..bf444f5d 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -10,6 +10,8 @@
## Unreleased
+- **Security fix:** The form builder's preview, language switch, undo/redo, element settings and element HTML requests now require an administrator and a valid security token, so other logged-in users can no longer load form markup or settings.
+- **Fix:** When the builder's save or preview request is rejected (for example after the session expired or a security plugin blocked it), the button is restored and a message asks you to reload the page instead of staying on "Saving..." forever.
- **Fix:** PDF Generator font data (Cyrillic and Arabic) is now attached to the Super Forms common script, so it still loads when a JavaScript optimization plugin reorders or delays scripts.
- **Fix:** PDF Generator "Enable debug mode" now only downloads the PDF and no longer submits the form, so no entry is saved and no e-mails are sent.
- **Fix:** When the PDF cannot be created, a message is shown and the visitor can close it and try again, instead of the form staying on "Generating PDF file..." forever. Nothing is submitted in that case.
diff --git a/includes/class-ajax.php b/includes/class-ajax.php
index 5cb683d5..39373923 100644
--- a/includes/class-ajax.php
+++ b/includes/class-ajax.php
@@ -633,6 +633,8 @@ public static function language_switcher() {
* @since 4.7.0
*/
public static function switch_language() {
+ self::authorize_admin_ajax_request();
+ // phpcs:ignore WordPress.Security.NonceVerification.Missing -- guarded by SUPER_Ajax::authorize_admin_ajax_request() (manage_options + check_ajax_referer('super_admin_ajax'))
$form_id = absint($_POST['form_id']);
// Retrieve all settings with the correct default values
$settings = SUPER_Common::get_form_settings($form_id);
@@ -849,6 +851,8 @@ public static function print_custom_html() {
* @since 3.8.0
*/
public static function undo_redo() {
+ self::authorize_admin_ajax_request();
+ // phpcs:ignore WordPress.Security.NonceVerification.Missing -- guarded by SUPER_Ajax::authorize_admin_ajax_request() (manage_options + check_ajax_referer('super_admin_ajax'))
$form_id = absint($_POST['form_id']);
$elements = $_POST['elements'];
$shortcodes = SUPER_Shortcodes::shortcodes();
@@ -2983,7 +2987,10 @@ public static function import_global_settings() {
* @since 1.0.0
*/
public static function load_preview() {
- $form_id = absint( $_POST['form_id'] );
+ self::authorize_admin_ajax_request();
+ // phpcs:ignore WordPress.Security.NonceVerification.Missing -- guarded by SUPER_Ajax::authorize_admin_ajax_request() (manage_options + check_ajax_referer('super_admin_ajax'))
+ $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0;
+ self::require_admin_form( $form_id );
echo SUPER_Shortcodes::super_form_func( array( 'id'=>$form_id ) );
die();
}
@@ -3300,6 +3307,10 @@ public static function loop_over_element_setting_fields($fields, $data, $shortco
* @since 1.0.0
*/
public static function load_element_settings( $tag=null, $group=null, $data=null ) {
+ // Only the builder AJAX request is guarded; internal PHP calls pass their own arguments
+ if( doing_action( 'wp_ajax_super_load_element_settings' ) ) {
+ self::authorize_admin_ajax_request();
+ }
if($tag==null) $tag = $_POST['tag'];
if($group==null) $group = $_POST['group'];
if($data==null) $data = $_POST['data'];
@@ -3448,6 +3459,10 @@ public static function load_element_settings( $tag=null, $group=null, $data=null
* @since 1.0.0
*/
public static function get_element_builder_html( $tag=null, $group=null, $inner=null, $data=null, $method=1 ) {
+ // Only the builder AJAX request is guarded; internal PHP calls pass their own arguments
+ if( doing_action( 'wp_ajax_super_get_element_builder_html' ) ) {
+ self::authorize_admin_ajax_request();
+ }
$i18n = (isset($_POST['i18n']) ? $_POST['i18n'] : '');
$form_id = 0;
if( isset( $_POST['form_id'] ) ) {
diff --git a/super-forms.php b/super-forms.php
index 8b91feac..f7d54a1f 100644
--- a/super-forms.php
+++ b/super-forms.php
@@ -3622,6 +3622,8 @@ public static function get_scripts() {
'localize'=> array(
'save_form_nonce' => wp_create_nonce( 'super_save_form' ),
'admin_nonce' => wp_create_nonce( 'super_admin_ajax' ),
+ /* translators: %s: HTTP status code of the rejected request */
+ 'request_rejected' => esc_html__( 'The request was rejected (HTTP %s). Your session may have expired or a security plugin blocked it. Please reload the page and try again.', 'super-forms' ),
'not_editing_an_element' => sprintf( esc_html__( 'You are currently not editing an element.%sEdit any alement by clicking the %s icon.', 'super-forms' ), '
', '' ),
'no_backups_found' => esc_html__( 'No backups found...', 'super-forms' ),
'confirm_reset' => esc_html__( 'Are you sure you want to reset all the form settings according to your current global settings?', 'super-forms' ),
diff --git a/tests/test-security-builder-ajax.php b/tests/test-security-builder-ajax.php
new file mode 100644
index 00000000..e131ddbb
--- /dev/null
+++ b/tests/test-security-builder-ajax.php
@@ -0,0 +1,257 @@
+marker = 'builder_ajax_marker_' . str_replace( '-', '', wp_generate_uuid4() );
+ }
+
+ private function elements() {
+ return array(
+ array(
+ 'tag' => 'text',
+ 'group' => 'form_elements',
+ 'data' => array(
+ 'name' => $this->marker,
+ 'email' => 'Marker:',
+ ),
+ 'inner' => array(),
+ ),
+ );
+ }
+
+ private function set_actor( $role ) {
+ $user_id = self::factory()->user->create( array( 'role' => $role ) );
+ wp_set_current_user( $user_id );
+ return $user_id;
+ }
+
+ private function request_for( $action, $form_id ) {
+ $request = array(
+ 'action' => 'super_' . $action,
+ 'form_id' => $form_id,
+ );
+ if ( 'switch_language' === $action ) {
+ $request['i18n'] = '';
+ } elseif ( 'undo_redo' === $action ) {
+ $request['elements'] = wp_json_encode( $this->elements() );
+ } elseif ( 'load_element_settings' === $action || 'get_element_builder_html' === $action ) {
+ $request['tag'] = 'text';
+ $request['group'] = 'form_elements';
+ $request['data'] = $this->text_element_settings_data();
+ $request['translating'] = 'false';
+ $request['i18n'] = '';
+ }
+ return $request;
+ }
+
+ /**
+ * The element data the builder posts when a text field is edited: the element's setting
+ * values. Settings without a 'default' in their definition (such as the "type" select)
+ * only render when the element data supplies their value
+ * (SUPER_Ajax::loop_over_element_setting_fields() copies it into 'default').
+ */
+ private function text_element_settings_data() {
+ $shortcodes = SUPER_Shortcodes::shortcodes( false, false, false );
+ $data = array();
+ foreach ( $shortcodes['form_elements']['shortcodes']['text']['atts'] as $tab ) {
+ $groups = isset( $tab['fields'] ) ? array( $tab ) : array_filter( $tab, 'is_array' );
+ foreach ( $groups as $group ) {
+ if ( empty( $group['fields'] ) ) {
+ continue;
+ }
+ foreach ( $group['fields'] as $key => $field ) {
+ if ( isset( $field['default'] ) ) {
+ $data[ $key ] = $field['default'];
+ } elseif ( isset( $field['type'] ) && 'select' === $field['type'] && ! empty( $field['values'] ) ) {
+ $data[ $key ] = (string) key( $field['values'] );
+ }
+ }
+ }
+ }
+ $data['name'] = $this->marker;
+ return $data;
+ }
+
+ private function apply_nonce( $request, $mode ) {
+ unset( $request['nonce'] );
+ if ( 'valid' === $mode ) {
+ $request['nonce'] = wp_create_nonce( 'super_admin_ajax' );
+ } elseif ( 'wrong' === $mode ) {
+ $request['nonce'] = wp_create_nonce( 'super_save_form' );
+ }
+ $_POST = $request;
+ $_REQUEST = $request;
+ }
+
+ /**
+ * Run the wp_ajax_super_* action out of process, because every handler ends in a raw die().
+ * A wp_die() rejection (check_ajax_referer() or a capability check) is reported as
+ * self::REJECTED with exit status 97; a handler that rendered and died exits with 0.
+ */
+ private function run_builder_ajax( $action ) {
+ return $this->run_dying_handler(
+ static function () use ( $action ) {
+ $die_handler = static function () {
+ return static function ( $message = '' ) {
+ throw new Super_Forms_Builder_Ajax_Rejected_Exception( is_scalar( $message ) ? (string) $message : 'non-scalar' );
+ };
+ };
+ add_filter( 'wp_doing_ajax', '__return_true' );
+ add_filter( 'wp_die_ajax_handler', $die_handler );
+ add_filter( 'wp_die_handler', $die_handler );
+ do_action( 'wp_ajax_super_' . $action );
+ },
+ false
+ );
+ }
+
+ private function success_marker( $action ) {
+ return 'load_element_settings' === $action ? 'super-update-element' : $this->marker;
+ }
+
+ public static function builder_actions() {
+ return array(
+ 'load_preview' => array( 'load_preview' ),
+ 'switch_language' => array( 'switch_language' ),
+ 'undo_redo' => array( 'undo_redo' ),
+ 'load_element_settings' => array( 'load_element_settings' ),
+ 'get_element_builder_html' => array( 'get_element_builder_html' ),
+ );
+ }
+
+ public static function rejected_request_provider() {
+ $actors = array(
+ 'subscriber with valid nonce' => array( 'subscriber', 'valid' ),
+ 'editor with valid nonce' => array( 'editor', 'valid' ),
+ 'administrator without nonce' => array( 'administrator', 'missing' ),
+ 'administrator with wrong nonce' => array( 'administrator', 'wrong' ),
+ );
+ $cases = array();
+ foreach ( array_keys( self::builder_actions() ) as $action ) {
+ foreach ( $actors as $label => $actor ) {
+ $cases[ $action . ': ' . $label ] = array( $action, $actor[0], $actor[1] );
+ }
+ }
+ return $cases;
+ }
+
+ /**
+ * @dataProvider rejected_request_provider
+ */
+ public function test_builder_ajax_rejects_without_administrator_and_admin_nonce( $action, $role, $nonce_mode ) {
+ $form_id = $this->create_form( 'draft', $this->elements(), array() );
+ $this->set_actor( $role );
+ $this->apply_nonce( $this->request_for( $action, $form_id ), $nonce_mode );
+
+ $result = $this->run_builder_ajax( $action );
+
+ $this->assertTrue( $result['exited'] );
+ $this->assertSame( 97, $result['status'], $result['output'] );
+ $this->assertSame( self::REJECTED, trim( $result['output'] ) );
+ $this->assertStringNotContainsString( $this->marker, $result['output'] );
+ $this->assertStringNotContainsString( 'super-update-element', $result['output'] );
+ $this->assertStringNotContainsString( '"settings"', $result['output'] );
+ }
+
+ /**
+ * @dataProvider builder_actions
+ */
+ public function test_administrator_with_admin_nonce_can_use_builder_ajax( $action ) {
+ $form_id = $this->create_form( 'publish', $this->elements(), array() );
+ $this->set_actor( 'administrator' );
+ $this->apply_nonce( $this->request_for( $action, $form_id ), 'valid' );
+
+ $result = $this->run_builder_ajax( $action );
+
+ $this->assertTrue( $result['exited'] );
+ $this->assertSame( 0, $result['status'], $result['output'] );
+ $this->assertStringNotContainsString( self::REJECTED, $result['output'] );
+ $this->assertStringContainsString( $this->success_marker( $action ), $result['output'] );
+ if ( 'switch_language' === $action ) {
+ $payload = json_decode( $result['output'], true );
+ $this->assertIsArray( $payload, $result['output'] );
+ $this->assertArrayHasKey( 'settings', $payload );
+ $this->assertStringContainsString( $this->marker, $payload['elements'] );
+ }
+ }
+
+ public function test_preview_rejects_a_form_backup_for_an_administrator() {
+ $form_id = $this->create_form( 'publish', array(), array() );
+ $backup_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'super_form',
+ 'post_status' => 'backup',
+ 'post_parent' => $form_id,
+ )
+ );
+ update_post_meta( $backup_id, '_super_elements', $this->elements() );
+ update_post_meta( $backup_id, '_super_form_settings', array() );
+ $this->set_actor( 'administrator' );
+ $this->apply_nonce( $this->request_for( 'load_preview', $backup_id ), 'valid' );
+
+ $result = $this->run_builder_ajax( 'load_preview' );
+
+ $this->assertTrue( $result['exited'] );
+ $this->assertSame( 97, $result['status'], $result['output'] );
+ $this->assertSame( self::REJECTED, trim( $result['output'] ) );
+ $this->assertStringNotContainsString( $this->marker, $result['output'] );
+ }
+
+ public function test_internal_get_element_builder_html_call_still_returns_html() {
+ $this->set_actor( 'subscriber' );
+ $html = SUPER_Ajax::get_element_builder_html( 'text', 'form_elements', array(), array( 'name' => $this->marker ), 0 );
+ $this->assertIsString( $html );
+ $this->assertStringContainsString( $this->marker, $html );
+ }
+
+ public function test_internal_load_element_settings_call_still_renders_settings() {
+ $this->set_actor( 'subscriber' );
+ $_POST = array(
+ 'form_id' => 0,
+ 'translating' => 'false',
+ 'i18n' => '',
+ );
+ $_REQUEST = $_POST;
+ $data = $this->text_element_settings_data();
+
+ $result = $this->run_dying_handler(
+ static function () use ( $data ) {
+ SUPER_Ajax::load_element_settings( 'text', 'form_elements', $data );
+ },
+ false
+ );
+
+ $this->assertTrue( $result['exited'] );
+ $this->assertSame( 0, $result['status'], $result['output'] );
+ $this->assertStringContainsString( 'super-update-element', $result['output'] );
+ }
+
+
+}