Bird
Raised Fist0
Ruby on Railsframework~20 mins

CORS configuration in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CORS Mastery in Rails
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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
end
AThe GET request from https://example.com will succeed with CORS headers allowing it.
BThe GET request will succeed but without CORS headers, causing browser to block response.
CThe GET request will be blocked because only POST methods are allowed.
DThe GET request will be blocked because origins must be '*' to allow any domain.
Attempts:
2 left
💡 Hint
Check which HTTP methods are allowed and which origins are permitted.
📝 Syntax
intermediate
2: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
end
A
origins '*'
resource '*', headers: :any, methods: [get, post, put, delete]
B
origins '*'
resource '*', headers: :any, methods: [:get :post :put :delete]
C
origins '*'
resource '*', headers: :any, methods: [:get, :post, :put, :delete]
D
origins *
resource '*', headers: any, methods: [:get, :post, :put, :delete]
Attempts:
2 left
💡 Hint
Look carefully for missing commas or colons in the methods array.
🔧 Debug
advanced
2: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:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'https://myapp.com'
    resource '/api/*', headers: :any, methods: [:get, :post]
  end
end

What is the most likely reason?
AThe origins string must be an array, not a string.
BThe resource path '/api/*' does not match the actual request path '/api/v1/users'.
CThe methods array is missing the :options method needed for preflight.
DThe middleware is inserted after other middlewares, so it is ignored.
Attempts:
2 left
💡 Hint
Check how resource path patterns match request URLs.
❓ state_output
advanced
2:00remaining
What CORS headers are sent with this config for a POST request?
Given this Rails CORS config:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'https://client.com'
    resource '/submit', headers: :any, methods: [:post]
  end
end

What CORS headers will the server send in response to a POST request from https://client.com?
ANo CORS headers are sent because only GET is allowed.
BAccess-Control-Allow-Origin: *<br>Access-Control-Allow-Methods: GET, POST<br>Access-Control-Allow-Headers: Content-Type
CAccess-Control-Allow-Origin: https://client.com<br>Access-Control-Allow-Methods: GET, POST<br>Access-Control-Allow-Headers: *
DAccess-Control-Allow-Origin: https://client.com<br>Access-Control-Allow-Methods: POST<br>Access-Control-Allow-Headers: *
Attempts:
2 left
💡 Hint
Look at origins, methods, and headers options in the config.
🧠 Conceptual
expert
3: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?

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :options]
  end
end

Choose the best explanation.
ATo ensure CORS headers are added before other middlewares process the request, especially before authentication or session middlewares.
BBecause Rack::Cors must be the last middleware to modify the response after all processing.
CTo delay CORS checks until after the controller has handled the request.
DIt does not matter where Rack::Cors is inserted; position 0 is just a convention.
Attempts:
2 left
💡 Hint
Think about when CORS headers must be added in the request lifecycle.

Practice

(1/5)
1. What is the main purpose of configuring CORS in a Rails application?
easy
A. To speed up database queries
B. To control which external websites can access your app's resources
C. To manage user sessions securely
D. To style the frontend pages

Solution

  1. Step 1: Understand CORS role

    CORS (Cross-Origin Resource Sharing) controls access from other websites to your app's resources.
  2. Step 2: Identify main purpose

    It restricts or allows external origins to make requests, enhancing security.
  3. Final Answer:

    To control which external websites can access your app's resources -> Option B
  4. Quick Check:

    CORS controls access = B [OK]
Hint: Remember CORS is about cross-site access control [OK]
Common Mistakes:
  • Confusing CORS with database or session management
  • Thinking CORS affects frontend styling
  • Assuming CORS speeds up app performance
2. Which of the following is the correct way to add CORS middleware in a Rails app initializer?
easy
A. Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post] end end
B. Rails.middleware.use Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post] end end
C. config.middleware.add Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post] end end
D. Rails.application.middleware.insert_after 0, Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post] end end

