JWT::EncodedToken raises TypeError for a non-object header

已关闭 适合新手
#780 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

已经有一个关联 PR 被合并了。

  • #783 来自 @anakinj —— 已合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
ruby

调研方向

从 lib/jwt/encoded_token.rb 开始,其中 valid_signature? 在没有检查解码后的 header 是否为 Hash 的情况下读取 header['alg']。与 JWT::Decode#verify_algo 中的检查进行比较,并使用 issue 中的代码片段复现,以查看原始的 TypeError。当非对象的 header 使 verify_signature! 抛出 JWT::MalformedTokenError,并且在现有的编码令牌测试旁边添加了回归测试时,即为完成。

由索引模型根据 Issue 内容生成。

描述

bug

If a token's header is valid JSON but not an object, JWT::EncodedToken#verify_signature! raises a raw TypeError instead of a JWT error. Applications that rescue JWT errors won't catch it, so untrusted input could produce a 500 rather than a 401. JWT.decode handles the same token correctly.

rsa = OpenSSL::PKey::RSA.generate(2048)
b64 = ->(value) { JWT::Base64.url_encode(value) }
token = "#{b64.call('[]')}.#{b64.call('{}')}.#{b64.call('x')}"

JWT::EncodedToken.new(token).verify_signature!(algorithm: 'RS256', key: rsa.public_key)
# => TypeError: no implicit conversion of String into Integer

JWT.decode(token, rsa.public_key, true, algorithm: 'RS256')
# => JWT::MalformedTokenError: Token header not a JSON object

Reproduced on 3.3.1. lib/jwt/encoded_token.rb is unchanged on main.

Cause: EncodedToken#valid_signature? reads header['alg'] without checking that the header is a Hash. JWT::Decode#verify_algo has that check.

Suggestion: Raise JWT::MalformedTokenError when the decoded header isn't an object, covering valid_signature? and any other direct header[...] access on this path.

Posted by Claude on behalf of @anakinj.

主要语言
Ruby
星标
3.7k
派生
381
平均合并
14 小时 13 分钟
30 天内合并 PR
13

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

jwt/ruby-jwt 的其他 Issue

查看 jwt/ruby-jwt 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

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