Rails:ParameterFilter::compiled_filter 尝试复制符号

发布于 2024-10-21 11:33:11 字数 1419 浏览 2 评论 0原文

我正在使用 Rails 异常通知程序 gem 运行 Rails3。当发生异常并且应该发送电子邮件时,我从 ParameterFilter 类收到异常。我在 Rails 源代码中发现了问题,但不确定继续的最佳方法。

问题出现在 ActionDispatch::Http::ParameterFilter 中。在compile_filter方法中,当key是符号时,第38行会出现错误:key = key.dup,因为符号不可重复。这是来源:

def compiled_filter
    ...
    elsif blocks.present?
        key = key.dup
        value = value.dup if value.duplicable?
        blocks.each { |b| b.call(key, value) }
    end

我看到他们只在可复制时对value调用dup。如果我修补源代码,仅在 key可复制 时对 key 调用 dup,那么我的问题就消失了。我假设作者将该条件放在 value 而不是 key 上是有原因的,所以我很好奇是否有人对这段代码有更好的理解。

仅当您将块添加到 application.rb 中的过滤器参数时,才会发生此错误。因此,也许我原来的问题有一个解决方法,不需要在这里使用块。如果您有兴趣,请参阅我同事的问题 Rails:过滤敏感数据日志中的 JSON 参数

出现此问题的关键是 :action。这来自 Rails,我不知道是否有任何方法可以强制它成为字符串。

我提交了一个 Rails 错误 https:/ /rails.lighthouseapp.com/projects/8994/tickets/6557-symbol-duplication-error-in-parameterfilter-compiled_filter 并且我已经准备好一个补丁,添加 if key.duplicable?对于 key.dup 行,我正在寻找有关这是否是正确解决方案的输入。

I'm running rails3 with rails exception-notifier gem. When an exception occurs, and an email should be sent, I'm getting an exception from the ParameterFilter class. I've found the problem in the rails source, and am not sure the best way to proceed.

The problem occurs in ActionDispatch::Http::ParameterFilter. In the compiled_filter method, an error occurs on line 38: key = key.dup when key is a symbol, because symbols are not duplicable. Here is the source:

def compiled_filter
    ...
    elsif blocks.present?
        key = key.dup
        value = value.dup if value.duplicable?
        blocks.each { |b| b.call(key, value) }
    end

I see that they only call dup on value when it is duplicable. If I patch the source to only call dup on key when key is duplicable, then my problem goes away. I'm assuming there is a reason why the author put that condition on value and not key, so I'm curious if someone out there has a better understanding of this code.

This error only occurs when you add a block to your filter params in application.rb. So, maybe there is a workaround for my original issue that does not require using a block here. If you're interested see my coworker's question Rails: Filter sensitive data in JSON parameter from logs

The key for which this is a problem is :action. This comes from rails and I don't know if there is any way to force it to be a string instead.

I filed a rails bug https://rails.lighthouseapp.com/projects/8994/tickets/6557-symbol-duplication-error-in-parameterfilter-compiled_filter and I have a patch ready that adds if key.duplicable? to the key.dup line, I'm looking for input on whether or not that is the right solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-10-28 11:33:11

这看起来像是 Rails 中的一个错误。要么键应该是字符串而不是符号,要么dup应该受到duplicable?保护。

您应该在 https://rails.lighthouseapp.com/ 提交错误,如果可能的话,包括一个最小的测试用例。

This looks like a bug in Rails. Either the key should be a string rather than a symbol, or the dup should be protected by duplicable?.

You should file a bug at https://rails.lighthouseapp.com/, including a minimal test case if possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文