为了确保我的应用程序不易受this exploit攻击,我试图在RSpec中创建一个控制器测试来覆盖它。为了做到这一点,我需要能够发布原始JSON,但我似乎没有找到办法做到这一点。在进行一些研究时,我已经确定使用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
当我查看params散列时,令牌根本没有设置,它只包含{ "controller" => "user", "action" => "reset_password" }
。我在尝试使用XML时得到了相同的结果,或者甚至在尝试使用常规的帖子数据时,在所有情况下,它似乎都没有设置它的周期。
我知道在最近的Rails漏洞中,参数被散列的方式已经改变了,但是还有办法通过RSpec发布原始数据吗?我可以以某种方式直接使用Rack::Test::Methods
吗?
答案 0 :(得分:73)
据我所知,在控制器规范中不再可能发送原始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
答案 1 :(得分:11)
这是将原始JSON发送到控制器操作(Rails 3 +)的方法:
我们假设我们有这样的路线:
post "/users/:username/posts" => "posts#create"
让我们说你希望身体成为你所做的json:
JSON.parse(request.body.read)
然后您的测试将如下所示:
it "should create a post from a json body" do
json_payload = '{"message": "My opinion is very important"}'
post :create, json_payload, {format: 'json', username: "larry" }
end
{format: 'json'}
是实现这一目标的神奇之处。另外,如果我们查看TestCase#post http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-process的源代码,您可以看到它在操作(json_payload)之后需要第一个参数,如果它是一个字符串,它将其设置为原始帖子体,并解析其余的正常的args。
指出rspec只是Rails测试架构之上的DSL也很重要。上面的post
方法是ActionController :: TestCase#post,而不是某些rspec发明。
答案 2 :(得分:9)
我们在控制器测试中完成的工作是明确设置RAW_POST_DATA:
before do
@request.env['RAW_POST_DATA'] = payload.to_json
post :my_action
end
答案 3 :(得分:8)
Rails 5示例:
RSpec.describe "Sessions responds to JSON", :type => :request do
scenario 'with correct authentication' do
params = {id: 1, format: :json}
post "/users/sign_in", params: params.to_json, headers: { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
expect(response.header['Content-Type']).to include 'application/json'
end
end
答案 4 :(得分:5)
以下是发送原始json数据的控制器测试的完整工作示例:
describe UsersController, :type => :controller do
describe "#update" do
context 'when resource is found' do
before(:each) do
@user = FactoryGirl.create(:user)
end
it 'updates the resource with valid data' do
@request.headers['Content-Type'] = 'application/vnd.api+json'
old_email = @user.email
new_email = Faker::Internet.email
jsondata =
{
"data" => {
"type" => "users",
"id" => @user.id,
"attributes" => {
"email" => new_email
}
}
}
patch :update, jsondata.to_json, jsondata.merge({:id => old_id})
expect(response.status).to eq(200)
json_response = JSON.parse(response.body)
expect(json_response['data']['id']).to eq(@user.id)
expect(json_response['data']['attributes']['email']).to eq(new_email)
end
end
end
end
重要的部分是:
@request.headers['Content-Type'] = 'application/vnd.api+json'
和
patch :update, jsondata.to_json, jsondata.merge({:id => old_id})
第一个确保为您的请求正确设置内容类型,这非常简单。 第二部分让我头疼几个小时,我的初步方法有点不同,但事实证明有Rails bug,这阻止我们在功能测试中发送原始帖子数据(但允许我们在集成测试中),这是一个丑陋的解决方法,但它可以工作(在rails 4.1.8和rspec-rails 3.0.0上)。
答案 5 :(得分:0)
在 Rails 4 上:
params = { shop: { shop_id: new_subscrip.shop.id } }
post api_v1_shop_stats_path, params.to_json, { 'CONTENT_TYPE' => 'application/json',
'ACCEPT' => 'application/json' }