w3c / webappsec-fetch-metadata

Fetch Metadata
https://w3c.github.io/webappsec-fetch-metadata/
Other
75 stars 28 forks source link

New 'Sec-Fetch-Dest' Type - "import" #59

Closed Randy-Buchholz closed 3 years ago

Randy-Buchholz commented 4 years ago

Consider adding "import" to the list of valid sec-fetch-dest values. https://www.w3.org/TR/fetch-metadata/#sec-fetch-dest-header

This would indicate that the response is intended to be processed by a Module Import function.

Currently (in Chromium) an import from Window produces type "script", but from a Worker produces "worker". Adding "import" to the options would allow more consistent behavior and provide the server with meta information about the intended use of the response.

mikewest commented 4 years ago

Pedantically, this is a suggestion for Fetch, not for this specification. Fetch Metadata relies on the destination definition in the Fetch specification, and hopefully won't diverge from it: https://fetch.spec.whatwg.org/#concept-request-destination.

That said, it's not clear to me what decisions this would enable on the server that aren't covered by script or worker. Those destinations do seem to cover how the import is actually going to be used, and the risks of the import are tied in some real respect to that context (imports in a worker don't have DOM access, while imports in a window might). Can you help me see the value of this addition?

Randy-Buchholz commented 4 years ago

@mikewest, thanks for the Fetch reference. Since Fetch destination piggy-backs on sec-fetch-dest what is the thinking on this being more restrictive in terms of valid values? Wouldn't this spec be "All Fetch Destination" + ...

Regarding server decisions, here are two cases.

  1. I see import as a specialized case of script that has distinct features/keywords (e.g., export and default) and a distinct target environment - module, and also gets special processing. Imports aren't just executed, but are parsed into "import/export/default objects" and "immediate execution commands" (for lack of a better descriptions). If they are served as static files the distinction doesn't really matter to the server - it's up to the client to know what they are requesting. But, if they are dynamically composed it can impact how the server handles the request.
  1. Another more common situation is "namespace imports" - where an import file only contains a list of other imports. This triggers several additional imports and server trips. I could send all of the imports in a single file on the initial request, but would need to inspect the content of every static script/worker file request to handle this situation. Seeing sec-fetch-dest: import could be a hint to "peek" into the file and determine if it should be served static or can use aggregation.

Note: My ultimate goal is to get to "smarter" imports. Having import as a valid type here would make it easier to get it as a valid type in Fetch, and then into import as a value set by the import request process. Eventually, I can see import sending the parts it wants (import { parts, it, wants } from "/source") where we can tree-shake or resolve dependencies on the server.

annevk commented 4 years ago

Sec-Fetch-Dest builds on destination, not the other way around.

annevk commented 3 years ago

I suggest closing this. We don't want these fields to diverge.