Skip to content
Open
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
1 change: 1 addition & 0 deletions classes/controllers/FrmHooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public static function load_ajax_hooks() {
add_action( 'wp_ajax_frmpro_css', 'FrmStylesController::load_saved_css' );
add_action( 'wp_ajax_nopriv_frmpro_css', 'FrmStylesController::load_saved_css' );
add_action( 'wp_ajax_frm_rename_style', 'FrmStylesController::rename_style' );
add_action( 'wp_ajax_frm_styler_theme_css', 'FrmStylesController::load_preview_theme_css' );

// XML Controller.
add_action( 'wp_ajax_frm_install_template', 'FrmXMLController::install_template' );
Expand Down
111 changes: 110 additions & 1 deletion classes/controllers/FrmStylesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public static function admin_init() {
wp_enqueue_style( 'formidable-color-picker' );
}

wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), $version );
wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1' ), array(), self::get_scoped_css_version() );
self::enqueue_preview_theme_css();

$style = apply_filters( 'frm_style_head', false );

Expand All @@ -153,6 +154,109 @@ public static function admin_init() {
}
}

/**
* Version the styler's copy of the saved stylesheet by the last style save and the global
* custom CSS, so the browser can keep it until either one changes.
*
* @since x.x
*
* @return string
*/
private static function get_scoped_css_version() {
$last_update = get_option( 'frm_last_style_update' );
$custom_css = self::get_custom_css();

return FrmAppHelper::plugin_version() . '-' . ( $last_update ? $last_update : '0' ) . '-' . substr( md5( is_string( $custom_css ) ? $custom_css : '' ), 0, 8 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of insecure md5() function found


Using md5(), sha1() function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.

It is recommended to use PHP's password hashing function password_hash() to create a secure password hash.

}

/**
* Style the styler preview with the active theme's front end CSS, scoped to the preview.
*
* Without this the preview inherits wp-admin typography, so a form that looks right on the
* front end can look different in the styler.
*
* @since x.x
*
* @return void
*/
private static function enqueue_preview_theme_css() {
// Undo the wp-admin element styles inside the preview. The theme CSS depends on this so it loads after it and wins.
wp_enqueue_style( 'frm-styler-preview', FrmAppHelper::plugin_url() . '/css/admin/styler-preview.css', array( 'frm-custom-theme' ), FrmAppHelper::plugin_version() );

/**
* Allow the active theme's CSS to be kept out of the Visual Styler preview.
*
* @since x.x
*
* @param bool $load_theme_css True to style the preview with the theme's front end CSS.
*/
if ( ! apply_filters( 'frm_styler_load_theme_css', true ) ) {
return;
}

$url = add_query_arg(
array(
'action' => 'frm_styler_theme_css',
'nonce' => wp_create_nonce( 'frm_ajax' ),
),
admin_url( 'admin-ajax.php' )
);
wp_enqueue_style( 'frm-styler-theme-preview', $url, array( 'frm-styler-preview' ), FrmStylesThemeCssHelper::get_version() );
}

/**
* Output the active theme's front end CSS, scoped to the styler preview.
*
* @since x.x
*
* @return void
*/
public static function load_preview_theme_css() {
FrmAppHelper::permission_check( 'frm_change_settings' );
check_ajax_referer( 'frm_ajax', 'nonce' );

$version = FrmStylesThemeCssHelper::get_version();
$cache_key = 'frm_styler_theme_css_' . md5( $version );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of insecure md5() function found


Using md5(), sha1() function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.

It is recommended to use PHP's password hashing function password_hash() to create a secure password hash.

$css = get_transient( $cache_key );

if ( ! is_string( $css ) ) {
$css = FrmStylesThemeCssHelper::get_scoped_theme_css( '#frm_active_style_form, #frm_sample_form' );
set_transient( $cache_key, $css, DAY_IN_SECONDS );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: an empty result is cached for a day, and the version key misses real inputs.

  • get_scoped_theme_css() returns '' when the theme exposes no readable CSS or the sandboxed enqueue throws. is_string( '' ) is true, so set_transient() stores it and every request for the next 24h gets an empty sheet. Consider not caching an empty string.
  • FrmStylesThemeCssHelper::get_version() is built from the theme slug, the theme's Version header and a hash of wp_get_global_stylesheet(). Editing a classic theme's style.css without a version bump, or anything added through frm_preview_theme_stylesheets or a plugin's wp_enqueue_scripts, leaves the key unchanged. The transient then serves the old CSS for a day, and the immutable header keeps the browser copy too. The docblock also says a Customizer edit changes the version; only the Global Styles part does. Consider adding the stylesheet files' filemtime() to the version.

}

header( 'Content-type: text/css' );
self::maybe_send_browser_cache_headers( $version );

echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Generated CSS, escaping would corrupt it.

// Not wp_die(). Its AJAX handler sends no-cache headers again, which would undo the cache headers above.
// css/custom_theme.css.php ends the saved stylesheet request the same way.
die();
}

/**
* Let the browser keep a versioned stylesheet served from admin-ajax.php.
*
* WordPress sends no-cache headers from admin-ajax.php before any action runs. A stylesheet requested with
* the current version in its URL can be cached anyway, since any change to its content also
* changes the version, and with it the URL the page asks for.
*
* @since x.x
*
* @param string $version The current version of the stylesheet.
*
* @return void
*/
private static function maybe_send_browser_cache_headers( $version ) {
if ( headers_sent() || FrmAppHelper::simple_get( 'ver' ) !== $version ) {
return;
}

header_remove( 'Expires' );
header_remove( 'Pragma' );
header( 'Cache-Control: private, max-age=' . YEAR_IN_SECONDS . ', immutable' );
}

/**
* @since 6.0
*
Expand Down Expand Up @@ -1184,6 +1288,11 @@ public static function load_css() {
public static function load_saved_css() {
$css = get_transient( 'frmpro_css' );

if ( FrmAppHelper::simple_get( 'frm_scope_custom_css' ) ) {
// The styler loads the full stylesheet on every visit, so let the browser keep it.
self::maybe_send_browser_cache_headers( self::get_scoped_css_version() );
}

ob_start();
include FrmAppHelper::plugin_path() . '/css/custom_theme.css.php';
$output = ob_get_clean();
Expand Down
6 changes: 4 additions & 2 deletions classes/helpers/FrmStylesPreviewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,12 @@ public static function get_additional_preview_style( $settings, $is_preview = fa
#frm_style_preview .with_frm_style .frm_slimselect.ss-main {
width: var(--field-width);
}
#frm_style_preview .with_frm_style .frm_radio {
#frm_style_preview .with_frm_style .frm-default-option-align .frm_radio,
#frm_sample_form .with_frm_style .frm_radio {
display:' . $radio_display_type . ';
}
#frm_style_preview .with_frm_style .frm_checkbox {
#frm_style_preview .with_frm_style .frm-default-option-align .frm_checkbox,
#frm_sample_form .with_frm_style .frm_checkbox {
display:' . $check_display_type . ';
}';

Expand Down
Loading
Loading