Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using this in your Sinatra app should solve your problem:</p> <pre><code>set :protection, :except =&gt; [:json_csrf] </code></pre> <p>A better solution may be to upgrade Sinatra to 1.4, which uses Rack::Protection 1.5 and should not cause <a href="https://github.com/rkh/rack-protection/issues/39" rel="noreferrer">the problem</a> you are seeing.</p> <p>The problem is that your version of <code>RackProtection::JsonCsrf</code> in is incompatible with <a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing" rel="noreferrer">CORS</a> when you respond with Content-Type: application/json. Here is a snippet from the <a href="https://github.com/rkh/rack-protection/blob/a91810fa030e939a5262b587a89b64285ccaa7b9/lib/rack/protection/json_csrf.rb" rel="noreferrer">old json_csrf.rb</a> in rack-protection:</p> <pre><code>def call(env) status, headers, body = app.call(env) if headers['Content-Type'].to_s.split(';', 2).first =~ /^\s*application\/json\s*$/ if referrer(env) != Request.new(env).host result = react(env) warn env, "attack prevented by #{self.class}" end end result or [status, headers, body] end </code></pre> <p>You can see this rejects requests that have an <code>application/json</code> response when the referrer is not from the same host as the server.</p> <p>This problem was solved in a later version of rack-protection, which now considers whether the request is an XMLHttpRequest:</p> <pre><code> def has_vector?(request, headers) return false if request.xhr? return false unless headers['Content-Type'].to_s.split(';', 2).first =~ /^\s*application\/json\s*$/ origin(request.env).nil? and referrer(request.env) != request.host end </code></pre> <p>If you are using Sinatra 1.3.2 and cannot upgrade the solution is to disable this particular protection. With CORS you are explicitly enabling cross-domain XHR requests. Sinatra lets you disable protection entirely, or disable specific components of <code>Rack::Protection</code> (see <a href="http://www.sinatrarb.com/intro.html#Configuring%20attack%20protection" rel="noreferrer">"Configuring Attack Protection"</a> in the Sinatra docs).</p> <p><a href="http://rkh.github.io/rack-protection/" rel="noreferrer"><code>Rack::Protection</code></a> provides <a href="https://github.com/rkh/rack-protection/tree/master/lib/rack/protection" rel="noreferrer">12 middleware components</a> that help defeat common attacks:</p> <ul> <li><code>Rack::Protection::AuthenticityToken</code></li> <li><code>Rack::Protection::EscapedParams</code></li> <li><code>Rack::Protection::FormToken</code></li> <li><code>Rack::Protection::FrameOptions</code></li> <li><code>Rack::Protection::HttpOrigin</code></li> <li><code>Rack::Protection::IPSpoofing</code></li> <li><code>Rack::Protection::JsonCsrf</code></li> <li><code>Rack::Protection::PathTraversal</code></li> <li><code>Rack::Protection::RemoteReferrer</code></li> <li><code>Rack::Protection::RemoteToken</code></li> <li><code>Rack::Protection::SessionHijacking</code></li> <li><code>Rack::Protection::XssHeader</code></li> </ul> <p>At time of writing, all but four of these are loaded automatically when you use the Rack::Protection middleware (<code>Rack::Protection::AuthenticityToken</code>, <code>Rack::Protection::FormToken</code>, <code>Rack::Protection::RemoteReferrer</code>, and <code>Rack::Protection::EscapedParams</code> must be added explicitly).</p> <p>Sinatra uses Rack::Protection's default settings with <a href="https://github.com/sinatra/sinatra/blob/7c3a194935b7c9871783039958ac2963f83539b9/lib/sinatra/base.rb#L1683-1691" rel="noreferrer">one exception</a>: it only adds <code>SessionHijacking</code> and <code>RemoteToken</code> if you enable sessions.</p> <p>And, finally, if you are trying to use CORS with Sinatra, you might try <a href="https://github.com/cyu/rack-cors" rel="noreferrer">rack-cors</a>, which takes care of a lot of the details for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload