Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a better way to test :admin security
    primarykey
    data
    text
    <p>I am going through Hartl's Rails Tutorial. I'm up to the first exercise of 9.6, where he asks me to test that the User <code>admin</code> attribute isn't accessible. The justification is earlier in the book:</p> <p>After <a href="http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#code-attr_accessible_review" rel="nofollow">Listing 9.42</a>, Hartl's Rails Tutorial says</p> <blockquote> <p>If we omitted the attr_accessible list in the User model (or foolishly added :admin to the list), a malicious user could send a PUT request as follows:</p> <p><code>put /users/17?admin=1</code></p> </blockquote> <p>The corresponding exercise (<a href="http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec-updating_deleting_exercises" rel="nofollow">exercise 9.6.1</a>) in the tutorial says</p> <blockquote> <p>add a test to verify that the User admin attribute isn’t accessible</p> </blockquote> <p><strong>I have completed that test</strong> with this code in <code>user_spec.rb</code>:</p> <pre><code>expect do @user.update_attributes(:admin =&gt; true) end.to raise_error(ActiveModel::MassAssignmentSecurity::Error) </code></pre> <p>But I used stackoverflow to get that test. <strong>This was my original idea</strong> (in <code>user_pages_spec.rb</code>):</p> <pre><code>expect do put user_path(user) + "?admin=1" end.to raise_error(ActiveModel::MassAssignmentSecurity::Error) # or some other error </code></pre> <p><strong>But I couldn't get it to work.</strong></p> <p>So my questions are:</p> <ol> <li><p><strong>Is my idea possible?</strong> Isn't it better to test directly for what a potential hacker might do from the command line? Isn't that the idea of Capybara, testing user actions?</p></li> <li><p><strong>If it is possible, is there a difference between testing mass assignment and testing the PUT action?</strong></p></li> <li><p><strong>If it isn't possible, why?</strong> Is it just not necessary or am I missing something here?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload