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

[SDK-3722] Fix: Stateless strategies should not invoke stateful session classes #662

Merged
merged 5 commits into from
Oct 21, 2022

Conversation

evansims
Copy link
Member

@evansims evansims commented Oct 21, 2022

Changes

This PR fixes a bug (manifested downstream in the Laravel SDK) in which the SDK invokes stateful session classes and checks in applications using stateless strategies, in some cases throwing errors as those session classes are not (and should not) be accessible.

References

Resolves auth0/laravel-auth0#325

Testing

Run vendor/bin/pest from a local clone of the repo, or review of the GitHub status checks below.

Contributor Checklist

@evansims evansims changed the title [SDK-3722] Fix: Stateless strategies should not invoke stateful sessi… [SDK-3722] Fix: Stateless strategies should not invoke stateful session classes Oct 21, 2022
@codecov-commenter
Copy link

codecov-commenter commented Oct 21, 2022

Codecov Report

Base: 94.44% // Head: 94.35% // Decreases project coverage by -0.09% ⚠️

Coverage data is based on head (54649c2) compared to base (81b8ddd).
Patch coverage: 91.07% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #662      +/-   ##
============================================
- Coverage     94.44%   94.35%   -0.10%     
- Complexity     1254     1275      +21     
============================================
  Files            65       65              
  Lines          4211     4231      +20     
============================================
+ Hits           3977     3992      +15     
- Misses          234      239       +5     
Impacted Files Coverage Δ
src/Auth0.php 98.28% <90.56%> (-1.72%) ⬇️
src/Configuration/SdkConfiguration.php 71.48% <100.00%> (+0.10%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -93,11 +94,16 @@ public function login(
?string $redirectUrl = null,
?array $params = null
): string {
if (! $this->configuration()->usingStatefulness()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a usingStatefulness() helper method on the Auth0\SDK\Configuration\SdkConfiguration class, which simply returns true if the host application has configured a stateless application strategy.

These throw a ConfigurationException if a method dependant on sessions/statefulness is invoked. (This is not a breaking change as this would have naturally thrown an exception from being inaccessible — just a cleaner exception type is thrown now.)

$params = $params ?? [];
$state = $params['state'] ?? $this->getTransientStore()->issue('state');
$params['nonce'] = $params['nonce'] ?? $this->getTransientStore()->issue('nonce');
$state = $params['state'] ?? $store?->issue('state') ?? uniqid();
Copy link
Member Author

@evansims evansims Oct 21, 2022

Choose a reason for hiding this comment

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

Null-safe operators are a new language feature in PHP 8.0+ which offers cleaner syntax than null coalescing, as that does not work on method calls. I'll be transitioning the SDK to use these as time goes on, now that the SDK requires PHP 8+.

@@ -601,9 +632,9 @@ public function getInvitationParameters(): ?array
/**
* Create a transient storage handler using the configured transientStorage medium.
*/
private function getTransientStore(): TransientStoreHandler
private function getTransientStore(bool $reset = false): ?TransientStoreHandler
Copy link
Member Author

@evansims evansims Oct 21, 2022

Choose a reason for hiding this comment

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

Enables the recreation of TransientStore class instances for memory cleanup. getTransientStore() now correctly returns a potential null value when a store isn't configured (in stateless cases.) Previously calling this method in a stateless strategy would have thrown an exception.

@@ -141,7 +141,7 @@ public function __construct(
$this->setupStateCookies();
$this->setupStateFactories();

if (in_array($this->getStrategy(), self::STRATEGIES_USING_SESSIONS, true)) {
if ($this->usingStatefulness()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This logic has been moved into the new usingStatefuless() method for reusability.

@@ -48,6 +49,13 @@ public static function requiresStrategy(
return new self(self::MSG_STRATEGY_REQUIRED, 0, $previous);
}

public static function requiresStatefulness(
Copy link
Member Author

Choose a reason for hiding this comment

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

A new message variant for the ConfigurationException type, when a stateful method is incorrectly invoked in a stateless application type.

@evansims evansims marked this pull request as ready for review October 21, 2022 03:58
@evansims evansims requested a review from a team as a code owner October 21, 2022 03:58
@evansims evansims added this to the 8.3.5 milestone Oct 21, 2022
@evansims evansims enabled auto-merge (squash) October 21, 2022 04:40
Copy link
Contributor

@0xMinerva 0xMinerva left a comment

Choose a reason for hiding this comment

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

thanks for fixing so quickly

@evansims evansims disabled auto-merge October 21, 2022 17:34
@evansims evansims merged commit e727a9b into main Oct 21, 2022
@evansims evansims deleted the fix/sessions-not-required-for-stateless-apps branch October 21, 2022 17:35
github-actions bot added a commit that referenced this pull request Oct 21, 2022
…ke stateful session classes (#662)

* [SDK-3722] Fix: Stateless strategies should not invoke stateful session checks

* Fix: getState($reset: true) should still be run on clear(), for in-memory state cleanup.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@evansims evansims mentioned this pull request Oct 21, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERROR: Auth0\SDK\Utility\TransientStoreHandler
3 participants