Skip to content

Commit e79bf93

Browse files
committed
fix(runtime): spell remote-module allowlist entries like the URLs they gate
The allowlist compares fetched URLs, which the loader now spells with the URL parser (dot segments resolved, characters such as non-ASCII percent-encoded). Each security.remoteModuleAllowlist entry is spelled the same way once, when the allowlist is loaded, with the written authority kept; an entry that is not an http(s) URL stays verbatim. The prefix-and-boundary rule is unchanged. The rule is factored into RemoteUrlMatchesAllowlist, which IsRemoteUrlAllowed applies, and debug builds expose it on ns:module as remoteUrlMatchesAllowlist(url, entries) so it can be tested; release builds omit the member.
1 parent 77d79bb commit e79bf93

9 files changed

Lines changed: 188 additions & 20 deletions

File tree

‎NativeScript/runtime/HttpLoader.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,16 @@ bool IsRemoteModulesAllowed();
143143
// (or all URLs if the allowlist is empty).
144144
bool IsRemoteUrlAllowed(const std::string& url);
145145

146+
// An allowlist entry spelled the way the loader spells the URLs it fetches
147+
// (NormalizeHttpModuleUrl), so path-scoped entries compare against resolved
148+
// paths; an entry that is not an http(s) URL is kept verbatim. Applied once,
149+
// when the allowlist is loaded.
150+
std::string NormalizeRemoteModuleAllowlistEntry(const std::string& entry);
151+
152+
// The allowlist rule IsRemoteUrlAllowed applies: `url`, as fetched, matches
153+
// one of `entries` (each already normalized) by prefix, the match ending at a
154+
// URL-component boundary.
155+
bool RemoteUrlMatchesAllowlist(const std::string& url,
156+
const std::vector<std::string>& entries);
157+
146158
} // namespace tns

‎NativeScript/runtime/HttpLoader.mm‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ static bool RemoteUrlMatchesAllowlistEntry(const std::string& url, const std::st
5555
return next == '/' || next == '?' || next == '#';
5656
}
5757

