|
| 1 | +// |
| 2 | +// Security configuration |
| 3 | +// { |
| 4 | +// "security": { |
| 5 | +// "allowRemoteModules": true, // Enable remote module loading in production |
| 6 | +// "remoteModuleAllowlist": [ // Optional: restrict to specific URL prefixes |
| 7 | +// "https://cdn.example.com/modules/", |
| 8 | +// "https://esm.sh/" |
| 9 | +// ] |
| 10 | +// } |
| 11 | +// } |
| 12 | +// |
| 13 | +// Behavior: |
| 14 | +// - Debug mode: Remote modules always allowed |
| 15 | +// - Production mode: Requires security.allowRemoteModules = true |
| 16 | +// - With allowlist: Only URLs matching a prefix in remoteModuleAllowlist are allowed |
| 17 | + |
| 18 | +describe("Remote Module Security", function() { |
| 19 | + |
| 20 | + describe("Debug Mode Behavior", function() { |
| 21 | + |
| 22 | + it("should allow HTTP module imports in debug mode", function(done) { |
| 23 | + // This test uses a known unreachable IP to trigger the HTTP loading path |
| 24 | + // In debug mode, the security check passes and we get a network error |
| 25 | + // (not a security error) |
| 26 | + import("http://192.0.2.1:5173/test-module.js").then(function(module) { |
| 27 | + // If we somehow succeed, that's fine too |
| 28 | + expect(module).toBeDefined(); |
| 29 | + done(); |
| 30 | + }).catch(function(error) { |
| 31 | + // Should fail with a network/timeout error, NOT a security error |
| 32 | + var message = error.message || String(error); |
| 33 | + // In debug mode, we should NOT see security-related error messages |
| 34 | + expect(message).not.toContain("not allowed in production"); |
| 35 | + expect(message).not.toContain("remoteModuleAllowlist"); |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should allow HTTPS module imports in debug mode", function(done) { |
| 41 | + // Test HTTPS URL - should be allowed in debug mode |
| 42 | + import("https://192.0.2.1:5173/test-module.js").then(function(module) { |
| 43 | + expect(module).toBeDefined(); |
| 44 | + done(); |
| 45 | + }).catch(function(error) { |
| 46 | + var message = error.message || String(error); |
| 47 | + // Should NOT be a security error in debug mode |
| 48 | + expect(message).not.toContain("not allowed in production"); |
| 49 | + expect(message).not.toContain("remoteModuleAllowlist"); |
| 50 | + done(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("Security Configuration", function() { |
| 56 | + |
| 57 | + it("should have security configuration in package.json", function() { |
| 58 | + var context = com.tns.Runtime.getCurrentRuntime().getContext(); |
| 59 | + var assetManager = context.getAssets(); |
| 60 | + |
| 61 | + try { |
| 62 | + var inputStream = assetManager.open("app/package.json"); |
| 63 | + var reader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream)); |
| 64 | + var sb = new java.lang.StringBuilder(); |
| 65 | + var line; |
| 66 | + |
| 67 | + while ((line = reader.readLine()) !== null) { |
| 68 | + sb.append(line); |
| 69 | + } |
| 70 | + reader.close(); |
| 71 | + |
| 72 | + var jsonString = sb.toString(); |
| 73 | + var config = JSON.parse(jsonString); |
| 74 | + |
| 75 | + // Verify security config structure |
| 76 | + expect(config.security).toBeDefined(); |
| 77 | + expect(typeof config.security.allowRemoteModules).toBe("boolean"); |
| 78 | + expect(Array.isArray(config.security.remoteModuleAllowlist)).toBe(true); |
| 79 | + } catch (e) { |
| 80 | + fail("Failed to read package.json: " + e.message); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it("should parse security allowRemoteModules from package.json", function() { |
| 85 | + var allowed = com.tns.Runtime.getSecurityAllowRemoteModules(); |
| 86 | + expect(typeof allowed).toBe("boolean"); |
| 87 | + expect(allowed).toBe(true); // Matches our test package.json config |
| 88 | + }); |
| 89 | + |
| 90 | + it("should parse security remoteModuleAllowlist from package.json", function() { |
| 91 | + var allowlist = com.tns.Runtime.getSecurityRemoteModuleAllowlist(); |
| 92 | + expect(allowlist).not.toBeNull(); |
| 93 | + expect(Array.isArray(allowlist)).toBe(true); |
| 94 | + expect(allowlist.length).toBeGreaterThan(0); |
| 95 | + |
| 96 | + // Verify our test allowlist entries are present |
| 97 | + var hasEsmSh = false; |
| 98 | + var hasCdn = false; |
| 99 | + for (var i = 0; i < allowlist.length; i++) { |
| 100 | + if (allowlist[i].indexOf("esm.sh") !== -1) hasEsmSh = true; |
| 101 | + if (allowlist[i].indexOf("cdn.example.com") !== -1) hasCdn = true; |
| 102 | + } |
| 103 | + expect(hasEsmSh).toBe(true); |
| 104 | + expect(hasCdn).toBe(true); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("URL Allowlist Matching", function() { |
| 109 | + |
| 110 | + it("should match URLs in the allowlist (esm.sh)", function() { |
| 111 | + // esm.sh is in our test allowlist |
| 112 | + var isAllowed = com.tns.Runtime.isRemoteUrlAllowed("https://esm.sh/lodash"); |
| 113 | + expect(isAllowed).toBe(true); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should match URLs in the allowlist (cdn.example.com)", function() { |
| 117 | + // cdn.example.com is in our test allowlist |
| 118 | + var isAllowed = com.tns.Runtime.isRemoteUrlAllowed("https://cdn.example.com/modules/utils.js"); |
| 119 | + expect(isAllowed).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should allow non-allowlisted URLs in debug mode", function() { |
| 123 | + // In debug mode, all URLs should be allowed even if not in allowlist |
| 124 | + var isAllowed = com.tns.Runtime.isRemoteUrlAllowed("https://unknown-domain.com/evil.js"); |
| 125 | + // In debug mode, this returns true because debug bypasses allowlist |
| 126 | + expect(isAllowed).toBe(true); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe("Static Import HTTP Loading", function() { |
| 131 | + |
| 132 | + it("should attempt to load HTTP module in debug mode", function(done) { |
| 133 | + // Use a valid but unreachable URL to test the HTTP loading path |
| 134 | + // In debug mode, security check passes, then network fails |
| 135 | + import("http://10.255.255.1:5173/nonexistent-module.js").then(function(module) { |
| 136 | + // Unexpected success - but OK if it happens |
| 137 | + expect(module).toBeDefined(); |
| 138 | + done(); |
| 139 | + }).catch(function(error) { |
| 140 | + // Should be network error, not security error |
| 141 | + var message = error.message || String(error); |
| 142 | + expect(message).not.toContain("not allowed in production"); |
| 143 | + expect(message).not.toContain("security"); |
| 144 | + // Network errors contain phrases like "fetch", "network", "connect", etc |
| 145 | + done(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe("Dynamic Import HTTP Loading", function() { |
| 151 | + // Test dynamic imports (ImportModuleDynamicallyCallback path) |
| 152 | + |
| 153 | + it("should attempt to load HTTPS module dynamically in debug mode", function(done) { |
| 154 | + var url = "https://10.255.255.1:5173/dynamic-module.js"; |
| 155 | + |
| 156 | + import(url).then(function(module) { |
| 157 | + expect(module).toBeDefined(); |
| 158 | + done(); |
| 159 | + }).catch(function(error) { |
| 160 | + var message = error.message || String(error); |
| 161 | + // In debug mode, should NOT be a security error |
| 162 | + expect(message).not.toContain("not allowed in production"); |
| 163 | + expect(message).not.toContain("remoteModuleAllowlist"); |
| 164 | + done(); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments