From c6ab9ea43d98bb7e048c08545a1b8ee2e3bf0505 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 15:18:11 -0700 Subject: [PATCH] Run ATH with CSP (report only) --- Jenkinsfile | 2 +- pom.xml | 2 +- .../test/acceptance/junit/CspRule.java | 45 +++++++++++++++++ .../csp/ContentSecurityPolicyReport.java | 50 +++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java create mode 100644 src/main/java/org/jenkinsci/test/acceptance/plugins/csp/ContentSecurityPolicyReport.java diff --git a/Jenkinsfile b/Jenkinsfile index 6ada015aa..095a3bbd0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -39,7 +39,7 @@ if (needSplittingFromWorkspace) { } def axes = [ - jenkinsVersions: ['lts', 'latest'], + jenkinsVersions: ['latest'], platforms: ['linux'], jdks: [17, 21], browsers: ['firefox'], diff --git a/pom.xml b/pom.xml index d6cc6fab1..13fb62ea9 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 999999-SNAPSHOT true - 2.479 + 2.480 4.25.0 33.3.1-jre diff --git a/src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java b/src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java new file mode 100644 index 000000000..b1d55e4f7 --- /dev/null +++ b/src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java @@ -0,0 +1,45 @@ +package org.jenkinsci.test.acceptance.junit; + +import com.google.inject.Inject; +import com.google.inject.Injector; +import java.util.List; +import java.util.logging.Logger; +import org.jenkinsci.test.acceptance.plugins.csp.ContentSecurityPolicyReport; +import org.jenkinsci.test.acceptance.po.Jenkins; +import org.jenkinsci.test.acceptance.update_center.PluginSpec; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +@GlobalRule +public final class CspRule implements TestRule { + + private static final Logger LOGGER = Logger.getLogger(CspRule.class.getName()); + + @Inject + Injector injector; + + @Override + public Statement apply(final Statement base, final Description d) { + return new Statement() { + private Jenkins jenkins; + + @Override + public void evaluate() throws Throwable { + jenkins = injector.getInstance(Jenkins.class); + final PluginSpec plugin = new PluginSpec("csp"); + LOGGER.info("Installing plugin for test: " + plugin); + jenkins.getPluginManager().installPlugins(plugin); + try { + base.evaluate(); + } finally { + ContentSecurityPolicyReport csp = new ContentSecurityPolicyReport(jenkins); + List lines = csp.getReport(); + if (lines.size() > 2) { + throw new AssertionError(String.join("\n", csp.getReport())); + } + } + } + }; + } +} diff --git a/src/main/java/org/jenkinsci/test/acceptance/plugins/csp/ContentSecurityPolicyReport.java b/src/main/java/org/jenkinsci/test/acceptance/plugins/csp/ContentSecurityPolicyReport.java new file mode 100644 index 000000000..7fecf16f1 --- /dev/null +++ b/src/main/java/org/jenkinsci/test/acceptance/plugins/csp/ContentSecurityPolicyReport.java @@ -0,0 +1,50 @@ +package org.jenkinsci.test.acceptance.plugins.csp; + +import java.util.ArrayList; +import java.util.List; +import org.jenkinsci.test.acceptance.po.Jenkins; +import org.jenkinsci.test.acceptance.po.PageObject; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; + +public class ContentSecurityPolicyReport extends PageObject { + public ContentSecurityPolicyReport(Jenkins context) { + super(context, context.url("content-security-policy-reports/")); + } + + public List getReport() { + List lines = new ArrayList<>(); + WebElement table; + try { + open(); + table = find(By.className("bigtable")); + } catch (NoSuchElementException e) { + try { + getContext().getJenkins().logout(); + } catch (Exception ignored) { + // Ignore + } + getContext().getJenkins().login().doLogin("admin"); + open(); + table = find(By.className("bigtable")); + } + List headers = table.findElements(By.tagName("th")); + StringBuilder sb = new StringBuilder(); + for (WebElement header : headers) { + sb.append(header.getText()).append("\t"); + } + lines.add(sb.toString()); + sb = new StringBuilder(); + List rows = table.findElements(By.tagName("tr")); + for (WebElement row : rows) { + List cells = row.findElements(By.tagName("td")); + for (WebElement cell : cells) { + sb.append(cell.getText()).append("\t"); + } + lines.add(sb.toString()); + sb = new StringBuilder(); + } + return lines; + } +}