You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fetch: Enable fetch via file URL (under flag) #2751
There are many use cases where fetching a local file using fetch would be handy, and this is particularly true for WebAssembly modules.
Right now, libraries like wasm-pack (a Rust to WASM lib) have to perform different builds for browsers and Node.js, since Node.js is not able to fetch the compiled WASM binary from the file system.
Other runtimes and browsers handle this without any particular problem, but except for Deno (which has its own security model), I do understand the security implications of fetching a file from the filesystem.
What is the feature you are proposing to solve the problem?
With the introduction of the new Node.js permission APIs, we could enable fetch on the file system under an option, such as allowFileUrl: true.
That way, the user could explicitly enable fetching a local file, acknowledging the security implications that come with it.
Reacted by Tommaso Allevi, Johannes Schickling, Alexander Akait, Cohee, Tianze Ds Qiu, Jonas Strehle, Tommy D. Rossi, Artem Melnyk, Juan Campa, John L. Armstrong IV and 1 more
@KhafraDev I agree with you that it’d be optimal to have wintercg to formalize this. What do you mean by “will deviate from literally every other platform”? Other runtimes (Bun and Deno for sure) implement a this already
Bun allows fetching file:// urls with any method, Deno throws on non-GET calls.
Chrome only allows this for extensions, if users opt-in to it.
Firefox and Safari do not implement this.
If we implement our own implementation (ie. with a non-standard option), that's another implementation that differs from every browser and every server environment. No implementation is spec compliant because there is no spec, which will also cause issues when a spec is written, and these platforms now have to break their users to match it.
I'm quite confused as to your mention of wasm-pack
Right now, libraries like wasm-pack (a Rust to WASM lib) have to perform different builds for browsers and Node.js, since Node.js is not able to fetch the compiled WASM binary from the file system.
Browsers aren't able to do this either. But from looking at the code, they instead download the binary from a url, which will work in node and browsers, ignoring CORs errors. Maybe you could expand on this?
No implementation is spec compliant because there is no spec, which will also cause issues when a spec is written, and these platforms now have to break their users to match it.
This is very true, but that's also the purpose of experimental APIs. I personally think that missing APIs cause more damage to the user than experimental APIs.
Browsers aren't able to do this either. But from looking at the code, they instead download the binary from a url, which will work in node and browsers, ignoring CORs errors. Maybe you could expand on this?
Correct. Right now, if you use wasm-pack, you have to run at least three compilation processes:
For browsers and runtimes. Since the mainstream runtimes support fetching from a file, they can share the same bindings built for the browsers.
ESM bindings for Node.js, which doesn't support fetch for local files
CJS bindings for Node.js, which doesn't support fetch for local files
The only real blocker I found to enable a single compilation process, it's by allowing Node.js to fetch the compiled WASM binaries from the filesystem at runtime.
I wouldn't focus too much on the wasm-pack use case only, though. I'm using Deno a lot and being able to avoid calling server-side methods (such as fs.readFileSync for Node.js) allows you to write code once and use it both on the frontend and the backend, which is kinda handy.
I understand the spec is basically non-existent, yet, as I said earlier, an experimental API is far better than no API at all to me.
But of course, that's a personal opinion and I'm happy to discuss
I'm also still not sure how fetching file urls can be used in both browsers and runtimes, but that doesn't seem relevant.
tbf it doesn't need to be supported in browsers to improve interop, the use case I have in mind is fetch(new URL('./some/relative/path', import.meta.url)), where import.meta.url would be a HTTPS URL in browsers and a file: URL on runtimes.
Reacted by Michele Riva, James Edwards-Jones, easrng and Herman Bergwerf
Thanks for the use case, makes sense now! Interop with other server environments is precisely the point of WinterCG. In the past we've followed Deno, which has only ever caused issues for us (namely security vulnerabilities, edge cases, more edge cases, other issues). But the proposed solution is not interoperable with other runtimes as it requires explicitly opting in to it, for every request - the other two runtimes just work. As in, you would have to change your code for it to work in node, whereas you don't for other runtimes, nor will you when/if it's ever added into the spec.
I mentioned earlier, but Bun isn't entirely interoperable with Deno, and there are also a number of questions about the api that WinterCG should solve before we even consider implementing it.
Agreed here -- I don't think I demand file:// URLs in particular, but fetch(new URL('foo.bin', imports.meta.url)) is a really convenient one-liner to load something relative to the current file that works both in the browser and in deno/bun, and it would be nice to see it supported in node as well.
If someone has recommendations for similar one-liners that works everywhere without using file URLs, I would be happy to use that instead.
Reacted by Luis Castro Martín, Nick Renieris, Dasa Paddock and stefnotch
Agreed here -- I don't think I demand file:// URLs in particular, but fetch(new URL('foo.bin', imports.meta.url)) is a really convenient one-liner to load something relative to the current file that works both in the browser and in deno/bun, and it would be nice to see it supported in node as well.
If someone has recommendations for similar one-liners that works everywhere without using file URLs, I would be happy to use that instead.
+1. That is the obvious way to go for loading webassembly and it works everywhere else.
@magciusnew URL('foo.bin', imports.meta.url) is a file url.
What I meant was that I would be happy for any sort of polyglot way to load a file that works everywhere, whether using fetch/file URIs or something else. You could have new URL('foo.bin', imports.meta.url) return a special type of "relative meta URL" that allows fetching, while still not supporting file URI support globally if you consider it too major of a risk.
Hello folks, long time no see ... so @mcollina pointed me to this Opened issue and here my take: the current guard does not guarantee anything concretely, really, because there's an easy hack around it:
The guard is on something that's not even a foreign user input, but most importantly, WASM files cannot be streamed if passed along as files as intended, and validated, source of truth: no WebAssembly streaming possible whatsoever!
But "fear not", Trusted Types API is supposed to solve exactly that trusted feeling around strings, and whatnot, so I wonder: how about we make fetch in Node able to deal with explicit intents, as fetch URLs, validated via explicit program intents too?
Because right now, if the foreign/undesired input is the reason to not allow file:// protocol, something embedded in the PL runtime itself as valid, any naive enough users' program could just check for file.startsWith('file://') and slice/retrieve that file after without a sweat so ... should we instead embrace the standards' proposed way to deal with untrusted input?
I hope we should/could and agree the fact file:// protocol is both banned and easy to hack in various ways is a hint, something ain't right around the current fetch limitations.
Thank you in advance for listening and, eventually, following up, happy to help, as usual.
to whoever thumbs down without follow up, you are saying:
URL.createObjectURL(blob) is OK as a hack that makes this whole story pointless
bringing in the whole Trusted Types API is too much effort
happy to learn your thumb down but please don't do that without follow ups, I've been bitten by this limitation in Emscripten and it's taking a long time to explan why NodeJS and blob URLs should be more relaxed as a whole, thanks!
Activity
[-]fetch: Enable fetch via file URL[/-][+]fetch: Enable fetch via file URL (under flag)[/+]mcollina commentedon Feb 13, 2024
I’m ok in supporting this under a allowFileUrl option in fetch.
aduh95 commentedon Feb 13, 2024
Related: sebelga/fetch#5
mcollina commentedon Feb 13, 2024
Moving to Undici repository
metcoder95 commentedon Feb 13, 2024
What would be the shape of it, and its considerations?
From what I've seen from the issue shared by @aduh95 the main security points seem valid; Chromium seems to have some initial implementation.
KhafraDev commentedon Feb 13, 2024
If we implement this we will deviate from literally every other platform. We should either let wintercg handle it, or let the spec handle it. -1
micheleriva commentedon Feb 13, 2024
@KhafraDev I agree with you that it’d be optimal to have wintercg to formalize this. What do you mean by “will deviate from literally every other platform”? Other runtimes (Bun and Deno for sure) implement a this already
KhafraDev commentedon Feb 13, 2024
From a quick glance:
If we implement our own implementation (ie. with a non-standard option), that's another implementation that differs from every browser and every server environment. No implementation is spec compliant because there is no spec, which will also cause issues when a spec is written, and these platforms now have to break their users to match it.
There are also a number of edge cases that haven't been decided on sebelga/fetch#5 (comment)
I'm quite confused as to your mention of wasm-pack
Browsers aren't able to do this either. But from looking at the code, they instead download the binary from a url, which will work in node and browsers, ignoring CORs errors. Maybe you could expand on this?
micheleriva commentedon Feb 14, 2024
This is very true, but that's also the purpose of experimental APIs. I personally think that missing APIs cause more damage to the user than experimental APIs.
Correct. Right now, if you use wasm-pack, you have to run at least three compilation processes:
The only real blocker I found to enable a single compilation process, it's by allowing Node.js to fetch the compiled WASM binaries from the filesystem at runtime.
I wouldn't focus too much on the wasm-pack use case only, though. I'm using Deno a lot and being able to avoid calling server-side methods (such as
fs.readFileSyncfor Node.js) allows you to write code once and use it both on the frontend and the backend, which is kinda handy.I understand the spec is basically non-existent, yet, as I said earlier, an experimental API is far better than no API at all to me.
But of course, that's a personal opinion and I'm happy to discuss
KhafraDev commentedon Feb 14, 2024
But we aren't missing any apis, we implement fetching file urls exactly as the spec tells us to (throw an error). I would claim that Deno and Bun, by implementing this, cause much more damage in the long run (in the style of mootools or others that added methods to Array/Object prototypes, preventing the tc39 from adding certain methods).
I'm also still not sure how fetching file urls can be used in both browsers and runtimes, but that doesn't seem relevant.
aduh95 commentedon Feb 14, 2024
tbf it doesn't need to be supported in browsers to improve interop, the use case I have in mind is
fetch(new URL('./some/relative/path', import.meta.url)), whereimport.meta.urlwould be a HTTPS URL in browsers and afile:URL on runtimes.KhafraDev commentedon Feb 14, 2024
Thanks for the use case, makes sense now! Interop with other server environments is precisely the point of WinterCG. In the past we've followed Deno, which has only ever caused issues for us (namely security vulnerabilities, edge cases, more edge cases, other issues). But the proposed solution is not interoperable with other runtimes as it requires explicitly opting in to it, for every request - the other two runtimes just work. As in, you would have to change your code for it to work in node, whereas you don't for other runtimes, nor will you when/if it's ever added into the spec.
I mentioned earlier, but Bun isn't entirely interoperable with Deno, and there are also a number of questions about the api that WinterCG should solve before we even consider implementing it.
magcius commentedon Oct 24, 2024
Agreed here -- I don't think I demand file:// URLs in particular, but
fetch(new URL('foo.bin', imports.meta.url))is a really convenient one-liner to load something relative to the current file that works both in the browser and in deno/bun, and it would be nice to see it supported in node as well.If someone has recommendations for similar one-liners that works everywhere without using file URLs, I would be happy to use that instead.
mcollina commentedon Oct 25, 2024
@magcius
new URL('foo.bin', imports.meta.url)is a file url.luiscastro193 commentedon Oct 27, 2024
+1. That is the obvious way to go for loading webassembly and it works everywhere else.
magcius commentedon Nov 21, 2024
What I meant was that I would be happy for any sort of polyglot way to load a file that works everywhere, whether using fetch/file URIs or something else. You could have
new URL('foo.bin', imports.meta.url)return a special type of "relative meta URL" that allows fetching, while still not supporting file URI support globally if you consider it too major of a risk.scris commentedon May 2, 2025
Any update on this? Thanks.
WebReflection commentedon Aug 18, 2026
Hello folks, long time no see ... so @mcollina pointed me to this Opened issue and here my take: the current guard does not guarantee anything concretely, really, because there's an easy hack around it:
The guard is on something that's not even a foreign user input, but most importantly, WASM files cannot be streamed if passed along as files as intended, and validated, source of truth: no WebAssembly streaming possible whatsoever!
But "fear not", Trusted Types API is supposed to solve exactly that trusted feeling around strings, and whatnot, so I wonder: how about we make
fetchin Node able to deal with explicit intents, as fetch URLs, validated via explicit program intents too?Because right now, if the foreign/undesired input is the reason to not allow
file://protocol, something embedded in the PL runtime itself as valid, any naive enough users' program could just check forfile.startsWith('file://')and slice/retrieve that file after without a sweat so ... should we instead embrace the standards' proposed way to deal with untrusted input?I hope we should/could and agree the fact
file://protocol is both banned and easy to hack in various ways is a hint, something ain't right around the currentfetchlimitations.Thank you in advance for listening and, eventually, following up, happy to help, as usual.
mcollina commentedon Aug 18, 2026
We would need to add TrustedTypes to node.js core.
WebReflection commentedon Aug 18, 2026
That's definitively a path forward, as inputs, of all kinds, are the reason most APIs are restrictive these days, right?
WebReflection commentedon Aug 18, 2026
to whoever thumbs down without follow up, you are saying:
URL.createObjectURL(blob)is OK as a hack that makes this whole story pointlesshappy to learn your thumb down but please don't do that without follow ups, I've been bitten by this limitation in Emscripten and it's taking a long time to explan why NodeJS and blob URLs should be more relaxed as a whole, thanks!
for reference: emscripten-core/emscripten#27560
KnorpelSenf commentedon Aug 20, 2026
The reaction is from @KhafraDev