-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Block Theme Previews: Add a nonce for activation #4795
Conversation
…always load the filters even outside of wp-admin
Thanks for the fix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me with a couple of suggestions inline.
I've suggested prefixing the constant name with WP_
(putting it in the wp
global could also work), would such a change require an upstream change in the Gutenberg repo?
@scruffian / @peterwilsoncc would this make it into 6.3 still? |
This needs to go into 6.3, or we need to take block theme previews out of 6.3. |
@scruffian @bph I submitted the PR WordPress/gutenberg#52398 to rename the variable to use an |
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks folks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems I'm a bit late :) Nevertheless, I have added my code review below. Thanks!
* @private | ||
*/ | ||
function wp_block_theme_activate_nonce() { | ||
$nonce_handle = 'switch-theme_' . wp_get_theme_preview_path(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I don't see many PHP frameworks using inline tags to output text these days. Instead, I would use echo
or print
to output the nonce code to the buffer. But I admit, this is just a personal preference.
$nonce_handle = 'switch-theme_' . wp_get_theme_preview_path(); | ||
?> | ||
<script type="text/javascript"> | ||
window.WP_BLOCK_THEME_ACTIVATE_NONCE = '<?php echo wp_create_nonce( $nonce_handle ); ?>'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could potentially be a security issue. Is there a specific reason for not escaping the nonce value?
window.WP_BLOCK_THEME_ACTIVATE_NONCE = '<?php echo wp_create_nonce( $nonce_handle ); ?>'; | |
window.WP_BLOCK_THEME_ACTIVATE_NONCE = '<?php echo esc_js( wp_create_nonce( $nonce_handle ) ); ?>'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @anton-vlasenko -- it's more a bug than a security issue as it's not user input, however it may break with custom nonce implementations using special characters.
It will need to change to = <?php echo wp_json_encode( wp_create_nonce( $nonce_handle ) );
(without quotes) as esc_js()
is intended for use in DOM attributes.
wp> esc_js( 'Pens & Pencils' );
=> string(18) "Pens & Pencils"
wp> wp_json_encode( 'Pens & Pencils' );
=> string(16) ""Pens & Pencils""
I'll reopen the ticket and add a PR to the GB repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for getting back to my code review, @peterwilsoncc!
Yes, using wp_json_encode()
is also an option.
In fact, I prefer wp_json_encode()
over esc_js()
because it automatically adds quotes.
I was just uncertain about which function aligns better with the "true WordPress way" of doing things.
This makes a few changes to block theme previews:
Also ensure that we always load the filters even outside of wp-admin.
Trac ticket: https://core.trac.wordpress.org/ticket/58712
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.