# CVE-2023-22794: SQL Injection Vulnerability via ActiveRecord comments

> Insufficient sanitization in ActiveRecord allows SQL injection via malicious input in query annotations or optimizer hints.

- **Severity:** High 8.8 (`CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H`)
- **Exploited in the wild:** Not known
- **Login needed:** Any signed-in user
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 9 Feb 2023
- **Updated:** 2 Aug 2024
- **Also known as:** DEBIAN-CVE-2023-22794, GHSA-hq7p-j377-6v63, UBUNTU-CVE-2023-22794

## What is CVE-2023-22794?

CVE-2023-22794 is a high-severity SQL injection vulnerability in https://github.com/rails/rails, published on 9 Feb 2023. Any signed-in user can exploit it over the network, with no action from the victim.

### Official description

A vulnerability in ActiveRecord <6.0.6.1, v6.1.7.1 and v7.0.4.1 related to the sanitization of comments. If malicious user input is passed to either the `annotate` query method, the `optimizer_hints` query method, or through the QueryLogs interface which automatically adds annotations, it may be sent to the database withinsufficient sanitization and be able to inject SQL outside of the comment.

Weakness: CWE-89 (Improper Neutralization of Special Elements used in an SQL Command)

## Which versions are affected by CVE-2023-22794?

6 open source packages ship an affected version, including activerecord. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `activerecord` | RubyGems | From 6.0.0 | 6.0.6.1 |
| `activerecord` | RubyGems | From 6.0.0 | 6.0.6.1 |
| `activerecord` | RubyGems | From 6.1.0 | 6.1.7.1 |
| `activerecord` | RubyGems | From 6.1.0 | 6.1.7.1 |
| `activerecord` | RubyGems | From 7.0.0 | 7.0.4.1 |
| `activerecord` | RubyGems | From 7.0.0 | 7.0.4.1 |

Products: Activerecord Project Activerecord.

## How do I fix CVE-2023-22794?

Upgrade activerecord to a fixed version for your release line: 6.0.6.1, 6.1.7.1 or 7.0.4.1.

## Is CVE-2023-22794 being exploited?

There is no known exploitation of CVE-2023-22794 in the wild and no public exploit code.

## Other vulnerabilities in https://github.com/rails/rails

- [CVE-2022-32224](https://insights.sbomify.com/CVE-2022-32224/): Active Record RCE bug with Serialized Columns (critical 9.8)
- [CVE-2022-44566](https://insights.sbomify.com/CVE-2022-44566/): Denial of Service Vulnerability in ActiveRecord's PostgreSQL adapter (high 7.5)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2023-22794 and fix it if it is affected.

What it is: CVE-2023-22794 is a high-severity SQL injection vulnerability in https://github.com/rails/rails, published on 9 Feb 2023. Any signed-in user can exploit it over the network, with no action from the victim.
Exploitation: There is no known exploitation of CVE-2023-22794 in the wild and no public exploit code.

Affected packages and fixed versions:
- activerecord (RubyGems): affected from 6.0.0; fixed in 6.0.6.1
- activerecord (RubyGems): affected from 6.0.0; fixed in 6.0.6.1
- activerecord (RubyGems): affected from 6.1.0; fixed in 6.1.7.1
- activerecord (RubyGems): affected from 6.1.0; fixed in 6.1.7.1
- activerecord (RubyGems): affected from 7.0.0; fixed in 7.0.4.1
- activerecord (RubyGems): affected from 7.0.0; fixed in 7.0.4.1

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2023-22794/ (Markdown: https://insights.sbomify.com/CVE-2023-22794/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2023-22794
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2023-22794
- OSV: https://osv.dev/vulnerability/CVE-2023-22794

---
From sbomify Insights: https://insights.sbomify.com/CVE-2023-22794/
Summaries are generated and may contain mistakes; the official description above is the record.
