1414 */
1515package org .htmlunit .html ;
1616
17+ import static org .htmlunit .BrowserVersionFeatures .CSP_OBJECT_BLOCKED_FIRES_ERROR ;
18+
1719import java .util .Map ;
1820
1921import org .apache .commons .logging .Log ;
2022import org .apache .commons .logging .LogFactory ;
2123import org .htmlunit .SgmlPage ;
24+ import org .htmlunit .WebClient ;
25+ import org .htmlunit .javascript .PostponedAction ;
26+ import org .htmlunit .javascript .host .event .Event ;
2227import org .htmlunit .javascript .host .html .HTMLObjectElement ;
2328import org .htmlunit .util .StringUtils ;
2429import org .htmlunit .xml .XmlPage ;
@@ -265,7 +270,7 @@ public final String getVspaceAttribute() {
265270 @ Override
266271 protected void onAddedToPage () {
267272 super .onAddedToPage ();
268- ContentSecurityPolicy . allowsObject ( this , getAttributeDirect ("data" ));
273+ checkContentSecurityPolicy ( getAttributeDirect ("data" ));
269274 }
270275
271276 /**
@@ -283,7 +288,40 @@ protected void setAttributeNS(final String namespaceURI, final String qualifiedN
283288 notifyAttributeChangeListeners , notifyMutationObservers );
284289
285290 if (changed && isAttachedToPage ()) {
286- ContentSecurityPolicy .allowsObject (this , value );
291+ checkContentSecurityPolicy (value );
292+ }
293+ }
294+
295+ /**
296+ * Checks the url against the Content-Security-Policy (object-src); if the url is blocked,
297+ * Firefox fires the error event (async).
298+ */
299+ private void checkContentSecurityPolicy (final String data ) {
300+ if (ContentSecurityPolicy .allowsObject (this , data )) {
301+ return ;
302+ }
303+
304+ final HtmlPage htmlPage = getHtmlPageOrNull ();
305+ if (htmlPage == null || !hasFeature (CSP_OBJECT_BLOCKED_FIRES_ERROR )) {
306+ return ;
307+ }
308+
309+ final WebClient client = htmlPage .getWebClient ();
310+ if (!client .isJavaScriptEngineEnabled ()) {
311+ return ;
312+ }
313+
314+ final PostponedAction action = new PostponedAction (htmlPage , "HtmlObject.blocked" ) {
315+ @ Override
316+ public void execute () {
317+ HtmlObject .this .fireEvent (Event .TYPE_ERROR );
318+ }
319+ };
320+ if (READY_STATE_LOADING .equals (htmlPage .getReadyState ())) {
321+ htmlPage .addAfterLoadAction (action );
322+ }
323+ else {
324+ client .getJavaScriptEngine ().addPostponedAction (action );
287325 }
288326 }
289327
0 commit comments