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

Error while decoding from JSON #681

Closed
cyrilbosson opened this issue Feb 17, 2021 · 16 comments
Closed

Error while decoding from JSON #681

cyrilbosson opened this issue Feb 17, 2021 · 16 comments
Assignees
Labels

Comments

@cyrilbosson
Copy link

Hello,

I had this issue on my hosting server so I upgraded to 4.1.1 but now I have this error:

Error while decoding from JSON

Stacktrace
/home/clients/9ec8ad5583918ce55944b1cc6bdd9057/web/releases/95/vendor/lcobucci/jwt/src/Encoding/JoseEncoder.php:41 /home/clients/9ec8ad5583918ce55944b1cc6bdd9057/web/releases/95/vendor/lcobucci/jwt/src/Token/Parser.php:67 /home/clients/9ec8ad5583918ce55944b1cc6bdd9057/web/releases/95/vendor/lcobucci/jwt/src/Token/Parser.php:30

I use it with Laravel 8.28.1 & Telescope 10.1.0

PHP 7.4.15

Thanks !

@Ocramius
Copy link
Collaborator

That usually happens when your input string is not JSON:

/** @inheritdoc */
public function jsonEncode($data): string
{
try {
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw CannotEncodeContent::jsonIssues($exception);
}
}
/** @inheritdoc */
public function jsonDecode(string $json)
{
try {
return json_decode($json, true, self::JSON_DEFAULT_DEPTH, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw CannotDecodeContent::jsonIssues($exception);
}
}

The library is correctly throwing an exception here.

@Ocramius Ocramius self-assigned this Feb 17, 2021
@cyrilbosson
Copy link
Author

Ok but my JSON didn't change 🤨
I switched to 3.4.5 and I works now

@Ocramius
Copy link
Collaborator

@cyrilbosson can the string you are trying to encode (with 3.x) be posted here (as in no NDA'd secrets)? Can you perhaps provide a full JWT token string? Can you try looking at it on https://jwt.io/ ?

Also, I wonder if some of the fields you are trying to encode contain non-utf8 characters (which leads to null output for fields, but would throw when using JSON_THROW_ON_ERROR)

@cyrilbosson
Copy link
Author

The JSON response :

{
"data": {
"user": {
"id": 59,
"name": "TEST",
"firstname": "Cyril",
"email": "[email protected]",
"locale": "fr",
"avatar": "https://www.example.com/images/default.gif"
},
"patient": {
"id": 66,
"name": "TEST",
"birthname": "",
"firstname": "Cyril",
"gender": "m",
"birthdate": "2020-10-25",
"nir": "145121212312308",
"email": "[email protected]",
"phone": "123456789",
"referring_doctor": null,
"new_patient": 0,
"address": {
"line_1": "route",
"postcode": "75000",
"city": "Paris",
"country": "fr"
},
"appointments": [
]
},
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiNDIyZDUyNTRkNWZjZTM5OWFhNThhYWU5ZGJmZDBmNTUwM2JiOWQ4OTgwYTcwNzJjZDk5NTcyMTg1NzcwODYyZjZhNDUwNTQ2ZTIyNjk4ZjUiLCJpYXQiOjE2MTM1ODE2OTQsIm5iZiI6MTYxMzU4MTY5NCwiZXhwIjoxNjQ1MTE3Njk0LCJzdWIiOiI1OSIsInNjb3BlcyI6W119.nNA0Uz9uLptDAJ57rmnYnoP80bp2eGlqPbv3jkANGgXLPm1W8GxibEyKMivzgg_7U5I4D5_IB19OR9ESTg_tNUztBydFSNsYnmsxuQxiH3cKt-1-JzeVyYORUhk1cPPlKp7g_s3hZxSWjCwtmeYdfitpvA7MvVhZfLCZG1UC5ukrrCrsM84_itdb5QFNGL6RwXPwBDGzdVHLX9MaBgB4oL6K_QtfauW6ki2TracqzTYtzCbTGSnVVSR5yFBnnzXd_uyx0schZneLSEQRUOlfI1P4LRhvn_dfzMfHQ8GMZEpuoHrPPnioQym8EV5lXmWzfC2TKi0nV1W89e-_W8apWchFtIDeg6wpRCNiGgMUomz4bPpCxUH13XSJIzELFSWCehH4dZcvnAW-K9788H3UExgCc5hQlcKdIONlWLUlAvWLNR58huaFA1TAcLfiuv3Xjjv4G9ihmdo47dtDlyWpsXlCZE56HycKZwwCC0_F49ws9C4ZsrbCYk1Cq5-gY_55ruqfTURk0pNVwFg9bxiHQrHcxDChvnotmWi9NYmBBYrETYGP4DYrOH4_k1CHJwn3GRpW9ma8XTL75M3Ejl5DWduZnS1Yk_d59g-d5EmS6862_giyWMATuStufnpibgLT8jaViuQ2TfcEVArijS07KKk7IJADavXgPh6ZAFECDC8"
}
}

@lcobucci
Copy link
Owner

@cyrilbosson I cannot reproduce your issue using the latest version of the lib and the token in the access_token property you sent:

<?php
declare(strict_types=1);

use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token\Parser;
use Lcobucci\JWT\UnencryptedToken;

require 'vendor/autoload.php';

$token = (new Parser(new JoseEncoder()))->parse(
    'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiNDIyZDUyNTRkNWZjZTM5OWFhNThhYWU5ZGJmZDBmNTUwM2JiOWQ4OTgwYTcwNzJjZDk5NTcyMTg1NzcwODYyZjZhNDUwNTQ2ZTIyNjk4ZjUiLCJpYXQiOjE2MTM1ODE2OTQsIm5iZiI6MTYxMzU4MTY5NCwiZXhwIjoxNjQ1MTE3Njk0LCJzdWIiOiI1OSIsInNjb3BlcyI6W119.nNA0Uz9uLptDAJ57rmnYnoP80bp2eGlqPbv3jkANGgXLPm1W8GxibEyKMivzgg_7U5I4D5_IB19OR9ESTg_tNUztBydFSNsYnmsxuQxiH3cKt-1-JzeVyYORUhk1cPPlKp7g_s3hZxSWjCwtmeYdfitpvA7MvVhZfLCZG1UC5ukrrCrsM84_itdb5QFNGL6RwXPwBDGzdVHLX9MaBgB4oL6K_QtfauW6ki2TracqzTYtzCbTGSnVVSR5yFBnnzXd_uyx0schZneLSEQRUOlfI1P4LRhvn_dfzMfHQ8GMZEpuoHrPPnioQym8EV5lXmWzfC2TKi0nV1W89e-_W8apWchFtIDeg6wpRCNiGgMUomz4bPpCxUH13XSJIzELFSWCehH4dZcvnAW-K9788H3UExgCc5hQlcKdIONlWLUlAvWLNR58huaFA1TAcLfiuv3Xjjv4G9ihmdo47dtDlyWpsXlCZE56HycKZwwCC0_F49ws9C4ZsrbCYk1Cq5-gY_55ruqfTURk0pNVwFg9bxiHQrHcxDChvnotmWi9NYmBBYrETYGP4DYrOH4_k1CHJwn3GRpW9ma8XTL75M3Ejl5DWduZnS1Yk_d59g-d5EmS6862_giyWMATuStufnpibgLT8jaViuQ2TfcEVArijS07KKk7IJADavXgPh6ZAFECDC8'
);

var_dump($token instanceof UnencryptedToken); // bool(true)

Can you provide more information or check if your application is sending the JWT properly to the parser?

@amrishkakadiya
Copy link

I am facing similar error. Its working fine in other dedicated server but not in Shared Hosting.
Code and even project is same in both server.

My Token:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOiIxNjIwNjIxMjY4LjI2Mzc3MCIsImV4cCI6IjE2MjA2MjEzMjguMjYzNzcwIiwiaWQiOjEsInJvbGUiOiJhZG1pbiJ9.K2VEqI5XnHMf-Je4iYh857GgXhs2z9AmlnvvJhjbRUw

Error

throwable JsonException: Syntax error in /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Encoding/JoseEncoder.php:39
Stack trace:
#0 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Encoding/JoseEncoder.php(39): json_decode('ZXlKMGVYQWlPaUp...', true, 512, 4194304)
#1 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Token/Parser.php(67): Lcobucci\JWT\Encoding\JoseEncoder->jsonDecode('ZXlKMGVYQWlPaUp...')
#2 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Token/Parser.php(30): Lcobucci\JWT\Token\Parser->parseHeader('eyJ0eXAiOiJKV1Q...')
#3 /home/smartmpsco/public_html/cust/rms/api/app/Http/Middleware/ParseToken.php(41): Lcobucci\JWT\Token\Parser->parse('eyJ0eXAiOiJKV1Q...')
#4 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(167): App\Http\Middleware\ParseToken->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#5 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#6 /home/smartmpsco/public_html/cust/rms/api/app/Http/Middleware/CorsMiddleware.php(60): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#7 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(167): App\Http\Middleware\CorsMiddleware->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#8 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#9 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(103): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#10 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(423): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(172): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#12 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(109): Laravel\Lumen\Application->dispatch(NULL)
#13 /home/smartmpsco/public_html/cust/rms/api/public/index.php(28): Laravel\Lumen\Application->run()
#14 {main}

Next Lcobucci\JWT\Encoding\CannotDecodeContent: Error while decoding from JSON in /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Encoding/CannotDecodeContent.php:14
Stack trace:
#0 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Encoding/JoseEncoder.php(41): Lcobucci\JWT\Encoding\CannotDecodeContent::jsonIssues(Object(JsonException))
#1 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Token/Parser.php(67): Lcobucci\JWT\Encoding\JoseEncoder->jsonDecode('ZXlKMGVYQWlPaUp...')
#2 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Token/Parser.php(30): Lcobucci\JWT\Token\Parser->parseHeader('eyJ0eXAiOiJKV1Q...')
#3 /home/smartmpsco/public_html/cust/rms/api/app/Http/Middleware/ParseToken.php(41): Lcobucci\JWT\Token\Parser->parse('eyJ0eXAiOiJKV1Q...')
#4 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(167): App\Http\Middleware\ParseToken->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#5 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#6 /home/smartmpsco/public_html/cust/rms/api/app/Http/Middleware/CorsMiddleware.php(60): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#7 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(167): App\Http\Middleware\CorsMiddleware->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#8 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#9 /home/smartmpsco/public_html/cust/rms/api/vendor/illuminate/pipeline/Pipeline.php(103): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#10 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(423): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(172): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#12 /home/smartmpsco/public_html/cust/rms/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(109): Laravel\Lumen\Application->dispatch(NULL)
#13 /home/smartmpsco/public_html/cust/rms/api/public/index.php(28): Laravel\Lumen\Application->run()
#14 {main}  

My Code to parse the token.

$aToken = $aConfig->parser()->parse(Crypt::decrypt($req->header('x-token')));

can you help me ?

@Ocramius
Copy link
Collaborator

Ocramius commented May 10, 2021 via email

@amrishkakadiya
Copy link

I can see this in your trace:
json_decode('ZXlKMGVYQWlPaUp
You aren't decoding JSON there?

no, its working in other server(nginx). I am passing raw string to parse which is stated in Documentation.

@Ocramius
Copy link
Collaborator

I can clearly see this:

#1 /home/smartmpsco/public_html/cust/rms/api/vendor/lcobucci/jwt/src/Token/Parser.php(67): Lcobucci\JWT\Encoding\JoseEncoder->jsonDecode('ZXlKMGVYQWlPaUp...')

That means that gibberish is passed to JSON decoding logic :-)

The exception is just the symptom.

Locking this thread: please do write an integration test about your use-case, before reporting a new issue with a reproducible scenario.

Repository owner locked as resolved and limited conversation to collaborators May 10, 2021
@lcobucci
Copy link
Owner

Is it possible that a bug in libsodium is the thing to blame here? That's the only explanation I have for it to fail on specific servers only. @Slamdunk have you experienced something like this?

@lcobucci
Copy link
Owner

@amrishkakadiya can you run the following code on both the new and old servers:

<?php

$headers = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9';

var_dump(
    base64_decode(strtr($headers, '-_', '+/'), true), 
    sodium_base642bin($headers, 7, '') 
);

Repository owner unlocked this conversation May 10, 2021
@amrishkakadiya
Copy link

Dedicated server response
string(27) "{"typ":"JWT","alg":"HS256"}" string(27) "{"typ":"JWT","alg":"HS256"}"

and Shared Server respone
Call to undefined function App\\Http\\Middleware\\sodium_base642bin()

So is it because of Sodium extenstion?

@lcobucci
Copy link
Owner

ext-sodium is required extension (as stated in the composer.json). Please make sure it is installed.

@amrishkakadiya
Copy link

ext-sodium is required extension (as stated in the composer.json). Please make sure it is installed.

Is there any way to install in Shared Hosting usign Cpanel without terminal access?

@lcobucci
Copy link
Owner

@amrishkakadiya sorry but I can't help you with that, it's been ages since I last used shared hosting. Try contacting them.

It's also important to note that copying vendor files from one place to another is a bit error-prone. Try to use composer install to detect any potential incompatibility.

@lcobucci
Copy link
Owner

Locking here as we discovered it's not related to the lib itself but the environment in which the library runs.

Repository owner locked as off-topic and limited conversation to collaborators May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants