RubySec

Providing security resources for the Ruby community

CVE-2023-22794 (activerecord): SQL Injection Vulnerability via ActiveRecord comments

ADVISORIES

GEM

activerecord

FRAMEWORK

Ruby on Rails

SEVERITY

CVSS v3.x: 8.8 (High)

UNAFFECTED VERSIONS

  • < 6.0.0

PATCHED VERSIONS

  • ~> 6.0.6, >= 6.0.6.1
  • ~> 6.1.7, >= 6.1.7.1
  • >= 7.0.4.1

DESCRIPTION

There is a possible vulnerability in ActiveRecord related to the sanitization of comments. This vulnerability has been assigned the CVE identifier CVE-2023-22794.

Versions Affected: >= 6.0.0 Not affected: < 6.0.0 Fixed Versions: 6.0.6.1, 6.1.7.1, 7.0.4.1

Impact

Previously the implementation of escaping for comments was insufficient for

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 with insufficient sanitization and be able to inject SQL outside of the comment.

In most cases these interfaces won’t be used with user input and users should avoid doing so.

Example vulnerable code:

Post.where(id: 1).annotate(&quot;#{params[:user_input]}&quot;)

Post.where(id: 1).optimizer_hints(&quot;#{params[:user_input]}&quot;)

Example vulnerable QueryLogs configuration (the default configuration is not vulnerable):

config.active_record.query_log_tags = [
  {
    something: -&gt; { &lt;some value including user input&gt; }
  }
]

All users running an affected release should either upgrade or use one of the workarounds immediately.

Workarounds

Avoid passing user input to annotate and avoid using QueryLogs configuration which can include user input.

RELATED