Login dies with "Unauthorized action or expired link" when a plugin calls yourls_is_valid_user() before the admin auth check (root cause of #3087)
Code of Conduct
- I agree to follow this project's Code of Conduct.
Submission validity
- This is not a personal support request, that should be posted on discussions community.
- I checked current issues and this request isn't a duplicate of an existing issue, opened or closed.
Self troubleshooting
- I've read the documentation and made sure to follow it.
- I've read the troubleshooting first steps and frequent issues guides.
Version
1.10.6
Description
After submitting the login form, YOURLS shows "Unauthorized action or expired link" (HTTP 403). Going back and reloading the page shows the admin area: the user is actually logged in, the auth cookie was set.
This is the behaviour reported in #3087, which was closed without a root cause. I traced it down on 1.10.6.
The login form is processed twice in the same request when any plugin calls yourls_is_valid_user() before yourls_maybe_require_auth() runs in admin/index.php:
- A plugin calls
yourls_is_valid_user()early, e.g. in akses_allowed_protocolsfilter. That filter runs onplugins_loadedviayourls_kses_init()(functions-kses.php#L46). usernameandpasswordare in$_REQUEST, soyourls_check_username_password()runs. The nonce is verified for user-1(nobody logged in yet), which is correct. Thenyourls_set_user()definesYOURLS_USER, the cookie is stored and a redirect header is sent.admin/index.phpcallsyourls_maybe_require_auth(), which callsyourls_is_valid_user()again.usernameandpasswordare still in$_REQUEST, soyourls_check_username_password()runs a second time.yourls_verify_nonce('admin_login')now usesYOURLS_USERinstead of-1(functions-auth.php#L133-L136, #L804-L809). The nonce was created for-1, so the check fails andyourls_die()sends the 403.
Plugins known to trigger it:
- YOURLS/custom-protocol (current master): calls
yourls_is_valid_user()in itskses_allowed_protocolsfilter. - dgw/yourls-dont-track-admins 1.2: called it on
plugins_loaded. Version 1.3 moved the call toredirect_shorturl, which fixed it there. This matches the observations in #3087.
Expected Behavior
Calling yourls_is_valid_user() more than once per request should be safe. Plugins do it, and the docs do not warn against it.
Two possible fixes in core, either would do:
- Verify the login nonce explicitly for the anonymous user, since the login form is always rendered for
-1:yourls_verify_nonce( 'admin_login', false, '-1' );inyourls_check_username_password(). - Or remember the result of
yourls_is_valid_user()for the rest of the request and return it on later calls.
Steps to Reproduce
- Fresh YOURLS 1.10.6, private mode.
- Activate the plugin YOURLS/custom-protocol.
- Log out, open
/admin/, submit valid credentials. - Result: "Unauthorized action or expired link". Reload
/admin/: logged in.
Deactivating the plugin, or changing its check to look at the auth cookie only (isset( $_COOKIE[ yourls_cookie_name() ] ) && yourls_check_auth_cookie()), makes the login work again.
Context
Seen on a shared host (all-inkl), PHP 8.x, locale de_DE. After changing the check in custom-protocol as described above, the login works again.
Source: YOURLS/YOURLS