Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3fcad40
test(runtime): pin the spelling of the module URLs dev tooling emits
edusperoni Nov 26, 2026
ae45c1f
refactor(runtime): one shared unit for the loader's module URL helpers
edusperoni Nov 26, 2026
ed556e1
fix(runtime): spell every absolute http(s) module URL with the URL pa…
edusperoni Nov 26, 2026
28850ec
fix(runtime): drop the legacy module URL forms
edusperoni Nov 26, 2026
77d79bb
docs: how the loader spells absolute http(s) module URLs
edusperoni Nov 26, 2026
e79bf93
fix(runtime): spell remote-module allowlist entries like the URLs the…
edusperoni Nov 26, 2026
92bf295
docs: an HTTP module's import() follows one import-map entry
edusperoni Nov 26, 2026
0d28268
fix(runtime): spell http(s) import-map scope keys like the referrer k…
edusperoni Nov 26, 2026
02e939d
perf(runtime): spell each module URL once per key
edusperoni Nov 26, 2026
e6b5bf0
refactor(runtime): build the URL a module is reported under in one place
edusperoni Nov 26, 2026
b684e7b
fix(runtime): never report an http(s) module URL as a file:// URL
edusperoni Nov 26, 2026
0006b12
feat(runtime): warn in debug when an ES module passes a relative spec…
edusperoni Nov 26, 2026
145b8d9
docs: the global require in ES modules resolves against the app root
edusperoni Nov 26, 2026
bdf02e2
Merge remote-tracking branch 'origin/refactor/loader-url-entry-points…
edusperoni Nov 26, 2026
1fe53a0
refactor(runtime): name the module in the global require warning with…
edusperoni Nov 26, 2026
139c7a7
fix(runtime): keep a configured remote-module allowlist in force and …
edusperoni Nov 27, 2026
77cc495
fix(runtime): read import-map strings exactly
edusperoni Nov 27, 2026
b18fcc6
Merge remote-tracking branch 'origin/refactor/loader-url-entry-points…
edusperoni Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions NativeScript/runtime/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdint>
#include <cstdio>
#include <functional>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -171,6 +172,26 @@ inline std::string ToString(v8::Isolate* isolate, const NSString* value) {
return tns::ToString(isolate, tns::ToV8String(isolate, value));
}

// The exact UTF-8 bytes of `value`, sized by length so an embedded U+0000 is
// kept (a C-string conversion stops there); nullopt for nil or for a string
// with a lone surrogate, which has no UTF-8 spelling (-UTF8String returns NULL).
inline std::optional<std::string> ToExactUtf8(const NSString* value) {
if (value == nil) {
return std::nullopt;
}
CFStringRef str = (__bridge CFStringRef)value;
const CFRange range = CFRangeMake(0, CFStringGetLength(str));
CFIndex byteCount = 0;
if (CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &byteCount) !=
range.length) {
return std::nullopt;
}
std::string bytes(static_cast<size_t>(byteCount), '\0');
CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false,
reinterpret_cast<UInt8*>(bytes.data()), byteCount, nullptr);
return bytes;
}

