UX: Improve error handling for DiscourseConnect (#26140)

Previously, if the sso= payload was invalid Base64, but signed correctly, there would be no useful log or error. This commit improves things by:

- moving the base64 check before the signature checking so that it's properly surfaced
- split the ParseError exception into PayloadParseError and SignatureError
- add user-facing errors for both of those
- add/improve spec for both
This commit is contained in:
David Taylor
2024-03-12 16:16:04 +00:00
committed by GitHub
parent ec3d29a1fa
commit 127214c613
4 changed files with 53 additions and 13 deletions

View File

@ -168,13 +168,19 @@ class SessionController < ApplicationController
begin
sso = DiscourseConnect.parse(request.query_string, secure_session: secure_session)
rescue DiscourseConnect::ParseError => e
rescue DiscourseConnect::PayloadParseError => e
connect_verbose_warn do
"Verbose SSO log: Signature parse error\n\n#{e.message}\n\n#{sso&.diagnostics}"
"Verbose SSO log: Payload is not base64\n\n#{e.message}\n\n#{sso&.diagnostics}"
end
return render_sso_error(text: I18n.t("discourse_connect.payload_parse_error"), status: 422)
rescue DiscourseConnect::SignatureError => e
connect_verbose_warn do
"Verbose SSO log: Signature verification failed\n\n#{e.message}\n\n#{sso&.diagnostics}"
end
# Do NOT pass the error text to the client, it would give them the correct signature
return render_sso_error(text: I18n.t("discourse_connect.login_error"), status: 422)
return render_sso_error(text: I18n.t("discourse_connect.signature_error"), status: 422)
end
if !sso.nonce_valid?