Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.
This repository was archived by the owner on May 5, 2022. It is now read-only.

Make "as" attribute mandatory? #2

Closed

Description

@igrigorik

From: https://groups.google.com/a/chromium.org/d/msg/blink-dev/CM5BaP6uVvE/zZOB9uANPzAJ


Intuitively, preload is a declarative fetch()... so, "connect-src" as default seems reasonable, since that's what fetch() is subject to [3] -- any objections?

The fact that preload is declarative, and the fact that a script cannot observe the response of the declarative preload, are exactly what make connect-src wrong. connect-src would only be correct if the preload is for a resource that's going to be fetched with fetch() or XHR. But, it seems unclear (probably unlikely, IMO) that most preloads without an "as" are really going to used for fetch() or XHR.

Is it really important to have a default context? If there's no default context, but instead the UA just logs a warning to the console saying the prefetch will be ignored due to a missing "as" attribute, then the developer is likely to notice the message and/or the fact that the prefetch doesn't happen, and add the (probably) correct "as" attribute.

I'm a fan of having defaults for making the developer's life easy when the default is intuitive, but there doesn't seem to be any intuitive default here. Having the developer explicitly tell us the correct context is likely going to help us with prioritization and maybe withapplying the correct referrer policy and other security things. Assuming the wrong context means we'll get those things wrong. So, I lean towards the behavior I described in the previous paragraph.


Let's say the developer writes:

Content-Security-Policy: default-src 'self' https://cdn.example.com
<link href=//cdn.example.com/10guy.jpg rel=preload>

Then, if the default was connect-src, there would be an error in the developer console like this:

Refused to load 'https://cdn.example.com/10guy.jpg' because it violates the following Content Security Policy directive: "default-src' self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.

I think a lot of developers would react to this message by adding "connect-src cdn.example.com" to their CSP. But, that is not the best thing to do. Conversely, if the message was:

Refused to preload 'https://cdn.example.com/10guy.jpg' because there was no as attribute given.

Then the developer would probably fix the problem by adding the as attribute.

Activity

  1. igrigorik commented on Jan 27, 2015

    @igrigorik
    MemberAuthor

    Given that the primary purpose of "as" is to initialize the right request priority and headers (to minimize unnecessary contention), the mandatory requirement might actually be a good one in that it forces the developer to explicitly think through and declare how this resource should be treated. A clean(er) integration with CSP is also a nice bonus.

    Any objections?

  2. getify commented on Mar 10, 2015

    @getify

    Another thought related to as=..:


    If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?


  3. getify commented on Mar 10, 2015

    @getify

    Also possibly related to #14

  4. igrigorik commented on Mar 11, 2015

    @igrigorik
    MemberAuthor

    If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?

    No. "as" is used as a signal to initialize fetch settings: priority, settings, etc. As such, you can "lie" and fetch an asset with a different priority (that's a feature in some cases), but in practice this should be used very carefully (generally, it's a bad idea), since it does run the risk of sending out the wrong headers and/or affecting how the resource is cached.

  5. added a commit that references this issue on Apr 1, 2015
  6. yoavweiss commented on Feb 16, 2016

    @yoavweiss
    Contributor

    If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?

    This actually would result in a double download, since the browser (at least in the Blink implementation) uses the as value to determine the resource type, and only resources from the same types can be reused, unless we somehow carve out preload from that match check.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions