Integer Overflow and Buffer Overflow in Ruby CGI.escape_html

ruby Updated 02 Oct 2025

Summary

06/02/2022
N/A
Yes i
Critical
9.8

CVE-2021-41816: CGI.escape_html in Ruby before 2.7.5 and 3.x before 3.0.3 has an integer overflow and resultant buffer overflow via a long string on platforms (such as Windows) where size_t and long have different numbers of bytes. Affects ruby across 2 version ranges.

< 2.7.5-r0 < c9c800715ef268421ce7fbe9bc62d19a2ab67fee

Fixed in 2.7.5-r0. How to fix ↓

Free scan

Check if this vulnerability affects your application

Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.

Check my app free

No signup required · Results in about 60 seconds

Upload your dependency manifest or SBOM

Drag & drop your file here or

Java Javascript PHP Python Go Rust Ruby Dart C/C++ Elixir CycloneDX SPDX

Upload a supported dependency manifest or SBOM. No installation or source-code access required.

See supported files and SBOM formats
SBOM sbom.json · bom.json · bom.xml · *.spdx.json · *.cdx.json · *.cdx.xml
Java pom.xml · gradle.lockfile · buildscript-gradle.lockfile
Javascript package-lock.json · yarn.lock · pnpm-lock.yaml
PHP composer.lock
Python poetry.lock · requirements.txt · Pipfile.lock
Go go.mod
Rust Cargo.lock
Ruby Gemfile.lock
Dart pubspec.lock
C/C++ conan.lock
Elixir mix.lock
Enter your work email to receive your report link.

Verify your email to view the full report and start 30 days of continuous monitoring. No credit card required.

Sponsored MarkAI Find prospects already talking about the problem you solve AI watches the communities your customers use to find qualified prospects, then alerts you when a conversation is worth joining. See MarkAI

Background

Ruby is a popular programming language known for its simplicity and flexibility. It is widely used for web development and scripting. The CGI module in Ruby provides functionality for handling Common Gateway Interface (CGI) requests and generating HTML content. The CGI.escape_html function is used to escape special characters in HTML strings to prevent cross-site scripting (XSS) attacks. However, versions of Ruby before 2.7.5 and 3.x before 3.0.3 are vulnerable to an integer overflow and resultant buffer overflow in the CGI.escape_html function. This vulnerability can be exploited by an attacker to execute arbitrary code or crash the application.

Vulnerability Detail

The vulnerability (CVE-2021-41816) arises due to an integer overflow in the CGI.escape_html function. On platforms where the size_t and long data types have different sizes (e.g., Windows), a long string can trigger an integer overflow, leading to a buffer overflow. This vulnerability affects the CGI gem before version 0.3.1 as well. An attacker can exploit this vulnerability by crafting a malicious input string that exceeds the expected length, causing the buffer overflow. The consequences of this vulnerability include remote code execution, application crashes, and potential compromise of user data.

How To Fix

Minimum fixed version

2.7.5-r0

This is the first release that contains the fix.

sudo apk add --upgrade ruby

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Workaround

As a temporary workaround, developers can manually check the length of input strings passed to the CGI.escape_html function and ensure they do not exceed the maximum allowed length. This can be done by using the length method or a similar approach to validate the input before passing it to the function. Here's an example in Ruby:

input = params[:input]
if input.length <= MAX_LENGTH
  escaped_html = CGI.escape_html(input)
  # Continue with the escaped HTML
else
  # Handle the error or reject the input
end

Conclusion

The integer overflow and buffer overflow vulnerability in Ruby's CGI.escape_html function poses a significant risk to applications using vulnerable versions of Ruby or the CGI gem. It is crucial to update to the patched versions (2.7.5 or 3.0.3) and ensure the CGI gem is updated to version 0.3.1. Regularly updating the Ruby runtime and associated gems is essential to maintain the security of web applications. For further support on vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for ruby →

Share this vulnerability

FAQ

The vulnerability allows an attacker to execute arbitrary code or crash the application. This can lead to remote code execution, application instability, and potential compromise of user data.

Versions of Ruby before 2.7.5 and 3.x before 3.0.3 are affected by this vulnerability.

At the time of this report, there are no known public exploits for this vulnerability. However, it is advisable to patch the vulnerability as soon as possible to mitigate the risk.

You can use Vulert, a vulnerability monitoring tool, to check if your application is affected by this vulnerability. Vulert uses a manifest file like package-lock.json to monitor and alert in real-time for open-source vulnerabilities. Visit the Vulert Playground at Vulert Scanner to check if your application is affected without any signup or installation.

You can find more information about this vulnerability, including technical details and the latest updates, in the official CVE-2021-41816 advisory. Visit the following link for the complete details: https://storage.googleapis.com/cve-osv-conversion/osv-output/CVE-2021-41816.json

Related vulnerabilities

Recent vulnerabilities recorded for ruby.

How To Fix

Minimum fixed version

2.7.5-r0

This is the first release that contains the fix.

sudo apk add --upgrade ruby

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback