Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#233 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
冷清
技术栈
ruby
领域
backend

调研方向

从 lib/uri/common.rb 中所引用的 _decode_uri_component 路径开始,使用大型百分号编码 payload 重现 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 分钟
30 天内合并 PR
2

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/uri 的其他 Issue

查看 ruby/uri 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。