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

(WIP) Remove loadpolicy attribute - #17

Merged
igrigorik merged 1 commit into
gh-pagesfrom
drop-loadpolicy
Jun 10, 2015
Merged

igrigorik merged 1 commit into
gh-pagesfrom
drop-loadpolicy

Conversation

@igrigorik

Copy link
Copy Markdown
Member

Preview: https://rawgit.com/w3c/preload/drop-loadpolicy/index.html

Continuation of w3c/resource-hints#27.

  • Removed loadpolicy attribute
  • Remove language around optional "preprocessing" as it muddies the waters. Nothing stops the UA from applying custom/smart resource-specific preprocessing steps as long as the resource is not executed within, or applied to, the requesting context (this latter part is already explicit in the spec).

TODO's and open questions:

  • Fetch does allow for an empty context - should we disallow this? It certainly clashes with as being a required attribute. /cc @yoavweiss

@igrigorik

Copy link
Copy Markdown
Member Author

@yoavweiss any thoughts on the empty context? How should we handle this? Seems like allowing empty value would be equivalent to allowing as to be optional.

@annevk what kind of fetches would result in empty context today?

@yoavweiss

Copy link
Copy Markdown
Contributor

@yoavweiss any thoughts on the empty context? How should we handle this? Seems like allowing empty value would be equivalent to allowing as to be optional.

Yeah, I think we should ban an empty context (and I'm assuming that an invalid as value would result in an empty context. Did we define that somewhere?).

@annevk

annevk commented Apr 3, 2015

Copy link
Copy Markdown
Member

There is no fetch that uses the empty string (and that must remain so). It is only used for synthetic Request instances.

Does this as attribute properly enforce the resource can only be used by the indicated context?

@igrigorik

Copy link
Copy Markdown
Member Author

@annevk ah, thanks. And no, it does not, see note at the bottom of: https://rawgit.com/w3c/preload/drop-loadpolicy/index.html#initializing-fetch-settings

@yoavweiss kinda, sorta? This is what we have currently:

The as attribute is a required attribute for a preload relationship. The specified as value must be a valid request context as defined in [FETCH]. If the as attribute is omitted, or the specified value does not contain a valid request context, then the user agent should output a developer-friendly warning and ignore the preload relationship.

@annevk

annevk commented Apr 3, 2015

Copy link
Copy Markdown
Member

If you don't enforce I think this is a bad idea. There's no default settings defined in Fetch for request contexts. Those are set by the request contexts themselves. All request contexts are for is CSP, Mixed Content, nosniff, and service workers. By doing this you're forking their semantics quite badly.

@yoavweiss

Copy link
Copy Markdown
Contributor

Hmm, on top of context semantics, we should also think of how CSP would be enforced for preload requests.
e.g. AFAIK today it's enforced at request time and not when the resource is applied, which means preload would be an effective way to work around it :(

/cc @mikewest

@annevk

annevk commented Apr 3, 2015

Copy link
Copy Markdown
Member

To be clear, I'm opposed to abuse of request contexts in this way. You are tacking semantics on them that they just don't have and undermining the semantics they are supposed to have. Bad bad bad.

@igrigorik

Copy link
Copy Markdown
Member Author

Hmm, ok, so here's what I was thinking:

  • All requests initiated via rel=preload have the corresponding preload context.
    • We'd need to add it to the fetch context table and map it to.. connect-src? After all, CSP is, in effect, a declarative fetch. With that in place, preload fetches are subject to CSP.
    • Later, when the preload response is consumed, whatever that thing is (e.g. img element), it'll have its own CSP policy, and it can enforce it on whether it's allowed to use the preloaded response. Concretely..
<!-- fetch subject to connect-src -->
<link rel=preload href="/photo.jpg">
...
<!-- subject to img-src prior to consuming the preload response -->
<img src="/photo.jpg">

Does that sound reasonable?


As a whole separate concern, preload (and prefetch, for that matter) also needs a mechanism to communicate fetch options, such as priority of request, request headers, etc -- e.g. when UA fetches an image it may send different request headers, and to get the right response + get it matched to an <img> later, we need to ensure that these are initialized correctly... The thinking behind using as was as a shortcut to initialize the appropriate fetch settings: initialize your fetch settings for this asset as if it was an image.

If the concern is that reusing the Fetch request context's is confusing their semantics, should we define our own list within preload? I'd expect it to be a near duplicate, but perhaps it affords us more flexibility as well. My only hesitation here is that I'd much prefer to reuse some existing primitives, to avoid having to curate yet another ~type/~context list. Any tips?

As background, see our earlier discussion on this: igrigorik/resource-hints#6 (comment) ... later, fetch-settings got renamed to "as" via igrigorik/resource-hints#21, and that's how we got where we are today.

@annevk

annevk commented Apr 3, 2015

Copy link
Copy Markdown
Member

The part above the horizontal line seems reasonable to me.

For the second half, it would make some sense if Fetch did the initialization of these context-specific settings based on the context, but currently it doesn't and it's not clear to me that Fetch has the necessary information. E.g. features such as client hints really need to be decided and set by the <img> and background-image implementations and cannot be determined at a later stage.

However, based on your proposal it seems that you think you can determine all these things based on a single string value without knowing anything about the environment? If that is truly the case we could revisit the architecture and use context for even more things in Fetch (and then reconsider how as in that light).

Another thought I have is that maybe before we communicate fetch options to preload/prefetch, we try to tackle the general problems discussed here. E.g. how do set a custom header for <img>? How do we fetch something for <img> using fetch() (requires feeding a Response to <img>, and might require response locking if we want to get CSP/Mixed Content/nosniff correct too)? Or in other words, maybe we should start from the low-level principles before we design high-level features.

@igrigorik

Copy link
Copy Markdown
Member Author

The part above the horizontal line seems reasonable to me.

👍 ... opened whatwg/fetch#36.

Another thought I have is that maybe before we communicate fetch options to preload/prefetch, we try to tackle the general problems discussed here. E.g. how do set a custom header for ?

Sure, let's step back for a second... The general question here is: how do I use fetch() to fetch a resource with appropriate fetch settings? For example, when the browser initiates a fetch via <img src>, the request may have image specific request headers (e.g. Accept, various client hints, etc), and different request priority than, say, a <script src>-initiated fetch.

Today, <img src="/photo.jpg"> and fetch("/photo.jpg") would result in different semantics: request priorities would not match, request headers might not as well... How do we teach fetch() to be smarter about this?

  1. The intent behind as was/is is to act as a shortcut to help initialize appropriate defaults - i.e. fetch this as if this fetch() was coming from "context". As a developer I may not even know all the right values to set, so I want/need to defer this to the user agent.
  2. After the defaults are initialized, there is the next step/question of: how do I override them and/or pass my own headers/priorities, etc?

Coming back to our conversation from way back when (igrigorik/resource-hints#6 (comment)), we had:

<link rel="preload" href="/some/image.jpg" 
        params="{destinationContext: 'image', headers: {'Accept': 'image/jpeg'}}" />
  • destinationContext (terrible name :)) is the thing that can be used to initialize defaults
  • headers can be used to pass custom headers
  • <new key> can be used to set priority/dependencies (of h2 flavor)

Same options hash could then, perhaps, be supported by fetch(url, options?).

</hand waving> ... not saying this is pretty or great, just trying to recap how we got here. Any ideas or suggestions for how to make this all work? :)

