Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow anything through CORS Policy

How can I disable cors? For some reason I wild carded the allowed origins and headers yet my ajax requests still complain that the origin was not allowed by my CORS policy....

My applications controller :

class ApplicationController < ActionController::Base   protect_from_forgery   before_filter :current_user, :cors_preflight_check   after_filter :cors_set_access_control_headers  # For all responses in this controller, return the CORS access control headers.  def cors_set_access_control_headers   headers['Access-Control-Allow-Origin'] = '*'   headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'   headers['Access-Control-Allow-Headers'] = '*'   headers['Access-Control-Max-Age'] = "1728000" end  # If this is a preflight OPTIONS request, then short-circuit the # request, return only the necessary headers and return an empty # text/plain.  def cors_preflight_check   if request.method == :options     headers['Access-Control-Allow-Origin'] = '*'     headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'     headers['Access-Control-Allow-Headers'] = '*'     headers['Access-Control-Max-Age'] = '1728000'     render :text => '', :content_type => 'text/plain'   end end   private   # get the user currently logged in   def current_user     @current_user ||= User.find(session[:user_id]) if session[:user_id]   end   helper_method :current_user  end 

routes:

  match "*all" => "application#cors_preflight_check", :constraints => { :method => "OPTIONS" }   match "/alert" => "alerts#create"   match "/alerts" => "alerts#get"   match "/login" => "sessions#create"   match "/logout" => "sessions#destroy"   match "/register" => "users#create" 

Edit---

I also tried:

   config.middleware.use Rack::Cors do       allow do         origins '*'         resource '*',              :headers => :any,              :methods => [:get, :post, :delete, :put, :options]       end     end 

in application.rb

--edit 2---

The problem is that Chrome Extensions may not support CORS I think. How can I fetch information bypassing CORS? How should I respond to the preflight check?

like image 430
Nonconformist Avatar asked Jul 25 '13 12:07

Nonconformist


People also ask

How do I fix a blocked CORS policy?

Use a Chrome extension to add Access-Control-Allow-Origin header into every response. To find one of them, just head over to Chrome Webstore and type in "CORS", dozens will show up in the search result. Or you can install CORS Helper, CORS Unblock or dyna CORS right away.

What does it mean to enable CORS?

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. A web page may freely embed cross-origin images, stylesheets, scripts, iframes, and videos.

How do I fix CORS policy no Access-Control allow origin?

< access-control-allow-origin: * You can solve this temporarily by using the Firefox add-on, CORS Everywhere. Just open Firefox, press Ctrl+Shift+A , search the add-on and add it! Thanks this helps to avoid all the hassle and test the code from localhost.


2 Answers

I've your same requirements on a public API for which I used rails-api.

I've also set header in a before filter. It looks like this:

headers['Access-Control-Allow-Origin'] = '*' headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' headers['Access-Control-Request-Method'] = '*' headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization' 

It seems you missed the Access-Control-Request-Method header.

like image 86
matteo Avatar answered Oct 10 '22 16:10

matteo


Have a look at the rack-cors middleware. It will handle CORS headers in a configurable manner.

like image 28
Jef Avatar answered Oct 10 '22 16:10

Jef