Skip to content

Use of global state in latest version breaks thread safety of JWT.decode #268

@dklotz

Description

@dklotz

The commit aea6da4 by @ab320012 switches the implementation of JWT.decode from something completely functional (i.e. where all arguments are passed around as parameters to all called methods) to using lots of module variables (@jwt, @key etc.) which introduce global state that gets shared between all threads. This introduces more or less subtle errors when used in a multi-threaded environment (e.g. puma), since two calls to JWT.decode can overwrite each others key, headers, payload etc. when a context switch happens in the middle of JWT.decode (e.g. after Decode.new(...) but before verify_signature.

Since this all sounds pretty theoretical so far, I've create a simple reproducer that manages to reproduce this bug every time. It basically just calls JWT.decode(JWT.encode(...)) from several threads in parallel and introduces some artificial delay to increase the likelihood of a problem.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions