Skip to main content

Insufficient data authenticity validation - Checksum verification

Need

Ensure data authenticity through proper checksum verification

Context

  • Usage of Ruby for building web applications and scripting
  • Usage of net/http for creating HTTP servers in a Node.js application
  • Usage of URI for manipulating and working with Uniform Resource Identifiers (URIs)

Description

Non compliant code

require 'net/http'
require 'uri'

def fetch_data
url = URI.parse('http://external-server.com/resource')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
return res.body
end

The above code fetches data from an external server using the Net::HTTP library in Ruby. The function fetch_data sends a GET request to the specified URL and returns the response body.

However, this code is vulnerable due to insufficient data authenticity validation. It does not verify the integrity of the data received from the external server.

This means that if the data is tampered with during transmission or if the server is compromised, the application would still accept and use the corrupted data. This could lead to various security issues, such as data corruption, data leakage, or execution of malicious code.

A checksum verification should be implemented to ensure the data received is exactly the same as the data sent by the server. This involves generating a checksum or hash of the data at the server side, sending this along with the data, and then comparing this with the checksum generated at the client side. If the checksums do not match, the data should be rejected as it indicates that the data has been altered.

Steps

  • Implement data authenticity validation by adding checksum verification.
  • Generate a checksum for the resource before sending the request.
  • Retrieve the checksum from a trusted source or calculate it using a secure algorithm.
  • Compare the received resource with the calculated checksum to ensure its integrity.
  • If the checksums do not match, reject the resource and handle the error appropriately.
  • Consider using secure protocols such as HTTPS to ensure the integrity of the data in transit.
  • Regularly update and patch the application and its dependencies to address any security vulnerabilities.

Compliant code

require 'net/http'
require 'uri'
require 'digest'

def fetch_data
url = URI.parse('http://external-server.com/resource')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}

# Generate a checksum for the resource before sending the request
checksum = Digest::SHA256.hexdigest(res.body)

# Retrieve the checksum from a trusted source
trusted_checksum = get_trusted_checksum(url)

# Compare the received resource with the calculated checksum to ensure its integrity
if checksum == trusted_checksum
return res.body
else
# If the checksums do not match, reject the resource and handle the error appropriately
raise "Checksum verification failed"
end
end

def get_trusted_checksum(url)
# This function should retrieve the checksum from a trusted source
# For the purpose of this example, we will return a hardcoded checksum
"hardcoded_checksum"
end

The updated code now includes a checksum verification process to validate the integrity of resources loaded from external servers.

The fetch_data function now generates a checksum for the resource using the SHA256 algorithm before sending the request. This checksum is then compared with a trusted checksum retrieved from a trusted source using the get_trusted_checksum function.

If the checksums match, the resource is considered valid and the function returns the resource data. If the checksums do not match, the function raises an error and rejects the resource, ensuring that only resources with validated integrity are accepted by the application.

This approach significantly improves the security of the application by preventing the loading of tampered or corrupted resources from external servers.

In addition to this, it is recommended to use secure protocols such as HTTPS to ensure the integrity of the data in transit and to regularly update and patch the application and its dependencies to address any security vulnerabilities.

References