Skip to content

Refresh nonce with heartbeat#2790

Merged
ellatrix merged 7 commits intoWordPress:masterfrom
adamsilverstein:refresh-nonce-with-heartbeat
Oct 10, 2017
Merged

Refresh nonce with heartbeat#2790
ellatrix merged 7 commits intoWordPress:masterfrom
adamsilverstein:refresh-nonce-with-heartbeat

Conversation

@adamsilverstein
Copy link
Copy Markdown
Member

@adamsilverstein adamsilverstein commented Sep 25, 2017

Summary

Refresh the wp-api nonce when a new nonce is available, keeping the nonce fresh and the client authorized. Fixes #2088.

Description

Add a wp_rest nonce to the heartbeat response when in Gutenberg.
Add a JavaScript callback on jQuery( document ) 'heartbeat-tick' which heartbeat triggers. Update the wp-api nonce if it has changed.

How Has This Been Tested?

Verified heartbeat returns nonce.
Verified if nonce is different, logic sets it on root endpoint.

Notes

Wasn't sure about the best place to put the JS callback, feedback appreciated!

Copy link
Copy Markdown
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

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

@adamsilverstein This doesn't solve anything as far as I can see. The way I test it is setting the nonce life to 10 seconds. This is low enough to ensure that by the first heartbeat after page load the nonce will be invalid, so it's just like being away for over 24 hours and coming back to an invalid nonce. With this patch, nothing will be sent back. You'll need to hook into wp_refresh_nonces (see https://github.com/WordPress/wordpress-develop/blob/4.8.2/src/wp-admin/includes/ajax-actions.php#L2794). heartbeat_send is too late.

I wouldn't use jQuery here, we don't want a dependency on jQuery as well. You can just use that native browser API as we don't need to support lower IE versions.

Additionally there are some listing errors.

@adamsilverstein
Copy link
Copy Markdown
Member Author

@iseulde thanks for testing and the feedback, also for explaining how you tested - I wasn't sure how to do that. I saw wp_refresh_nonces and didn’t realize i had to use it, thanks for clarifying.

I thought I saw jQuery already used, I'll switch that and fix the linting errors. :)

@adamsilverstein
Copy link
Copy Markdown
Member Author

@iseulde actually for the jquery, I am listening for the event triggered by heartbeat:

jQuery( document ).on( 'heartbeat-tick' ) which is triggered by heartbeat.js using jQuery. Can I listen for the jQuery event using native JS?

@codecov
Copy link
Copy Markdown

codecov bot commented Sep 28, 2017

Codecov Report

Merging #2790 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2790      +/-   ##
==========================================
- Coverage   34.43%   34.42%   -0.01%     
==========================================
  Files         196      196              
  Lines        5782     5783       +1     
  Branches     1020     1021       +1     
==========================================
  Hits         1991     1991              
  Misses       3206     3206              
- Partials      585      586       +1
Impacted Files Coverage Δ
editor/index.js 0% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a59e2e4...91211d5. Read the comment docs.

@adamsilverstein
Copy link
Copy Markdown
Member Author

@iseulde I switched the hook to wp_refresh_nonces and verified this actually works correctly - i set my nonce expiration (30 sec) and heartbeat rate (15 sec) low and watched as the nonce expired and got renewed.

Can you please re-test?

@ellatrix
Copy link
Copy Markdown
Member

actually for the jquery, I am listening for the event triggered by heartbeat:

Ah ok, that's unfortunate. :) Do we have proper dependencies set for heartbeat.js?

@adamsilverstein
Copy link
Copy Markdown
Member Author

Ah ok, that's unfortunate. :) Do we have proper dependencies set for heartbeat.js?

Yes, in script-loader, we add hearbeat with a jQuery dependency:
$scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array('jquery'), false, 1 );

Is that what you meant?

heartbeat.js has 6 $document.trigger() calls. If we swap out these for wp.hooks, we could remove the jQuery dependency.

@ellatrix
Copy link
Copy Markdown
Member

ellatrix commented Oct 2, 2017

@adamsilverstein I meant here, in Gutenberg. I don't see a Heartbeat dependency. :)

@adamsilverstein
Copy link
Copy Markdown
Member Author

I meant here, in Gutenberg. I don't see a Heartbeat dependency. :)

Ah, good point - I was testing on the post page so it must be loading there. i'll check/add the dependency.

@adamsilverstein
Copy link
Copy Markdown
Member Author

@iseulde I added the heartbeat dependency in 8e6e426 - does this work for you now?

@ellatrix
Copy link
Copy Markdown
Member

@adamsilverstein I fixed the failing tests, and replaced wp.api.endpoints.at(0).get( 'nonce' ), because that's returning undefined. Used the settings object instead.

