Skip to content

fix: return :token_malformed from peek_header/peek_claims on invalid JSON - #449

Closed
SAY-5 wants to merge 1 commit into
joken-elixir:mainfrom
SAY-5:fix-peek-invalid-json
Closed

SAY-5 wants to merge 1 commit into
joken-elixir:mainfrom
SAY-5:fix-peek-invalid-json

Conversation

@SAY-5

@SAY-5 SAY-5 commented Aug 25, 2026

Copy link
Copy Markdown

Fixes #447.

peek_header/1 and peek_claims/1 are spec'd as {:ok, claims} | {:error, error_reason}, but they bind JOSE.json_module().decode/1 directly in the with. On OTP's :json backend that call raises on a segment that base64-decodes to invalid JSON (e.g. Joken.peek_claims("invalid.jwt.token")), so the exception escapes the else clause instead of becoming {:error, :token_malformed}:

** (ErlangError) Erlang error: {:invalid_byte, 143}: invalid byte 16#8F at byte position 0
    (joken 2.6.2) lib/joken.ex:156: Joken.peek_claims/1

This wraps the JSON decode in a small safe_json_decode/1 helper that returns :error on a decode exception, matching the tagged {:decode64, _} handling already in these functions. The happy path is unchanged and still does a single decode, so peek_* stays fast (per the note in #447) while honouring its {:error, :token_malformed} contract for malformed input.

@victorolinasc

Copy link
Copy Markdown
Collaborator

This was fixed on #447 and already released as a new version.

Thanks!

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.

Erlang error on invalid token with separator dots

2 participants