@annevk

annevk commented Apr 4, 2015

Copy link
Copy Markdown
Member

(Sorry for being potentially repetitive, just trying to make sure we're on the same line.)

Fetch the specification controls all fetches within the platform, whether <img> or fetch(). If <img> passes Fetch only the string "image", is that enough for Fetch to determine headers and client hints? I thought client hints would require information that is only available to <img>. If just a string is enough for Fetch to get all the necessary data for setting headers from a hash table of sorts, we should just put that hash table in Fetch and indeed things like as/destinationContext become more feasible. But if client hints include e.g. figuring out the width of the viewport, it's not entirely clear to me that Fetch is the correct place, though perhaps we could still make that work. Do you know all the requirements?

The main problem with params or something similar seems to be that developers are not necessarily a fan of that syntax and I got some pushback here: w3c/webappsec#175 I don't really have anything better to offer though. (fetch() already takes a dictionary as second argument.)

@briansmith

Copy link
Copy Markdown

"We'd need to add it to the fetch context table and map it to.. connect-src. After all, CSP is, in effect, a declarative fetch. With that in place, preload fetches are subject to CSP." I think you meant "After all, preload is, in effect, a declarative fetch. With that in place, preload fetches are subject to CSP."

Everything is a declarative fetch except for fetch(), XHR, and websockets. But, we don't map them all to connect-src for CSP, for good reason.

Please remember the reasons described in #2 for why defaulting to connect-src is bad.

@mikewest

mikewest commented Apr 7, 2015

Copy link
Copy Markdown
Member

I agree that connect-src isn't the right directive here. If we know the type of the preload (because of the as attribute), then it might make sense to tie that fetch to the relevant CSP directive (e.g. as="script" would check against script-src).

Regardless of the preload, however, we ought to be checking the resource before we embed it into the page in some context. I think this should fall out of tying CSP to Fetch more clearly. That is, preload just fills the cache, and <img> is going to fetch the resource regardless. The CSP hook at the top of the fetch algorithm could allow the former but block the latter, which seems reasonable.

@igrigorik

Copy link
Copy Markdown
Member Author

@mikewest @briansmith @annevk thanks for your comments! FWIW, I think what we're discussing here goes well beyond rel=preload... I think if we solve this higher up (in Fetch), then preload behavior will fall out directly. With that in mind, opened: whatwg/fetch#43

@toddreifsteck

Copy link
Copy Markdown
Member

This generally looks good to me for merge. Per the call today, please clarify the dependency on fetch and we can lock that down in future.

@igrigorik igrigorik mentioned this pull request May 19, 2015
@igrigorik

Copy link
Copy Markdown
Member Author

Merging this, will address "as" in a separate pull.

igrigorik added a commit that referenced this pull request Jun 10, 2015
Remove loadpolicy attribute
@igrigorik
igrigorik merged commit 657da3e into gh-pages Jun 10, 2015
@igrigorik
igrigorik deleted the drop-loadpolicy branch July 8, 2015 23:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants