Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable ucfg rule for JS embedded in HTML files (#4074)
Co-authored-by: Victor Diez <victor.diez@sonarsource.com>
- Loading branch information
1 parent
911655e
commit ce1d001
Showing
7 changed files
with
246 additions
and
3 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/HtmlSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.javascript.it.plugin; | ||
|
||
import static com.sonar.javascript.it.plugin.OrchestratorStarter.JAVASCRIPT_PLUGIN_LOCATION; | ||
import static com.sonar.javascript.it.plugin.OrchestratorStarter.getSonarScanner; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.sonar.javascript.it.plugin.assertj.BuildResultAssert; | ||
import com.sonar.orchestrator.Orchestrator; | ||
import com.sonar.orchestrator.build.SonarScanner; | ||
import com.sonar.orchestrator.container.Edition; | ||
import com.sonar.orchestrator.junit5.OrchestratorExtension; | ||
import com.sonar.orchestrator.locator.FileLocation; | ||
import com.sonar.orchestrator.locator.MavenLocation; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HtmlSecurityTest { | ||
|
||
private static Orchestrator orchestrator; | ||
|
||
@Test | ||
void should_not_generate_ucfgs_for_html() throws IOException { | ||
var projectKey = "html-project"; | ||
var projectPath = TestUtils.projectDir(projectKey); | ||
|
||
OrchestratorStarter.setProfiles( | ||
orchestrator, | ||
projectKey, | ||
Map.of("html-security-profile", "js") | ||
); | ||
|
||
var result = orchestrator.executeBuild(getScanner(projectPath, projectKey)); | ||
assertThat(result.isSuccess()).isTrue(); | ||
|
||
var stream = Files.find( | ||
projectPath.toPath().resolve(".scannerwork"), | ||
3, | ||
BuildResultAssert::isUcfgFile | ||
); | ||
assertThat(stream.toList()).isEmpty(); | ||
} | ||
|
||
@BeforeAll | ||
public static void startOrchestrator() { | ||
var builder = OrchestratorExtension | ||
.builderEnv() | ||
.useDefaultAdminCredentialsForBuilds(true) | ||
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "LATEST_RELEASE")) | ||
.addPlugin(JAVASCRIPT_PLUGIN_LOCATION) | ||
.setEdition(Edition.DEVELOPER) | ||
.activateLicense() | ||
.addPlugin(MavenLocation.of("com.sonarsource.security", "sonar-security-plugin", "DEV")) | ||
.addPlugin( | ||
MavenLocation.of("com.sonarsource.security", "sonar-security-js-frontend-plugin", "DEV") | ||
) | ||
.addPlugin(MavenLocation.of("org.sonarsource.html", "sonar-html-plugin", "LATEST_RELEASE")) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/html-security-profile.xml")); | ||
|
||
orchestrator = builder.build(); | ||
// Installation of SQ server in orchestrator is not thread-safe, so we need to synchronize | ||
synchronized (OrchestratorStarter.class) { | ||
orchestrator.start(); | ||
} | ||
} | ||
|
||
@AfterAll | ||
public static void stopOrchestrator() { | ||
orchestrator.stop(); | ||
} | ||
|
||
private static SonarScanner getScanner(File projectDir, String projectKey) { | ||
return getSonarScanner() | ||
.setProjectKey(projectKey) | ||
.setSourceEncoding("UTF-8") | ||
.setDebugLogs(true) | ||
.setSourceDirs(".") | ||
.setProjectDir(projectDir); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
its/plugin/tests/src/test/java/com/sonar/javascript/it/plugin/YamlSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2012-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.sonar.javascript.it.plugin; | ||
|
||
import static com.sonar.javascript.it.plugin.OrchestratorStarter.JAVASCRIPT_PLUGIN_LOCATION; | ||
import static com.sonar.javascript.it.plugin.OrchestratorStarter.getSonarScanner; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.sonar.javascript.it.plugin.assertj.BuildResultAssert; | ||
import com.sonar.orchestrator.Orchestrator; | ||
import com.sonar.orchestrator.build.SonarScanner; | ||
import com.sonar.orchestrator.container.Edition; | ||
import com.sonar.orchestrator.junit5.OrchestratorExtension; | ||
import com.sonar.orchestrator.locator.FileLocation; | ||
import com.sonar.orchestrator.locator.MavenLocation; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class YamlSecurityTest { | ||
|
||
private static Orchestrator orchestrator; | ||
|
||
@Test | ||
void should_generate_ucfgs_for_yaml() throws IOException { | ||
var projectKey = "yaml-aws-lambda-analyzed"; | ||
var projectPath = TestUtils.projectDir(projectKey); | ||
|
||
OrchestratorStarter.setProfiles( | ||
orchestrator, | ||
projectKey, | ||
Map.of("yaml-security-profile", "js") | ||
); | ||
|
||
var result = orchestrator.executeBuild(getScanner(projectPath, projectKey)); | ||
assertThat(result.isSuccess()).isTrue(); | ||
|
||
var stream = Files.find( | ||
projectPath.toPath().resolve(".scannerwork"), | ||
3, | ||
BuildResultAssert::isUcfgFile | ||
); | ||
assertThat(stream.toList()).hasSize(1); | ||
} | ||
|
||
@BeforeAll | ||
public static void startOrchestrator() { | ||
var builder = OrchestratorExtension | ||
.builderEnv() | ||
.useDefaultAdminCredentialsForBuilds(true) | ||
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "LATEST_RELEASE")) | ||
.addPlugin(JAVASCRIPT_PLUGIN_LOCATION) | ||
.setEdition(Edition.DEVELOPER) | ||
.activateLicense() | ||
.addPlugin(MavenLocation.of("com.sonarsource.security", "sonar-security-plugin", "DEV")) | ||
.addPlugin( | ||
MavenLocation.of("com.sonarsource.security", "sonar-security-js-frontend-plugin", "DEV") | ||
) | ||
.addPlugin( | ||
MavenLocation.of("org.sonarsource.config", "sonar-config-plugin", "LATEST_RELEASE") | ||
) | ||
.restoreProfileAtStartup(FileLocation.ofClasspath("/yaml-security-profile.xml")); | ||
|
||
orchestrator = builder.build(); | ||
// Installation of SQ server in orchestrator is not thread-safe, so we need to synchronize | ||
synchronized (OrchestratorStarter.class) { | ||
orchestrator.start(); | ||
} | ||
} | ||
|
||
@AfterAll | ||
public static void stopOrchestrator() { | ||
orchestrator.stop(); | ||
} | ||
|
||
private static SonarScanner getScanner(File projectDir, String projectKey) { | ||
return getSonarScanner() | ||
.setProjectKey(projectKey) | ||
.setSourceEncoding("UTF-8") | ||
.setDebugLogs(true) | ||
.setSourceDirs(".") | ||
.setProjectDir(projectDir); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
its/plugin/tests/src/test/resources/html-security-profile.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<profile> | ||
<name>html-security-profile</name> | ||
<language>js</language> | ||
<rules> | ||
<rule> | ||
<repositoryKey>javascript</repositoryKey> | ||
<key>S1116</key> <!-- empty statement --> | ||
<priority>INFO</priority> | ||
</rule> | ||
<rule> | ||
<repositoryKey>jssecurity</repositoryKey> | ||
<key>S2076</key> <!-- OSCommandInjectionCheck --> | ||
<priority>INFO</priority> | ||
</rule> | ||
</rules> | ||
</profile> |
17 changes: 17 additions & 0 deletions
17
its/plugin/tests/src/test/resources/yaml-security-profile.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<profile> | ||
<name>yaml-security-profile</name> | ||
<language>js</language> | ||
<rules> | ||
<rule> | ||
<repositoryKey>javascript</repositoryKey> | ||
<key>S1116</key> <!-- empty statement --> | ||
<priority>INFO</priority> | ||
</rule> | ||
<rule> | ||
<repositoryKey>jssecurity</repositoryKey> | ||
<key>S2076</key> <!-- OSCommandInjectionCheck --> | ||
<priority>INFO</priority> | ||
</rule> | ||
</rules> | ||
</profile> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters