Skip to content

fix(runtime): U+0000 in specifiers, HTTP-relative worker paths, file-only import.meta.dirname - #614

Open
edusperoni wants to merge 6 commits into
mainfrom
fix/loader-nul-specifiers-worker-dirname
Open

edusperoni wants to merge 6 commits into
mainfrom
fix/loader-nul-specifiers-worker-dirname

Conversation

@edusperoni

@edusperoni edusperoni commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

Three small fixes in the ES module loader:

  1. Module specifiers and package.json main are no longer read as C strings.
    • The static resolver, the dynamic import() callback and the async graph walk read the specifier through one length-exact helper (ModuleSpecifierUtf8).
    • A specifier that contains U+0000 is rejected. A static import fails to link with an Error, and import() rejects with an Error. The graph walk skips the specifier, so nothing is fetched or read from disk for it.
    • The message is 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 (the require() of a package directory):
      • A package.json whose top level is not a JSON object is treated like invalid JSON.
      • A main that is not a string is ignored and resolution falls back to index, as Node does.
      • A main that has no exact UTF-8 form, or that contains U+0000, cannot be located. These cases end in the usual Unable to locate main entry in <package.json> error.
  2. new Worker("./x.js") / new Worker("../x.js") called from a module loaded over HTTP resolves against that module's URL.
    • The path goes through the WHATWG URL parser (ParseModuleUrl), and the worker loads from the resulting http(s) URL through the normal loader path.
    • If the result is not an http(s) URL, the constructor throws. An HTTP caller never falls back to the app root.
    • This extends NativeScript's existing caller-relative rule for relative worker paths to HTTP modules. It is not web behaviour: a browser resolves a Worker URL against the document's base URL.
  3. import.meta.dirname is 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

  • Specifiers. import("./a.mjs\0junk") loaded ./a.mjs, because the specifier went through v8::String::Utf8Value and was then used as a C string. The same truncation happened in a static import and in the graph walk's prefetch.
  • package.json. ResolvePathFromPackageJson used main with no type checks. On the unfixed runtime, three of the new fixtures abort the process on a require():
    • a numeric main: -[NSPathStore2 stringByAppendingPathComponent:] raised an unrecognized-selector exception;
    • a main containing U+0000: std::string was constructed from a NULL UTF8String;
    • a top-level JSON array: objectForKey: was sent to an NSArray.
  • Workers. From http://host/src/feature/a.ts, new Worker("./w.js") loaded <app>/w.js from disk. Only file:// 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.ts gave http://host/ns/asm?path=/src/app.

Behaviour changes

Case Before After
import x from "./a.mjs\0junk" (file or HTTP importer) loads ./a.mjs link fails: Error naming the U+0000 rule
import("./a.mjs\0junk") (file or HTTP importer) loads ./a.mjs promise rejects with that Error
graph walk of an HTTP module with such a static import prefetches the truncated URL nothing fetched
require() of a package whose main is not a string (e.g. 42) process abort resolves index
require() of a package whose main contains U+0000 process abort Unable to locate main entry in …/package.json
require() of a package whose package.json is a JSON array process abort Unable to locate main entry in …/package.json
require() of a package whose main is a lone surrogate ("a\ud800b") Unable to locate main entry (NSJSONSerialization rejects the file) unchanged
new Worker("./x.js") / "../x.js" from an HTTP module <app>/x.js (or <app>/../x.js) from disk the URL resolved against the module's URL, fetched over HTTP
new Worker(new URL("./x.js", import.meta.url)) from an HTTP module works unchanged
import.meta.dirname in an http(s) module URL cut at its last / property absent
import.meta.dirname in a blob: module the blob URL property absent
import.meta.dirname in a file module /app/<dir> unchanged

How it was verified

The new specs are in TestRunner/app/tests/LoaderHardeningTests.js and TestRunner/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):

  • Three package specs aborted the app. Each was found by skipping the previous one and rerunning: non-string main, then U+0000 main, then the array package.json.
  • With those three skipped, these specs failed:
    • both U+0000 specs from a file importer, where the decoy module was evaluated;
    • both U+0000 specs from an HTTP importer, where the truncated URL was requested once;
    • the ./ worker spec and the ../ worker spec, where the decoy at the app-root join ran and the server saw no request;
    • the HTTP import.meta.dirname spec and the blob: import.meta.dirname spec;
    • a spec for a file: worker URL from a file module, which is not part of this PR (see Not covered).
  • These new specs passed on unfixed code, as expected:
    • the lone-surrogate main spec;
    • the strengthened file-module import.meta.dirname spec;
    • 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:

Not covered

  • U+0000 in require(), createRequire() and file new Worker() paths. This PR does not change these paths.
    • Checked by reading the code, not with specs.
    • The path is cut at the NUL at its first conversion ([NSString stringWithUTF8String:x.c_str()] or ToV8String(isolate, x.c_str())), so a different file can load.
    • From an HTTP module, a ./ or ../ worker path now goes through the URL parser instead, which percent-encodes the NUL.
  • From an HTTP module, worker paths that are /-rooted or bare are unchanged. A /x path is still treated as a container-absolute file path, and bare and ~/ paths still resolve as before.
  • Deliberate deviation for HTTP importers. A browser percent-encodes U+0000 to %00 when 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 constructor file:///app/…. That string is treated as a bare tns_modules name and fails with "Failed to resolve module". This is the behaviour on main and is not changed here.
  • App's own main entry. ResolveMainEntryFromPackageJson already ignores a non-string main.
    • A main containing U+0000 is unchanged and was not reproduced.
    • It passes that main to the same stringByAppendingPathComponent: / UTF8String sequence that aborted for a package main.
    • There is no spec, because it would require changing the test app's own package.json.
  • Worker constructor throw. When ./ or ../ from an HTTP module does not resolve to an http(s) URL, the constructor throws. No spec covers this branch.
  • Release run. The suite ran in Debug only; the Release configuration was compiled, not run.
    • The U+0000 error text is built the same way in both configurations, by reading the code.
    • An HTTP-relative worker loads through the same loader path as an absolute http(s) worker URL, so the Release remote-module rules apply to it unchanged.
  • Empty registry key. A module with an empty registry key still reports import.meta.url as file:///app/ and import.meta.dirname as /app.
  • Builtins. No spec observes import.meta of a builtin module.
  • import.meta.filename is not added.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aa9f0b29-b936-4ecd-8b1b-0e18a6878b36
Commits

Reviewing files that changed from the base of the PR and between 8e013af and 4021b2a.

Files selected for processing: 20

  • NativeScript/runtime/ModuleInternal.mm
  • NativeScript/runtime/ModuleInternalCallbacks.mm
  • NativeScript/runtime/Runtime.mm
  • NativeScript/runtime/Worker.mm
  • TestRunner/app/tests/HttpEsmLoaderTests.js
  • TestRunner/app/tests/LoaderHardeningTests.js
  • TestRunner/app/tests/esm/relative/meta.mjs
  • TestRunner/app/tests/loaderhardening/nulDecoyDynamic.mjs
  • TestRunner/app/tests/loaderhardening/nulDecoyStatic.mjs
  • TestRunner/app/tests/loaderhardening/nulDynamicImporter.mjs
  • TestRunner/app/tests/loaderhardening/nulStaticImporter.mjs
  • TestRunner/app/tests/loaderhardening/pkgArrayJson/index.js
  • TestRunner/app/tests/loaderhardening/pkgArrayJson/package.json
  • TestRunner/app/tests/loaderhardening/pkgNonStringMain/index.js
  • TestRunner/app/tests/loaderhardening/pkgNonStringMain/package.json
  • TestRunner/app/tests/loaderhardening/pkgNulMain/index.js
  • TestRunner/app/tests/loaderhardening/pkgNulMain/package.json
  • TestRunner/app/tests/loaderhardening/pkgSurrogateMain/index.js
  • TestRunner/app/tests/loaderhardening/pkgSurrogateMain/package.json
  • TestRunnerTests/TestRunnerTests.swift
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant