Skip to content

Commit

Permalink
Don't mess with CSP listener
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-jeckels committed Nov 27, 2024
1 parent f85c935 commit e595248
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
35 changes: 8 additions & 27 deletions src/org/labkey/test/BaseWebDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public abstract class BaseWebDriverTest extends LabKeySiteWrapper implements Cle
private static boolean _dumpedHeap = false;
private final ArtifactCollector _artifactCollector;
private final DeferredErrorCollector _errorCollector;
protected final CspCheckPageLoadListener _cspCheckPageLoadListener; // Need a strong reference to this
private final CspCheckPageLoadListener _cspCheckPageLoadListener; // Need a strong reference to this

public AbstractContainerHelper _containerHelper = new APIContainerHelper(this);
public final CustomizeView _customizeViewsHelper;
Expand Down Expand Up @@ -2817,46 +2817,27 @@ private void clear()
}
}

public class CspCheckPageLoadListener implements PageLoadListener
private static class CspCheckPageLoadListener implements PageLoadListener
{
private final ArtifactCollector _artifactCollector;
private final DeferredErrorCollector _checker;

private boolean _enabled = true;

public CspCheckPageLoadListener(BaseWebDriverTest baseWebDriverTest)
{
_artifactCollector = baseWebDriverTest.getArtifactCollector();
_checker = baseWebDriverTest.checker();
}

/** Allows test code to suppress expected CSP violations */
public void setEnabled(boolean enabled)
{
log("Setting CSP to " + enabled + " on " + this);
if (enabled && !_enabled)
{
// Turning back on, so ignore anything that was logged in the interim
CspLogUtil.resetCspLogMark();
}

_enabled = enabled;
}

@Override
public void beforePageLoad()
{
if (_enabled)
try
{
try
{
CspLogUtil.checkNewCspWarnings(_artifactCollector);
}
catch (CspLogUtil.CspWarningDetectedException ex)
{
log("CSP problem detected on " + this);
_checker.withScreenshot("csp_violation").recordError(ex);
}
CspLogUtil.checkNewCspWarnings(_artifactCollector);
}
catch (CspLogUtil.CspWarningDetectedException ex)
{
_checker.withScreenshot("csp_violation").recordError(ex);
}
}

Expand Down
35 changes: 21 additions & 14 deletions src/org/labkey/test/tests/wiki/WikiCspTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.labkey.test.tests.wiki;

import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -10,6 +11,7 @@
import org.labkey.test.categories.Wiki;
import org.labkey.test.pages.core.admin.SiteValidationPage;
import org.labkey.test.pages.pipeline.PipelineStatusDetailsPage;
import org.labkey.test.util.CspLogUtil;
import org.labkey.test.util.TextSearcher;
import org.labkey.test.util.WikiHelper;

Expand Down Expand Up @@ -51,29 +53,34 @@ private void doSetup()
goToProjectHome();
}

@Override
protected void checkLeaks()
{
// No-op to avoid triggering the CSP violation during the crawl
}

@Test
public void testCspChecks()
{
goToProjectHome(PROJECT_NAME);

try
{
_cspCheckPageLoadListener.setEnabled(false);
// Issue 51749 - Create a CSP problem and a Wiki table of contents that caused a problem when checked in the background
WikiHelper wikiHelper = new WikiHelper(this);
wikiHelper.createNewWikiPage("HTML");
wikiHelper.setWikiName(WIKI_PAGE_TITLE);
wikiHelper.setWikiTitle(WIKI_PAGE_TITLE);
wikiHelper.setWikiBody(WIKI_PAGE_BODY);
wikiHelper.saveWikiPage();

// Issue 51749 - Create a CSP problem and a Wiki table of contents that caused a problem when checked in the background
WikiHelper wikiHelper = new WikiHelper(this);
wikiHelper.createNewWikiPage("HTML");
wikiHelper.setWikiName(WIKI_PAGE_TITLE);
wikiHelper.setWikiTitle(WIKI_PAGE_TITLE);
wikiHelper.setWikiBody(WIKI_PAGE_BODY);
wikiHelper.saveWikiPage();
waitForText("Click me");

goToAdminConsole().goToSettingsSection();
}
finally
try
{
_cspCheckPageLoadListener.setEnabled(true);
CspLogUtil.checkNewCspWarnings(getArtifactCollector());
}
catch (CspLogUtil.CspWarningDetectedException ignore) {}

goToAdminConsole().goToSettingsSection();

SiteValidationPage validationPage = goToAdminConsole().clickSiteValidation();
validationPage.setAllValidators(false);
Expand Down

0 comments on commit e595248

Please sign in to comment.