Skip to content

Commit 7636762

Browse files
SONARJAVA-5523 Update rule metadata (#4965)
1 parent 76c87a6 commit 7636762

File tree

10 files changed

+67
-9
lines changed

10 files changed

+67
-9
lines changed

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1075.html

+24
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ <h2>Why is this an issue?</h2>
1010
<p>For all those reasons, a URI should never be hard coded. Instead, it should be replaced by a customizable parameter.</p>
1111
<p>Further, even if the elements of a URI are obtained dynamically, portability can still be limited if the path delimiters are hard-coded.</p>
1212
<p>This rule raises an issue when URIs or path delimiters are hard-coded.</p>
13+
<h3>Exceptions</h3>
14+
<p>This rule does not raise an issue when:</p>
15+
<ul>
16+
<li> A constant path is relative and contains at most two parts. </li>
17+
<li> A constant path is used in an annotation </li>
18+
<li> A path is annotated </li>
19+
</ul>
1320
<h2>How to fix it</h2>
1421
<h3>Code examples</h3>
1522
<h4>Noncompliant code example</h4>
1623
<pre data-diff-id="1" data-diff-type="noncompliant">
1724
public class Foo {
25+
public static final String FRIENDS_ENDPOINT = "/user/friends"; // Compliant path is relative and has only two parts
26+
1827
public Collection&lt;User&gt; listUsers() {
1928
File userList = new File("/home/mylogin/Dev/users.txt"); // Noncompliant
2029
Collection&lt;User&gt; users = parse(userList);
@@ -40,4 +49,19 @@ <h4>Compliant solution</h4>
4049
}
4150
}
4251
</pre>
52+
<p>Exceptions examples:</p>
53+
<pre>
54+
public class Foo {
55+
public static final String FRIENDS_ENDPOINT = "/user/friends"; // Compliant path is relative and has only two parts
56+
57+
public static final String ACCOUNT = "/account/group/list.html"; // Compliant path is used in an annotation
58+
59+
@Value("${base.url}" + ACCOUNT)
60+
private String groupUrl;
61+
62+
@MyAnnotation()
63+
String path = "/default/url/for/site"; // Compliant path is annotated
64+
65+
}
66+
</pre>
4367

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S115.html

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ <h4>Compliant solution</h4>
4646
OPTION_TWO;
4747
}
4848
</pre>
49+
<h3>Exceptions</h3>
50+
<p>The rule applies to fields of primitive types (for example, <code>float</code>), boxed primitives (<code>Float</code>), and Strings. We do not
51+
apply it to other types, which can be mutated, or have methods with side effects.</p>
52+
<pre>
53+
public static final Logger log = getLogger(MyClass.class);
54+
public static final List&lt;Integer&gt; myList = new ArrayList&lt;&gt;();
55+
56+
// call with side-effects
57+
log.info("message")
58+
59+
// mutating an object
60+
myList.add(28);
61+
</pre>
4962
<h2>Resources</h2>
5063
<h3>External coding guidelines</h3>
5164
<ul>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1190.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h4>Noncompliant code example</h4>
1717
<h4>Compliant solution</h4>
1818
<pre data-diff-id="1" data-diff-type="compliant">
1919
public class MyClass {
20-
String s = ""; // Noncompliant
20+
String s = ""; // Compliant
2121
}
2222
</pre>
2323
<h2>Resources</h2>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1451.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"func": "Constant\/Issue",
1313
"constantCost": "5min"
1414
},
15-
"tags": [],
15+
"tags": [
16+
"convention"
17+
],
1618
"defaultSeverity": "Blocker",
1719
"ruleSpecification": "RSPEC-1451",
1820
"sqKey": "S1451",

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1751.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
"func": "Constant\/Issue",
1313
"constantCost": "5min"
1414
},
15-
"tags": [],
15+
"tags": [
16+
"confusing",
17+
"bad-practice"
18+
],
1619
"defaultSeverity": "Major",
1720
"ruleSpecification": "RSPEC-1751",
1821
"sqKey": "S1751",

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1948.html

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<p>This rule raises an issue on a non-transient and non-serializable field within a serializable class, if said class does not have
22
<code>writeObject</code> and <code>readObject</code> methods defined.</p>
33
<h2>Why is this an issue?</h2>
4-
<p>By contract, fields in a <code>Serializable</code> class must themselves be either <code>Serializable</code> or <code>transient</code>. Even if the
5-
class is never explicitly serialized or deserialized, it is not safe to assume that this cannot happen. For instance, under load, most J2EE
6-
application frameworks flush objects to disk.</p>
4+
<p>By contract, non-static fields in a <code>Serializable</code> class must themselves be either <code>Serializable</code> or <code>transient</code>.
5+
Even if the class is never explicitly serialized or deserialized, it is not safe to assume that this cannot happen. For instance, under load, most
6+
J2EE application frameworks flush objects to disk.</p>
77
<p>An object that implements <code>Serializable</code> but contains non-transient, non-serializable data members (and thus violates the contract)
88
could cause application crashes and open the door to attackers. In general, a <code>Serializable</code> class is expected to fulfil its contract and
99
not exhibit unexpected behaviour when an instance is serialized.</p>
@@ -84,6 +84,16 @@ <h2>How to fix it</h2>
8484
}
8585
}
8686
</pre>
87+
<p>Finally, static fields are out of scope for serialization, so making a field static prevents issues from being raised.</p>
88+
<pre>
89+
public class Person implements Serializable {
90+
private static final long serialVersionUID = 1905122041950251207L;
91+
92+
private String name;
93+
94+
private static Logger log = getLogger(); // Compliant, static fields are not serialized
95+
}
96+
</pre>
8797
<h2>Resources</h2>
8898
<ul>
8999
<li> CWE - <a href="https://cwe.mitre.org/data/definitions/594">CWE-594 - Saving Unserializable Objects to Disk</a> </li>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S2077.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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 {rule:javasecurity:S3649}), the goal is only to highlight complex/formatted queries.</p>
2+
query. However, this rule doesn’t detect SQL injections (unlike rule {rule:java:S3649}), the goal is only to highlight complex/formatted queries.</p>
33
<h2>Ask Yourself Whether</h2>
44
<ul>
55
<li> Some parts of the query come from untrusted values (like user inputs). </li>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S3749.html

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ <h2>Why is this an issue?</h2>
1010
<li> <code>org.springframework.beans.factory.annotation.Value</code> </li>
1111
<li> <code>javax.annotation.Inject</code> </li>
1212
<li> <code>javax.annotation.Resource</code> </li>
13+
<li> <code>javax.persistence.PersistenceContext</code> </li>
14+
<li> <code>jakarta.annotation.Resource</code> </li>
15+
<li> <code>jakarta.inject.Inject</code> </li>
16+
<li> <code>jakarta.persistence.PersistenceContext</code> </li>
1317
</ul>
1418
<h2>How to fix it</h2>
1519
<p>Add one of these annotations to all non-<code>static</code> members: <code>@Resource</code>, <code>@Inject</code>, <code>@Autowired</code> or

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S3981.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"func": "Constant\/Issue",
1313
"constantCost": "2min"
1414
},
15-
"tags": [],
15+
"tags": [
16+
"confusing"
17+
],
1618
"defaultSeverity": "Major",
1719
"ruleSpecification": "RSPEC-3981",
1820
"sqKey": "S3981",

sonarpedia.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"JAVA"
55
],
6-
"latest-update": "2024-11-29T11:04:25.911576775Z",
6+
"latest-update": "2024-12-17T09:08:48.208626612Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": false

0 commit comments

Comments
 (0)