Skip to content

Commit 2966ddd

Browse files
committed
docs: add errors docs
1 parent 115b5c5 commit 2966ddd

8 files changed

Lines changed: 222 additions & 108 deletions

File tree

docs/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Changelog
1818
**Unreleased**
1919

2020
- Show security warnings for ``none`` and ``RSA1_5`` algorithms.
21+
- Show security warnings for ``OctKey.generate_key`` and ``RSAKey.generate_key``
22+
when key size is too short, per `NIST SP 800-131A`_.
23+
24+
.. _`NIST SP 800-131A`: https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final
2125

2226
**Breaking changes**:
2327

@@ -52,7 +56,7 @@ Changelog
5256
**Released on Feb 28, 2025**
5357

5458
- Use secrets module to generate random bytes.
55-
- Use warnings for possible unsafe ``OctKey``` instead of raising error, via :issue:`32`.
59+
- Use warnings for possible unsafe ``OctKey`` instead of raising error, via :issue:`32`.
5660

5761
1.0.3
5862
-----

docs/guide/errors.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Errors & Warnings
2+
=================
3+
4+
Here are some common errors and warnings, and how to handle them.
5+
6+
SecurityWarning
7+
---------------
8+
9+
You may encounter a ``SecurityWarning`` when using potentially
10+
unsafe algorithms or generating insecure keys. These warnings
11+
do not interrupt the execution of your application — they are
12+
simply printed to standard output (e.g., your terminal).
13+
14+
If you prefer to suppress these warnings, you can use Python’s
15+
built-in ``warnings`` module:
16+
17+
.. code-block:: python
18+
19+
import warnings
20+
from joserfc.errors import SecurityWarning
21+
22+
warnings.simplefilter('ignore', SecurityWarning)
23+
24+
With this configuration, ``SecurityWarning`` messages will no
25+
longer appear. Be cautious when suppressing these warnings, as
26+
they are meant to alert you to potentially insecure practices.
27+
28+
UnsupportedAlgorithmError
29+
-------------------------
30+
31+
By default, **ONLY recommended** :ref:`jwa` are allowed. With non recommended
32+
algorithms, you may encounter the ``UnsupportedAlgorithmError``` error.
33+
34+
.. code-block:: python
35+
36+
>>> from joserfc import jws
37+
>>> from joserfc.jwk import OctKey
38+
>>> key = OctKey.generate_key()
39+
>>> jws.serialize_compact({"alg": "HS384"}, b"payload", key)
40+
Traceback (most recent call last):
41+
File "<stdin>", line 1, in <module>
42+
File ".../joserfc/jws.py", line 112, in serialize_compact
43+
alg: JWSAlgModel = registry.get_alg(protected["alg"])
44+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
File ".../joserfc/_rfc7515/registry.py", line 60, in get_alg
46+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not recommended')
47+
joserfc.errors.UnsupportedAlgorithmError: unsupported_algorithm: Algorithm of "HS384" is not recommended
48+
49+
Because "HS384" is not a recommended algorithm, it is not allowed by default. You
50+
SHOULD enable it manually by passing an ``algorithms`` parameter:
51+
52+
.. code-block:: python
53+
54+
>>> jws.serialize_compact({"alg": "HS384"}, b"payload", key, algorithms=["HS384"])

docs/guide/jws.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ the below error.
279279
280280
>>> from joserfc import jws
281281
>>> from joserfc.jwk import OctKey
282-
>>> key = OctKey.import_key("secret")
282+
>>> key = OctKey.generate_key()
283283
>>> jws.serialize_compact({"alg": "HS384"}, b"payload", key)
284284
Traceback (most recent call last):
285285
File "<stdin>", line 1, in <module>
286286
File ".../joserfc/jws.py", line 112, in serialize_compact
287287
alg: JWSAlgModel = registry.get_alg(protected["alg"])
288288
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
289-
File ".../joserfc/rfc7515/registry.py", line 60, in get_alg
289+
File ".../joserfc/_rfc7515/registry.py", line 60, in get_alg
290290
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not recommended')
291291
joserfc.errors.UnsupportedAlgorithmError: unsupported_algorithm: Algorithm of "HS384" is not recommended
292292

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Explore the following sections to discover more about ``joserfc`` and its featur
7878
guide/index
7979
guide/algorithms
8080
guide/registry
81+
guide/errors
8182
migrations/index
8283

8384
.. toctree::

0 commit comments

Comments
 (0)