58+
std::string NormalizeRemoteModuleAllowlistEntry(const std::string& entry) {
59+
const std::string url = NormalizeHttpModuleUrl(entry);
60+
return url.empty() ? entry : url;
61+
}
62+
63+
bool RemoteUrlMatchesAllowlist(const std::string& url, const std::vector<std::string>& entries) {
64+
for (const std::string& entry : entries) {
65+
if (RemoteUrlMatchesAllowlistEntry(url, entry)) {
66+
return true;
67+
}
68+
}
69+
return false;
70+
}
71+
5872
static void InitializeSecurityConfig() {
5973
std::call_once(s_securityConfigInitFlag, []() {
6074
@autoreleasepool {
@@ -77,7 +91,8 @@ static void InitializeSecurityConfig() {
7791
if ([item isKindOfClass:[NSString class]]) {
7892
NSString* str = (NSString*)item;
7993
if (str.length > 0) {
80-
s_remoteModuleAllowlist.push_back(std::string([str UTF8String]));
94+
s_remoteModuleAllowlist.push_back(
95+
NormalizeRemoteModuleAllowlistEntry(std::string([str UTF8String])));
8196
}
8297
}
8398
}
@@ -110,13 +125,7 @@ bool IsRemoteUrlAllowed(const std::string& url) {
110125
return true;
111126
}
112127

113-
for (const std::string& entry : s_remoteModuleAllowlist) {
114-
if (RemoteUrlMatchesAllowlistEntry(url, entry)) {
115-
return true;
116-
}
117-
}
118-
119-
return false;
128+
return RemoteUrlMatchesAllowlist(url, s_remoteModuleAllowlist);
120129
}
121130

122131
// ─────────────────────────────────────────────────────────────

‎NativeScript/runtime/ModuleInternalCallbacks.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ void SetVolatilePatterns(const std::vector<std::string>& patterns);
236236
// filename/URL and splits the two
237237
// exported flavors)
238238
// - canonicalizeHttpUrlKey(url) (debug builds only; test diagnostic)
239+
// - remoteUrlMatchesAllowlist(url, entries)
240+
// (debug builds only; test diagnostic)
239241
//
240242
// Worker teardown across HMR cycles is userland: the dev client intercepts
241243
// the global `Worker` constructor and terminates tracked instances

‎NativeScript/runtime/ModuleInternalCallbacks.mm‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,6 +4194,47 @@ bool BuildNsModuleBinding(v8::Local<v8::Context> context, v8::Local<v8::Object>
41944194
return false;
41954195
}
41964196
}
4197+
4198+
// Debug-only diagnostic: the release allowlist rule, which debug builds
4199+
// never apply to fetches. `url` is spelled as the loader spells the URL it
4200+
// fetches and `entries` as the allowlist is loaded.
4201+
auto allowlistCb = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
4202+
v8::Isolate* iso = info.GetIsolate();
4203+
v8::Local<v8::Context> ctx = iso->GetCurrentContext();
4204+
std::vector<std::string> entries;
4205+
bool valid = info.Length() >= 2 && info[0]->IsString() && info[1]->IsArray();
4206+
if (valid) {
4207+
v8::Local<v8::Array> list = info[1].As<v8::Array>();
4208+
for (uint32_t i = 0; valid && i < list->Length(); i++) {
4209+
v8::Local<v8::Value> item;
4210+
if (!list->Get(ctx, i).ToLocal(&item)) {
4211+
return; // the element getter threw
4212+
}
4213+
valid = item->IsString();
4214+
if (valid) {
4215+
entries.push_back(NormalizeRemoteModuleAllowlistEntry(tns::ToString(iso, item)));
4216+
}
4217+
}
4218+
}
4219+
if (!valid) {
4220+
iso->ThrowException(v8::Exception::TypeError(tns::ToV8String(
4221+
iso, "remoteUrlMatchesAllowlist expects a URL string and an array of strings")));
4222+
return;
4223+
}
4224+
const std::string raw = tns::ToString(iso, info[0]);
4225+
const std::string url = NormalizeHttpModuleUrl(raw);
4226+
info.GetReturnValue().Set(RemoteUrlMatchesAllowlist(url.empty() ? raw : url, entries));
4227+
};
4228+
v8::Local<v8::Function> allowlistFn;
4229+
if (v8::Function::New(context, allowlistCb).ToLocal(&allowlistFn)) {
4230+
allowlistFn->SetName(tns::ToV8String(isolate, "remoteUrlMatchesAllowlist"));
4231+
if (!binding
4232+
->CreateDataProperty(context, tns::ToV8String(isolate, "remoteUrlMatchesAllowlist"),
4233+
allowlistFn)
4234+
.FromMaybe(false)) {
4235+
return false;
4236+
}
4237+
}
41974238
}
41984239
41994240
return true;

‎NativeScript/runtime/js/ns-module.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// and freezes the exports.
77
//
88
// Membership varies by build:
9-
// - `canonicalizeHttpUrlKey` exists only in debug builds (test diagnostic).
9+
// - `canonicalizeHttpUrlKey` and `remoteUrlMatchesAllowlist` exist only in
10+
// debug builds (test diagnostics).
1011
// Missing members are simply absent — never present-but-throwing — so
1112
// feature checks work.
1213

