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

Regression: GET /me returns 401 when user is authenticated via 'dbAuth' middleware #959

Open
NorthFred opened this issue Feb 22, 2023 · 8 comments
Assignees
Labels

Comments

@NorthFred
Copy link
Contributor

Regression issue found in PHP API v.2.14.19 and not reproducible in at least 2.14.10.

An existing project which uses the 'dbAuth' authentication middleware now reports a 401 with error code 1011 when a user is logged in and makes a request to the '/me' endpoint. Issue was observed when upgrading to API v.2.14.19. Previously, GET /me would return a 200 for logged-in users.

@NorthFred NorthFred changed the title Regression: GET /me returns 401 when user is authenticated with 'dbAuth' middleware Regression: GET /me returns 401 when user is authenticated via 'dbAuth' middleware Feb 22, 2023
@mevdschee mevdschee self-assigned this Feb 22, 2023
@mevdschee mevdschee added the bug label Feb 22, 2023
@mevdschee
Copy link
Owner

I checked the code path and I see no obvious problems. Can you reproduce this with only the dbAuth middleware loaded?

@mevdschee
Copy link
Owner

mevdschee commented Mar 22, 2023

@NorthFred Can you help me with some reproduction steps?

@mevdschee
Copy link
Owner

Did you set one or both of these?

"dbAuth.usersTable": The table that is used to store the users in ("users")
"dbAuth.loginTable": The table or view that is used to retrieve the users info for login ("users")

I've fixed a bug there recently.

@NorthFred
Copy link
Contributor Author

@mevdschee I have retested this with API v.2.14.19 and the error is reproducible when I serve the front-end of the application locally. This is NOT reproducible with a live production version of the UI client.

I realized now that in the old version 2.14.10, I had made a small modification to this block of code (added an extra condition for same site requests):

        public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
        {
            if (session_status() == PHP_SESSION_NONE) {
                if (!headers_sent()) {
                    $sessionName = $this->getProperty('sessionName', '');
                    if ($sessionName) {
                        session_name($sessionName);
                    }
	            // Custom condition added below:
	            if (isset($body->allowSameSite)) {
		            session_set_cookie_params(['samesite' => 'None', 'secure' => true]);
	            }
                    // End custom condition
                    session_start();
                }
            }

Whereas in version 2.14.19, I see that same block has been updated as follows:

        public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
        {
            if (session_status() == PHP_SESSION_NONE) {
                if (!headers_sent()) {
                    $sessionName = $this->getProperty('sessionName', '');
                    if ($sessionName) {
                        session_name($sessionName);
                    }
                    if (!ini_get('session.cookie_samesite')) {
                        ini_set('session.cookie_samesite', 'Lax');
                    }
                    if (!ini_get('session.cookie_httponly')) {
                        ini_set('session.cookie_httponly', 1);
                    }
                    if (!ini_get('session.cookie_secure') && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                        ini_set('session.cookie_secure', 1);
                    }
                    session_start();
                }
            }

@mevdschee
Copy link
Owner

mevdschee commented Mar 23, 2023

Ah.. thank you! You are reporting the same issue as is reported in #953. Can you help me how to decide between 'Lax' and 'None'? What are the conditions that apply for the 'None' case? Should the server be on localhost?

@NorthFred
Copy link
Contributor Author

@mevdschee For my use case, the condition was added to fix issues with localhost only. I'm afraid I'm not able to comment further on this :/

@mevdschee
Copy link
Owner

I think a cookie should either be set to secure and samesite lax or not secure and samesite none.

@nik2208
Copy link
Contributor

nik2208 commented Oct 6, 2023

there should be some avoid_cors_issues_on_localhost opition to set session_set_cookie_params(['samesite' => 'None', 'secure' => true]); for development purposes

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

No branches or pull requests

3 participants