Challenge - 5 Problems
CORS Mastery in Rails
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Rails app has this CORS config?
Given this CORS configuration in a Rails app, what will be the behavior when a browser from https://example.com tries to make a GET request?
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'https://example.com'
resource '*', headers: :any, methods: [:get, :post]
end
endAttempts:
2 left
💡 Hint
Check which HTTP methods are allowed and which origins are permitted.
✗ Incorrect
The config allows origins from 'https://example.com' and permits GET and POST methods. So a GET request from that origin will succeed with proper CORS headers.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this CORS config snippet
Which option contains the correct syntax for allowing all origins and all methods in Rails CORS config?
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :put, :delete]
end
endAttempts:
2 left
💡 Hint
Look carefully for missing commas or colons in the methods array.
✗ Incorrect
Option C is the only one with correct syntax: origins is a string, resource has headers as symbol :any, and methods is an array of symbols with commas separating them.
🔧 Debug
advanced2:00remaining
Why does this Rails CORS config block requests from https://myapp.com?
A developer wrote this CORS config but requests from https://myapp.com are blocked by the browser:
What is the most likely reason?
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'https://myapp.com'
resource '/api/*', headers: :any, methods: [:get, :post]
end
endWhat is the most likely reason?
Attempts:
2 left
💡 Hint
Check how resource path patterns match request URLs.
✗ Incorrect
The resource path '/api/*' matches only one level after /api/, but '/api/v1/users' has two levels. The pattern should be '/api/**'.
❓ state_output
advanced2:00remaining
What CORS headers are sent with this config for a POST request?
Given this Rails CORS config:
What CORS headers will the server send in response to a POST request from https://client.com?
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'https://client.com'
resource '/submit', headers: :any, methods: [:post]
end
endWhat CORS headers will the server send in response to a POST request from https://client.com?
Attempts:
2 left
💡 Hint
Look at origins, methods, and headers options in the config.
✗ Incorrect
The config allows origin https://client.com, method POST only, and headers :any which means all headers allowed. So the server sends Access-Control-Allow-Origin with the origin, allows POST method, and allows any headers.
🧠 Conceptual
expert3:00remaining
Why is CORS middleware inserted before other middlewares in Rails?
In Rails, why do we insert the Rack::Cors middleware at position 0 like this?
Choose the best explanation.
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :options]
end
endChoose the best explanation.
Attempts:
2 left
💡 Hint
Think about when CORS headers must be added in the request lifecycle.
✗ Incorrect
CORS headers must be added early so browsers receive them before any authentication or session middleware might block the request. Inserting Rack::Cors at position 0 ensures it runs first.