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

Guard against PKCS1 PEM-encoded public keys #277

Merged
merged 3 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ This project adheres to [Semantic Versioning](http://semver.org/).
[Unreleased][unreleased]
-------------------------------------------------------------------------
### Changed
### Fixed
### Added

[v1.5.1][1.5.1]
-------------------------------------------------------------------------
### Changed

- Change optparse for argparse. [#238][238]

### Fixed

- Guard against PKCS1 PEM encododed public keys
- Add deprecation warning when decoding without specifying `algorithms`

### Added

- Support for Python 3.6 [#262][262]
Expand Down Expand Up @@ -132,6 +141,7 @@ rarely used. Users affected by this should upgrade to 3.3+.
[1.4.1]: https://github.com/jpadilla/pyjwt/compare/1.4.0...1.4.1
[1.4.2]: https://github.com/jpadilla/pyjwt/compare/1.4.1...1.4.2
[1.5.0]: https://github.com/jpadilla/pyjwt/compare/1.4.2...1.5.0
[1.5.1]: https://github.com/jpadilla/pyjwt/compare/1.5.0...1.5.1

[109]: https://github.com/jpadilla/pyjwt/pull/109
[110]: https://github.com/jpadilla/pyjwt/pull/110
Expand Down
1 change: 1 addition & 0 deletions jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def prepare_key(self, key):
invalid_strings = [
b'-----BEGIN PUBLIC KEY-----',
b'-----BEGIN CERTIFICATE-----',
b'-----BEGIN RSA PUBLIC KEY-----',
b'ssh-rsa'
]

Expand Down
9 changes: 9 additions & 0 deletions jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def encode(self, payload, key, algorithm='HS256', headers=None,

def decode(self, jws, key='', verify=True, algorithms=None, options=None,
**kwargs):

if not algorithms:
warnings.warn(
'It is strongly recommended that you pass in a ' +
'value for the "algorithms" argument when calling decode(). ' +
'This argument will be mandatory in a future version.',
DeprecationWarning
)

payload, signing_input, header, signature = self._load(jws)

if verify:
Expand Down
9 changes: 9 additions & 0 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def encode(self, payload, key, algorithm='HS256', headers=None,

def decode(self, jwt, key='', verify=True, algorithms=None, options=None,
**kwargs):

if not algorithms:
warnings.warn(
'It is strongly recommended that you pass in a ' +
'value for the "algorithms" argument when calling decode(). ' +
'This argument will be mandatory in a future version.',
DeprecationWarning
)

payload, signing_input, header, signature = self._load(jwt)

decoded = super(PyJWT, self).decode(jwt, key, verify, algorithms,
Expand Down
5 changes: 5 additions & 0 deletions tests/keys/testkey_pkcs1.pub.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN RSA PUBLIC KEY-----
MIGHAoGBAOV/0Vl/5VdHcYpnILYzBGWo5JQVzo9wBkbxzjAStcAnTwvv1ZJTMXs6
fjz91f9hiMM4Z/5qNTE/EHlDWxVdj1pyRaQulZPUs0r9qJ02ogRRGLG3jjrzzbzF
yj/pdNBwym0UJYC/Jmn/kMLwGiWI2nfa9vM5SovqZiAy2FD7eOtVAgED
-----END RSA PUBLIC KEY-----
7 changes: 7 additions & 0 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def test_hmac_should_throw_exception_if_key_is_x509_cert(self):
with open(key_path('testkey2_rsa.pub.pem'), 'r') as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_should_throw_exception_if_key_is_pkcs1_pem_public(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path('testkey_pkcs1.pub.pem'), 'r') as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_jwk_should_parse_and_verify(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ def test_verify_false_deprecated(self, jws, recwarn):

pytest.deprecated_call(jws.decode, example_jws, verify=False)

def test_decode_with_optional_algorithms(self, jws):
example_secret = 'secret'
example_jws = (
b'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'
b'aGVsbG8gd29ybGQ.'
b'SIr03zM64awWRdPrAM_61QWsZchAtgDV3pphfHPPWkI'
)

pytest.deprecated_call(jws.decode, example_jws, key=example_secret)

def test_load_no_verification(self, jws, payload):
right_secret = 'foo'
jws_message = jws.encode(payload, right_secret)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,13 @@ def test_decode_with_verify_expiration_kwarg(self, jwt, payload):
secret,
verify_expiration=True
)

def test_decode_with_optional_algorithms(self, jwt, payload):
secret = 'secret'
jwt_message = jwt.encode(payload, secret)

pytest.deprecated_call(
jwt.decode,
jwt_message,
secret
)