Skip to content

Do browsers allow empty/missing Content-Type headers on <link rel="stylesheet">? #3457

Open

Description

@domenic

Some investigation by @anforowicz implies that at least Chrome does: https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/css/StyleElement.cpp?type=cs&sq=package:chromium&l=156

We should write some tests, and see what browsers do. In the best case, Chrome is the only browser doing this, and we can fix it to pass some newly-written web platform tests. In the worst case, everyone has this hole, and we need to patch the spec to allow it.

Activity

  1. added
    interop
    topic: style
    on Feb 6, 2018
  2. guest271314 commented on Feb 16, 2018

    @guest271314
    Contributor

    Both Chromium 64 and Firefox 57 load the CSS with no value for Content-Type set

    <?php 
    
        header("Content-Type: ");
        echo "body{color:blue}";
    
    php?>
    

    With '' set as value of Content-Type

    <?php 
    
        header("Content-Type: '' ");
        echo "body{color:blue}";
    
    php?>
    

    Chromium create an entry at document.styleSheets though the style rules are not applied. At Firefox the style rules are applied.

  3. guest271314 commented on Feb 16, 2018

    @guest271314
    Contributor

    <link rel="stylesheet" href="data:,body{color:blue}" type="text/css"> adds an entry to document.styleSheets at both Chromium and Firefox. At Chromium the CSSStyleSheet does not have a cssRules property, at Firefox the CSSStyleSheet does have a cssRules property with the length of the CSSRuleList set to 0.

  4. guest271314 commented on Feb 16, 2018

    @guest271314
    Contributor

    Firefox 60 has the same behaviour as Firefox 57

    <?php 
    
        header("Content-Type: '' ");
        echo "body{color:blue}";
    
    php?>
    

    does apply style rules to the document.

    Response headers

    HTTP/1.1 200 OK
    Connection: close
    Content-Type: ''
    Host: localhost:8000
    
  5. annevk commented on Feb 17, 2018

    @annevk
    Member

    @guest271314 is your HTML document in standards mode? Because otherwise you're hitting a quirk. Another thing to test here is a cross-origin stylesheet. And maybe also the unknown/unknown, application/unknown, and */* MIME types (from MIME Sniffing). (And the various combinations of these.)

  6. guest271314 commented on Feb 17, 2018

    @guest271314
    Contributor

    @annevk What is "standards mode"? What exactly are we trying to determine? If the resource is requested, or if the styles are applied to the document?

  7. annevk commented on Feb 17, 2018

    @annevk
    Member

    @guest271314 if an document starts with <!doctype html> it'll be in "standards mode" (no-quirks mode per the specifications).

    I think what you tested was a good start:

    • Is document.styleSheets affected (test after load has happened/failed)?
    • Are the styles applied (e.g., via getComputedStyle())?
    • Does the load event dispatch on <link>?

    Don't think there are many other assertable things here.

  8. guest271314 commented on Feb 17, 2018

    @guest271314
    Contributor

    @annevk Not entirely sure what the parameters of a "cross-origin" stylesheet are? Do you mean where Access-Control-Allow-Origin:* header is not set?

    These are the previous tests with suggested inclusions

    <!DOCTYPE html>
    <html>
    
    <head>
      <script>
        function handleLinkLoadEvent(link) {
          try {
            console.log(`link load event for ${link.sheet.href}, cssText: ${link.sheet.cssRules[0].cssText}, computedStyle: ${window.getComputedStyle(document.querySelector(link.sheet.cssRules[0].selectorText)).color}`);
          } catch (err) {
            console.error(err, link.sheet.href);
          }
        }
    
        function handleLinkErrorEvent(link) {
          try {
            console.log(`link error event for ${link.sheet.href}, cssText: ${link.sheet.cssRules[0].cssText}, computedStyle: ${window.getComputedStyle(document.querySelector(link.sheet.cssRules[0].selectorText)).color}`);
          } catch (err) {
            console.error(err, link.sheet.href);
          }
        }
    
        onload = function() {
          console.assert(document.styleSheets.length === document.querySelectorAll("p").length, [document.styleSheets]);
          console.log(document.styleSheets, document.styleSheets.length, document.querySelectorAll("p").length);
        }
      </script>
      <link rel="stylesheet" type="text/css" href="style.css" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="styleNoExtension" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="data:,p:nth-child(3){color:green}" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="data:unknown/unknown,p:nth-child(4){color:purple}" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="data:application/unknown,p:nth-child(5){color:gold}" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="data:*/*,p:nth-child(6){color:orange}" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
      <link rel="stylesheet" type="text/css" href="https://gist.githubusercontent.com/guest271314/451634131a32d6f302744f2cbee92512/raw/296db2d197f81f073eb1285f630ebcc87a53692c/crossOriginCSSRequestedWithLinkElement.css" onload="handleLinkLoadEvent(this)" onerror="handleLinkErrorEvent(this)">
    
    </head>
    
    <body>
      <p>.css</p>
      <p>styleNoExtension</p>
      <p>data URL without MIME type</p>
      <p>data URL with "unknown/unknown" MIME type</p>
      <p>data URL with "application/unknown" MIME type</p>
      <p>data URL with "*/*" MIME type</p>
      <p>crossOriginCSSRequestedWithLinkElement gist does not set Content-Type header</p>
    </body>
    
    </html>
    

    plnkr https://plnkr.co/edit/wiEL5drnUvIGkdqmQQu2?p=preview.

    Interesting results:

    Chromium 64

    Only dispatches error event for a requested gist https://gist.githubusercontent.com/guest271314/451634131a32d6f302744f2cbee92512/raw/296db2d197f81f073eb1285f630ebcc87a53692c/crossOriginCSSRequestedWithLinkElement.css where was attempting to satisfy "cross-origin" request test. MIME checking occurs for the cross-origin request as well

    Refused to apply style from 'https://gist.githubusercontent.com/guest271314/451634131a32d6f302744f2cbee92512/raw/296db2d197f81f073eb1285f630ebcc87a53692c/crossOriginCSSRequestedWithLinkElement.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
    

    The load event is dispatched for the remaining requests.

    Only the local request where .css extension is set has style applied to the element (the first <p> element).

    Firefox 60

    The load event is dispatched for the local request where .css extension is set has style applied to the element (the first <p> element); and for data URL with MIME type set to "*/*" (the sixth <p> element).

    The error message for the cross-origin (gist) request

    DOMException: The operation is insecure.
     https://gist.githubusercontent.com/guest271314/451634131a32d6f302744f2cbee92512/raw/296db2d197f81f073eb1285f630ebcc87a53692c/crossOriginCSSRequestedWithLinkElement.css
    

    The error event is dispatched for the remainder of <p> elements.

  9. guest271314 commented on Feb 17, 2018

    @guest271314
    Contributor

    Including crossOrigin="anonymous" to gist request results in DOMException: The operation is insecure. not being logged at Firefox 60, though the error event is still dispatched and the associated style is not applied to the selector.

  10. annevk commented on Feb 17, 2018

    @annevk
    Member

    Cross-origin was meant as a stylesheet on a different origin from the HTML document. No crossorigin attribute needed (though maybe we should test that as well, but I hope that it doesn't change the results).

  11. guest271314 commented on Feb 17, 2018

    @guest271314
    Contributor

    The only change that can detect when crossOrigin attribute set to "anonymous" is that the error DOMException: The operation is insecure. is not logged at console.

    The differences between Chromium and Firefox that have noticed so far are described at #3457 (comment):

    1. The error event is not dispatched at Chromium for any of the tests save for gist request (cross-origin)
    2. Firefox dispatches error event for all but two requests
    3. Firefox applies styles for response having MIME type set to "*/*"
  12. annevk commented on Feb 18, 2018

    @annevk
    Member

    The best way would be through https://github.com/w3c/web-platform-tests which has support for that, so once we have tests all browsers can then use them as well.

  13. annevk commented on Sep 24, 2018

    @annevk
    Member

    I have tests for this at web-platform-tests/wpt#13144. The HTML Standard does allude to missing and broken Content-Type resulting in text/css treatment, but this should be spelled out better. While testing I also found some issues with Chrome/Edge/Safari that I don't think are allowed (they're loose for some cases where there's a valid Content-Type). I'll file bugs on those.

    It's a little bit unclear to me how to clarify the standard here, since there isn't a clear loading pipeline as things stand.

    cc @domfarolino

  14. annevk commented on Sep 24, 2018

    @annevk
    Member

    See whatwg/mimesniff#30 (comment) for why I abstained from testing */* for now. It'd be easy to add, but it's unclear to me what the correct result is and where it should be special cased. I suspect we don't want to special case it for style sheet loadding.

  15. added
    topic: links
    and removed
    topic: style
    on May 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interoptopic: links

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions