blob: b4ed175e6adb6fc2fe267bb22f56aca79fe1bb6f [file] [log] [blame]
Alex Stapletonc5fffd32014-03-18 15:29:00 +00001Submitting patches
Paul Kehrer0839aa82014-02-11 22:36:51 -06002==================
3
4* Always make a new branch for your work.
5* Patches should be small to facilitate easier review. `Studies have shown`_
6 that review quality falls off as patch size grows. Sometimes this will result
7 in many small PRs to land a single large feature.
8* Larger changes should be discussed on `our mailing list`_ before submission.
9* New features and significant bug fixes should be documented in the
10 :doc:`/changelog`.
Alex Gaynorfda92472014-11-06 17:05:48 -030011* You must have legal permission to distribute any code you contribute to
12 ``cryptography``, and it must be available under both the BSD and Apache
13 Software License Version 2.0 licenses.
Paul Kehrer0839aa82014-02-11 22:36:51 -060014
15If you believe you've identified a security issue in ``cryptography``, please
16follow the directions on the :doc:`security page </security>`.
17
18Code
19----
20
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060021When in doubt, refer to :pep:`8` for Python code. You can check if your code
Lucia Lic6ba99d2021-11-08 22:06:11 +080022meets our automated requirements by formatting it with ``black`` and running
23``flake8`` against it. If you've installed the development requirements this
24will automatically use our configuration. You can also run the ``tox`` job with
25``tox -e pep8``.
Paul Kehrer0839aa82014-02-11 22:36:51 -060026
27`Write comments as complete sentences.`_
28
Alex Gaynoree1d96c2014-12-12 10:50:46 -080029Class names which contains acronyms or initialisms should always be
30capitalized. A class should be named ``HTTPClient``, not ``HttpClient``.
31
Alex Gaynor5951f462014-11-16 09:08:42 -080032Every code file must start with the boilerplate licensing notice:
33
34.. code-block:: python
35
36 # This file is dual licensed under the terms of the Apache License, Version
37 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
38 # for complete details.
39
Paul Kehrer0839aa82014-02-11 22:36:51 -060040Additionally, every Python code file must contain
41
42.. code-block:: python
43
44 from __future__ import absolute_import, division, print_function
45
Alex Stapletonc5fffd32014-03-18 15:29:00 +000046API considerations
Paul Kehrer0839aa82014-02-11 22:36:51 -060047~~~~~~~~~~~~~~~~~~
48
49Most projects' APIs are designed with a philosophy of "make easy things easy,
50and make hard things possible". One of the perils of writing cryptographic code
51is that secure code looks just like insecure code, and its results are almost
Alex Gaynor0c11d042016-06-02 22:24:22 -070052always indistinguishable. As a result, ``cryptography`` has, as a design
Paul Kehrer0839aa82014-02-11 22:36:51 -060053philosophy: "make it hard to do insecure things". Here are a few strategies for
54API design that should be both followed, and should inspire other API choices:
55
56If it is necessary to compare a user provided value with a computed value (for
57example, verifying a signature), there should be an API provided that performs
58the verification in a secure way (for example, using a constant time
59comparison), rather than requiring the user to perform the comparison
60themselves.
61
62If it is incorrect to ignore the result of a method, it should raise an
63exception, and not return a boolean ``True``/``False`` flag. For example, a
64method to verify a signature should raise ``InvalidSignature``, and not return
65whether the signature was valid.
66
67.. code-block:: python
68
69 # This is bad.
70 def verify(sig):
71 # ...
72 return is_valid
73
74 # Good!
75 def verify(sig):
76 # ...
77 if not is_valid:
78 raise InvalidSignature
79
80Every recipe should include a version or algorithmic marker of some sort in its
81output in order to allow transparent upgrading of the algorithms in use, as
82the algorithms or parameters needed to achieve a given security margin evolve.
83
Lucia Lic6ba99d2021-11-08 22:06:11 +080084APIs at the :doc:`/hazmat/primitives/index` and recipes layer should
85automatically use the :func:`~cryptography.hazmat.backends.default_backend`,
86but optionally allow specifying a different backend.
Paul Kehrer0839aa82014-02-11 22:36:51 -060087
88C bindings
89~~~~~~~~~~
90
Laurens Van Houtven0a1d9e12014-06-23 14:06:16 +020091More information on C bindings can be found in :doc:`the dedicated
92section of the documentation <c-bindings>`.
93
Paul Kehrer0839aa82014-02-11 22:36:51 -060094Tests
95-----
96
97All code changes must be accompanied by unit tests with 100% code coverage (as
98measured by the combined metrics across our build matrix).
99
100When implementing a new primitive or recipe ``cryptography`` requires that you
Paul Kehrer1681a692014-02-11 23:43:51 -0600101provide a set of test vectors. See :doc:`/development/test-vectors` for more
102details.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600103
104Documentation
105-------------
106
Paul Kehrer813b2942014-06-05 12:40:56 -0500107All features should be documented with prose in the ``docs`` section. To ensure
108it builds and passes `doc8`_ style checks you can run ``tox -e docs``.
Paul Kehrer0839aa82014-02-11 22:36:51 -0600109
110Because of the inherent challenges in implementing correct cryptographic
111systems, we want to make our documentation point people in the right directions
112as much as possible. To that end:
113
114* When documenting a generic interface, use a strong algorithm in examples.
115 (e.g. when showing a hashing example, don't use
116 :class:`~cryptography.hazmat.primitives.hashes.MD5`)
117* When giving prescriptive advice, always provide references and supporting
118 material.
119* When there is real disagreement between cryptographic experts, represent both
120 sides of the argument and describe the trade-offs clearly.
121
122When documenting a new module in the ``hazmat`` package, its documentation
123should begin with the "Hazardous Materials" warning:
124
125.. code-block:: rest
126
127 .. hazmat::
128
Gabriel Orisakab6e0ba02016-08-02 23:25:49 -0300129Always prefer terminology that is most broadly accepted. For example:
130
131* When referring to class instances use "an instance of ``Foo``"
132 instead of "a ``Foo`` provider".
133
Paul Kehrer0839aa82014-02-11 22:36:51 -0600134When referring to a hypothetical individual (such as "a person receiving an
135encrypted message") use gender neutral pronouns (they/them/their).
136
137Docstrings are typically only used when writing abstract classes, but should
138be written like this if required:
139
140.. code-block:: python
141
142 def some_function(some_arg):
143 """
144 Does some things.
145
146 :param some_arg: Some argument.
147 """
148
149So, specifically:
150
151* Always use three double quotes.
152* Put the three double quotes on their own line.
153* No blank line at the end.
154* Use Sphinx parameter/attribute documentation `syntax`_.
155
156
Alex Gaynor4a772a82017-03-23 09:23:24 -0400157.. _`Write comments as complete sentences.`: https://nedbatchelder.com/blog/201401/comments_should_be_sentences.html
Alex Gaynorebaa5702018-12-30 14:28:48 -0600158.. _`syntax`: https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists
Lucia Lic6ba99d2021-11-08 22:06:11 +0800159.. _`Studies have shown`: https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/
Paul Kehrer0839aa82014-02-11 22:36:51 -0600160.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Paul Kehrer277b7012016-01-01 09:58:53 -0600161.. _`doc8`: https://github.com/openstack/doc8