2727import org .labkey .test .pages .core .admin .logger .ManagerPage ;
2828import org .labkey .test .pages .reports .ManageViewsPage ;
2929import org .labkey .test .util .CodeMirrorHelper ;
30+ import org .labkey .test .util .CspLogUtil ;
3031import org .labkey .test .util .Log4jUtils ;
3132import org .labkey .test .util .LogMethod ;
3233import org .labkey .test .util .LoggedParam ;
3334import org .labkey .test .util .PortalHelper ;
3435import org .labkey .test .util .RReportHelper ;
36+ import org .labkey .test .util .WikiHelper ;
3537import org .labkey .test .util .core .admin .CspConfigHelper ;
3638import org .openqa .selenium .WebElement ;
3739
@@ -55,7 +57,9 @@ public abstract class AbstractKnitrReportTest extends BaseWebDriverTest
5557 protected static final Path rmdReport_no_scriptpad = TestFileUtils .getSampleData ("reports/knitr_no_scriptpad.rmd" ).toPath ();
5658 private static final Path rhtmlReport = scriptpadReports .resolve ("script_rhtml.rhtml" );
5759 private static final Path rhtmlReport_no_scriptpad = TestFileUtils .getSampleData ("reports/knitr_no_scriptpad.rhtml" ).toPath ();
58- protected static final Path rmdReport_embedded_script = TestFileUtils .getSampleData ("reports/plotly.rmd" ).toPath ();
60+ private static final Path rhtmlNonceCheck = TestFileUtils .getSampleData ("reports/nonce_check.rhtml" ).toPath ();
61+ private static final Locator .XPathLocator nonceCheckLoc = Locator .id ("nonce-check-result" );
62+ private static final Locator .XPathLocator nonceCheckSuccessLoc = nonceCheckLoc .withText ("SUCCESS" );
5963
6064 protected final RReportHelper _rReportHelper = new RReportHelper (this );
6165
@@ -170,7 +174,9 @@ protected void htmlFormat()
170174 Locator .tag ("pre" ).containing ("## \" 1\" ,249318596,\" 2008-05-17\" ,86,36,129,76,64" ),
171175 Locator .tag ("pre" ).withText ("## knitr says hello to HTML!" ),
172176 Locator .tag ("pre" ).startsWith ("## Error" ).containing (": non-numeric argument to binary operator" ),
173- Locator .tag ("p" ).startsWith ("Well, everything seems to be working. Let's ask R what is the value of \u03C0 ? Of course it is 3.141" )};
177+ Locator .tag ("p" ).startsWith ("Well, everything seems to be working. Let's ask R what is the value of \u03C0 ? Of course it is 3.141" ),
178+ nonceCheckSuccessLoc // Inline script should run
179+ };
174180 String [] reportNotContains = {"<html>" , // Uninterpreted html
175181 "<!--" , // ditto
176182 "A minimal knitr example in HTML" , // report title element
@@ -199,7 +205,8 @@ protected void markdownV2()
199205 Locator .tag ("h2" ).withText ("R code chunks" ),
200206 Locator .tag ("code" ).containing ("set.seed(123)" ), // Echoed R code
201207 Locator .css ("p" ).containing ("2 x pi = 6.283" ),
202- Locator .tag ("sup" ).withText ("write" ) //should not contain the hat markdown v2 closing tag
208+ Locator .tag ("sup" ).withText ("write" ), //should not contain the hat markdown v2 closing tag
209+ nonceCheckSuccessLoc // Inline script should run
203210 };
204211
205212 String [] reportNotContains = {"```" , // Markdown for R code chunks
@@ -233,13 +240,36 @@ protected void moduleReportDependencies()
233240 * Issue 53211: CSP reports when an R/Plotly graph is displayed in Reports web part, same thing wrapped in a wiki works fine with strict csp
234241 */
235242 @ Test
236- public void reportEmbeddedScript ()
243+ public void testEmbeddedReportNonce ()
237244 {
238- Locator [] reportContains = {};
239-
240- String [] reportNotContains = {};
241-
242- createAndVerifyKnitrReport (rmdReport_embedded_script , RReportHelper .ReportOption .knitrMarkdown , reportContains ,
243- reportNotContains , true );
245+ String name = "rhtml nonce check" ;
246+ String success = "SUCCESS" ;
247+ Locator [] reportContains = {nonceCheckSuccessLoc };
248+
249+ createAndVerifyKnitrReport (rhtmlNonceCheck , RReportHelper .ReportOption .knitrHtml , reportContains ,
250+ null , true , name );
251+ CspLogUtil .checkNewCspWarnings (getArtifactCollector ());
252+
253+ log ("Create wiki with embedded report" );
254+ new WikiHelper (this ).createNewWikiPage ()
255+ .setName (name )
256+ .setBody ("""
257+ ${labkey.webPart(partName='Report',
258+ reportName='%s',
259+ showFrame='false'
260+ )}
261+ """ .formatted (name ))
262+ .saveAndClose ();
263+ clickAndWait (Locator .linkWithText (name ));
264+ assertEquals ("Nonce check result" , success , getText (nonceCheckLoc ));
265+ CspLogUtil .checkNewCspWarnings (getArtifactCollector ());
266+
267+ log ("Add report webpart" );
268+ new PortalHelper (this ).doInAdminMode (ph -> {
269+ ph .addTab (name ); // Use a separate tab to ensure report isn't run accidentally
270+ ph .addReportWebPart (name );
271+ assertEquals ("Nonce check result" , success , getText (nonceCheckLoc ));
272+ CspLogUtil .checkNewCspWarnings (getArtifactCollector ());
273+ });
244274 }
245275}
0 commit comments