<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>236624</bug_id>
          
          <creation_ts>2022-02-14 17:23:43 -0800</creation_ts>
          <short_desc>Document.write into iframe with CSP sometimes blocks resources loads in inner iframe</short_desc>
          <delta_ts>2022-02-23 13:58:50 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>DOM</component>
          <version>Safari 15</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>macOS 12</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Matt Bierner">matb</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>achristensen</cc>
    
    <cc>bfulgham</cc>
    
    <cc>cdumez</cc>
    
    <cc>katherine_cheney</cc>
    
    <cc>pgriffis</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1841495</commentid>
    <comment_count>0</comment_count>
      <attachid>451967</attachid>
    <who name="Matt Bierner">matb</who>
    <bug_when>2022-02-14 17:23:43 -0800</bug_when>
    <thetext>Created attachment 451967
Simple repo with two html files

== Steps to reproduce the problem ==

1. On a page with a restrictive content security policy 
2. Embed a sandboxed, same origin iframe
3. On the parent page, use `setInterval` to check the ready state of the iframe. Once it is listed as `complete`, try using `document.write` to write a new html document into it. This new html document should have its own CSP and try to load content that would be allowed by that CSP but blocked by the parent page&apos;s

I&apos;ve attached two simple html files that show this issue in action. To reproduce, start a simple local server in the folder and open `index.html`


== What is the expected behavior? == 
The new content of the inner iframe should load (it should load a picture of a cat with a red background)


== What went wrong? ==
Occasionally resources in the inner iframe will be blocked due to a CSP violation. This happens maybe 25% of the time for me. I can reproduce it even more reliably by reducing the `setInterval` timeout

The fact that the code sometimes works suggests to me that there is some sort of race.


== Does this work in other browsers? ==
This is also broken in Chrome (https://bugs.chromium.org/p/chromium/issues/detail?id=1297377). I can&apos;t test in Firefox because the entire scenario is blocked by https://bugzilla.mozilla.org/show_bug.cgi?id=1754872

== Other notes==
This example seems contrived, but is used in the VS Code codebase to work around https://bugs.webkit.org/show_bug.cgi?id=33604</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1841557</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-02-14 19:57:02 -0800</bug_when>
    <thetext>&lt;rdar://problem/88943548&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1845304</commentid>
    <comment_count>2</comment_count>
    <who name="Matt Bierner">matb</who>
    <bug_when>2022-02-23 13:58:50 -0800</bug_when>
    <thetext>Over on Chrome, we determined this is likely by-design (although quite confusing): https://bugs.chromium.org/p/chromium/issues/detail?id=1297377

The issue with the polling here is that the iframe document may still be on `about:blank` when the ready state is first checked. This results in the new content written into the document inheriting the parent page&apos;s CSP instead of providing its own

To fix this, I added a check to our code to make sure we are checking the ready state of the expected page instead of `about:blank`</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>451967</attachid>
            <date>2022-02-14 17:23:43 -0800</date>
            <delta_ts>2022-02-14 17:23:43 -0800</delta_ts>
            <desc>Simple repo with two html files</desc>
            <filename>example.zip</filename>
            <type>application/zip</type>
            <size>1359</size>
            <attacher name="Matt Bierner">matb</attacher>
            
              <data encoding="base64">UEsDBBQAA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