@ellatrix ellatrix force-pushed the refresh-nonce-with-heartbeat branch from 62c1d0f to 6e2bdc1 Compare October 10, 2017 21:59
@ellatrix ellatrix force-pushed the refresh-nonce-with-heartbeat branch from 6e2bdc1 to 91211d5 Compare October 10, 2017 22:04
@ellatrix ellatrix merged commit 5fab1d7 into WordPress:master Oct 10, 2017
DevinWalker added a commit to DevinWalker/gutenberg that referenced this pull request Oct 12, 2017
* master: (45 commits)
  Parser: Hide the core namespace in serialised data (WordPress#2950)
  fix: Undefined index warning (WordPress#2982)
  navigation via keydown
  PHPCS improvements (WordPress#2914)
  Update/html block description (WordPress#2917)
  Framework: Merge EditorSettingsProvider into EditorProvider
  Framework: Move the store initialization to a dedicated component
  Rotate header ellipsis to match block ellipsis
  add/459: Added support for ins, sub and sup
  Refresh nonce with heartbeat (WordPress#2790)
  Paste: add table support
  Bump version to 1.4.0. (WordPress#2954)
  Blocks: The custom classname should be persisted properly in the block's comment
  Editor: Fix the scroll position when reordering blocks
  Polish spacing in block ellipsis menu (WordPress#2955)
  Editor: HTML Editor per block (WordPress#2797)
  Show most frequently used blocks next to inserter (WordPress#2877)
  add/459: Added light grey background to selected inline boundaries
  Extend inline boundaries to other elements
  Move markdown fix to own plugin-compatibility file
  ...
DevinWalker added a commit to DevinWalker/gutenberg that referenced this pull request Oct 12, 2017
* master: (45 commits)
  Parser: Hide the core namespace in serialised data (WordPress#2950)
  fix: Undefined index warning (WordPress#2982)
  navigation via keydown
  PHPCS improvements (WordPress#2914)
  Update/html block description (WordPress#2917)
  Framework: Merge EditorSettingsProvider into EditorProvider
  Framework: Move the store initialization to a dedicated component
  Rotate header ellipsis to match block ellipsis
  add/459: Added support for ins, sub and sup
  Refresh nonce with heartbeat (WordPress#2790)
  Paste: add table support
  Bump version to 1.4.0. (WordPress#2954)
  Blocks: The custom classname should be persisted properly in the block's comment
  Editor: Fix the scroll position when reordering blocks
  Polish spacing in block ellipsis menu (WordPress#2955)
  Editor: HTML Editor per block (WordPress#2797)
  Show most frequently used blocks next to inserter (WordPress#2877)
  add/459: Added light grey background to selected inline boundaries
  Extend inline boundaries to other elements
  Move markdown fix to own plugin-compatibility file
  ...
DevinWalker added a commit to DevinWalker/gutenberg that referenced this pull request Oct 12, 2017
* master: (45 commits)
  Parser: Hide the core namespace in serialised data (WordPress#2950)
  fix: Undefined index warning (WordPress#2982)
  navigation via keydown
  PHPCS improvements (WordPress#2914)
  Update/html block description (WordPress#2917)
  Framework: Merge EditorSettingsProvider into EditorProvider
  Framework: Move the store initialization to a dedicated component
  Rotate header ellipsis to match block ellipsis
  add/459: Added support for ins, sub and sup
  Refresh nonce with heartbeat (WordPress#2790)
  Paste: add table support
  Bump version to 1.4.0. (WordPress#2954)
  Blocks: The custom classname should be persisted properly in the block's comment
  Editor: Fix the scroll position when reordering blocks
  Polish spacing in block ellipsis menu (WordPress#2955)
  Editor: HTML Editor per block (WordPress#2797)
  Show most frequently used blocks next to inserter (WordPress#2877)
  add/459: Added light grey background to selected inline boundaries
  Extend inline boundaries to other elements
  Move markdown fix to own plugin-compatibility file
  ...
@adamsilverstein
Copy link
Copy Markdown
Member Author

replaced wp.api.endpoints.at(0).get( 'nonce' ), because that's returning undefined. Used the settings object instead.

@iseulde Thanks for merging! I think I was testing in trunk and you were maybe on 4.8? The nonce handling changes a little in 4.9 and I don't think this will work since https://core.trac.wordpress.org/changeset/41553 - I am going to open a new PR that will use more flexible code to use the right approach in all cases.

@ellatrix
Copy link
Copy Markdown
Member

Yeah, I'm just on the latest version. :)

@adamsilverstein
Copy link
Copy Markdown
Member Author

Ok, makes sense and we need it to work there as well, so I'l work on a follow up PR.

@getsource
Copy link
Copy Markdown
Member

I'm having this issue. Is there a different place where it's being tracked?

@adamsilverstein
Copy link
Copy Markdown
Member Author

@getsource yes, see #9260

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants