Security Defense for Firebase's PHP-JWT Library

Overview

PHP-JWT-Guard

Build Status Latest Stable Version Latest Unstable Version License Downloads

Protect your code from being impacted by issue 351 in firebase/php-jwt.

Installation

First, install this library with Composer:

composer require paragonie/php-jwt-guard

And then in your PHP namespace imports, swap the namespace:

- use Firebase\JWT\JWT;
+ use ParagonIE\PhpJwtGuard\JWT;

You're no longer going to provide an array or ArrayAccess object to JWT. You will instead need to use the provided KeyRing class.

<?php
use ParagonIE\PhpJwtGuard\KeyRing;
use ParagonIE\PhpJwtGuard\JWT;

// Setup keyring:
$keyring = (new KeyRing())
    ->withHS256('key-id-foo', 'raw-key-data-goes-here')
    ->withHS384('key-id-bar', 'raw-key-data-goes-here-too')
    // ...
    ->withPS384('key-id-xyzzy', 'raw-key-data-goes-here-too')
    ->withPS512('key-id-thud', 'raw-key-data-goes-here-too');

// Pass it to JWT Dcode:
JWT::decode($jwt, $keyring, array($allowedAlgs));

Using the KeyRing class

KeyRing->with($alg, $keyId, $rawKeyData)

Parameters:

  1. string $alg - The algorithm this key is intended for
  2. string $keyId - The kid header that maps to this key
  3. string $rawKeyData - The actual key material. For asymmetric keys, this is usually PEM-encoded.

Returns the KeyRing object. Chainable.

KeyRing->count()

Returns an integer.

KeyRing->partition($alg)

Parameters:

  1. string $alg - The algorithm this key is intended for

Returns a new KeyRing object with a subset of all supported keys.

You might also like...
Sistema de Administrativo - Cliente e Vendedor - Autenticação JWT e Relacionamentos  BD
Sistema de Administrativo - Cliente e Vendedor - Autenticação JWT e Relacionamentos BD

Hi there, My name is ATTILA SAMUELL TABORY, I love technology 👋 Sistema Administrativo Laravel e Vue JS - JWT e Relacionamentos BD Sistema Administra

Aplicação criada com Slim Framework com objetivo de criar autenticação com JWT e aprender sobre o framework Slim

Slim JWT App Essa aplicação tem como foco o aprendizado do Framework Slim e também a utilização de JWT. Como rodar a Aplicação A aplicação está config

JWT Authenticator for symfony

HalloVerdenJwtAuthenticatorBundle This bundle provides a JWT authenticator for Symfony applications. It's using PHP JWT Framework for parsing and vali

User registration and login form with validations and escapes for total security made with PHP.

Login and Sign Up with PHP User registration and login form with validations and escapes for total security made with PHP. Validations Required fields

Learn Cookies and Tokens Security in Practice.
Learn Cookies and Tokens Security in Practice.

The full article is posted on my blog. The video presentation is shared here. The presentation slides are shared here. The exploit codes are shared he

This system will provide security and comfortable opportunities to protect your gaming account.

VK Security – Auth system VK Security provides the ability to use game authorization inside in conjunction with the official VKontakte groups. Conveni

PASETO: Platform-Agnostic Security Tokens

PASETO: Platform-Agnostic Security Tokens Paseto is everything you love about JOSE (JWT, JWE, JWS) without any of the many design deficits that plague

This library extends the 'League OAuth2 Client' library to provide OpenID Connect Discovery support for supporting providers that expose a .well-known configuration endpoint.

OpenID Connect Discovery support for League - OAuth 2.0 Client This library extends the League OAuth2 Client library to provide OpenID Connect Discove

Comments
  • Still vulnerable with multiple allowed algorithms

    Still vulnerable with multiple allowed algorithms

    Maybe I'm missing something obvious, but I tried your library and it is still vulnerable to type confusion when using multiple allowed algorithms (i.e. your first example in https://github.com/firebase/php-jwt/issues/351).

    Furthermore your JWT::encode function has two problems:

    • Encode does not work when you supply a keyring (keyId check is wrong)
    • PHPDoc parameters are wrong for $key, $keyId and $head

    Consider this test case:

    <?php
    
    use ParagonIE\PhpJwtGuard\JWT;
    use ParagonIE\PhpJwtGuard\KeyRing;
    use PHPUnit\Framework\TestCase;
    
    class JWTTest extends TestCase
    {
        public function testConfusion(): void
        {
            $hsKey = hash('sha256', 'phpunit-test-key-for-issue-351');
    
            $esPubkey = <<<END
                -----BEGIN PUBLIC KEY-----
                MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9
                q9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==
                -----END PUBLIC KEY-----
                END;
    
            $keyring = new KeyRing();
            $keyring->with('HS256', 'foo', $hsKey);
            $keyring->with('ES256', 'bar', $esPubkey);
    
            $payload = ['sub' => 'phpunit'];
            $token = JWT::encode($payload, $esPubkey, 'HS256', 'bar'); // wrong algo
    
            $fail = false;
            try {
                JWT::decode($token, $keyring, ['HS256', 'ES256']);
                $fail = true;
            } catch (UnexpectedValueException $ex) {
            }
            $this->assertFalse($fail, 'Expected an exception');
        }
    
        public function testEncode(): void
        {
            $keyring = new KeyRing();
            $keyring->with('HS256', 'foo', hash('sha256', 'phpunit-test-key-for-issue-351'));
    
            $payload = ['sub' => 'phpunit'];
            JWT::encode($payload, $keyring, 'HS256', 'foo'); // should not throw exception
        }
    }
    
    opened by cschomburg 3
Releases(v0.3.0)
Owner
Paragon Initiative Enterprises
Technology should support your ambitions, not hinder them. We are a team of technology consultants that specialize in application security.
Paragon Initiative Enterprises
Simple JWT Auth support for Laravel PHP Framework

Laravel JWT Simple JWT Auth for Laravel PHP Framework using Firebase JWT under the hood. Installation Standard Composer package installation: composer

Ricardo Čerljenko 34 Nov 21, 2022
PHP package for JWT

PHP-JWT A simple library to encode and decode JSON Web Tokens (JWT) in PHP, conforming to RFC 7519. Installation Use composer to manage your dependenc

Firebase 8.6k Jan 7, 2023
A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready

A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready.

Damiano Petrungaro 58 Aug 10, 2022
JWT auth for Laravel and Lumen

JWT Artisan Token auth for Laravel and Lumen web artisans JWT is a great solution for authenticating API requests between various services. This packa

⑅ Generation Tux ⑅ 141 Dec 21, 2022
Laravel Auth guard for FusionAuth JWT

Laravel FusionAuth JWT Implement an Auth guard for FusionAuth JWTs in Laravel. It ships with also a middleware to check against the user role. Install

Theraloss 7 Feb 21, 2022
Probando JWT en Laravel

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

SelsiusRC28 1 Nov 2, 2021
Rest API - JWT - Symfony5

Symfony5 JWT - REST API Example Symfony5 JWT - REST API Example Built With PHP Symfony 5 PostgreSQL Getting Started This is an example of how you may

Salih Gencer 1 Dec 24, 2021
PSR-7 and PSR-15 JWT Authentication Middleware

PSR-7 and PSR-15 JWT Authentication Middleware This middleware implements JSON Web Token Authentication. It was originally developed for Slim but can

Mika Tuupola 782 Dec 18, 2022
Laravel JWT-Authentication API starter kit for rapid backend prototyping.

Laravel JWT API A Laravel JWT API starter kit. Features Laravel 8 Login, register, email verification and password reset Authentication with JWT Socia

Oybek Odilov 3 Nov 6, 2022
JSON Web Token (JWT) for webman plugin

JSON Web Token (JWT) for webman plugin Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场景。

 ShaoBo Wan(無尘) 25 Dec 30, 2022