Skip to content

Do not throw an exception when the samlAuthenticationLogger is used before forAuthentication() is called on it#130

Merged
pmeulen merged 3 commits into
mainfrom
bugfix/exception-from-logger
May 26, 2025
Merged

Do not throw an exception when the samlAuthenticationLogger is used before forAuthentication() is called on it#130
pmeulen merged 3 commits into
mainfrom
bugfix/exception-from-logger

Conversation

@pmeulen
Copy link
Copy Markdown
Member

@pmeulen pmeulen commented Apr 30, 2025

Do not throw an exception when the samlAuthenticationLogger is used before forAuthentication() is called on it.

  • This obscures the original log message.
  • Exception from Logger are unexpected behaviour.

I've seen this error occur both in selfservice and the gateway, so this does happen in practice.
I changed the behaviour so that the sari is not added to the logging context if it is not set (i.e. when forAuthentication() has not been called ).

I do not understand why this check was added in the first place. There was a test for it too. Please review if I'm missing something here.

@pmeulen pmeulen requested a review from pablothedude April 30, 2025 15:28
pmeulen added 3 commits April 30, 2025 18:44
…efore forAuthentication() is called on it

This obscures the original log message
@pmeulen pmeulen force-pushed the bugfix/exception-from-logger branch from 7bb42fd to 6671293 Compare April 30, 2025 16:52
@pmeulen
Copy link
Copy Markdown
Member Author

pmeulen commented Apr 30, 2025

Rebased on top of #131 to make the integration tests pass

{
if (!$this->sari) {
throw new RuntimeException('Authentication logging context is unknown');
if ($this->sari) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will indeed prevent an exception from being raised, but if this logger is used, it should use forAuthentication first.

If something needs to be logged in situations where we don't have a requestID, another logger should be used.

So I suspect this has something to do with the wrong logger being used after the Symfony upgrade.

It would be safe to merge, but then the sari in the logs might disappear in certain cases were a sari would be helpful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agree with everything you said, except:

... but then the sari in the logs might disappear in certain cases were a sari would be helpful.

This patch can never make a sari disappear, all it does is not throw when the sari is not there.

E.g. when expectedInResponseTo is null here, for instance because of session troubles,

$logger = $this->authenticationLogger->forAuthentication($expectedInResponseTo);
the later calls to the logger will throw an exception, hiding the original problem.

Or this could happen because the logger is used before forAuthentication() is called like you said.

These issues in the use of this logger should be addresses to, but the choice to make a log action throw hides valuable info, and does not help at all. I ran into this while debugging such an issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Discussed with @pablothedude : The point is that without the exception a developer can use this class wrongly (i.e DI'ing it without calling forAuthentication()) and not notice this.

@pablothedude pablothedude self-requested a review May 26, 2025 08:57
@pmeulen pmeulen merged commit 3ee8ae9 into main May 26, 2025
4 checks passed
@pmeulen pmeulen deleted the bugfix/exception-from-logger branch May 26, 2025 09:04
@pmeulen pmeulen self-assigned this Aug 11, 2025
@pmeulen pmeulen moved this from New to Delivered in PHP development Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Delivered

Development

Successfully merging this pull request may close these issues.

2 participants