CVE-2021-21289 is a high-severity (CVSS 8.3) OS Command Injection vulnerability in mechanize. A fix is available for mechanize — see the affected versions and patch details below.
Command Injection Vulnerability in Mechanize
EPSS Exploitation Probability
Probability of exploitation in the next 30 days, from FIRST.org EPSS.
How urgent is this, really
Where this sits among everything scored
Real-World Exposure
mechanizeReal-time download stats are indexed for npm and PyPI packages. This vulnerability affects RubyGems packages — download data is not available via public APIs for these ecosystems.
Description
This security advisory has been created for public disclosure of a Command Injection vulnerability that was responsibly reported by @kyoshidajp (Katsuhiko YOSHIDA).
Impact
Mechanize >= v2.0, < v2.7.7 allows for OS commands to be injected using several classes' methods which implicitly use Ruby's Kernel.open method. Exploitation is possible only if untrusted input is used as a local filename and passed to any of these calls:
Mechanize::CookieJar#load: since v2.0 (see 208e3ed)Mechanize::CookieJar#save_as: since v2.0 (see 5b776a4)Mechanize#download: since v2.2 (see dc91667)Mechanize::Download#saveand#save!since v2.1 (see 98b2f51, bd62ff0)Mechanize::File#saveand#save_as: since v2.1 (see 2bf7519)Mechanize::FileResponse#read_body: since v2.0 (see 01039f5)
Patches
These vulnerabilities are patched in Mechanize v2.7.7.
Workarounds
No workarounds are available. We recommend upgrading to v2.7.7 or later.
References
See https://docs.rubocop.org/rubocop/cops_security.html#securityopen for background on why Kernel.open should not be used with untrusted input.
For more information
If you have any questions or comments about this advisory, please open an issue in sparklemotion/mechanize.
Affected Packages
| Ecosystem | Package | Vulnerable range | Fix |
|---|---|---|---|
| 💎RubyGems | mechanize | ≥ 2.0.0&&< 2.7.7 | 2.7.7bundle update mechanize --conservative |
Affected Products
debian linuxdebianfedorafedoraprojectmechanizemechanize_projectDetection & mitigation playbook
Open-source dependencyDetect
Scan your dependency tree (package-lock.json, pnpm-lock.yaml, requirements.txt, go.sum, etc.) for mechanize, including transitive dependencies — a direct dependency you never call can still pull in a vulnerable version.
Fix
Update mechanize to 2.7.7 or later, then make sure no transitive (indirect) dependency still pins the vulnerable range — O3 confirms CVE-2021-21289 is resolved across your whole dependency graph.
Workarounds
Stop passing untrusted input into the interpreter or shell: call the affected binary with an argument array rather than a composed command string, reject anything outside a strict allowlist of expected values, and run the component under an account that cannot reach beyond the work it legitimately does.
Frequently Asked Questions
Is CVE-2021-21289 in your dependencies?
Find it across RubyGems, including transitive dependencies.