Repository navigation
fix(runtime): U+0000 in specifiers, HTTP-relative worker paths, file-only import.meta.dirname - #614
Open
edusperoni wants to merge 6 commits into
Open
edusperoni wants to merge 6 commits into
edusperoni wants to merge 6 commits into
Conversation
…elative workers and import.meta.dirname
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 19 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three small fixes in the ES module loader:
package.jsonmainare no longer read as C strings.import()callback and the async graph walk read the specifier through one length-exact helper (ModuleSpecifierUtf8).Error, andimport()rejects with anError. The graph walk skips the specifier, so nothing is fetched or read from disk for it.Cannot resolve module specifier "<spec with \u0000 escaped>": a module specifier must not contain U+0000. It is the same in Debug and Release.ModuleInternal::ResolvePathFromPackageJson(therequire()of a package directory):package.jsonwhose top level is not a JSON object is treated like invalid JSON.mainthat is not a string is ignored and resolution falls back toindex, as Node does.mainthat has no exact UTF-8 form, or that contains U+0000, cannot be located. These cases end in the usualUnable to locate main entry in <package.json>error.new Worker("./x.js")/new Worker("../x.js")called from a module loaded over HTTP resolves against that module's URL.ParseModuleUrl), and the worker loads from the resulting http(s) URL through the normal loader path.import.meta.dirnameis defined only for file-backed modules, as in Node. For an http(s),blob:or other URL-keyed module the property is absent ("dirname" in import.meta === false). File modules keep their current value.Why
import("./a.mjs\0junk")loaded./a.mjs, because the specifier went throughv8::String::Utf8Valueand was then used as a C string. The same truncation happened in a static import and in the graph walk's prefetch.package.json.ResolvePathFromPackageJsonusedmainwith no type checks. On the unfixed runtime, three of the new fixtures abort the process on arequire():main:-[NSPathStore2 stringByAppendingPathComponent:]raised an unrecognized-selector exception;maincontaining U+0000:std::stringwas constructed from a NULLUTF8String;objectForKey:was sent to anNSArray.http://host/src/feature/a.ts,new Worker("./w.js")loaded<app>/w.jsfrom disk. Onlyfile://and/callers got caller-relative resolution; every other caller fell through to the app-root join.import.meta.dirname. For a URL module the value was the key cut at its last/, even when that slash was inside the query. For example,http://host/ns/asm?path=/src/app/main.tsgavehttp://host/ns/asm?path=/src/app.Behaviour changes
import x from "./a.mjs\0junk"(file or HTTP importer)./a.mjsErrornaming the U+0000 ruleimport("./a.mjs\0junk")(file or HTTP importer)./a.mjsErrorrequire()of a package whosemainis not a string (e.g.42)indexrequire()of a package whosemaincontains U+0000Unable to locate main entry in …/package.jsonrequire()of a package whosepackage.jsonis a JSON arrayUnable to locate main entry in …/package.jsonrequire()of a package whosemainis a lone surrogate ("a\ud800b")Unable to locate main entry(NSJSONSerialization rejects the file)new Worker("./x.js")/"../x.js"from an HTTP module<app>/x.js(or<app>/../x.js) from disknew Worker(new URL("./x.js", import.meta.url))from an HTTP moduleimport.meta.dirnamein an http(s) module/import.meta.dirnamein ablob:moduleimport.meta.dirnamein a file module/app/<dir>How it was verified
The new specs are in
TestRunner/app/tests/LoaderHardeningTests.jsandTestRunner/app/tests/HttpEsmLoaderTests.js. The test server gained/esm/meta/…and/esm/worker/…routes, and its JS string escaping now covers U+0000.On the unfixed runtime (specs only):
main, then U+0000main, then the arraypackage.json../worker spec and the../worker spec, where the decoy at the app-root join ran and the server saw no request;import.meta.dirnamespec and theblob:import.meta.dirnamespec;file:worker URL from a file module, which is not part of this PR (see Not covered).mainspec;import.meta.dirnamespec;new Worker(new URL("./x.js", import.meta.url))from an HTTP module;new Worker("<absolute http URL>")from a file module.With the fixes:
mainat feat(runtime): load HTTP modules through an HTTP cache over persistent connections #585 (HTTP cache and persistent connections): 2353 cases, 0 failures.mainhas 2339.mainat refactor(runtime): one URL parse for every http(s) module entry point #609: 2352 cases, 0 failures, and the same under AddressSanitizer. The ASan run was not repeated after the merge.NativeScriptscheme forgeneric/platform=iOS Simulator(arm64 and x86_64, with the project's-Werror): succeeded.Not covered
require(),createRequire()and filenew Worker()paths. This PR does not change these paths.[NSString stringWithUTF8String:x.c_str()]orToV8String(isolate, x.c_str())), so a different file can load../or../worker path now goes through the URL parser instead, which percent-encodes the NUL./-rooted or bare are unchanged. A/xpath is still treated as a container-absolute file path, and bare and~/paths still resolve as before.%00when it resolves a specifier against an http(s) base. The loader instead rejects such a specifier from every importer.file:worker URLs from a file module.new Worker(new URL("./x.js", import.meta.url))gives the constructorfile:///app/…. That string is treated as a baretns_modulesname and fails with "Failed to resolve module". This is the behaviour onmainand is not changed here.mainentry.ResolveMainEntryFromPackageJsonalready ignores a non-stringmain.maincontaining U+0000 is unchanged and was not reproduced.mainto the samestringByAppendingPathComponent:/UTF8Stringsequence that aborted for a packagemain.package.json../or../from an HTTP module does not resolve to an http(s) URL, the constructor throws. No spec covers this branch.import.meta.urlasfile:///app/andimport.meta.dirnameas/app.import.metaof a builtin module.import.meta.filenameis not added.