<?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>233071</bug_id>
          
          <creation_ts>2021-11-12 13:45:59 -0800</creation_ts>
          <short_desc>Allowlisting empty elements via content hashes in CSP directives is inconsistent across browser engines</short_desc>
          <delta_ts>2022-10-16 22:12:20 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit Misc.</component>
          <version>WebKit Local Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://github.com/web-platform-tests/wpt/pull/34902</see_also>
          <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="Andy Bonventre">andybons</reporter>
          <assigned_to name="Matthew Finkel">m_finkel</assigned_to>
          <cc>bfulgham</cc>
    
    <cc>gsnedders</cc>
    
    <cc>katherine_cheney</cc>
    
    <cc>m_finkel</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1814617</commentid>
    <comment_count>0</comment_count>
    <who name="Andy Bonventre">andybons</who>
    <bug_when>2021-11-12 13:45:59 -0800</bug_when>
    <thetext>The following page will render a red background on Chromium and Gecko, but not WebKit due to a CSP violation:

&lt;!DOCTYPE html&gt;
&lt;meta
  http-equiv=&quot;Content-Security-Policy&quot;
  content=&quot;default-src &apos;self&apos;; style-src &apos;self&apos; &apos;sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=&apos;; script-src &apos;unsafe-inline&apos;&quot;
/&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;script type=&quot;module&quot;&gt;
    const style = document.createElement(&quot;style&quot;);
    style.appendChild(document.createTextNode(&quot;&quot;));
    document.head.appendChild(style);
    const { sheet } = style;
    if (sheet) {
      sheet.insertRule(&quot;body { background: red; }&quot;);
      console.info(&quot;background should be red now&quot;);
    } else {
      console.error(&quot;no sheet found :(&quot;);
    }
  &lt;/script&gt;
&lt;/html&gt;

The &lt;style&gt; node is empty, so the sha256 of the empty string (sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=) allows for it to successfully apply to the page on Chromium and Gecko.

On WebKit, ContentSecurityPolicy::findHashOfContentInPolicies will always return false for an empty style element (see https://sourcegraph.com/github.com/WebKit/WebKit/-/blob/Source/WebCore/page/csp/ContentSecurityPolicy.cpp?L362)

A workaround on WebKit is to append some arbitrary content to the &lt;style&gt; tag:

&lt;!DOCTYPE html&gt;
&lt;meta
  http-equiv=&quot;Content-Security-Policy&quot;
  content=&quot;default-src &apos;self&apos;; style-src &apos;self&apos; &apos;sha256-0hAheEzaMe6uXIKV4EehS9pu1am1lj/KnnzrOYqckXk=&apos;; script-src &apos;unsafe-inline&apos;&quot;
/&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;script type=&quot;module&quot;&gt;
    const style = document.createElement(&quot;style&quot;);
    style.appendChild(document.createTextNode(&quot;/**/&quot;));
    document.head.appendChild(style);
    const { sheet } = style;
    if (sheet) {
      sheet.insertRule(&quot;body { background: red; }&quot;);
      console.info(&quot;background should be red now&quot;);
    } else {
      console.error(&quot;no sheet found :(&quot;);
    }
  &lt;/script&gt;
&lt;/html&gt;

From Kate Cheney in WebKit Slack:

&gt; After a brief look at the spec, the empty string hash case doesn&apos;t seem to be explicitly talked about, but I don&apos;t see a reason here why we shouldn&apos;t match behavior of other major browsers. Could you file a bug on https://bugs.webkit.org about this?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1814620</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-11-12 13:47:23 -0800</bug_when>
    <thetext>&lt;rdar://problem/85356188&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1815015</commentid>
    <comment_count>2</comment_count>
    <who name="Sam Sneddon [:gsnedders]">gsnedders</who>
    <bug_when>2021-11-15 03:58:30 -0800</bug_when>
    <thetext>From Kate:

&gt; After a brief look at the spec, the empty string hash case doesn&apos;t seem to be explicitly talked about, but I don&apos;t see a reason here why we shouldn&apos;t match behavior of other major browsers.

That implies per spec that the empty string is just another string, no? If it isn&apos;t special-cased, then it&apos;s just another length of string, hence this is both &quot;match the spec and match other browsers&quot;.

(Also a WPT test for this would be nice, if there isn&apos;t already one!)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1884912</commentid>
    <comment_count>3</comment_count>
    <who name="Matthew Finkel">m_finkel</who>
    <bug_when>2022-07-18 15:15:31 -0700</bug_when>
    <thetext>Pull request: https://github.com/webkit/WebKit/pull/2526</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1884914</commentid>
    <comment_count>4</comment_count>
    <who name="Matthew Finkel">m_finkel</who>
    <bug_when>2022-07-18 15:16:10 -0700</bug_when>
    <thetext>Wrote this a few weeks ago, finally got around to posting it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1884929</commentid>
    <comment_count>5</comment_count>
    <who name="Matthew Finkel">m_finkel</who>
    <bug_when>2022-07-18 15:44:01 -0700</bug_when>
    <thetext>And, because I didn&apos;t actually say this in the previous comment:

This bug is not present in trunk. It was (possibly accidentally) fixed as part of https://bugs.webkit.org/show_bug.cgi?id=235199

Canonical link: https://commits.webkit.org/246139@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@288132 268f45cc-cd09-0410-ab3c-d52691b4dbfc

The PR is adding a WPT test, as Sam suggested.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1885157</commentid>
    <comment_count>6</comment_count>
    <who name="Matthew Finkel">m_finkel</who>
    <bug_when>2022-07-19 11:57:40 -0700</bug_when>
    <thetext>I hacked at export-w3c-test-changes and got it to create a PR.

https://github.com/web-platform-tests/wpt/pull/34902</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1906033</commentid>
    <comment_count>7</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2022-10-16 22:12:18 -0700</bug_when>
    <thetext>Committed 255611@main (2142cd8c9e15): &lt;https://commits.webkit.org/255611@main&gt;

Reviewed commits have been landed. Closing PR #2526 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>