@@ -179,5 +180,8 @@ const surface = {
179180
if (binding.canonicalizeHttpUrlKey !== undefined) {
180181
surface.canonicalizeHttpUrlKey = binding.canonicalizeHttpUrlKey;
181182
}
183+
if (binding.remoteUrlMatchesAllowlist !== undefined) {
184+
surface.remoteUrlMatchesAllowlist = binding.remoteUrlMatchesAllowlist;
185+
}
182186

183187
module.exports = ObjectFreeze(surface);

‎TestRunner/app/tests/HttpEsmLoaderTests.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,14 +1433,15 @@ describe("HTTP ESM Loader", function() {
14331433

14341434
// The export set is public API, declared in types/ns-module.d.ts and
14351435
// docs/ns-builtin-modules.md — all three must change together.
1436-
// `canonicalizeHttpUrlKey` is a debug-only diagnostic (absent from the
1437-
// .d.ts and from release builds), so the expected set varies by build.
1436+
// `canonicalizeHttpUrlKey` and `remoteUrlMatchesAllowlist` are
1437+
// debug-only diagnostics (absent from the .d.ts and from release
1438+
// builds), so the expected set varies by build.
14381439
it("exposes exactly the declared surface", function () {
14391440
var nsModule = require("ns:module");
14401441
var expected = ["configureLoader", "createPumpingRequire", "createRequire",
14411442
"getLoadedModuleUrls", "invalidateModules"];
14421443
if (typeof nsModule.canonicalizeHttpUrlKey === "function") {
1443-
expected.push("canonicalizeHttpUrlKey");
1444+
expected.push("canonicalizeHttpUrlKey", "remoteUrlMatchesAllowlist");
14441445
}
14451446
expect(Object.keys(nsModule).sort()).toEqual(expected.sort());
14461447
});

‎TestRunner/app/tests/RemoteModuleSecurityTests.js‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,100 @@ describe("Remote Module Security", function() {
227227
});
228228
});
229229
});
230+
231+
// The release allowlist rule, through the debug-only diagnostic that runs
232+
// it: debug builds never gate a fetch on it.
233+
describe("Allowlist rule (ns:module remoteUrlMatchesAllowlist)", function() {
234+
function matches(url, entries) {
235+
var fn = require("ns:module").remoteUrlMatchesAllowlist;
236+
if (typeof fn !== "function") {
237+
pending("ns:module.remoteUrlMatchesAllowlist not exposed (release build)");
238+
return undefined;
239+
}
240+
return fn(url, entries);
241+
}
242+
243+
function expectRows(entry, rows) {
244+
rows.forEach(function (row) {
245+
var actual = matches(row[0], [entry]);
246+
if (actual === undefined) {
247+
return;
248+
}
249+
expect(entry + " vs " + row[0] + ": " + actual).toBe(entry + " vs " + row[0] + ": " + row[1]);
250+
});
251+
}
252+
253+
it("matches a plain entry by prefix at a component boundary", function() {
254+
expectRows("https://cdn.example", [
255+
["https://cdn.example/x.js", true],
256+
["https://cdn.example", true],
257+
["https://cdn.example.evil/x.js", false],
258+
["https://cdn.example@evil/x.js", false],
259+
["https://cdn.example:9999/x.js", false],
260+
]);
261+
});
262+
263+
it("keeps a path-scoped entry from being escaped by dot segments", function() {
264+
expectRows("https://cdn.example/modules/", [
265+
["https://cdn.example/modules/x.js", true],
266+
["https://cdn.example/modules/../../x.js", false],
267+
["https://cdn.example/modules/%2e%2e/x.js", false],
268+
["https://cdn.example/other/../modules/x.js", true],
269+
]);
270+
});
271+
272+
it("treats an entry with and without a trailing slash by the boundary rule", function() {
273+
expectRows("https://cdn.example/modules", [
274+
["https://cdn.example/modules/x.js", true],
275+
["https://cdn.example/modules?v=1", true],
276+
["https://cdn.example/modulesx/y.js", false],
277+
]);
278+
expectRows("https://cdn.example/modules/", [
279+
["https://cdn.example/modules", false],
280+
["https://cdn.example/modulesx/y.js", false],
281+
]);
282+
});
283+
284+
it("compares the host as written", function() {
285+
expectRows("https://CDN.example/modules/", [
286+
["https://CDN.example/modules/x.js", true],
287+
["https://cdn.example/modules/x.js", false],
288+
]);
289+
expectRows("HTTPS://cdn.example/modules/", [
290+
["https://cdn.example/modules/x.js", true],
291+
]);
292+
});
293+
294+
it("spells an entry's path the way fetched URLs are spelled", function() {
295+
expectRows("https://cdn.example/mödule/", [
296+
["https://cdn.example/mödule/x.js", true],
297+
["https://cdn.example/m%C3%B6dule/x.js", true],
298+
]);
299+
});
300+
301+
it("keeps an entry that is not a parseable http(s) URL verbatim", function() {
302+
expectRows("https://[fe80::1%25en0]/", [
303+
["https://[fe80::1%25en0]/x.js", true],
304+
]);
305+
expectRows("cdn.example/", [
306+
["https://cdn.example/x.js", false],
307+
]);
308+
});
309+
310+
it("rejects arguments that are not a URL string and an array of strings", function() {
311+
var fn = require("ns:module").remoteUrlMatchesAllowlist;
312+
if (typeof fn !== "function") {
313+
pending("ns:module.remoteUrlMatchesAllowlist not exposed (release build)");
314+
return;
315+
}
316+
[[], ["https://h/x.js"], ["https://h/x.js", "https://h/"], [1, []],
317+
["https://h/x.js", [1]]].forEach(function (args) {
318+
expect(function () { fn.apply(null, args); })
319+
.toThrowError(TypeError,
320+
"remoteUrlMatchesAllowlist expects a URL string and an array of strings");
321+
});
322+
});
323+
});
230324
});
231325

