blob: 475e703714ee7dc92177e6f082f7e5b3c5e5e2e9 [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
22meets our automated requirements by running ``flake8`` against it. If you've
23installed the development requirements this will automatically use our
24configuration. You can also run the ``tox`` job with ``tox -e pep8``.
Paul Kehrer0839aa82014-02-11 22:36:51 -060025
26`Write comments as complete sentences.`_
27
Alex Gaynoree1d96c2014-12-12 10:50:46 -080028Class names which contains acronyms or initialisms should always be
29capitalized. A class should be named ``HTTPClient``, not ``HttpClient``.
30
Alex Gaynor5951f462014-11-16 09:08:42 -080031Every code file must start with the boilerplate licensing notice:
32
33.. code-block:: python
34
35 # This file is dual licensed under the terms of the Apache License, Version
36 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
37 # for complete details.
38
Paul Kehrer0839aa82014-02-11 22:36:51 -060039Additionally, every Python code file must contain
40
41.. code-block:: python
42
43 from __future__ import absolute_import, division, print_function
44
Alex Stapletonc5fffd32014-03-18 15:29:00 +000045API considerations
Paul Kehrer0839aa82014-02-11 22:36:51 -060046~~~~~~~~~~~~~~~~~~
47
48Most projects' APIs are designed with a philosophy of "make easy things easy,
49and make hard things possible". One of the perils of writing cryptographic code
50is that secure code looks just like insecure code, and its results are almost
Alex Gaynor0c11d042016-06-02 22:24:22 -070051always indistinguishable. As a result, ``cryptography`` has, as a design
Paul Kehrer0839aa82014-02-11 22:36:51 -060052philosophy: "make it hard to do insecure things". Here are a few strategies for
53API design that should be both followed, and should inspire other API choices:
54
55If it is necessary to compare a user provided value with a computed value (for
56example, verifying a signature), there should be an API provided that performs
57the verification in a secure way (for example, using a constant time
58comparison), rather than requiring the user to perform the comparison
59themselves.
60
61If it is incorrect to ignore the result of a method, it should raise an
62exception, and not return a boolean ``True``/``False`` flag. For example, a
63method to verify a signature should raise ``InvalidSignature``, and not return
64whether the signature was valid.
65
66.. code-block:: python
67
68 # This is bad.
69 def verify(sig):
70 # ...
71 return is_valid
72
73 # Good!
74 def verify(sig):
75 # ...
76 if not is_valid:
77 raise InvalidSignature
78
79Every recipe should include a version or algorithmic marker of some sort in its
80output in order to allow transparent upgrading of the algorithms in use, as
81the algorithms or parameters needed to achieve a given security margin evolve.
82
83APIs at the :doc:`/hazmat/primitives/index` layer should always take an
84explicit backend, APIs at the recipes layer should automatically use the
85:func:`~cryptography.hazmat.backends.default_backend`, but optionally allow
86specifying a different backend.
87
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
129When referring to a hypothetical individual (such as "a person receiving an
130encrypted message") use gender neutral pronouns (they/them/their).
131
132Docstrings are typically only used when writing abstract classes, but should
133be written like this if required:
134
135.. code-block:: python
136
137 def some_function(some_arg):
138 """
139 Does some things.
140
141 :param some_arg: Some argument.
142 """
143
144So, specifically:
145
146* Always use three double quotes.
147* Put the three double quotes on their own line.
148* No blank line at the end.
149* Use Sphinx parameter/attribute documentation `syntax`_.
150
151
152.. _`Write comments as complete sentences.`: http://nedbatchelder.com/blog/201401/comments_should_be_sentences.html
153.. _`syntax`: http://sphinx-doc.org/domains.html#info-field-lists
Paul Kehrer277b7012016-01-01 09:58:53 -0600154.. _`Studies have shown`: https://smartbear.com/SmartBear/media/pdfs/11_Best_Practices_for_Peer_Code_Review.pdf
Paul Kehrer0839aa82014-02-11 22:36:51 -0600155.. _`our mailing list`: https://mail.python.org/mailman/listinfo/cryptography-dev
Paul Kehrer277b7012016-01-01 09:58:53 -0600156.. _`doc8`: https://github.com/openstack/doc8