Skip to content

Commit 2c08eec

Browse files
authored
chore: add checker to detect 'none' algo in JWT token encode/decode method (#122)
* chore: add checker to detect 'none' algo in JWT token encode/decode method Signed-off-by: Maharshi Basu <basumaharshi10@gmail.com> * chore: update checker message and add reference to test file Signed-off-by: Maharshi Basu <basumaharshi10@gmail.com> --------- Signed-off-by: Maharshi Basu <basumaharshi10@gmail.com>
1 parent c41d72e commit 2c08eec

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import jwt
2+
3+
# adapted from
4+
# - https://github.com/Shopify/shopify_python_api/blob/main/test/session_token_test.py#L59
5+
# - https://github.com/flipkart-incubator/Astra/blob/master/modules/jwt_attack.py#L37
6+
def bad1():
7+
# <expect-error>
8+
encoded = jwt.encode({'some': 'payload'}, None, algorithm='none')
9+
return encoded
10+
11+
12+
def bad2(encoded):
13+
# <expect-error>
14+
jwt.decode(encoded, None, algorithms=['none'])
15+
return encoded
16+
17+
def ok(secret_key):
18+
# <no-error>
19+
encoded = jwt.encode({'some': 'payload'}, secret_key, algorithm='HS256')
20+
return encoded
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
language: py
2+
name: jwt-python-none-alg
3+
message: Do not use `none` algorithm for encoding/decoding JWT tokens
4+
category: security
5+
6+
pattern: |
7+
(call
8+
function: (attribute
9+
object: (identifier) @jwt
10+
attribute: (identifier) @encode)
11+
arguments: (argument_list
12+
(_)*
13+
(keyword_argument
14+
name: (identifier) @algorithm
15+
value: (string
16+
(string_content) @none))
17+
(_)*)
18+
(#eq? @jwt "jwt")
19+
(#eq? @encode "encode")
20+
(#eq? @algorithm "algorithm")
21+
(#eq? @none "none")) @jwt-python-none-alg
22+
23+
24+
(call
25+
function: (attribute
26+
object: (identifier) @jwt
27+
attribute: (identifier) @decode)
28+
arguments: (argument_list
29+
(_)*
30+
(keyword_argument
31+
name: (identifier) @algorithms
32+
value: (list
33+
(string
34+
(string_content) @none)))
35+
(_)*)
36+
(#eq? @jwt "jwt")
37+
(#eq? @decode "decode")
38+
(#eq? @algorithms "algorithms")
39+
(#eq? @none "none")) @jwt-python-none-alg
40+
41+
desciption: |
42+
The JWT token uses the 'none' algorithm, which assumes its integrity is already verified. This allows attackers to forge tokens that get automatically verified. Avoid using 'none'; use a secure algorithm like 'HS256' instead.

0 commit comments

Comments
 (0)