232326
console.log("Remote Module Security tests loaded");

‎docs/ns-builtin-modules.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,11 @@ works; adding them is a change to this specification first.
622622
623623
Debug builds additionally carry `canonicalizeHttpUrlKey(url)`, a pure test
624624
diagnostic that takes a string and throws
625-
`canonicalizeHttpUrlKey expects a URL string` otherwise; release builds omit
626-
it. Missing members are simply absent — never
625+
`canonicalizeHttpUrlKey expects a URL string` otherwise, and
626+
`remoteUrlMatchesAllowlist(url, entries)`, which applies the release
627+
`security.remoteModuleAllowlist` rule to `url` against `entries` and throws
628+
`remoteUrlMatchesAllowlist expects a URL string and an array of strings`
629+
otherwise; release builds omit both. Missing members are simply absent — never
627630
present-but-throwing — so feature checks work. The module is registered in
628631
every build: the security boundary for remote module loading sits at the
629632
network layer (`security.allowRemoteModules` in nativescript.config, enforced
@@ -657,7 +660,8 @@ A **scope key is matched as a plain prefix of the importing module's canonical
657660
registry key** — an absolute `http(s)` URL for a served module, or a canonical
658661
absolute path for a file on disk. That key is this runtime's analogue of the
659662
web's resolved referrer URL, which is what scope prefixes match in a browser.
660-
End a scope key with `/` to keep it on a directory boundary. Resolution
663+
End a scope key with
664+
`/` to keep it on a directory boundary. Resolution
661665
consults the most specific matching scope first (longest prefix wins), then
662666
progressively less specific ones, then `imports` — so a scope can override a
663667
global mapping for one subtree and fall through to it everywhere else. The

‎types/ns-module.d.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ declare module "ns:module" {
158158
options?: PumpingRequireOptions,
159159
): (specifier: string) => any;
160160

161-
// Debug builds additionally carry `canonicalizeHttpUrlKey(url)`, a pure
162-
// test diagnostic; release builds omit the member entirely. It is
163-
// deliberately not declared here: it is not public API, and declaring it
164-
// unconditionally would misrepresent release builds, where feature checks
165-
// must observe it as absent.
161+
// Debug builds additionally carry `canonicalizeHttpUrlKey(url)` and
162+
// `remoteUrlMatchesAllowlist(url, entries)`, pure test diagnostics; release
163+
// builds omit both members entirely. They are deliberately not declared
164+
// here: they are not public API, and declaring them unconditionally would
165+
// misrepresent release builds, where feature checks must observe them as
166+
// absent.
166167
}

0 commit comments

Comments
 (0)