Skip to content

Commit 4ff6866

Browse files
guillaume-dequennesonartech
authored andcommitted
SONARPY-2806 Update rules metadata (#241)
GitOrigin-RevId: 631974c5093711fc0b7c03cab544ce8876b78e1b
1 parent 482506b commit 4ff6866

5 files changed

Lines changed: 56 additions & 17 deletions

File tree

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5547.html

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ <h3>Code examples</h3>
5151
<h4>Noncompliant code example</h4>
5252
<pre data-diff-id="11" data-diff-type="noncompliant">
5353
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
54-
from cryptography.hazmat.backends import default_backend
5554

56-
cipher = Cipher(algorithms.TripleDES(key), mode=None, backend=default_backend()) # Noncompliant
55+
cipher = Cipher(algorithms.TripleDES(key), mode=None) # Noncompliant
5756
</pre>
5857
<h4>Compliant solution</h4>
5958
<pre data-diff-id="11" data-diff-type="compliant">
6059
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
61-
from cryptography.hazmat.backends import default_backend
6260

63-
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
61+
cipher = Cipher(algorithms.AES(key), modes.CTR(nonce))
6462
</pre>
63+
<p>In this example, the counter mode (CTR) of AES is used. This mode takes a cryptographic nonce (a <strong>n</strong>umber used only
64+
<strong>once</strong>) as its initialization vector (IV). This value must never be reused, as doing so allows attackers to decrypt any message
65+
encrypted with the same key.</p>
6566
<h3>How does this work?</h3>
6667
<h4>Use a secure algorithm</h4>
6768
<p>It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
@@ -74,15 +75,15 @@ <h4>Noncompliant code example</h4>
7475
<pre data-diff-id="21" data-diff-type="noncompliant">
7576
from Crypto.Cipher import DES
7677

77-
cipher = DES.new(key) # Noncompliant
78+
cipher = DES.new(key, DES.MODE_OFB) # Noncompliant
7879
</pre>
7980
<h4>Compliant solution</h4>
80-
<p>PyCrypto is deprecated, thus it is recommended to use another library like pyca.</p>
81+
<p>PyCrypto is deprecated, thus it is recommended to use another library like PyCryptodome.</p>
8182
<pre data-diff-id="21" data-diff-type="compliant">
82-
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
83-
from cryptography.hazmat.backends import default_backend
83+
from Crypto.Cipher import AES # pycryptodome
84+
from Cryptodome.Cipher import AES # pycryptodomex
8485

85-
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
86+
cipher = AES.new(key, AES.MODE_CCM)
8687
</pre>
8788
<h3>How does this work?</h3>
8889
<h4>Use a secure algorithm</h4>
@@ -99,13 +100,15 @@ <h4>Noncompliant code example</h4>
99100
cipher = pyDes.des(key) # Noncompliant
100101
</pre>
101102
<h4>Compliant solution</h4>
102-
<p>Since pyDes only provides DES, it is recommended to use another library like pyca.</p>
103+
<p>Since pyDes only provides DES, it is recommended to use another library like <code>pyca/cryptography</code>.</p>
103104
<pre data-diff-id="31" data-diff-type="compliant">
104105
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
105-
from cryptography.hazmat.backends import default_backend
106106

107-
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
107+
cipher = Cipher(algorithms.AES(key), modes.CTR(nonce))
108108
</pre>
109+
<p>In this example, the counter mode (CTR) of AES is used. This mode takes a cryptographic nonce (a <strong>n</strong>umber used only
110+
<strong>once</strong>) as its initialization vector (IV). This value must never be reused, as doing so allows attackers to decrypt any message
111+
encrypted with the same key.</p>
109112
<h3>How does this work?</h3>
110113
<h4>Use a secure algorithm</h4>
111114
<p>It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
@@ -129,7 +132,38 @@ <h4>Compliant solution</h4>
129132
ctx = ssl.create_default_context()
130133
</pre>
131134
<h3>How does this work?</h3>
132-
<p>It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time.</p>
135+
<p>It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time. If
136+
specific cipher suites need to be enabled or disabled, then this is also possible by adding them after the <code>DEFAULT</code> cipher suite
137+
string.</p>
138+
<p>For example, <code>DEFAULT:!RSA:!SHA</code> enables all default cipher suites except those using RSA and SHA1. <code>DEFAULT:HIGH+AESGCM</code>
139+
enables all default cipher suites, as well as all high encryption cipher suites that use AES-GCM.</p>
140+
<p>More information about possible options can be found in the <a
141+
href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL documentation</a>.</p>
142+
<h2>How to fix it in OpenSSL</h2>
143+
<h3>Code examples</h3>
144+
<p>The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.</p>
145+
<h4>Noncompliant code example</h4>
146+
<pre data-diff-id="51" data-diff-type="noncompliant">
147+
from OpenSSL import SSL
148+
149+
ciphers = b"DEFAULT:RC4-SHA:RC4-MD5"
150+
ctx = SSL.Context(SSL.TLS1_3_VERSION)
151+
ctx.set_cipher_list(ciphers) # Noncompliant
152+
</pre>
153+
<h4>Compliant solution</h4>
154+
<pre data-diff-id="51" data-diff-type="compliant">
155+
from OpenSSL import SSL
156+
157+
ctx = SSL.Context(SSL.TLS1_3_VERSION)
158+
</pre>
159+
<h3>How does this work?</h3>
160+
<p>It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time. If
161+
specific cipher suites need to be enabled or disabled, then this is also possible by adding them after the <code>DEFAULT</code> cipher suite
162+
string.</p>
163+
<p>For example, <code>DEFAULT:!RSA:!SHA</code> enables all default cipher suites except those using RSA and SHA1. <code>DEFAULT:HIGH+AESGCM</code>
164+
enables all default cipher suites, as well as all high encryption cipher suites that use AES-GCM.</p>
165+
<p>More information about possible options can be found in the <a
166+
href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL documentation</a>.</p>
133167
<h2>Resources</h2>
134168
<h3>Standards</h3>
135169
<ul>

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S6554.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ <h4>Compliant solution</h4>
2626
</pre>
2727
<h2>Resources</h2>
2828
<h3>Documentation</h3>
29-
<p><a href="https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model.<em>str</em>">Django Model.<em>str</em>()</a></p>
29+
<p><a
30+
href="https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model">https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model</a>.<em>str</em>[Django
31+
Model.<em>str</em>()]</p>
3032

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S6662.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ <h4>Compliant solution</h4>
3131
<h2>Resources</h2>
3232
<h3>Documentation</h3>
3333
<ul>
34-
<li> Python Documentation - <a href="https://docs.python.org/3/reference/datamodel.html#object.<em>hash</em>">object.<em>hash</em></a> </li>
34+
<li> Python Documentation - <a
35+
href="https://docs.python.org/3/reference/datamodel.html#object">https://docs.python.org/3/reference/datamodel.html#object</a>.<em>hash</em>[object.<em>hash</em>] </li>
3536
<li> Python Documentation - <a href="https://docs.python.org/3/library/functions.html#hash">the hash built-in function</a> </li>
3637
</ul>
3738

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S6663.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ <h4>Compliant solution</h4>
3434
</pre>
3535
<h2>Resources</h2>
3636
<h3>Documentation</h3>
37-
<p>Python Documentation - <a href="https://docs.python.org/3/library/operator.html#operator.<em>index</em>"><em>index</em> method</a></p>
37+
<p>Python Documentation - <a
38+
href="https://docs.python.org/3/library/operator.html#operator">https://docs.python.org/3/library/operator.html#operator</a>.<em>index</em>[<em>index</em>
39+
method]</p>
3840

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"PY"
55
],
6-
"latest-update": "2025-04-29T12:46:37.789290Z",
6+
"latest-update": "2025-05-01T09:47:33.813232Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": true

0 commit comments

Comments
 (0)