Skip to content

Commit 0b34c12

Browse files
JS-1619 Reduce Sonar coverage import log noise (#6861)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 5d2cbb0 commit 0b34c12

File tree

13 files changed

+120
-144
lines changed

13 files changed

+120
-144
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,8 @@ jobs:
220220
development/artifactory/token/{REPO_OWNER_NAME_DASH}-qa-deployer access_token | ARTIFACTORY_DEPLOY_ACCESS_TOKEN;
221221
development/kv/data/sign key | SIGN_KEY;
222222
development/kv/data/sign passphrase | PGP_PASSPHRASE;
223-
- &java_coverage_cache
224-
name: Cache Java coverage
225-
uses: SonarSource/gh-action_cache@v1
226-
with:
227-
path: coverage/java
228-
key: java-coverage-${{ github.sha }}
229223
- name: Build, test and deploy Maven artifacts
230-
run: mvn deploy -Pdeploy-sonarsource,coverage,coverage-report,sign,release -T1C
224+
run: mvn deploy -Pdeploy-sonarsource,coverage,sign,release -T1C
231225
env:
232226
ARTIFACTORY_DEPLOY_USERNAME: ${{ fromJSON(steps.deployer-secrets.outputs.vault).ARTIFACTORY_DEPLOY_USERNAME }}
233227
ARTIFACTORY_DEPLOY_PASSWORD: ${{ fromJSON(steps.deployer-secrets.outputs.vault).ARTIFACTORY_DEPLOY_ACCESS_TOKEN }}
@@ -249,6 +243,13 @@ jobs:
249243
**/target/
250244
!**/target/site/
251245
retention-days: 1
246+
- &upload_jacoco_xml_reports
247+
name: Upload JaCoCo XML reports
248+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
249+
with:
250+
name: jacoco-xml-reports-${{ github.sha }}
251+
path: sonar-plugin/**/target/site/jacoco/jacoco.xml
252+
retention-days: 1
252253
# Clean up SonarJS artifacts before post-job cache save (only on default branch where cache is saved)
253254
- name: Clean up SonarJS artifacts before cache save
254255
if: github.ref_name == github.event.repository.default_branch
@@ -532,12 +533,16 @@ jobs:
532533
with:
533534
path: coverage/js
534535
key: js-coverage-${{ needs.setup.outputs.js-files-hash }}
535-
- *java_coverage_cache
536536
- &download_maven_targets
537537
name: Download Maven target artifacts
538538
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
539539
with:
540540
name: maven-targets-${{ github.sha }}
541+
- &download_jacoco_xml_reports
542+
name: Download JaCoCo XML reports
543+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
544+
with:
545+
name: jacoco-xml-reports-${{ github.sha }}
541546
- *config_maven
542547
- id: secrets
543548
uses: SonarSource/vault-action-wrapper@v3
@@ -587,8 +592,8 @@ jobs:
587592
- *npm_cache
588593
- *maven_cache
589594
- *js_coverage_cache
590-
- *java_coverage_cache
591595
- *download_maven_targets
596+
- *download_jacoco_xml_reports
592597
- *config_maven
593598
- id: secrets
594599
uses: SonarSource/vault-action-wrapper@v3

packages/analysis/src/jsts/rules/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ If you have any questions, encounter any bugs, or have feature requests, please
125125
| [aws-apigateway-public-api](https://sonarsource.github.io/rspec/#/rspec/S6333/javascript) | Creating public APIs is security-sensitive || | | | |
126126
| [aws-ec2-rds-dms-public](https://sonarsource.github.io/rspec/#/rspec/S6329/javascript) | Allowing public network access to cloud resources is security-sensitive || | | | |
127127
| [aws-ec2-unencrypted-ebs-volume](https://sonarsource.github.io/rspec/#/rspec/S6275/javascript) | EBS volumes should be encrypted || | | | |
128-
| [aws-efs-unencrypted](https://sonarsource.github.io/rspec/#/rspec/S6332/javascript) | Using unencrypted EFS file systems is security-sensitive || | | | |
128+
| [aws-efs-unencrypted](https://sonarsource.github.io/rspec/#/rspec/S6332/javascript) | Amazon EFS file systems should be encrypted || | | | |
129129
| [aws-iam-all-privileges](https://sonarsource.github.io/rspec/#/rspec/S6302/javascript) | Policies should not grant all privileges || | | | |
130130
| [aws-iam-all-resources-accessible](https://sonarsource.github.io/rspec/#/rspec/S6304/javascript) | Policies granting access to all resources of an account are security-sensitive | | | | | |
131131
| [aws-iam-privilege-escalation](https://sonarsource.github.io/rspec/#/rspec/S6317/javascript) | AWS IAM policies should limit the scope of permissions given || | | | |
@@ -328,7 +328,7 @@ If you have any questions, encounter any bugs, or have feature requests, please
328328
| [null-dereference](https://sonarsource.github.io/rspec/#/rspec/S2259/javascript) | Properties of variables with "null" or "undefined" values should not be accessed || | | 💭 | |
329329
| [object-alt-content](https://sonarsource.github.io/rspec/#/rspec/S5264/javascript) | "<object>" tags should provide an alternative content || | | | |
330330
| [operation-returning-nan](https://sonarsource.github.io/rspec/#/rspec/S3757/javascript) | Arithmetic operations should not result in "NaN" | | | | 💭 | |
331-
| [os-command](https://sonarsource.github.io/rspec/#/rspec/S4721/javascript) | Using shell interpreter when executing OS commands is security-sensitive || | | | |
331+
| [os-command](https://sonarsource.github.io/rspec/#/rspec/S4721/javascript) | OS commands should not be executed using a shell interpreter | | | | | |
332332
| [post-message](https://sonarsource.github.io/rspec/#/rspec/S2819/javascript) | Origins should be verified during cross-origin communications || | | 💭 | |
333333
| [prefer-default-last](https://sonarsource.github.io/rspec/#/rspec/S4524/javascript) | "default" clauses should be last || | | | |
334334
| [prefer-immediate-return](https://sonarsource.github.io/rspec/#/rspec/S1488/javascript) | Local variables should not be declared and then immediately returned or thrown | | 🔧 | | 💭 | |

pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@
8282
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
8383
<maven.compiler.release>17</maven.compiler.release>
8484
<jdk.min.version>17</jdk.min.version>
85+
<jacoco.version>0.8.12</jacoco.version>
8586
<version.surefire.plugin>3.1.2</version.surefire.plugin>
86-
<sonar.coverage.jacoco.xmlReportPaths>${maven.multiModuleProjectDirectory}/coverage/java/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
87-
8887
<!-- FIXME fix javadoc errors -->
8988
<doclint>none</doclint>
9089

9190
<!-- sonar analysis -->
9291
<sonar.sources>packages/</sonar.sources>
93-
<sonar.exclusions>packages/**/*.test.ts,packages/**/*.fixture.*,packages/**/fixtures/**/*,packages/**/fixtures-*/**/*,packages/grpc/src/proto/*,packages/analysis/src/jsts/parsers/estree.js,packages/analysis/src/jsts/parsers/estree.d.ts</sonar.exclusions>
92+
<sonar.exclusions>packages/**/*.test.ts,packages/**/*.fixture.*,packages/**/fixtures/**/*,packages/**/fixtures-*/**/*,packages/grpc/src/proto/*,packages/analysis/src/jsts/parsers/estree.js,packages/analysis/src/jsts/parsers/estree.d.ts,packages/analysis/src/jsts/rules/**/generated-meta.ts,packages/analysis/src/jsts/rules/metas.ts,packages/analysis/src/jsts/rules/plugin-rules.ts,packages/analysis/src/jsts/rules/rules.ts</sonar.exclusions>
9493
<sonar.tests>packages/</sonar.tests>
9594
<sonar.test.inclusions>packages/**/*.test.ts</sonar.test.inclusions>
9695
<sonar.javascript.lcov.reportPaths>coverage/js/lcov.info</sonar.javascript.lcov.reportPaths>
@@ -99,7 +98,7 @@
9998
${project.basedir}/packages/tsconfig.app.json,${project.basedir}/packages/tsconfig.test.json
10099
</sonar.typescript.tsconfigPath>
101100
<sonar.cpd.exclusions>sonar-plugin/javascript-checks/src/main/resources/**/*.html</sonar.cpd.exclusions>
102-
<sonar.coverage.exclusions>packages/ruling/**/*,packages/*/tests/**/*,resources/org/sonar/l10n/javascript/rules/javascript/**</sonar.coverage.exclusions>
101+
<sonar.coverage.exclusions>packages/ruling/**/*,packages/*/tests/**/*,resources/org/sonar/l10n/javascript/rules/javascript/**,packages/analysis/src/jsts/rules/**/generated-meta.ts,packages/analysis/src/jsts/rules/metas.ts,packages/analysis/src/jsts/rules/plugin-rules.ts,packages/analysis/src/jsts/rules/rules.ts</sonar.coverage.exclusions>
103102
</properties>
104103

105104
<dependencyManagement>

sonar-plugin/api/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
<name>SonarQube JavaScript :: API</name>
1414

15+
<properties>
16+
<sonar.coverage.jacoco.xmlReportPaths>${project.reporting.outputDirectory}/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
17+
</properties>
18+
1519
<dependencies>
1620
<dependency>
1721
<groupId>org.sonarsource.api.plugin</groupId>

sonar-plugin/bridge/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<name>SonarQube JavaScript :: Bridge</name>
1212
<properties>
1313
<protobuf.version>4.34.1</protobuf.version>
14+
<sonar.coverage.jacoco.xmlReportPaths>${project.reporting.outputDirectory}/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
1415
</properties>
1516
<dependencies>
1617
<dependency>
@@ -117,6 +118,16 @@
117118
</filesets>
118119
</configuration>
119120
</plugin>
121+
<plugin>
122+
<groupId>org.jacoco</groupId>
123+
<artifactId>jacoco-maven-plugin</artifactId>
124+
<version>${jacoco.version}</version>
125+
<configuration>
126+
<excludes>
127+
<exclude>org/sonar/plugins/javascript/bridge/protobuf/*</exclude>
128+
</excludes>
129+
</configuration>
130+
</plugin>
120131
<plugin>
121132
<groupId>org.xolstice.maven.plugins</groupId>
122133
<artifactId>protobuf-maven-plugin</artifactId>

sonar-plugin/coverage-report/pom.xml

Lines changed: 0 additions & 88 deletions
This file was deleted.

sonar-plugin/css/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
<name>SonarQube JavaScript :: CSS</name>
1414

15+
<properties>
16+
<sonar.coverage.jacoco.xmlReportPaths>${project.reporting.outputDirectory}/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
17+
</properties>
18+
1519
<dependencies>
1620
<dependency>
1721
<groupId>${project.groupId}</groupId>

sonar-plugin/javascript-checks/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
<name>SonarQube JavaScript :: Checks</name>
1414

15+
<properties>
16+
<sonar.coverage.jacoco.xmlReportPaths>${project.reporting.outputDirectory}/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
17+
<sonar.exclusions>src/main/java/org/sonar/javascript/checks/AllChecks.java,src/main/java/org/sonar/javascript/checks/S*.java</sonar.exclusions>
18+
<sonar.coverage.exclusions>src/main/java/org/sonar/javascript/checks/AllChecks.java,src/main/java/org/sonar/javascript/checks/S*.java</sonar.coverage.exclusions>
19+
</properties>
20+
1521
<dependencies>
1622
<dependency>
1723
<groupId>${project.groupId}</groupId>
@@ -65,6 +71,17 @@
6571
</filesets>
6672
</configuration>
6773
</plugin>
74+
<plugin>
75+
<groupId>org.jacoco</groupId>
76+
<artifactId>jacoco-maven-plugin</artifactId>
77+
<version>${jacoco.version}</version>
78+
<configuration>
79+
<excludes>
80+
<exclude>org/sonar/javascript/checks/AllChecks*</exclude>
81+
<exclude>org/sonar/javascript/checks/S*</exclude>
82+
</excludes>
83+
</configuration>
84+
</plugin>
6885
<plugin>
6986
<groupId>org.codehaus.mojo</groupId>
7087
<artifactId>exec-maven-plugin</artifactId>

sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/EscapeUtils.java

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,65 @@ public final class EscapeUtils {
2121
private EscapeUtils() {}
2222

2323
public static String unescape(String value) {
24-
StringBuilder result = new StringBuilder();
25-
StringBuilder escapeSequence = new StringBuilder(4);
24+
if (value.indexOf('\\') < 0) {
25+
return value;
26+
}
27+
28+
StringBuilder result = new StringBuilder(value.length());
2629
int i = 0;
2730
while (i < value.length()) {
2831
char ch = value.charAt(i);
29-
if (ch == '\\') {
30-
i++;
31-
ch = value.charAt(i);
32-
if (ch == 'u') {
33-
i = consumeEscapeSequence(i, 4, value, escapeSequence, result);
34-
} else if (ch == 'x') {
35-
i = consumeEscapeSequence(i, 2, value, escapeSequence, result);
36-
} else {
37-
result.append(unescape(ch));
38-
i++;
39-
}
40-
} else {
32+
if (ch != '\\') {
4133
result.append(ch);
4234
i++;
35+
continue;
36+
}
37+
38+
if (i + 1 == value.length()) {
39+
result.append(ch);
40+
break;
41+
}
42+
43+
char escaped = value.charAt(i + 1);
44+
if (escaped == 'u') {
45+
i = consumeEscapeSequence(i, 4, value, result);
46+
} else if (escaped == 'x') {
47+
i = consumeEscapeSequence(i, 2, value, result);
48+
} else {
49+
result.append(unescape(escaped));
50+
i += 2;
4351
}
4452
}
4553
return result.toString();
4654
}
4755

48-
private static int consumeEscapeSequence(
49-
int i,
50-
int len,
51-
String value,
52-
StringBuilder escapeSequence,
53-
StringBuilder result
54-
) {
55-
int ii = i;
56-
while (escapeSequence.length() != len && ii < value.length()) {
57-
ii++;
58-
escapeSequence.append(value.charAt(ii));
56+
private static int consumeEscapeSequence(int i, int len, String value, StringBuilder result) {
57+
int escapeStart = i;
58+
int digitsStart = i + 2;
59+
int digitsEnd = digitsStart + len;
60+
61+
if (digitsEnd > value.length()) {
62+
result.append(value, escapeStart, value.length());
63+
return value.length();
64+
}
65+
66+
String escapeSequence = value.substring(digitsStart, digitsEnd);
67+
if (isHexadecimal(escapeSequence)) {
68+
result.append((char) Integer.parseInt(escapeSequence, 16));
69+
} else {
70+
result.append(value, escapeStart, digitsEnd);
5971
}
60-
if (escapeSequence.length() == len) {
61-
result.append((char) Integer.parseInt(escapeSequence.toString(), 16));
62-
escapeSequence.setLength(0);
72+
73+
return digitsEnd;
74+
}
75+
76+
private static boolean isHexadecimal(String value) {
77+
for (int i = 0; i < value.length(); i++) {
78+
if (Character.digit(value.charAt(i), 16) < 0) {
79+
return false;
80+
}
6381
}
64-
ii++;
65-
return ii;
82+
return true;
6683
}
6784

6885
private static char unescape(char ch) {

sonar-plugin/javascript-checks/src/test/java/org/sonar/javascript/checks/EscapeUtilsTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
class EscapeUtilsTest {
2424

2525
@Test
26-
void test() {
27-
assertThat(EscapeUtils.unescape("foo")).isEqualTo("foo");
26+
void should_unescape_known_sequences() {
27+
String plainText = "foo";
28+
assertThat(EscapeUtils.unescape(plainText)).isSameAs(plainText);
2829
assertThat(EscapeUtils.unescape("\\u000B")).isEqualTo("\u000B");
2930
assertThat(EscapeUtils.unescape("\\x0B")).isEqualTo("\u000B");
3031

@@ -39,4 +40,14 @@ void test() {
3940
assertThat(EscapeUtils.unescape("\\\\")).isEqualTo("\\");
4041
assertThat(EscapeUtils.unescape("\\|")).isEqualTo("|");
4142
}
43+
44+
@Test
45+
void should_preserve_incomplete_or_invalid_escape_sequences() {
46+
assertThat(EscapeUtils.unescape("\\")).isEqualTo("\\");
47+
assertThat(EscapeUtils.unescape("foo\\")).isEqualTo("foo\\");
48+
assertThat(EscapeUtils.unescape("\\u00")).isEqualTo("\\u00");
49+
assertThat(EscapeUtils.unescape("\\x0")).isEqualTo("\\x0");
50+
assertThat(EscapeUtils.unescape("\\u00GG")).isEqualTo("\\u00GG");
51+
assertThat(EscapeUtils.unescape("\\xGG")).isEqualTo("\\xGG");
52+
}
4253
}

0 commit comments

Comments
 (0)