Skip to content
Crypto Disclosed report Intermediate CVE-2023-27531

Unexpected deserialization in Kredis

Kredis casts JSON values using JSON.load, which honours the json_class key and can instantiate classes that define json_create rather than returning a plain hash. An attacker who controls a value stored through Kredis.json could cause unexpected object types to be deserialised, and if json/add/core is loaded, a regular expression could be revived, risking ReDoS.

ooooooo_q Follow

Originally disclosed on HackerOne

  • Triaged in 25 days
  • Fixed in 6 months
  • Disclosed after 11 months

Unexpected classes may be deserialized because JSON.load is used to cast json in Kredis.

https://github.com/rails/kredis/blob/v1.3.0/lib/kredis/type/json.rb

Ruby
module Kredis
  module Type
    class Json < ActiveModel::Type::Value
      def type
        :json
      end

      def cast_value(value)
        JSON.load(value)
      end

PoC

prepare kredis with rails

❯ rails new rails_server -G -M -O -C -A -J -T
# Rails 7.0.4 install

❯ cd rails_server

# Edit Gemfile to uncomment `gem "kredis"` 
❯ bundle install
# kredis 1.3.0 install

❯ rails kredis:install
Ruby
❯ bundle exec rails c
Loading development environment (Rails 7.0.4)
irb(main):001:0> abc = 'abc'.to_json_raw_object
=> {"json_class"=>"String", "raw"=>[97, 98, 99]}

irb(main):002:0> json = Kredis.json "json_load"
=>
#<Kredis::Types::Scalar:0x00000001099ea250
...

irb(main):003:0> json.value = abc
=> {"json_class"=>"String", "raw"=>[97, 98, 99]}

irb(main):004:0> json.value
=> "abc"

The return value of json.value should be a hash object, but it is deserialized as a string object.

Ruby
irb(main):005:0> json.value = /test/
=> /test/

irb(main):006:0> json.value
=> "(?-mix:test)"

irb(main):007:0> json.value = /test/.as_json
=> "(?-mix:test)"

irb(main):008:0> json.value
=> "(?-mix:test)"

irb(main):009:0> require 'json/add/core'
=> true

irb(main):010:0> json.value = /test/.as_json
=> {"json_class"=>"Regexp", "o"=>0, "s"=>"test"}

irb(main):011:0> json.value
=> /test/

If json/add/core is loaded, classes such as regular expressions can also be deserialized.

Impact

If a hash is passed to Kredis.json by user input, reading the value may cause unexpected problems.

The only deserializable classes are those with self.json_create declared, usually String class are possible.(https://github.com/flori/json/blob/v2.6.2/lib/json/pure/generator.rb#L434)

If json/add/core is loaded, it is possible to deserialize RegExp, etc., thus risking ReDoS, etc.

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 JSON type used JSON.load, which honours the json_class field and calls json_create, instead of a parser that only produces plain data.

Impact

If user input reaches Kredis.json, reading the value may cause unexpected problems; only classes declaring json_create can be revived, and with json/add/core loaded a regular expression could be revived, risking ReDoS.

What the attacker needed

User input stored via Kredis.json; json/add/core loaded for RegExp revival

How it was found

  1. Create a Rails app and enable the kredis gem, then run the kredis install task.
  2. Open a Rails console and build a JSON object that names a class and carries raw bytes.
  3. Store that object in a Kredis.json key and read the value back.
  4. Observe that the value returns as a string rather than a hash.
  5. Load json/add/core, store a regular expression as JSON, and read it back as a live regular expression.

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.

When a program reads JSON text, it can either build plain data (numbers, text, lists) or, with a looser parser, let the JSON itself name a class to create. Kredis used the looser parser, so a value that looked like ordinary data could come back as a different kind of object. The report shows a string and a regular expression coming back as objects instead of a hash.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

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.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.