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

[Security] Fail gracefully if the security token cannot be unserialized from the session #25669

Merged
merged 1 commit into from Jan 8, 2018

Conversation

thewilkybarkid
Copy link
Contributor

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

If the security token in the session can't be unserialized, an E_NOTICE is issued. This prevents it (and provides a better log message if it's not even a __PHP_Incomplete_Class).

This is similar to #24731, but I saw it triggered when changing OAuth library (elifesciences/journal#824), so the token class itself no longer exists. (I want to avoid having to manually invalidate all sessions, as not all sessions use that token class.)

@thewilkybarkid
Copy link
Contributor Author

Travis failure looks unrelated.

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Jan 3, 2018
@nicolas-grekas
Copy link
Member

Thanks for the fix, looks legit. I think for a full fledged implementation, we should borrow from ResourceCheckerConfigCache::safelyUnserialize().
The currently proposed logic is not robust enough AFAIK.

try {
$unserialized = unserialize($serialized);
} catch (\ErrorException $e) {
// To be rethrown after restoring the error handler.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can use finally in Symfony 3+.

$token = $this->unserialize($serializedToken);
} catch (\ErrorException $e) {
if (null !== $this->logger) {
$this->logger->warning('Failed to unserialize the security token from the session.', array('key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e));
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be an error instead? It means that something goes wrong somewhere.

Can you rename key to session_key? This should describe the function a bit more when viewing the logs. At first I thought this was the firewall (context) key, but that's included in the session key.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do, though I was keeping it in-line with

$this->logger->warning('Expected a security token from the session, got something else.', array('key' => $this->sessionKey, 'received' => $token));

Copy link
Contributor

@linaori linaori Jan 4, 2018

Choose a reason for hiding this comment

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

Fair enough, best to keep it consistent then (regarding the key).

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

feels like nobody noticed my previous comment, here it is :)

Thanks for the fix, looks legit. I think for a full fledged implementation, we should borrow from ResourceCheckerConfigCache::safelyUnserialize().
The currently proposed logic is not robust enough AFAIK.

@thewilkybarkid
Copy link
Contributor Author

@nicolas-grekas Implemented in 23e6fee.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

@thewilkybarkid I pushed some changes directly on your fork, PR ready on my side.

@fabpot
Copy link
Member

fabpot commented Jan 8, 2018

Thank you @thewilkybarkid.

@fabpot fabpot merged commit 053fa43 into symfony:2.7 Jan 8, 2018
fabpot added a commit that referenced this pull request Jan 8, 2018
…t be unserialized from the session (thewilkybarkid)

This PR was merged into the 2.7 branch.

Discussion
----------

[Security] Fail gracefully if the security token cannot be unserialized from the session

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

If the security token in the session can't be unserialized, an `E_NOTICE` is issued. This prevents it (and provides a better log message if it's not even a `__PHP_Incomplete_Class`).

This is similar to #24731, but I saw it triggered when changing OAuth library (elifesciences/journal#824), so the token class itself no longer exists. (I want to avoid having to manually invalidate all sessions, as not all sessions use that token class.)

Commits
-------

053fa43 [Security] Fail gracefully if the security token cannot be unserialized from the session
@thewilkybarkid thewilkybarkid deleted the access-token-unserialization branch January 10, 2018 14:27
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants