-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add note about cookie protection #126
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| use RuntimeException; | ||
| use Throwable; | ||
| use Yiisoft\Auth\IdentityRepositoryInterface; | ||
| use Yiisoft\Cookies\CookieMiddleware; | ||
| use Yiisoft\User\CurrentUser; | ||
|
|
||
| use function array_key_exists; | ||
|
|
@@ -26,6 +27,10 @@ | |
|
|
||
| /** | ||
| * `CookieLoginMiddleware` automatically logs user in based on cookie. | ||
| * | ||
| * The auto-login cookie value isn't protected against tampering by this package. This isn't only about | ||
| * the end user editing their own cookie — anyone who steals the cookie value can modify it too, e.g. to | ||
| * remove its expiration. Use {@see CookieMiddleware} to sign or encrypt it if you want to prevent that. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to refer to README since order of middlware is critical for it to work proeprly. |
||
| */ | ||
| final class CookieLoginMiddleware implements MiddlewareInterface | ||
| { | ||
|
|
@@ -41,7 +46,7 @@ | |
| private IdentityRepositoryInterface $identityRepository, | ||
| private LoggerInterface $logger, | ||
| private CookieLogin $cookieLogin, | ||
| private bool $forceAddCookie = false, | ||
|
Check warning on line 49 in src/Login/Cookie/CookieLoginMiddleware.php
|
||
| ) {} | ||
|
|
||
| /** | ||
|
|
@@ -94,7 +99,7 @@ | |
| } | ||
|
|
||
| try { | ||
| $data = json_decode((string) $cookies[$cookieName], true, 512, JSON_THROW_ON_ERROR); | ||
|
Check warning on line 102 in src/Login/Cookie/CookieLoginMiddleware.php
|
||
| } catch (Throwable) { | ||
| $this->logger->warning('Unable to authenticate user by cookie. Invalid cookie.'); | ||
| return; | ||
|
|
@@ -129,12 +134,12 @@ | |
|
|
||
| if (!$identity->validateCookieLoginKey($key)) { | ||
| $this->logger->warning('Unable to authenticate user by cookie. Invalid key.'); | ||
| return; | ||
|
Check warning on line 137 in src/Login/Cookie/CookieLoginMiddleware.php
|
||
| } | ||
|
|
||
| if ($expires !== 0 && $expires < time()) { | ||
|
Check warning on line 140 in src/Login/Cookie/CookieLoginMiddleware.php
|
||
| $this->logger->warning('Unable to authenticate user by cookie. Lifetime has expired.'); | ||
| return; | ||
|
Check warning on line 142 in src/Login/Cookie/CookieLoginMiddleware.php
|
||
| } | ||
|
|
||
| $this->currentUser->login($identity); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it already mentioned in "Preventing the substitution of cookies" which mentions order which is critical for it to work?