inline NSString* ToNSString(const std::string& v) {
return [[[NSString alloc] initWithBytes:v.c_str() length:v.length()
encoding:NSUTF8StringEncoding] S_AUTORELEASE];
Expand Down
32 changes: 25 additions & 7 deletions NativeScript/runtime/HttpLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ struct CanonicalizationConfig {
// module under exactly one URL and never varies it for freshness.
std::string CanonicalizeHttpUrlKey(const std::string& url);

// Repairs `http:/host` (one slash) back to `http://host`. Upstream path joins
// collapse the double slash, and every key derivation has to undo it BEFORE it
// tests the scheme — otherwise the collapsed form fails the test, is passed
// through verbatim, and the same module ends up with two registry identities:
// one from the resolver (which repairs) and one from whatever came in through
// invalidateModules or a cache-bust mark (which did not).
std::string RepairCollapsedUrlScheme(const std::string& url);
// CanonicalizeHttpUrlKey for a URL already spelled by NormalizeHttpModuleUrl.
std::string CanonicalizeNormalizedHttpUrlKey(const std::string& normalizedUrl);

// What a module response turned out to be. Decided once, by the shared
// classifier, for whichever transport produced the response.
Expand Down Expand Up @@ -151,4 +146,27 @@ bool IsRemoteModulesAllowed();
// (or all URLs if the allowlist is empty).
bool IsRemoteUrlAllowed(const std::string& url);

// The allowlist IsRemoteUrlAllowed applies once remote modules are enabled.
struct RemoteModuleAllowlist {
// False only when `security.remoteModuleAllowlist` is absent, not an array,
// or an empty array: then every URL is allowed.
bool inForce = false;
// The usable items, each spelled the way the loader spells the URLs it
// fetches (NormalizeHttpModuleUrl) so path-scoped entries compare against
// resolved paths, or kept verbatim when it is not an http(s) URL. An item
// that is not a string, is empty or has no exact UTF-8 form is not here: it
// matches nothing but still keeps the list in force.
std::vector<std::string> entries;
};

#ifdef __OBJC__
// The allowlist for the raw `security.remoteModuleAllowlist` config value.
RemoteModuleAllowlist RemoteModuleAllowlistFromConfig(id value);
#endif

// Whether `url` (as fetched) passes `allowlist`: always when it is not in
// force, otherwise when one entry matches it by prefix with the match ending
// at a URL-component boundary.
bool RemoteUrlAllowedByAllowlist(const std::string& url, const RemoteModuleAllowlist& allowlist);

} // namespace tns
95 changes: 43 additions & 52 deletions NativeScript/runtime/HttpLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Helpers.h"
#include "ModuleInternal.h"
#include "ModuleInternalCallbacks.h"
#include "ModuleUrl.h"
#include "Runtime.h"
#include "RuntimeConfig.h"
#include "robin_hood.h"
Expand All @@ -32,7 +33,7 @@ static inline bool StartsWith(const std::string& s, const char* prefix) {

static std::once_flag s_securityConfigInitFlag;
static bool s_allowRemoteModules = false;
static std::vector<std::string> s_remoteModuleAllowlist;
static RemoteModuleAllowlist s_remoteModuleAllowlist;

// Returns true when `url` is authorized by allowlist `entry`.
//
Expand All @@ -54,6 +55,41 @@ static bool RemoteUrlMatchesAllowlistEntry(const std::string& url, const std::st
return next == '/' || next == '?' || next == '#';
}

static std::string NormalizeRemoteModuleAllowlistEntry(const std::string& entry) {
const std::string url = NormalizeHttpModuleUrl(entry);
return url.empty() ? entry : url;
}

RemoteModuleAllowlist RemoteModuleAllowlistFromConfig(id value) {
RemoteModuleAllowlist allowlist;
if (![value isKindOfClass:[NSArray class]] || [(NSArray*)value count] == 0) {
return allowlist;
}
allowlist.inForce = true;
for (id item in (NSArray*)value) {
if (![item isKindOfClass:[NSString class]]) {
continue;
}
const std::optional<std::string> entry = ToExactUtf8((NSString*)item);
if (entry.has_value() && !entry->empty()) {
allowlist.entries.push_back(NormalizeRemoteModuleAllowlistEntry(*entry));
}
}
return allowlist;
}

bool RemoteUrlAllowedByAllowlist(const std::string& url, const RemoteModuleAllowlist& allowlist) {
if (!allowlist.inForce) {
return true;
}
for (const std::string& entry : allowlist.entries) {
if (RemoteUrlMatchesAllowlistEntry(url, entry)) {
return true;
}
}
return false;
}

static void InitializeSecurityConfig() {
std::call_once(s_securityConfigInitFlag, []() {
@autoreleasepool {
Expand All @@ -69,18 +105,7 @@ static void InitializeSecurityConfig() {
s_allowRemoteModules = [allowRemote boolValue];
}

id allowlist = security[@"remoteModuleAllowlist"];
if (allowlist && [allowlist isKindOfClass:[NSArray class]]) {
NSArray* list = (NSArray*)allowlist;
for (id item in list) {
if ([item isKindOfClass:[NSString class]]) {
NSString* str = (NSString*)item;
if (str.length > 0) {
s_remoteModuleAllowlist.push_back(std::string([str UTF8String]));
}
}
}
}
s_remoteModuleAllowlist = RemoteModuleAllowlistFromConfig(security[@"remoteModuleAllowlist"]);
}
});
}
Expand All @@ -104,18 +129,7 @@ bool IsRemoteUrlAllowed(const std::string& url) {
return false;
}

// If no allowlist is configured, allow all URLs (user explicitly enabled remote modules)
if (s_remoteModuleAllowlist.empty()) {
return true;
}

for (const std::string& entry : s_remoteModuleAllowlist) {
if (RemoteUrlMatchesAllowlistEntry(url, entry)) {
return true;
}
}

return false;
return RemoteUrlAllowedByAllowlist(url, s_remoteModuleAllowlist);
}

// ─────────────────────────────────────────────────────────────
Expand All @@ -127,38 +141,15 @@ bool IsRemoteUrlAllowed(const std::string& url) {
// URLs touched before the client configures anything (the local trampoline's
// clean `/ns/core/*` imports) carry no query, so they canonicalize identically
// under any vocabulary.
std::string RepairCollapsedUrlScheme(const std::string& url) {
if (StartsWith(url, "http:/") && !StartsWith(url, "http://")) {
std::string repaired = url;
repaired.insert(5, "/");
return repaired;
}
if (StartsWith(url, "https:/") && !StartsWith(url, "https://")) {
std::string repaired = url;
repaired.insert(6, "/");
return repaired;
}
return url;
std::string CanonicalizeHttpUrlKey(const std::string& url) {
const std::string normalizedUrl = NormalizeHttpModuleUrl(url);
return normalizedUrl.empty() ? url : CanonicalizeNormalizedHttpUrlKey(normalizedUrl);
}

std::string CanonicalizeHttpUrlKey(const std::string& url) {
std::string CanonicalizeNormalizedHttpUrlKey(const std::string& normalizedUrl) {
// Per-isolate vocabulary, so this runs on the isolate's own thread. Fetch
// threads never reach here — they carry keys computed for them.
const CanonicalizationConfig* canonConfig = CanonicalizationConfigForCurrentIsolate();
// Some loaders wrap HTTP module URLs as file://http(s)://...
// Repaired before any scheme test, so a collapsed `http:/host` keys the same
// as the URL it means rather than passing through untouched.
std::string normalizedUrl = RepairCollapsedUrlScheme(url);
// The wrapper is tested on the one-slash forms as well: a path join can
// collapse the scheme INSIDE the wrapper (`file://http:/host/x`), which
// matches neither the two-slash unwrap nor the repair above (whose prefix is
// `file://`, not `http:/`) and would otherwise key as its own identity.
if (StartsWith(normalizedUrl, "file://http:/") || StartsWith(normalizedUrl, "file://https:/")) {
normalizedUrl = RepairCollapsedUrlScheme(normalizedUrl.substr(strlen("file://")));
}
if (!(StartsWith(normalizedUrl, "http://") || StartsWith(normalizedUrl, "https://"))) {
return normalizedUrl;
}
// Drop fragment entirely
size_t hashPos = normalizedUrl.find('#');
std::string noHash =
Expand Down
10 changes: 8 additions & 2 deletions NativeScript/runtime/ModuleInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,21 @@ class ModuleInternal {

private:
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
// The global require's native half in debug builds: RequireCallback plus a
// one-time warning per ES module that passes it a relative specifier.
static void DebugGlobalRequireCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void CreateRequireCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
// A require bound to `dirName`, whose ES module loads evaluate under
// `options`. The options ride along as trailing arguments to the require
// factory, so nothing about them is ambient — and they are resolved once at
// mint time, never per require() call.
// mint time, never per require() call. `requireInternal` defaults to the
// RequireCallback function every module-scoped require shares.
v8::Local<v8::Function> GetRequireFunction(
v8::Isolate* isolate, const std::string& dirName,
const ModuleEvaluationOptions& options);
const ModuleEvaluationOptions& options,
v8::Local<v8::Function> requireInternal = v8::Local<v8::Function>());
v8::Local<v8::Object> LoadImpl(v8::Isolate* isolate,
const std::string& moduleName,
const std::string& baseDir, bool& isData,
Expand Down
Loading
Loading