|
| 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"]) |
0 commit comments