I have a rails server witch expose APIs and i want to allow only some domains to do requests on my api.
I've added the gem rack-cors to my Gemfile and the following code to application.rb
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :put, :delete, :options]
end
end
This code allows every domain to send requests to my server, is there a way to dynamically updates the allowed domains list? Is this a good approach to my problem?
Related
I'm working on a legacy project in Rails 4 where the rack-cors gem is used for allowing CORS. I know that there is an option to whitelist a domain.
use Rack::Cors do
allow do
origins 'localhost:3000', '127.0.0.1:3000',
/\Ahttp:\/\/192\.168\.0\.\d{1,3}(:\d+)?\z/
# regular expressions can be used here
resource '/file/list_all/', :headers => 'x-domain-token'
resource '/file/at/*',
methods: [:get, :post, :delete, :put, :patch, :options, :head],
headers: 'x-domain-token',
expose: ['Some-Custom-Response-Header'],
max_age: 600
# headers to expose
end
end
Is there any option to blacklist a domain so that CORS is disabled for that particular domain. I'm trying to figure out this because I can't really find all the domains that are using the API in the project. I couldn't find anything in the documentation to blacklist a specific domain.
Is there any other way to implement this? Thanks for the help in advance.
The best way to blacklist unsafe domains is by using the rack-attack gem
# config/initializers/rack_attack.rb
Rack::Attack.blocklist('block spammers') do |req|
request.referer =~ spammer_regexp # /foo\.com|bar\.com/
end
I'm building an app with Rails 5 API. Currently, anyone sending request to my rails server can receive response.
I want to process only those requests whose origin is mydomain.com
How can I do so?
I believe you'll want to implement CORS on your API.
Simply add rack-cors gem.
And add:
#config/application.rb
config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'mydomain.com' resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
Please read carefully its documentation
You'll find very useful information there.
I am out of ideas. I made a small app for a friend who is using it in a school. As long as it is on non-school internet, it works fine. At school, though, there are two buttons that don't work. They both are making an ajax request to a different controller. They give a no access-control-allow-origin header error. I have tried every fix that I can find, and nothing will work. I am out of ideas. The current iteration involves the rack-cors gem, which I have below. I have also tried the fixes at
CORS issue: Getting error "No 'Access-Control-Allow-Origin' header is present" when it actually is, XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource, http://www.yihangho.com/rails-cross-origin-resource-sharing/, and http://leopard.in.ua/2012/07/08/using-cors-with-rails/. Is there anything else I can do?
config.ru
require 'rack/cors'
use Rack::Cors do
allow do
origins '*'
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options, :patch]
end
end
application.rb
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :options, :put]
end
end
CORS is designed to prevent cross domain XML requests.
I'm not sure as to the specific reasons for this, but the short of it is that if you try and hit an external domain with Ajax (without CORS permissions granted), it will be denied.
As long as it is on non-school internet, it works fine
Yep, because the "domain" will be considered "local" Sorry I misread that as "Intranet".
This will depend on the server you're accessing - you need to make sure the server has CORS enabled.
I've dealt with a similar issue to you before.
You've done the right thing by using rack-cors:
#config/application.rb
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*' #-> has to be "*" or specific
resource '*', headers: :any, methods: [:get, :post, :options]
end
end
If you're using this (remember, you have to restart your server to get it working), it should work.
The main considerations for CORS are the origin and resource - they either have to be "*" (all), or explicitly defined (IE google.com or something).
In your case, allowing them "all" should be okay to test - you'll then need to ensure you have the specific domain defined.
(sorry for bad english)
I try to execute an ajax request but it doesn't work due to same-origin policy. My application is not deployed yet and I use Ruby on Rails 3.2.3 with Unicorn server.
The AJAX request is in an asset javascript file and I call it in a view. when I try to get the datas from the AJAX requests, the console says
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [url]. This can be fixed by moving the resource to the same domain or enabling CORS."
I tried everything :
Use jsonp in ajax request : console.log said syntax error
Use rack cors, with making everything said on the readme (https://github.com/cyu/rack-cors), but it didn't work, still the same message in the console (restatring server or not)
Try some syntaxes for rack-cors said on every post about it in stack overflow I could find, I tried this :
application.rb :
config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options], expose: :location
end
but it didn't work, and I tried this in config.ru
use Rack::Cors do
# allow all origins in development
allow do
origins 'localhost:3000'
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options]
end
end
I tried this code with "origins 'localhost:3000'" ans with "origins '*'" but none of them worked, I didn't forgot the "require 'rack/cors'"
I am desperte, could you help me please ?
It is almost undoubtedly that you aren't defining the proper headers to allow access. The following walk-through should get you going:
http://dotnet-concept.com/Tip/2015/3/5798824/Cross-Origin-Request-Blocked-The-Same-Origin-Policy-disallows-reading-the-remote-resource-This-can-be-fixed-by-moving-the-resource-to-the-same-domain-or-enabling-CORS-
We had a similar situation that was resolved using rack-cors
Gemefile:
gem 'rack-cors', :require => 'rack/cors'
application.rb:
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
Don't forget to restart your rails server!
EDIT:
It just occurred to me, that this will only work in production if you are using your app server as the web server as well. If you are using Nginx or Apache as your web server, then your static assets will be served from it. You will have to enable CORS on Nginx/Apache.
Every time I'm trying to set the 'Access-Control-Allow-Origin' header for the Spree Commerce API, no header is being passed for Ajax calls.
I'm currently extending the base_controller.rb:
File: app/controllers/spree/api/base_controller_decorator.rb
after_filter :set_access_control_headers
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
end
The header is added to the requests every time I view a JSON directly from the browser. However, no header is visible when making AJAX calls...
Can anybody explain me how to do this? Is there a workaround for this issue?
You don't need anything in your application controller, rather you need a gem:
Add gem 'rack-cors', :require => 'rack/cors' to your Gemfile, then bundle install
In config/application.rb configure rack-cors to accept requests from any origin. It belongs anywhere inside the Application class.
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
origins '*' indicates you are allowing requests from any domain. If you want to restrict to a specific domain (i.e. internally open an api, but keep it closed to external users), change '*' to the specific domain name.
see https://github.com/cyu/rack-cors for more details