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

fix: route options are not merged (outer filter is merged with inner filter) #8033

Merged
merged 7 commits into from
Oct 14, 2023

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Oct 12, 2023

Description
Supersedes #7981
Fixes #7963

If routes are grouped, the outer filter should be applied to all routes within.
If there is a filter that should be excluded on an inner route, that route should not be defined within the group.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added bug Verified issues on the current code behavior or pull requests that will fix them breaking change Pull requests that may break existing functionalities 4.5 labels Oct 12, 2023
@kenjis kenjis changed the title fix: route options are not merged fix: route options are not merged (outer filter is merged with inner filter) Oct 12, 2023
@kenjis
Copy link
Member Author

kenjis commented Oct 12, 2023

In this case, there is no change.

$routes->group('', ['filter' => 'csrf'], function ($routes) {
    $routes->get('dashboard', function (){
        echo "CSRF is working";
    });

    $routes->group('profile', function($routes){
        $routes->get('/', function (){
            echo "CSRF is working";
        });
    });
});

Before:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    | csrf           | csrf toolbar  |
+--------+-----------+------+------------------------------+----------------+---------------+

After:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    | csrf           | csrf toolbar  |
+--------+-----------+------+------------------------------+----------------+---------------+

@kenjis
Copy link
Member Author

kenjis commented Oct 12, 2023

In this case, the outer filter will be applied to the inner route.

$routes->group('', ['filter' => 'csrf'], function ($routes) {
    $routes->get('dashboard', function (){
        echo "CSRF is working";
    });

    $routes->group('profile', ['namespace' => 'Foo\Controller'], function($routes){
        $routes->get('/', function (){
            echo "CSRF is working";
        });
    });
});

Before:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    |                | toolbar       |
+--------+-----------+------+------------------------------+----------------+---------------+

After:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    | csrf           | csrf toolbar  |
+--------+-----------+------+------------------------------+----------------+---------------+

@kenjis
Copy link
Member Author

kenjis commented Oct 12, 2023

In this case, the outer filter will be merged with the inner filter.

$routes->group('', ['filter' => 'csrf'], function ($routes) {
    $routes->get('dashboard', function (){
        echo "CSRF is working";
    });

    $routes->group('profile', ['filter' => 'honeypot'], function($routes){
        $routes->get('/', function (){
            echo "CSRF is working";
        });
    });
});

Before:

+--------+-----------+------+------------------------------+----------------+------------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters    |
+--------+-----------+------+------------------------------+----------------+------------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar          |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar     |
| GET    | profile   | »    | (Closure)                    | honeypot       | honeypot toolbar |
+--------+-----------+------+------------------------------+----------------+------------------+

After:

+--------+-----------+------+------------------------------+----------------+-----------------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters         |
+--------+-----------+------+------------------------------+----------------+-----------------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar               |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar          |
| GET    | profile   | »    | (Closure)                    | csrf honeypot  | honeypot csrf toolbar |
+--------+-----------+------+------------------------------+----------------+-----------------------+

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

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

I was leaning towards the "none" approach but this is probably more logical for people who don't want to dig.

user_guide_src/source/incoming/routing.rst Outdated Show resolved Hide resolved
Co-authored-by: MGatner <mgatner@icloud.com>
@neznaika0
Copy link
Contributor

In this case, the outer filter will be applied to the inner route.

$routes->group('', ['filter' => 'csrf'], function ($routes) {
    $routes->get('dashboard', function (){
        echo "CSRF is working";
    });

    $routes->group('profile', ['namespace' => 'Foo\Controller'], function($routes){
        $routes->get('/', function (){
            echo "CSRF is working";
        });
    });
});

Before:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    |                | toolbar       |
+--------+-----------+------+------------------------------+----------------+---------------+

After:

+--------+-----------+------+------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                      | Before Filters | After Filters |
+--------+-----------+------+------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index |                | toolbar       |
| GET    | dashboard | »    | (Closure)                    | csrf           | csrf toolbar  |
| GET    | profile   | »    | (Closure)                    | csrf           | csrf toolbar  |
+--------+-----------+------+------------------------------+----------------+---------------+

Namespace for inner groups ignored. You merged full $options. It good?

@kenjis
Copy link
Member Author

kenjis commented Oct 14, 2023

@neznaika0 What do you mean by "Namespace for inner groups ignored"?

$routes->group('', ['filter' => 'csrf'], function ($routes) {
    $routes->get('dashboard', 'Outer::index');

    $routes->group('profile', ['namespace' => 'Foo\Controller'], function($routes) {
        $routes->get('/', 'Inner::index');
    });
});
+--------+-----------+------+-------------------------------+----------------+---------------+
| Method | Route     | Name | Handler                       | Before Filters | After Filters |
+--------+-----------+------+-------------------------------+----------------+---------------+
| GET    | /         | »    | \App\Controllers\Home::index  |                | toolbar       |
| GET    | dashboard | »    | \App\Controllers\Outer::index | csrf           | csrf toolbar  |
| GET    | profile   | »    | \Foo\Controller\Inner::index  | csrf           | csrf toolbar  |
+--------+-----------+------+-------------------------------+----------------+---------------+

@neznaika0
Copy link
Contributor

neznaika0 commented Oct 14, 2023

I didn't see Foo\Controller in the example, so I thought it wouldn't overwrite the external $options. Everything seems to be fine now. Thanks!

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

It seems more logical than the alternative or current behavior. Thank you.

@kenjis kenjis merged commit 1277cae into codeigniter4:4.5 Oct 14, 2023
54 checks passed
@kenjis kenjis deleted the fix-route-option-merge-2 branch October 14, 2023 21:56
@kenjis
Copy link
Member Author

kenjis commented Oct 14, 2023

Yes, this is logical, understandable, and less of a surprise to developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.5 breaking change Pull requests that may break existing functionalities bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants