-
-
Notifications
You must be signed in to change notification settings - Fork 232
Fix: Return error from attempted security scheme instead of first defined scheme #1135
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ interface SecurityHandlerResult { | |||||||||||||||||||||||||||||||
| success: boolean; | ||||||||||||||||||||||||||||||||
| status?: number; | ||||||||||||||||||||||||||||||||
| error?: string; | ||||||||||||||||||||||||||||||||
| attempted?: boolean; // true if credentials were provided and handler was called | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| attempted?: boolean; // true if credentials were provided and handler was called | |
| attempted?: boolean; // true if AuthValidator.validate() passed for this scheme and the handler was called |
Copilot
AI
Feb 22, 2026
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.
The new attempted-error prioritization can mask configuration/5xx errors. For an OR security list where one scheme fails with a 5xx (e.g., missing/invalid handler or security scheme definition) and another scheme was attempted and failed with 401, this logic will now prefer the 401 and hide the 5xx. Consider prioritizing 5xx errors (or otherwise non-authn configuration errors) ahead of attempted-401s so misconfigurations still surface correctly.
| // Prioritize errors where authentication was actually attempted | |
| const attemptedErrors = errors.filter(e => e.attempted); | |
| const errorToThrow = attemptedErrors.length > 0 ? attemptedErrors[0] : errors[0]; | |
| // Prefer server/configuration errors (5xx) so misconfigurations surface | |
| const serverErrors = errors.filter( | |
| (e) => typeof e.status === 'number' && e.status >= 500 && e.status < 600, | |
| ); | |
| let errorToThrow; | |
| if (serverErrors.length > 0) { | |
| errorToThrow = serverErrors[0]; | |
| } else { | |
| // Otherwise, prioritize errors where authentication was actually attempted | |
| const attemptedErrors = errors.filter((e) => e.attempted); | |
| errorToThrow = attemptedErrors.length > 0 ? attemptedErrors[0] : errors[0]; | |
| } |
Copilot
AI
Feb 22, 2026
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.
This comment states “validation passed and credentials were provided”, but AuthValidator.validate() currently does not validate oauth2/openIdConnect credentials at all (those methods are TODO/no-op). That means validatorPassed can become true even when no credentials are present for those scheme types. Consider rewording this comment (and/or renaming validatorPassed) to avoid implying stronger guarantees than the code provides.
| // If we reach here, validation passed and credentials were provided | |
| // If we reach here, AuthValidator did not report validation errors for this scheme |
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.
SecurityHandlerResult.erroris typed asstring, but this code stores the caught exception object there and later readse.error.message. Updating the type to something likeunknown/anyor a structured error shape would make the contract accurate and prevent consumers from assuming it’s a string.