Solution

  1. Step 1: Recall correct middleware insertion

    In Rails, middleware is added using Rails.application.config.middleware.insert_before or insert_after.
  2. Step 2: Identify correct syntax

    The common pattern is insert_before 0, Rack::Cors with a block for configuration.
  3. Final Answer:

    Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post] end end -> Option A
  4. Quick Check:

    Use insert_before 0 with config.middleware = C [OK]
Hint: Use Rails.application.config.middleware.insert_before 0 for CORS [OK]
Common Mistakes:
  • Using Rails.middleware instead of Rails.application.config.middleware
  • Using insert_after instead of insert_before 0
  • Missing config in middleware insertion
3. Given this CORS config in Rails initializer:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'https://example.com'
    resource '*', headers: :any, methods: [:get, :post]
  end
end
What happens if a request comes from 'https://evil.com'?
medium
A. The request is blocked due to origin mismatch
B. The request causes a server error
C. The request is allowed but only GET method works
D. The request is allowed with all headers and methods

Solution

  1. Step 1: Analyze allowed origins

    The config allows only 'https://example.com' origin.
  2. Step 2: Check request origin

    Request from 'https://evil.com' is not in allowed origins, so it is blocked by CORS policy.
  3. Final Answer:

    The request is blocked due to origin mismatch -> Option A
  4. Quick Check:

    Origin not allowed = request blocked = D [OK]
Hint: Only origins listed in config are allowed [OK]
Common Mistakes:
  • Assuming wildcard '*' allows all origins when specific origins set
  • Thinking request causes server error instead of CORS block
  • Confusing allowed methods with allowed origins
4. You wrote this CORS config:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :put]
  end
end
But PUT requests from frontend are failing with CORS errors. What is the likely problem?
medium
A. The methods list is missing :put
B. The frontend is not sending the correct Origin header
C. The server needs to allow OPTIONS method for preflight
D. The origins '*' is too restrictive

Solution

  1. Step 1: Check methods allowed

    PUT is included in methods, so it's not missing.
  2. Step 2: Understand CORS preflight

    PUT requests trigger a preflight OPTIONS request which must be allowed by server.
  3. Step 3: Identify missing OPTIONS method

    OPTIONS method is not listed, so preflight fails causing CORS error.
  4. Final Answer:

    The server needs to allow OPTIONS method for preflight -> Option C
  5. Quick Check:

    Preflight OPTIONS missing = CORS fail = A [OK]
Hint: Always allow OPTIONS method for CORS preflight [OK]
Common Mistakes:
  • Forgetting to allow OPTIONS method
  • Assuming '*' origin is restrictive
  • Blaming frontend Origin header without checking server
5. You want to allow CORS only for two trusted domains: 'https://app1.com' and 'https://app2.com'. Which config correctly implements this securely in Rails?
hard
A. Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'app1.com' resource '*', headers: :any, methods: [:get, :post, :options] end allow do origins 'app2.com' resource '*', headers: :any, methods: [:get, :post, :options] end end
B. Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: :any end end
C. Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'app1.com', 'app2.com' resource '*', headers: :any, methods: [:get, :post] end end
D. Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'https://app1.com', 'https://app2.com' resource '*', headers: :any, methods: [:get, :post, :options] end end

Solution

  1. Step 1: Identify correct origins format

    Origins must include full URLs with protocol for security, so 'https://app1.com' and 'https://app2.com' are correct.
  2. Step 2: Check methods and resource

    Allowing common methods including OPTIONS for preflight is necessary.
  3. Step 3: Compare options

    Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'https://app1.com', 'https://app2.com' resource '*', headers: :any, methods: [:get, :post, :options] end end correctly lists both origins in one allow block with proper methods and full URLs.
  4. Final Answer:

    Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'https://app1.com', 'https://app2.com' resource '*', headers: :any, methods: [:get, :post, :options] end end -> Option D
  5. Quick Check:

    Trusted full origins with methods including OPTIONS = A [OK]
Hint: List full trusted origins with needed methods including OPTIONS [OK]
Common Mistakes:
  • Using '*' origin which is insecure
  • Omitting protocol in origins
  • Not allowing OPTIONS method for preflight
  • Splitting origins into multiple allow blocks unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard