Skip to content

Commit 51c3ad4

Browse files
SONARJAVA-3697 Update rules metadata
1 parent b79d4a9 commit 51c3ad4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+255
-319
lines changed

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1124_java.html

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<p>The Java Language Specification recommends listing modifiers in the following order:</p>
2-
<p>1. Annotations</p>
3-
<p>2. public</p>
4-
<p>3. protected</p>
5-
<p>4. private</p>
6-
<p>5. abstract</p>
7-
<p>6. static</p>
8-
<p>7. final</p>
9-
<p>8. transient</p>
10-
<p>9. volatile</p>
11-
<p>10. synchronized</p>
12-
<p>11. native</p>
13-
<p>12. strictfp</p>
2+
<ol>
3+
<li> Annotations </li>
4+
<li> public </li>
5+
<li> protected </li>
6+
<li> private </li>
7+
<li> abstract </li>
8+
<li> static </li>
9+
<li> final </li>
10+
<li> transient </li>
11+
<li> volatile </li>
12+
<li> synchronized </li>
13+
<li> native </li>
14+
<li> strictfp </li>
15+
</ol>
1416
<p>Not following this convention has no technical impact, but will reduce the code's readability because most developers are used to the standard
1517
order.</p>
1618
<h2>Noncompliant Code Example</h2>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1130_java.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<p>An exception in a <code>throws</code> declaration in Java is superfluous if it is:</p>
2-
<p> * listed multiple times</p>
3-
<p> * a subclass of another listed exception</p>
4-
<p> * completely unnecessary because the declared exception type cannot actually be thrown</p>
2+
<ul>
3+
<li> listed multiple times </li>
4+
<li> a subclass of another listed exception </li>
5+
<li> completely unnecessary because the declared exception type cannot actually be thrown </li>
6+
</ul>
57
<h2>Noncompliant Code Example</h2>
68
<pre>
79
void foo() throws MyException, MyException {} // Noncompliant; should be listed once
@@ -14,11 +16,13 @@ <h2>Compliant Solution</h2>
1416
</pre>
1517
<h2>Exceptions</h2>
1618
<p>The rule will not raise any issue for exceptions that cannot be thrown from the method body:</p>
17-
<p> * in overriding and implementation methods</p>
18-
<p> * in interface <code>default</code> methods</p>
19-
<p> * in non-private methods that only <code>throw</code>, have empty bodies, or a single return statement.</p>
20-
<p> * in overridable methods (non-final, or not member of a final class, non-static, non-private), if the exception is documented with a proper
21-
JavaDoc</p>
19+
<ul>
20+
<li> in overriding and implementation methods </li>
21+
<li> in interface <code>default</code> methods </li>
22+
<li> in non-private methods that only <code>throw</code>, have empty bodies, or a single return statement. </li>
23+
<li> in overridable methods (non-final, or not member of a final class, non-static, non-private), if the exception is documented with a proper
24+
JavaDoc </li>
25+
</ul>
2226
<p>Also, the rule won't raise issues on <code>RuntimeException</code>, or one of its descendants, because explicating runtime exceptions which could
2327
be thrown can ultimately help the method's users, and can even be considered as good practice.</p>
2428
<pre>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1168_java.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ <h2>Noncompliant Code Example</h2>
2020
}
2121
}
2222
}
23-
2423
</pre>
2524
<h2>Compliant Solution</h2>
2625
<pre>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1182_java.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ol>
66
<li> <code>x.clone() != x</code> </li>
77
<li> <code>x.clone().getClass() == x.getClass()</code> </li>
8-
<li> <code>x.clone().equals\(x\)</code> </li>
8+
<li> <code>x.clone().equals(x)</code> </li>
99
</ol>
1010
<p>Obtaining the object that will be returned by calling <code>super.clone()</code> helps to satisfy those invariants:</p>
1111
<ol>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1700_java.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ <h2>Compliant Solution</h2>
2525

2626
Foo foo = new Foo();
2727
foo.getName()
28-
2928
</pre>
3029
<h2>Exceptions</h2>
3130
<p>When the type of the field is the containing class and that field is static, no issue is raised to allow singletons named like the type. </p>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1860_java.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ <h2>Noncompliant Code Example</h2>
3535
synchronized(listLock) { // Noncompliant
3636
// ...
3737
}
38-
3938
</pre>
4039
<h2>Compliant Solution</h2>
4140
<pre>
@@ -60,6 +59,8 @@ <h2>Compliant Solution</h2>
6059
}
6160
</pre>
6261
<h2>See</h2>
63-
<p> * <a href="https://wiki.sei.cmu.edu/confluence/x/1zdGBQ">CERT, LCK01-J.</a> - Do not synchronize on objects that may be reused</p>
64-
<p> * <a href="https://openjdk.java.net/jeps/390">JEP-390.</a> - JEP 390: Warnings for Value-Based Classes</p>
62+
<ul>
63+
<li> <a href="https://wiki.sei.cmu.edu/confluence/x/1zdGBQ">CERT, LCK01-J.</a> - Do not synchronize on objects that may be reused </li>
64+
<li> <a href="https://openjdk.java.net/jeps/390">JEP-390.</a> - JEP 390: Warnings for Value-Based Classes </li>
65+
</ul>
6566

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1862_java.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ <h2>Compliant Solution</h2>
2121
else if (param == 3)
2222
moveWindowToTheBackground();
2323
}
24-
2524
</pre>
2625
<h2>See</h2>
2726
<ul>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077_java.html

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,18 @@
1-
<p>Formatting strings used as SQL queries is security-sensitive. It has led in the past to the following vulnerabilities:</p>
2-
<ul>
3-
<li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-9019">CVE-2018-9019</a> </li>
4-
<li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-7318">CVE-2018-7318</a> </li>
5-
<li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-5611">CVE-2017-5611</a> </li>
6-
</ul>
7-
<p>SQL queries often need to use a hardcoded SQL string with a dynamic parameter coming from a user request. Formatting a string to add those
8-
parameters to the request is a bad practice as it can result in an <a href="https://www.owasp.org/index.php/SQL_Injection">SQL injection</a>. The safe
9-
way to add parameters to a SQL query is to use SQL binding mechanisms.</p>
10-
<p>This rule raises an issue when an SQL query is built by formatting Strings, even if there is no injection. This rule does not detect SQL
11-
injections. The goal is to guide security code reviews and to prevent a common bad practice.</p>
12-
<p>The following method signatures from Java JDBC, JPA, JDO, Hibernate and Spring are tested: </p>
13-
<ul>
14-
<li> <code>org.hibernate.Session.createQuery</code> </li>
15-
<li> <code>org.hibernate.Session.createSQLQuery</code> </li>
16-
<li> <code>java.sql.Statement.executeQuery</code> </li>
17-
<li> <code>java.sql.Statement.execute</code> </li>
18-
<li> <code>java.sql.Statement.executeUpdate</code> </li>
19-
<li> <code>java.sql.Statement.executeLargeUpdate</code> </li>
20-
<li> <code>java.sql.Statement.addBatch</code> </li>
21-
<li> <code>java.sql.Connection.prepareStatement</code> </li>
22-
<li> <code>java.sql.Connection.prepareCall</code> </li>
23-
<li> <code>java.sql.Connection.nativeSQL</code> </li>
24-
<li> <code>javax.persistence.EntityManager.createNativeQuery</code> </li>
25-
<li> <code>javax.persistence.EntityManager.createQuery</code> </li>
26-
<li> <code>org.springframework.jdbc.core.JdbcOperations.batchUpdate</code> </li>
27-
<li> <code>org.springframework.jdbc.core.JdbcOperations.execute</code> </li>
28-
<li> <code>org.springframework.jdbc.core.JdbcOperations.query</code> </li>
29-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForList</code> </li>
30-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForMap</code> </li>
31-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForObject</code> </li>
32-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForRowSet</code> </li>
33-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForInt</code> </li>
34-
<li> <code>org.springframework.jdbc.core.JdbcOperations.queryForLong</code> </li>
35-
<li> <code>org.springframework.jdbc.core.JdbcOperations.update</code> </li>
36-
<li> <code>org.springframework.jdbc.core.PreparedStatementCreatorFactory.&lt;init&gt;</code> </li>
37-
<li> <code>org.springframework.jdbc.core.PreparedStatementCreatorFactory.newPreparedStatementCreator</code> </li>
38-
<li> <code>javax.jdo.PersistenceManager.newQuery</code> </li>
39-
<li> <code>javax.jdo.Query.setFilter</code> </li>
40-
<li> <code>javax.jdo.Query.setGrouping</code> </li>
41-
</ul>
42-
<p>If a method is defined in an interface, implementations are also tested. For example this is the case for
43-
<code>org.springframework.jdbc.core.JdbcOperations</code> , which is usually used as <code>org.springframework.jdbc.core.JdbcTemplate</code>). </p>
1+
<p>Formatted SQL queries can be difficult to maintain, debug and can increase the risk of SQL injection when concatenating untrusted values into the
2+
query. However, this rule doesn't detect SQL injections (unlike rule s3649), the goal is only to highlight complex/formatted queries.</p>
443
<h2>Ask Yourself Whether</h2>
454
<ul>
46-
<li> the SQL query is built using string formatting technics, such as concatenating variables. </li>
47-
<li> some of the values are coming from an untrusted source and are not sanitized. </li>
5+
<li> Some parts of the query come from untrusted values (like user inputs). </li>
6+
<li> The query is repeated/duplicated in other parts of the code. </li>
7+
<li> The application must support different types of relational databases. </li>
488
</ul>
499
<p>There is a risk if you answered yes to any of those questions.</p>
5010
<h2>Recommended Secure Coding Practices</h2>
5111
<ul>
52-
<li> Avoid building queries manually using formatting technics. If you do it anyway, do not include user input in this building process. </li>
5312
<li> Use <a href="https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet">parameterized queries, prepared statements, or stored
54-
procedures</a> whenever possible. </li>
55-
<li> You may also use ORM frameworks such as Hibernate which, if used correctly, reduce injection risks. </li>
56-
<li> Avoid executing SQL queries containing unsafe input in stored procedures or functions. </li>
57-
<li> <a href="https://www.owasp.org/index.php/Input_Validation_Cheat_Sheet">Sanitize</a> every unsafe input. </li>
13+
procedures</a> and bind variables to SQL query parameters. </li>
14+
<li> Consider using ORM frameworks if there is a need to have an abstract layer to access data. </li>
5815
</ul>
59-
<p>You can also reduce the impact of an attack by using a database account with low privileges.</p>
6016
<h2>Sensitive Code Example</h2>
6117
<pre>
6218
public User getUser(Connection con, String user) throws SQLException {

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077_java.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"hibernate",
1717
"sql"
1818
],
19-
"defaultSeverity": "Critical",
19+
"defaultSeverity": "Major",
2020
"ruleSpecification": "RSPEC-2077",
2121
"sqKey": "S2077",
2222
"scope": "Main",

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2115_java.html

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
<p>Databases should always be password protected. The use of a database connection with an empty password is a clear indication of a database that is
2-
not protected.</p>
3-
<p>This rule flags database connections with empty passwords.</p>
1+
<p>When relying on the password authentication mode for the database connection, a secure password should be chosen.</p>
2+
<p>This rule raises an issue when an empty password is used.</p>
43
<h2>Noncompliant Code Example</h2>
54
<pre>
6-
Connection conn = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true", "AppLogin", "");
7-
Connection conn2 = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true?user=user&amp;password=");
5+
Connection conn = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true", "login", "");
86
</pre>
97
<h2>Compliant Solution</h2>
108
<pre>
11-
DriverManager.getConnection("jdbc:derby:memory:myDB;create=true?user=user&amp;password=password");
12-
13-
DriverManager.getConnection("jdbc:mysql://address=(host=myhost1)(port=1111)(key1=value1)(user=sandy)(password=secret),address=(host=myhost2)(port=2222)(key2=value2)(user=sandy)(password=secret)/db");
14-
15-
DriverManager.getConnection("jdbc:mysql://sandy:secret@[myhost1:1111,myhost2:2222]/db");
16-
17-
String url = "jdbc:postgresql://localhost/test";
18-
Properties props = new Properties();
19-
props.setProperty("user", "fred");
20-
props.setProperty("password", "secret");
21-
DriverManager.getConnection(url, props);
9+
String password = System.getProperty("database.password");
10+
Connection conn = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true", "login", password);
2211
</pre>
2312
<h2>See</h2>
2413
<ul>

0 commit comments

Comments
 (0)