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

fix: interstitial of failed token verification #30

Merged
merged 4 commits into from
Mar 7, 2023
Merged

Conversation

dimkl
Copy link
Contributor

@dimkl dimkl commented Feb 28, 2023

Set signed-out as default request state and return interstitial ONLY when the token verification fails with expired or invalid issued_at (iat) errors.

Extra actions:

  • unit tests
  • manual testing

REVIEW IT PER COMMIT

@dimkl dimkl requested a review from agis February 28, 2023 18:26
@dimkl dimkl self-assigned this Feb 28, 2023
@linear
Copy link

linear bot commented Feb 28, 2023

JS-204 Direct invocation of DAPI endpoints in the browser causes a 401 redirect loop

Steps to reproduce

  1. Sign in to Dashboard
  2. Go to an instance page
  3. Get a DAPI instance API request e.g. https://dapi.clerk.dev/v1/instances/instance_idgoes_here and visit in a new tab

The new tab will enter a 401 redirect loop due to the interstitial.

Note that Dashboard operates with Authorization Headers by default. We can change this to cookies as DAPI is a Same-origin API. We will also do better dog-fooding.

When the API page is visited directly in the browser, there are no Authorization Headers but there is an outdated __session cookie.

This issue might reveal a bug in the interstitial logic for single or multi session applications.

Issue flow diagram:

JS-204-current-impl.png

@dimkl dimkl changed the title Js 204 interstitial fix: interstitial of failed token verification Mar 1, 2023
Copy link
Member

@gkats gkats left a comment

Choose a reason for hiding this comment

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

Looks great! Consider avoiding dumping methods in a "utils" class in favor of a more OO ruby idiomatic class.

lib/clerk/rack_middleware_v2.rb Outdated Show resolved Hide resolved
module_function
def camelize(term)
string = term.to_s
string = string.sub(/^[a-z\d]*/) { match.capitalize }
string.gsub!(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
string
end

# see: https://github.com/clerkinc/javascript/blob/main/packages/backend/src/util/parsePublishableKey.ts
Copy link
Member

Choose a reason for hiding this comment

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

🙃 I would remove this link, it's very likely that it will become obsolete in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean by becoming obsolete? Do you mean the algorithm change or the src/util/parsePublishableKey.ts being moved?
I think we should keep a reference to something that indicates the logic of the implementation.

Copy link
Member

Choose a reason for hiding this comment

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

I mean the file being moved. I think links in comments are risky in general, but that's just my opinion.

Of course, feel free to ignore this comment.

At the very least we can use this link instead https://github.com/clerkinc/javascript/blob/c11217cf4432aa640af67ab2fd48f4e5bb33ce6d/packages/backend/src/util/parsePublishableKey.ts
which references a specific commit which will never change.

lib/clerk/utils.rb Outdated Show resolved Hide resolved
@dimkl
Copy link
Contributor Author

dimkl commented Mar 6, 2023

@clerkinc/backend-team could you take a look ?

@dimkl dimkl force-pushed the js-204-interstitial branch 2 times, most recently from 7e8b988 to 995a16e Compare March 6, 2023 09:58
@@ -163,11 +163,14 @@ def call(env)
end

token = verify_token(cookie_token)
return signed_out(env) unless token
Copy link
Member

Choose a reason for hiding this comment

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

IIRC, verify_token() == false can also happen if the token is expired, for example. So, if I'm signed in and suddenly I close my browser, and come back after 1 hour, then verify_token() will return false here. In that case, don't we want to show the interstitial so that the token is refreshed (the long-lived FAPI cookie will still be there) and I remain signed in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case we jwt is expired we re-raise the ExpiredSignature error instead of returning false from verify_token . see rescue JWT::ExpiredSignature, JWT::InvalidIatError => e below.

CODEOWNERS Show resolved Hide resolved
Copy link
Member

@agis agis left a comment

Choose a reason for hiding this comment

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

Approving for expediency, but let's limit those rescue cals to #verify_token.

lib/clerk/rack_middleware_v2.rb Show resolved Hide resolved
lib/clerk/rack_middleware_v2.rb Outdated Show resolved Hide resolved
lib/clerk/rack_middleware_v2.rb Show resolved Hide resolved
lib/clerk/rack_middleware_v2.rb Outdated Show resolved Hide resolved
@dimkl dimkl force-pushed the js-204-interstitial branch 2 times, most recently from 7038ea9 to 6b06a6e Compare March 7, 2023 14:44
Set signed-out as default request state and return interstitial ONLY
when the token verification fails with expired or invalid_iat errors.
@dimkl dimkl merged commit 042423e into main Mar 7, 2023
@dimkl dimkl deleted the js-204-interstitial branch March 7, 2023 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants