Skip to main content

Insecure or unset HTTP headers - Accept

Need

Implementation of secure and restricted HTTP headers - Accept

Context

  • Usage of Ruby for building web applications and scripting
  • Usage of ActionController for handling actions and routing in a Ruby on Rails application

Description

Non compliant code

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def index
render :json => @data
end
end

In the above code, the ApplicationController's index action is rendering the @data object as JSON without specifying the "Accept" header. This means that any client can send a request to this endpoint and interpret the response in any MIME type they choose, which could potentially lead to unexpected behaviors.

The "Accept" header is a part of HTTP protocol that tells the server what media types the client is able to understand. If the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.

In the case of our Ruby on Rails application, if we do not specify the "Accept" header, the client can interpret the response in any way they choose. This could potentially lead to issues such as Cross-Site Scripting (XSS) if the client interprets the response as HTML and injects malicious scripts, or it could lead to other unexpected behaviors if the client interprets the response in a way that the server did not intend.

Steps

  • Implement input validation to ensure that only valid content-types are accepted.
  • Set a default Accept header in the requests to enforce a specific content-type.
  • Use a whitelist approach to explicitly allow only trusted content-types.
  • Implement strict content-type checking to reject requests with unexpected content-types.
  • Consider using a content-type negotiation mechanism to determine the appropriate response format based on the client's preferences.

Compliant code

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :set_default_response_format

def index
render :json => @data
end

private

def set_default_response_format
request.format = :json
end
end

The updated code includes a before_action callback set_default_response_format which is executed before the index action. This callback sets the default response format to JSON (request.format = :json).

This ensures that the application only accepts JSON format in the requests, thereby mitigating the risk of an attacker causing unexpected behaviors by sending requests with incorrect content-types.

This approach is a combination of setting a default Accept header (step 2) and implementing strict content-type checking (step 4).

However, it's important to note that this solution assumes that the application only needs to handle JSON requests. If the application needs to handle other content-types, you should consider implementing a content-type negotiation mechanism (step 5) and/or a whitelist of trusted content-types (step 3).

Also, remember that input validation (step 1) is a crucial part of any security strategy. Always validate the input data to ensure it matches the expected format and values.

References