gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

CloudFlare blocking REST API PUT Request (Draft Doesn't Get Saved) (solved by an update from Cloudflare)

Open ahmadawais opened this issue 9 years ago • 144 comments

[UPDATE: Cloudflare rolled out a fix for problems with the WP0025B rule 8-Aug-2018, please update.]

I have started to try Gutenberg on a production site and it is not working. The site is new and has no other plugin installed. Looks like the PUT request is not working.

https://i.imgur.com/tElNhnU.png

I am using the latest version of Gutenberg 1.1.0 available in the WP Repo. I have also tried this on two other sites and same error. I thought I should report it here?

ahmadawais avatar Sep 08 '17 11:09 ahmadawais

I have a sneaking suspicion that this is actually not a Gutenberg fault, but a deeper one with the REST api. Do you have the basic permalinks set? If so, try swapping to a custom permalink and give it another go. I wrote up some details (with links to tracs etc) when I ran into this symtom.

toddhalfpenny avatar Sep 08 '17 12:09 toddhalfpenny

Yeah, I have pretty permalinks set up. I even used a front end post submission plugin that's built with REST API and it works.

ahmadawais avatar Sep 08 '17 12:09 ahmadawais

This may be a duplicate of #1935.

westonruter avatar Sep 08 '17 20:09 westonruter

@westonruter I'm afraid you're right. Looks like there's an issue with the PUT request as it returns a 403.

PUT https://a2podcast.com/wp-json/wp/v2/posts/44 403 ()

for

load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,underscore,backbone,wp-util,wp-backbone,media-models,plupload,wp-plupload,jquery-ui-core,jquery&load[]=-ui-widget,jquery-ui-mouse,jquery-ui-sortable,mediaelement,wp-mediaelement,media-views&ver=4.8.1:4

ahmadawais avatar Sep 09 '17 07:09 ahmadawais

If I log XMLHttpRequests then this is the message I get.

XHR failed loading: PUT "https://a2podcast.com/wp-json/wp/v2/posts/46".

screenshot 2017-09-09 13 03 57

ahmadawais avatar Sep 09 '17 08:09 ahmadawais

What is the request/response data for the API request?

rmccue avatar Sep 10 '17 03:09 rmccue

Tried logging the actual API req/res objects via REST API Log plugin, but no dice. Maybe this is insightful in the meantime?

https://cloudup.com/cL5lSmvZize

JustinSainton avatar Sep 10 '17 15:09 JustinSainton

@rmccue I have the similar log as of @JustinSainton

https://i.imgur.com/hRi9cWJ.png https://i.imgur.com/ZXcRBgJ.png

It still doesn't work.

ahmadawais avatar Sep 12 '17 06:09 ahmadawais

What is the response payload that accompanies this?

rmccue avatar Sep 12 '17 07:09 rmccue

@rmccue Thanks for asking the right question. I haven't looked at the response payload.

Turns out CloudFlare is the reason why this is happening.

The response payload is this https://i.imgur.com/nZWecJR.png

I disabled CloudFlare on one of my domains to check if this works and it did. But this again is a big problem. So many sites are hosted on CloudFlare. I am currently looking into what is causing CloudFlare to ban the IP for adding a draft via Gutenberg. Looks like the Browser Integrity Check where it's banning a headless call. Which shouldn't be banned.

I have no page rules set. Nothing. Looks like default configurations of CloudFlare are banning the API from posting via Gutenberg.

ahmadawais avatar Sep 12 '17 07:09 ahmadawais

Looks like CloudFlare has a globally enabled rule set in WAF — even for free users — that no one can even disable (AFAIK).

Here are the details https://blog.cloudflare.com/protecting-everyone-from-wordpress-content-injection/

https://i.imgur.com/8SbZpYK.png

https://i.imgur.com/EJ7S5O4.png

I wonder if @aaroncampbell could help with this — I can see his comments on the post.

Looking forward!

ahmadawais avatar Sep 12 '17 08:09 ahmadawais

I'm not using Cloudflare, but this is interesting. I suppose something at the server-level with the host (We're on Siteground) could be blocking PUT requests. I'll ask them about this.

JustinSainton avatar Sep 12 '17 23:09 JustinSainton

@rmccue And my response payload is the CPanel 403 HTML response, fwiw:

https://cloudup.com/cUJByQ-troP

JustinSainton avatar Sep 12 '17 23:09 JustinSainton

Interesting. Based on this, it seems like it may be a REST API issue with these hosts. We may need to reach out and see about fixing this.

If you switch Gutenberg to use POST instead of PUT, does this still occur? (The API accepts POST everywhere it accepts PUT, but you can also do POST ?_method=PUT to simulate an actual PUT on the backend.)

rmccue avatar Sep 13 '17 00:09 rmccue

@rmccue How do you propose would one switch from PUT to POST in Gutenberg?

ahmadawais avatar Sep 13 '17 12:09 ahmadawais

@ahmadawais

Do the same PUT request you are doing as a POST request instead and add the request arg _method=PUT. This will trigger the x-http-method-override header so that the REST API will know you really mean PUT even though the server can't handle PUT and only wants to use POST requests. You can do the same for DELETE. For Gutenberg you can monkey patch the wp.api requests to use the method override. That would be a temporary solution, while this is figured out for the larger context of how the REST API will be used in this project and other areas of core.

BE-Webdesign avatar Sep 13 '17 13:09 BE-Webdesign

@rmccue Found the source. In our case, Siteground had put the following block in our .htaccess file

    # Block Request Method #
    RewriteCond %{REQUEST_METHOD} ^(connect|debug|delete|move|options|put|trace|track) [NC]
    RewriteRule .* - [F]

This was the root cause of the issue. I hadn't had the opportunity to attempt anything to prove the case, but I assume the DELETE and OPTIONS methods would have also failed.

JustinSainton avatar Sep 13 '17 16:09 JustinSainton

@JustinSainton Nice digging! DELETE, OPTIONS, and PUT all need to be whitelisted for the API (OPTIONS is used by browsers automatically for cross-origin requests).

Was this specific to your site, or a SiteGround-wide thing? If the latter, I'll try and follow up with them.

rmccue avatar Sep 14 '17 01:09 rmccue

@rmccue I'm not certain whether or not this is SiteGround-wide or not - I vaguely recall their support team modifying our htaccess file years ago, for some reason. So it may not be that widespread, but certainly feels like something to account for and educate hosts on.

JustinSainton avatar Sep 14 '17 18:09 JustinSainton

This definitely gives pause when you consider what it means for broader use of the REST API.

First-party use of all the API methods in core in wp-admin (with helpful error messages) would expose and get these fixed very quickly.

m avatar Sep 19 '17 15:09 m