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: treat improper token properly #237

Merged

Conversation

polvalente
Copy link
Contributor

Context

This pull request originated from a Joken crash when sending invalid tokens that contained two ".", like a.a.a or ... These tokens match correctly on [_, _, _] = String.split(token, "."), thus passing a first validation layer (Joken.expand(token)).

Furthermore, when using before_verify or before_validate hooks that used Joken.peek_claims or Joken.peek_header, there could be a problem where said invalid tokens would bleed through as {:ok, :error}, thus causing the crash (as it was expected to return {:ok, %{} = claims} instead of {:ok, atom})

Fix

Instead of relying on just passing through the Base.decode64 error return, it needs to be wrapped as a {:error, improper_token} tuple.

The test added ensures that both scenarios are now returning an error as expected

lib/joken.ex Outdated
header <- JOSE.json_module().decode(decoded_str) do
{:ok, header}
else
{:decode64, _error} -> {:error, :improper_token}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use the same return as in expand :token_malformed? I think it is easier to who ever uses the library.

@victorolinasc victorolinasc merged commit 7a3adec into joken-elixir:master Jul 11, 2019
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.

2 participants