Question: how to deal with Regexp::Timeout in _decode_uri_component?
还没有人认领这个 Issue。
评估
调研方向
从 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:
- https://github.com/ruby/uri/blob/v0.13.3/lib/uri/common.rb#L400
- https://github.com/ruby/uri/blob/v1.1.1/lib/uri/common.rb#L464
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
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/uri 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 48/100
-
难度 1/5 1 小时以内 新手友好度 25/100
-
难度 3/5 1-2 天 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 52/100
-
难度 4/5 3-5 天 新手友好度 45/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
riscv/riscv-unified-db#2626 ·
-
Component: GLib
难度 2/5 1-3 小时 新手友好度 70/100
-
ds-drift
难度 2/5 1-3 小时 新手友好度 70/100
we-promise/sure#3693 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
simp/pupmod-simp-simp#395 ·