为了确保我的应用程序不会受到this exploit的攻击,我try 在RSpec中创建一个控制器测试来涵盖它.为此,我需要能够发布原始JSON,但我似乎还没有找到这样做的方法.在做了一些研究后,我发现至少过go 有一种方法可以使用RAW_POST_DATA头来做到这一点,但现在似乎行不通了:

it "should not be exploitable by using an integer token value" do
  request.env["CONTENT_TYPE"] = "application/json"
  request.env["RAW_POST_DATA"]  = { token: 0 }.to_json
  post :reset_password
end

When I look at the params hash, token is not set at all, and it just contains { "controller" => "user", "action" => "reset_password" }. I get the same results when trying to use XML, or even when trying to just use regular post data, in all cases, it seems to not set it period.

I know that with the recent Rails vulnerabilities, the way parameters are hashed was changed, but is there still a way to post raw data through RSpec? Can I somehow directly use Rack::Test::Methods?

推荐答案

据我所知,在控制器规范中不再可能发送原始POST数据.但是,在请求规范中,发送原始POST数据非常容易:

describe "Example", :type => :request do
  params = { token: 0 }
  post "/user/reset_password", params.to_json, { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
  #=> params contains { "controller" => "user", "action" => "reset_password", "token" => 0 }
end

Json相关问答推荐

Allof Indide的JSON模式之一

Jolt变换将字段移动到数组的每个元素中

在AWS步骤函数中将字符串解析为JSON&S映射状态

织女星-没有循环的动画条形图第二部分(实际上是织女星)

从Postgres表中的JSON中提取值

使用jq过滤复杂json对象中的数据

将 std::可选值存储到 json 文件 C++

删除 JOLT 中的方括号

匹配来自不同数组的值

如何在 JSonPath 中按值查找列表中的所有元素

使用 jolt 将对象数组转换为数组

jq可以在两个JSON对象列表中依次添加对象吗?

jq json - 按键名 Select

如何在 Android Studio 中将 List 字段作为空列表[]返回?

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

在 Perl Mojolicious 中呈现 JSON 时防止转义字符

JSON extract\set 的 SQLite JSON1 示例

哪个更好:Json 或 XML (PHP)

apple-app-site-association json 文件是否会在应用程序中更新?

在视图中将 .Net 对象转换为 JSON 对象