Commit Graph

5825 Commits

Author SHA1 Message Date
Derek Prior
13fd5586ce
Add redirect_back for safer referrer redirects
`redirect_to :back` is a somewhat common pattern in Rails apps, but it
is not completely safe. There are a number of circumstances where HTTP
referrer information is not available on the request. This happens often
with bot traffic and occasionally to user traffic depending on browser
security settings.

When there is no referrer available on the request, `redirect_to :back`
will raise `ActionController::RedirectBackError`, usually resulting in
an application error.

`redirect_back` takes a required `fallback_location` keyword argument
that specifies the redirect when the referrer information is not
available.  This prevents 500 errors caused by
`ActionController::RedirectBackError`.
2015-12-16 11:42:05 -05:00
Tony Ta
2eda1a98e0 deletes code commented out in 72160d9f 2015-12-15 22:28:50 -08:00
Sean Griffin
1ad94e760d Merge pull request #14212 from tylerhunt/fix-token-regex
Handle tab in token authentication header.
2015-12-15 10:59:54 -07:00
Sam Stephenson
99caf9ae7e Add fragment_cache_key macro for controller-wide fragment cache key prefixes 2015-12-14 19:53:43 -06:00
Sean Griffin
1dc1c8e40b Merge pull request #22564 from maximeg/legit_name_errors
Don't catch all NameError to reraise as ActionController::RoutingError
2015-12-14 10:17:42 -07:00
Prem Sichanugrist
6d4aef984c Make Parameters#to_h and #to_unsafe_h return HWIA
This makes these two methods to be more inline with the previous
behavior of Parameters as Parameters used to be inherited from HWIA.

Fixes #21391
2015-12-14 10:28:54 -05:00
Maxime Garcia
d3dd3847bc Don't catch all NameError to reraise as ActionController::RoutingError #22368 2015-12-12 11:22:08 +01:00
Jon Atack
c6fe614e45 Show redirect response code in assert_response messages
Follow-up to PR #19977, which helpfully added the redirection path to the error message of assert_response if response is a redirection, but which removed the response code, obscuring the type of redirect.

This PR:

- brings back the response code in the error message,

- updates the tests so the new messages can be tested,

- and adds test cases for the change.
2015-12-11 18:18:08 +01:00
Rafael França
b0e5fc2737 Merge pull request #22514 from prathamesh-sonpatki/use-assert-over-assert-predicate
Use assert over assert_predicate in assert_response
2015-12-11 14:11:15 -02:00
Santiago Pastorino
b11bca98bf Merge pull request #20831 from jmbejar/rails-api-json-error-response
Rails API: Ability to return error responses in json format also in development
2015-12-09 15:26:46 -03:00
Jorge Bejar
84e8accd6f Do not add format key to request_params
I did this change but it is affecting how the request params end up
after being processed by the router.

To be in the safe side, I just take the format from the extension in the
URL when is not present in those params and it's being used only for the
`Request#formats` method
2015-12-09 10:53:46 -03:00
Jorge Bejar
6fa2023c81 DebugException initialize with a response_format value 2015-12-09 10:53:45 -03:00
Jorge Bejar
05d89410bf Fix some edge cases in AD::DebugExceptions in rails api apps 2015-12-09 10:53:44 -03:00
Jorge Bejar
83b4e9073f Response when error should be formatted properly in Rails API if local request 2015-12-09 10:53:41 -03:00
eileencodes
b05801754f Fix make_response! when called by serve in RouteSet
All of our tests were testing the `ActionController::Live` behavior in a
standalone environment, without going through the router or behaving
like a real application.

This resulted in `ActionController::Live` throwing the exception
`undefined method 'request' for #<ActionDispatch::Request:0x00000003ad1148>`
because `make_response!` was expecting a response instead of a request.

The expectation of a response came from `set_response!` in non-router
tests setting the response and passing it to `make_response!`. In the
case of an application we would hit `serve` in `RouteSet` first which
would send us to `make_response!` with a request sent instead of a
response.

The changes here remove `set_response!` so `make_response!` always
receives a request.

Thanks to KalabiYau for help with the investigation and solution.

Fixes #22524

[Eileen M. Uchitelle & KalabiYau]
2015-12-09 08:19:07 -05:00
Jorge Bejar
b79bfaadaf Use URL path extension as format in bad params exception handling 2015-12-08 21:23:47 -03:00
eileencodes
3979403781 Change the protect_from_forgery prepend default to false
Per this comment
https://github.com/rails/rails/pull/18334#issuecomment-69234050 we want
`protect_from_forgery` to default to `prepend: false`.

`protect_from_forgery` will now be insterted into the callback chain at the
point it is called in your application. This is useful for cases where you
want to `protect_from_forgery` after you perform required authentication
callbacks or other callbacks that are required to run after forgery protection.

If you want `protect_from_forgery` callbacks to always run first, regardless of
position they are called in your application, then you can add `prepend: true`
to your `protect_from_forgery` call.

Example:

```ruby
protect_from_forgery prepend: true
```
2015-12-07 10:38:26 -05:00
eileencodes
492b134433 Push before_sending to super class
We want to get rid of the `Live::Response` so we are consolidating methods
from `Live::Response` and `Response` by merging them together.

This adds an `#empty` method to the request so we don't need to
hard-code the empty array each time we call an empty
`ActionDispatch::Request`.

The work here is a continuation on combining controller and integration
test code bases into one.
2015-12-06 15:32:40 -05:00
Eileen M. Uchitelle
67688aa739 Merge pull request #22453 from wjessop/use_action_dispatch_default_tld_length
Test against the real value of tld_length unless explicitly set
2015-12-06 13:21:38 -05:00
Prathamesh Sonpatki
b247116210 Use assert over assert_predicate in assert_response
- `assert_predicate` appends its own error message at the end of message
  generated by `assert_response` and because of that the error message
  displays the whole `response` object.
- For eg.

  Expected response to be a <success>, but was a redirect to <http://test.host/posts>.
  Expected #<ActionDispatch::TestResponse:0x007fb1cc1cf6f8....(lambda)>}>> to be successful?.

- Complete message can be found here -
  https://gist.github.com/prathamesh-sonpatki/055afb74b66108e71ded#file-gistfile1-txt-L19.

- After this change the message from `assert_predicate` won't be
  displayed and only message generated by `assert_response` will be shown
  as follows:

  Expected response to be a <success>, but was a redirect to <http://test.host/posts>
2015-12-06 21:39:59 +05:30
Will Jessop
e8bfaa58bd Test against the real value of tld_length unless explicitly set
There were two places where the tld_length default
was hard-coded to 1, both overriding the real default
value of ActionDispatch::Http::URL.tld_length in this
set of tests.

This commit removes both of those, relying on the
actual value of ActionDispatch::Http::URL.tld_length,
unless it's specifically overridden.
2015-12-04 16:40:08 +00:00
Prathamesh Sonpatki
1ee87e42ca Add redirection path in the error message of assert_response if response is :redirect
- If the assert_response is checking for any non-redirect response like
  :success and actual response is :redirect then, the error message displayed
  was -

     Expected response to be a <success>, but was <302>

- This commit adds the redirect path to the error message of
  assert_response if the response is :redirect.
  So above message is changed to -

     Expected response to be a <success>, but was a redirect to <http://test.host/posts/lol>
2015-12-04 13:13:25 +05:30
Arthur Nogueira Neves
3513f80e53 Merge pull request #22371 from yui-knk/better_mount_error
Brush up errors of `ActionDispatch::Routing::Mapper#mount`
2015-11-28 11:29:46 -05:00
yui-knk
f8f7e66f5f Brush up errors of ActionDispatch::Routing::Mapper#mount
* Integrate to raise `ArgumentError`
* Detailed error message when `path` is not defined
* Add a test case, invalid rack app is passed
2015-11-28 10:50:11 +09:00
Arthur Nogueira Neves
9afb0b9c43 Merge pull request #21241 from pdg137/master
In url_for, never append ? when the query string is empty anyway.
2015-11-26 16:10:46 -05:00
Rafael França
e1e6499ede Merge pull request #22263 from mastahyeti/csrf-origin-check
Add option to verify Origin header in CSRF checks

[Jeremy Daer + Rafael Mendonça França]
2015-11-26 14:23:50 -02:00
Ben Toews
85783534fc Add option to verify Origin header in CSRF checks 2015-11-25 15:06:12 -07:00
Rafael França
40acdcfb7c Merge pull request #22172 from tijmenb/fix-source-in-show-exception
Add text template for source code
2015-11-24 02:11:09 -02:00
Sean Griffin
8404ed6408 Merge pull request #17928 from sergey-alekseev/remove-unused-form-data-method 2015-11-23 14:33:11 -07:00
yui-knk
8d23f37b15 Remove arity check for RouteSet#draw
This code was added for migration from Rails 3.1 to upper,
now we are developing Rails 5.
2015-11-21 01:12:45 +09:00
Jake Worth
432c7a5b76 Fix typo [ci skip] 2015-11-12 10:06:44 -06:00
Jerry D'Antonio
23b6f65fd1 Require only necessary concurrent-ruby classes. 2015-11-04 21:12:28 -05:00
Yves Senn
23bb31b158 tests, test should not care wether 9ms or 11ms have passed.
This solves:

```
Expected /Completed 200 OK in [\d]ms/ to match "Completed 200 OK in 943ms".
```
2015-11-04 16:42:27 -05:00
Tijmen Brommet
08689a2d1f Add text template for source code
When a request is made with AJAX and an error occurs, Rails will render
a text-template for the exception instead of the HTML error page
(#11960).

The `.text.erb` variant of the `_source` template is currently missing,
causing HTML to be rendered in the response. This commit adds the text
template.

To keep the page scannable we only only show the first three source
extracts.

Related to #14745.

Before:

```
~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H
'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

    <div class="source " id="frame-source-0">
      <div class="info">
        Extracted source (around line <strong>#3</strong>):
      </div>
      <div class="data">
        <table cellpadding="0" cellspacing="0" class="lines">
          <tr>
```

After:

```
~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H
'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

Extracted source (around line #3):

*3     raise
```
2015-11-03 22:13:10 +00:00
Yuki Nishijima
266455cf25 Deprecate exception#original_exception in favor of exception#cause 2015-11-03 06:54:34 -08:00
Sean Griffin
eb6cdcab39 Merge pull request #21251 from rodzyn/more_param_parser_tests
Add test for parsing application/vnd.api+json
2015-10-30 13:44:04 -06:00
Paul Grayson
e6e056c2c1 In url_for, never append ? when the query string is empty anyway.
It used to behave like this:

    url_for(controller: 'x', action: 'y', q: {})
    # -> "/x/y?"

We previously avoided empty query strings in most cases by removing
nil values, then checking whether params was empty.  But as you can
see above, even non-empty params can yield an empty query string.  So
I changed the code to just directly check whether the query string
ended up empty.

(To make everything more consistent, the "removing nil values"
functionality should probably move to ActionPack's Hash#to_query, the
place where empty hashes and arrays get removed.  However, this would
change a lot more behavior.)
2015-10-29 17:02:13 -07:00
Aaron Patterson
8941831733 Revert "ActionController::Base#process() now only takes an action name"
This reverts commit 9f93a5efbba3e1cbf0bfa700a17ec8d1ef60d7c6.
2015-10-29 16:18:27 -07:00
Aaron Patterson
9f93a5efbb ActionController::Base#process() now only takes an action name
rather than an action name and *args.  The *args were not being used in regular
applications outside tests.  This causes a backwards compatibility
issue, but reduces array allocations for most users.
2015-10-29 15:40:18 -07:00
Aaron Patterson
82328a563f switch to JSON.dump to avoid infinite recursion 2015-10-29 14:42:52 -07:00
Rafael Mendonça França
a05f410029 Write the cookie jar it was not committed in TestCase
For ActionController::Base we write the cookies in a middleware if it
was not yet committed no matter if the response was committed or not. [1]

For ActionController::Live we write the cookies before the response is
committed. [2]

We already mimic ActionController::Live in
ActionController::TestCase but we don't mimic the ActionController::Base
behavior because we were checking if the response was committed before
writing the cookies.

Now we are matching the behavior of the middleware and writing the
cookies if it was not written before.

[1]: 80c6b901d4/actionpack/lib/action_dispatch/middleware/cookies.rb (L599-L604)
[2]: 80c6b901d4/actionpack/lib/action_controller/metal/live.rb (L218-L223)
2015-10-28 02:45:29 -02:00
Guo Xiang Tan
190c69ece3 Remove skipped JRuby tests that are passing on 9.0.3.0. 2015-10-27 01:13:28 -02:00
Sebastian McKenzie
a97dcde689 Tweaked wording used in some tests. 2015-10-25 08:57:50 +00:00
Guo Xiang Tan
682d624a85 Remove skip on tests that have been fixed. 2015-10-24 08:08:26 +08:00
Grey Baker
59ab2d1ee5 Catch invalid UTF-8 querystring values and respond with BadRequest 2015-10-23 14:56:47 +01:00
Guo Xiang Tan
c285bbec17 FIX: Randomly failing test when running without GIL. 2015-10-23 21:15:51 +08:00
Sean Griffin
b1e40cff80 Merge pull request #19327 from rousisk/master
Change Integer#year to return a Fixnum instead of a Float to improve consistency
2015-10-22 07:07:27 -06:00
yui-knk
ad1f056016 Suppress warnings (warnings about AD::IntegrationTest HTTP request)
These warings have been appeared from
ea9bc06c9a47b839d5e2db94ba6bf7e29c8f0ae9.
2015-10-22 20:21:25 +09:00
Konstantinos Rousis
0d2ce9d723 Change Integer#year to return a Fixnum instead of a Float to improve consistency 2015-10-22 12:44:03 +02:00
Sean Griffin
ea9bc06c9a Merge pull request #20715 from simsalabim/feature/parse-rss-atom-as-xml
parse RSS/ATOM responses as XML, not HTML
2015-10-20 16:11:36 -06:00