Skip to content
Business logic Disclosed report Intermediate

Validation bypass for queries generated for PostgreSQL

When Rails builds a PostgreSQL query with a bound parameter containing a null byte, the string after the null byte is silently dropped from the generated SQL. An attacker could use this to slip past Ruby-side blacklist or suffix checks that inspect the full input string.

ooooooo_q Follow

Originally disclosed on HackerOne

Bounty$1,500 View original
  • Paid in 3 months
  • Disclosed after 3 months

When using DB for PostgreSQL, I discovered that if a parameter of a query contains null character, there is a pattern in which subsequent strings are lost.

how to reproduce

Prepare the environment

$ rails new postgresql_rails -TB --database=postgresql
$ cd postgresql_rails

$ bundle exec ruby -v
> ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
$ bundle exec rails --version
> Rails 5.2.1

$ bundle install

Prepare models and schemas.

$ bundle exec rails generate model Article title:string text:text
$ bundle exec rails db:create
$ bundle exec rails db:migrate

Save test data.

Ruby
$ bundle exec rails console
Loading development environment (Rails 5.2.1)
irb(main):001:0> Article.create(title: 'test title', text: 'dummy')
   (0.1ms)  BEGIN
  Article Create (3.7ms)  INSERT INTO "articles" ("title", "text", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "test title"], ["text", "dummy"], ["created_at", "2018-08-13 13:31:37.689587"], ["updated_at", "2018-08-13 13:31:37.689587"]]
   (1.3ms)  COMMIT
=> #<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">

Confirm query

Ruby
 $ bundle exec rails console
Loading development environment (Rails 5.2.1)

# Case A
irb(main):001:0> Article.where(title: "test title")
  Article Load (0.3ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."title" = $1 LIMIT $2  [["title", "test title"], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">]>

irb(main):002:0> Article.where(title: "test title\0suffix")
  Article Load (0.3ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."title" = $1 LIMIT $2  [["title", "test title\u0000suffix"], ["LIMIT", 11]]
Traceback (most recent call last):
ArgumentError (string contains null byte)


# Case B
irb(main):003:0> Article.find_by_title("test title")
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."title" = $1 LIMIT $2  [["title", "test title"], ["LIMIT", 1]]
=> #<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">

irb(main):004:0> Article.find_by_title("test title\0suffix")
  Article Load (0.5ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."title" = $1 LIMIT $2  [["title", "test title\u0000suffix"], ["LIMIT", 1]]
Traceback (most recent call last):
        1: from (irb):4
ArgumentError (string contains null byte)


# Case C
irb(main):005:0> Article.where("title = ?", "test title")
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE (title = 'test title') LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">]>

irb(main):006:0> Article.where("title = ?", "test title\0suffix")
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE (title = 'test title') LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">]>


# Case D
irb(main):007:0> Article.where("title = :title", {title: "test title"})
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE (title = 'test title') LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">]>

irb(main):008:0> Article.where("title = :title", {title: "test title\0suffix"})
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE (title = 'test title') LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, title: "test title", text: "dummy", created_at: "2018-08-13 13:31:37", updated_at: "2018-08-13 13:31:37">]>

In Case A and Case B, an error has occurred when null characters are included. On the other hand, in Case C and Case D, SQL is generated in such a way that the character string after the null character (\0suffix) is ignored.

sanitize_sql_array

For comparison, list the results of sanitize_sql_array for each DB.

PostgreSQL

Ruby
$ bundle exec rails console
Loading development environment (Rails 5.2.1)
irb(main):001:0> ActiveRecord::Base.send(:sanitize_sql_array,['SELECT * from articles WHERE title = ?', "abc\0suffix"])
=> "SELECT * from articles WHERE title = 'abc'"

MySQL

Ruby
$ bundle exec rails console
Loading development environment (Rails 5.2.1)
irb(main):001:0> ActiveRecord::Base.send(:sanitize_sql_array,['SELECT * from articles WHERE title = ?', "abc\0suffix"])
   (0.9ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
=> "SELECT * from articles WHERE title = 'abc\\0suffix'"

SQLite3

Ruby
$ bundle exec rails console
Running via Spring preloader in process 55281
Loading development environment (Rails 5.2.1)
irb(main):001:0> ActiveRecord::Base.send(:sanitize_sql_array,['SELECT * from articles WHERE title = ?', "abc\0suffix"])
=> "SELECT * from articles WHERE title = 'abc\u0000suffix'"

Impact

In this problem, SQL injection can not be done, but bypass is possible when ruby side is performing blacklist and suffix validation (extension checking etc.).

sample code

Ruby
title = "test\0dummy"
title != 'test' ?  Article.where("title = ?", title) : nil
> Article Load (1.9ms)  SELECT  "articles".* FROM "articles" WHERE (title = 'test') LIMIT $1  [["LIMIT", 11]]

The original report as published on HackerOne, by ooooooo_q. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the PostgreSQL adapter's string quoting stops at the first null byte, so the remainder of the parameter is discarded before the query is sent.

Impact

SQL injection itself is not possible, but an attacker could bypass Ruby-side blacklist or suffix validation, which may matter where such checks guard file extensions or similar rules.

What the attacker needed

Application uses PostgreSQL adapter; application validates input in Ruby before query; attacker can supply the parameter value

How it was found

  1. Create a Rails app using PostgreSQL and generate a model with a string column.
  2. Insert a test record and query it with a normal value to confirm the query works.
  3. Query using a placeholder form with a value containing a null byte followed by extra text.
  4. Observe that the generated SQL only contains the part before the null byte.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A null byte is a special character that some programs treat as the end of a string. Rails was cutting the value off at that character when building a database query, so the database only saw the part before it. If an application checked the whole input in Ruby but the database saw a shorter version, the check could be fooled.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by ooooooo_q

View all

53 more disclosed reports, best first.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.