Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http token parser bug #7312

Merged
merged 1 commit into from Dec 21, 2012

Conversation

krainboltgreene
Copy link
Contributor

Fixing a problem detailed in #7311. Not ready to pull until that discussion is resolved.

@arunagw
Copy link
Member

arunagw commented Aug 10, 2012

Please also squash commits into one :-)

@krainboltgreene
Copy link
Contributor Author

Yup, will do, I still have tests to write and code might change when the discussion is over.

@drogus
Copy link
Member

drogus commented Aug 11, 2012

@krainboltgreene
Copy link
Contributor Author

Tests showing the bug:

Running tests:

....................FFFF

Finished tests in 0.057636s, 416.4064 tests/s, 815.4626 assertions/s.

  1. Failure: test_0021_token_and_options returns correct token, equal bug(HttpTokenAuthenticationTest) [/Users/krainboltgreene/Code/Ruby/rails/rails/actionpack/test/controller/http_token_authentication_test.rb:120]:
    Expected: "rcHu+HzSFw89Ypyhn/896A=="
    Actual: "rcHu+HzSFw89Ypyhn/896A"

  2. Failure: test_0022_token_and_options returns correct token, after equal bug(HttpTokenAuthenticationTest) [/Users/krainboltgreene/Code/Ruby/rails/rails/actionpack/test/controller/http_token_authentication_test.rb:127]:
    Expected: "rcHu+=HzSFw89Ypyhn/896A==f34"
    Actual: "rcHu+"

  3. Failure: test_0023_token_and_options returns correct token, slash bug(HttpTokenAuthenticationTest) [/Users/krainboltgreene/Code/Ruby/rails/rails/actionpack/test/controller/http_token_authentication_test.rb:134]:
    Expected: "rcHu+\"/896A"
    Actual: "rcHu+\"/896A"

  4. Failure: test_0024_token_and_options returns correct token, slash quote bug(HttpTokenAuthenticationTest) [/Users/krainboltgreene/Code/Ruby/rails/rails/actionpack/test/controller/http_token_authentication_test.rb:141]:
    Expected: "\"quote\" pretty"
    Actual: ""quote" pretty"

24 tests, 47 assertions, 4 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1): [/Users/krainboltgreene/.rvm/rubies/ruby-1....]

Tasks: TOP => test => test_action_pack
(See full trace by running task with --trace)

@krainboltgreene
Copy link
Contributor Author

I've refactored this pull request to add tests showing the bugs in the initial code and re-added the fixes. Here are the tests after the fixes:

Running tests:

.......................

Finished tests in 0.053864s, 427.0013 tests/s, 816.8721 assertions/s.

23 tests, 44 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 4433

Running tests:

.......................

Finished tests in 0.048848s, 470.8483 tests/s, 900.7534 assertions/s.

23 tests, 44 assertions, 0 failures, 0 errors, 0 skips

end
end

def token_params_from(authorization)
raw = authorization.sub(%r/^Token /, '').split %r/"\s*(?:,|;|\t+)\s*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 times in this diff you're using %r/.../ do declare a regular expression. Why not just /.../?

In my code, I only use %r when I don't want to escape forward slashes in my regex, e.g.

url =~ %r{^http://.*$}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally there was a complex expression that had escapes. This was a quick way to make it easier to read. My refactor removed the need for he complex expression, but forgot to remove the change in syntax.

On Nov 7, 2012, at 1:35 PM, Benjamin Quorning notifications@github.com wrote:

In actionpack/lib/action_controller/metal/http_authentication.rb:

     end
   end
  •  def token_params_from(authorization)
    
  •    raw = authorization.sub(%r/^Token /, '').split %r/"\s_(?:,|;|\t+)\s_/
    
    4 times in this diff you're using %r/.../ do declare a regular expression. Why not just /.../?

In my code, I only use %r when I don't want to escape forward slashes in my regex, e.g.

url =~ %r{^http://.*$}

Reply to this email directly or view it on GitHub.

test "authentication request with valid credential" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials('"quote" pretty', :algorithm => 'test')
get :display
test "token_and_options returns correct token, equal bug" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be referencing the bug here. There's no more bug after the code is done. ;)

@steveklabnik
Copy link
Member

@krainboltgreene , I'm interested in merging this. Things I need:

  1. rename those tests
  2. take off the %rs as discussed
  3. squash
  4. changelog

I'm also gonna double check with someone else on core, but this all seems good to me, and would solve #8208, #8196, and #7311.

@krainboltgreene
Copy link
Contributor Author

Sure, I'll get it in tonight.

Adding a test for the equal trun bug

Adding a test for the after equal trunc bug

Adding a test for the slash bug

Adding a test for the slash quote bug

Adding a helper method for creating a sample request object with token

Writing a method to create params array from raw params

Writing a method to rewrite param values in the params

Writing a method to get the token params from an authorization value

Refactoring the token_and_options method to fix bugs

Removing unnessecary test

A constant for this shared regex seemed appropriate

Wanting to split up this logic

Adding small documentation pieces
@krainboltgreene
Copy link
Contributor Author

Run options: --seed 29311

# Running tests:

.......................

Finished tests in 0.036201s, 635.3416 tests/s, 1215.4360 assertions/s.

23 tests, 44 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 49338

# Running tests:

.......................

Finished tests in 0.051028s, 450.7329 tests/s, 862.2717 assertions/s.

23 tests, 44 assertions, 0 failures, 0 errors, 0 skips

@steveklabnik
Copy link
Member

🤘

If I can get a 👍 from one other person, then I'll put this in. @tenderlove @NZKoz, or someone else on core? It looks good to me.

@kalv
Copy link

kalv commented Dec 19, 2012

This work fixes my issues with handling the "=" character and other characters than what I did in my pull request #8208

@rafaelfranca
Copy link
Member

Seems good to me. :shipit:

steveklabnik added a commit that referenced this pull request Dec 21, 2012
@steveklabnik steveklabnik merged commit 625699a into rails:master Dec 21, 2012
@steveklabnik
Copy link
Member

Awesome. :shipit:

@krainboltgreene, thank you so much for your contribution. I know we took a long time to take care of this. ❤️

@krainboltgreene
Copy link
Contributor Author

Aww yeah

But seriously, was worth the wait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants