Skip to content

Commit 7970104

Browse files
chore(docs): example of unsafe header decode in README (#501)
1 parent d957f8e commit 7970104

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: README.md

+34
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,40 @@ $decoded_array = (array) $decoded;
6565
JWT::$leeway = 60; // $leeway in seconds
6666
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
6767
```
68+
Example encode/decode headers
69+
-------
70+
Decoding the JWT headers without verifying the JWT first is NOT recommended, and is not supported by
71+
this library. This is because without verifying the JWT, the header values could have been tampered with.
72+
Any value pulled from an unverified header should be treated as if it could be any string sent in from an
73+
attacker. If this is something you still want to do in your application for whatever reason, it's possible to
74+
decode the header values manually simply by calling `json_decode` and `base64_decode` on the JWT
75+
header part:
76+
```php
77+
use Firebase\JWT\JWT;
78+
79+
$key = 'example_key';
80+
$payload = [
81+
'iss' => 'http://example.org',
82+
'aud' => 'http://example.com',
83+
'iat' => 1356999524,
84+
'nbf' => 1357000000
85+
];
86+
87+
$headers = [
88+
'x-forwarded-for' => 'www.google.com'
89+
];
90+
91+
// Encode headers in the JWT string
92+
$jwt = JWT::encode($payload, $key, 'HS256', null, $headers);
93+
94+
// Decode headers from the JWT string WITHOUT validation
95+
// **IMPORTANT**: This operation is vulnerable to attacks, as the JWT has not yet been verified.
96+
// These headers could be any value sent by an attacker.
97+
list($headersB64, $payloadB64, $sig) = explode('.', $jwt);
98+
$decoded = json_decode(base64_decode($headersB64), true);
99+
100+
print_r($decoded);
101+
```
68102
Example with RS256 (openssl)
69103
----------------------------
70104
```php

0 commit comments

Comments
 (0)