Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Question: how to deal with Regexp::Timeout in _decode_uri_component?

オープン
#233 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
静か
技術スタック
ruby
領域
backend

調査の方向性

lib/uri/common.rb の参照されている _decode_uri_component パスから始め、大きなパーセントエンコード済みペイロードで Regexp::TimeoutError を再現します。maintainers と望ましい URI の動作について議論して定義し、その後 timeout のケースのカバレッジを追加して、選択したアプローチが報告された入力を処理できることを確認します。

索引モデルが issue の本文から書いたものです。

説明

Hi folks, thanks for maintaining the URI gem!

I faced the following issue with a 65MB mime-body payload and over 13 million percent-encoded characters:

Regexp::TimeoutError POST /rails/action_mailbox/mailgun/inbound_emails/mime

vendor/bundle/ruby/3.3.0/gems/uri-0.13.3/lib/uri/common.rb:400:in `match?': regexp match timeout (Regexp::TimeoutError)
    from vendor/bundle/ruby/3.3.0/gems/uri-0.13.3/lib/uri/common.rb:400:in `_decode_uri_component'

Ref:

My workaround was to monkey patch the decode_www_form_component to avoid the Regexp code path if it times out:

module URIFormComponentLinearDecode
  ORIGINAL_DECODE_WWW_FORM_COMPONENT = URI.method(:decode_www_form_component)

  DECODE_TABLE = URI.const_get(:TBLDECWWWCOMP_)

  def decode_www_form_component(str, enc = Encoding::UTF_8)
    ORIGINAL_DECODE_WWW_FORM_COMPONENT.call(str, enc)
  rescue Regexp::TimeoutError
    raise unless str.is_a?(String)

    Rails.logger.info("[URIFormComponentLinearDecode] bytesize=#{str.bytesize}")

    linear_decode_www_form_component(str, enc)
  end

  private

  def linear_decode_www_form_component(str, enc)
    source = str.b
    output = String.new(capacity: source.bytesize).b
    index = 0

    while index < source.bytesize
      byte = source.getbyte(index)

      case byte
      when 37 # "%"
        raise ArgumentError, "invalid %-encoding (#{str})" unless index + 2 < source.bytesize

        encoded = source.byteslice(index, 3)
        decoded = DECODE_TABLE[encoded]

        raise ArgumentError, "invalid %-encoding (#{str})" unless decoded

        output << decoded
        index += 3
      when 43 # "+"
        output << DECODE_TABLE["+"]
        index += 1
      else
        output << byte
        index += 1
      end
    end

    output.force_encoding(enc)
  end
end

URI.singleton_class.prepend(URIFormComponentLinearDecode)

I was wondering:

  • Did you guys face this problem before?
  • Do you have a better approach to it?
  • Do you think a solution to this issue belongs in the URI codebase?
  • Do you think it would make sense to use a native function in this case?

I'm happy to contribute with a PR if you would like me to. Please let me know if you have any thoughts.

Thanks.

主要言語
Ruby
スター
125
フォーク
65
平均マージ
6時間 4分
マージ済み PR(30日)
2

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/uri のほかの issue

ruby/uri の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。