Browse Source

rn: evaluate config.js in a sandboxed environment

We are downloading code off the Internet and executing it on the user's device,
so run it sandboxed to avoid potential bad actors.

Since it's impossible to eval() safely in JS and React Native doesn't offer
something akin to Node's vm module, here we are rolling our own.

On Android it uses the Duktape JavaScript engine and on iOS the builtin
JavaScriptCore engine. The extra JS engine is *only* used for evaluating the
downloaded code and returning a JSON string which is then passed back to RN.
master
Saúl Ibarra Corretgé 5 years ago
parent
commit
4a8f787519

+ 1
- 0
android/sdk/build.gradle View File

@@ -45,6 +45,7 @@ dependencies {
45 45
 
46 46
     implementation 'com.dropbox.core:dropbox-core-sdk:3.0.8'
47 47
     implementation 'com.jakewharton.timber:timber:4.7.1'
48
+    implementation 'com.squareup.duktape:duktape-android:1.3.0'
48 49
 
49 50
     if (!rootProject.ext.libreBuild) {
50 51
         implementation 'com.amplitude:android-sdk:2.14.1'

+ 57
- 0
android/sdk/src/main/java/org/jitsi/meet/sdk/JavaScriptSandboxModule.java View File

@@ -0,0 +1,57 @@
1
+/*
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+package org.jitsi.meet.sdk;
18
+
19
+import com.facebook.react.bridge.Promise;
20
+import com.facebook.react.bridge.ReactApplicationContext;
21
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
22
+import com.facebook.react.bridge.ReactMethod;
23
+import com.facebook.react.module.annotations.ReactModule;
24
+import com.squareup.duktape.Duktape;
25
+
26
+@ReactModule(name = JavaScriptSandboxModule.NAME)
27
+class JavaScriptSandboxModule extends ReactContextBaseJavaModule {
28
+    public static final String NAME = "JavaScriptSandbox";
29
+
30
+    public JavaScriptSandboxModule(ReactApplicationContext reactContext) {
31
+        super(reactContext);
32
+    }
33
+
34
+    /**
35
+     * Evaluates the given code in a Duktape VM.
36
+     * @param code - The code that needs to evaluated.
37
+     * @param promise - Resolved with the output in case of success or rejected with an exception
38
+     *                in case of failure.
39
+     */
40
+    @ReactMethod
41
+    public void evaluate(String code, Promise promise) {
42
+        Duktape vm = Duktape.create();
43
+        try {
44
+            Object res = vm.evaluate(code);
45
+            promise.resolve(res.toString());
46
+        } catch (Throwable tr) {
47
+            promise.reject(tr);
48
+        } finally {
49
+            vm.close();
50
+        }
51
+    }
52
+
53
+    @Override
54
+    public String getName() {
55
+        return NAME;
56
+    }
57
+}

+ 1
- 0
android/sdk/src/main/java/org/jitsi/meet/sdk/ReactInstanceManagerHolder.java View File

@@ -67,6 +67,7 @@ class ReactInstanceManagerHolder {
67 67
                 new AudioModeModule(reactContext),
68 68
                 new DropboxModule(reactContext),
69 69
                 new ExternalAPIModule(reactContext),
70
+                new JavaScriptSandboxModule(reactContext),
70 71
                 new LocaleDetector(reactContext),
71 72
                 new LogBridgeModule(reactContext),
72 73
                 new PictureInPictureModule(reactContext),

+ 6
- 10
ios/sdk/sdk.xcodeproj/project.pbxproj View File

@@ -42,6 +42,7 @@
42 42
 		C69EFA0E209A0F660027712B /* JMCallKitListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69EFA0B209A0F660027712B /* JMCallKitListener.swift */; };
43 43
 		C6A34261204EF76800E062DD /* DragGestureController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6A3425E204EF76800E062DD /* DragGestureController.swift */; };
44 44
 		C6CC49AF207412CF000DFA42 /* PiPViewCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CC49AE207412CF000DFA42 /* PiPViewCoordinator.swift */; };
45
+		DE438CDA2350934700DD541D /* JavaScriptSandbox.m in Sources */ = {isa = PBXBuildFile; fileRef = DE438CD82350934700DD541D /* JavaScriptSandbox.m */; };
45 46
 		DE65AACA2317FFCD00290BEC /* LogUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = DE65AAC92317FFCD00290BEC /* LogUtils.h */; };
46 47
 		DE65AACC2318028300290BEC /* JitsiMeetBaseLogHandler+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = DE65AACB2318028300290BEC /* JitsiMeetBaseLogHandler+Private.h */; };
47 48
 		DE762DB422AFDE76000DEBD6 /* JitsiMeetUserInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = DE762DB322AFDE76000DEBD6 /* JitsiMeetUserInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -104,6 +105,7 @@
104 105
 		C6A3425E204EF76800E062DD /* DragGestureController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DragGestureController.swift; sourceTree = "<group>"; };
105 106
 		C6CC49AE207412CF000DFA42 /* PiPViewCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PiPViewCoordinator.swift; sourceTree = "<group>"; };
106 107
 		C6F99C13204DB63D0001F710 /* JitsiMeetView+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "JitsiMeetView+Private.h"; sourceTree = "<group>"; };
108
+		DE438CD82350934700DD541D /* JavaScriptSandbox.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JavaScriptSandbox.m; sourceTree = "<group>"; };
107 109
 		DE65AAC92317FFCD00290BEC /* LogUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LogUtils.h; sourceTree = "<group>"; };
108 110
 		DE65AACB2318028300290BEC /* JitsiMeetBaseLogHandler+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "JitsiMeetBaseLogHandler+Private.h"; sourceTree = "<group>"; };
109 111
 		DE762DB322AFDE76000DEBD6 /* JitsiMeetUserInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JitsiMeetUserInfo.h; sourceTree = "<group>"; };
@@ -190,6 +192,7 @@
190 192
 				A4A934E7212F3AB8001E9388 /* dropbox */,
191 193
 				0BA13D301EE83FF8007BEF7F /* ExternalAPI.m */,
192 194
 				0BD906E91EC0C00300C8C18E /* Info.plist */,
195
+				DE438CD82350934700DD541D /* JavaScriptSandbox.m */,
193 196
 				0BD906E81EC0C00300C8C18E /* JitsiMeet.h */,
194 197
 				DEFE535821FB311F00011A3A /* JitsiMeet+Private.h */,
195 198
 				DEFE535321FB1BF800011A3A /* JitsiMeet.m */,
@@ -493,6 +496,7 @@
493 496
 				C69EFA0E209A0F660027712B /* JMCallKitListener.swift in Sources */,
494 497
 				0B412F191EDEC65D00B1A0A6 /* JitsiMeetView.m in Sources */,
495 498
 				DEFE535421FB1BF800011A3A /* JitsiMeet.m in Sources */,
499
+				DE438CDA2350934700DD541D /* JavaScriptSandbox.m in Sources */,
496 500
 			);
497 501
 			runOnlyForDeploymentPostprocessing = 0;
498 502
 		};
@@ -635,11 +639,7 @@
635 639
 				INFOPLIST_FILE = src/Info.plist;
636 640
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
637 641
 				IPHONEOS_DEPLOYMENT_TARGET = 10.0;
638
-				LD_RUNPATH_SEARCH_PATHS = (
639
-					"$(inherited)",
640
-					"@executable_path/Frameworks",
641
-					"@loader_path/Frameworks",
642
-				);
642
+				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
643 643
 				PRODUCT_BUNDLE_IDENTIFIER = org.jitsi.JitsiMeetSDK.ios;
644 644
 				PRODUCT_NAME = "$(TARGET_NAME)";
645 645
 				PROVISIONING_PROFILE_SPECIFIER = "";
@@ -670,11 +670,7 @@
670 670
 				INFOPLIST_FILE = src/Info.plist;
671 671
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
672 672
 				IPHONEOS_DEPLOYMENT_TARGET = 10.0;
673
-				LD_RUNPATH_SEARCH_PATHS = (
674
-					"$(inherited)",
675
-					"@executable_path/Frameworks",
676
-					"@loader_path/Frameworks",
677
-				);
673
+				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
678 674
 				PRODUCT_BUNDLE_IDENTIFIER = org.jitsi.JitsiMeetSDK.ios;
679 675
 				PRODUCT_NAME = "$(TARGET_NAME)";
680 676
 				PROVISIONING_PROFILE_SPECIFIER = "";

+ 55
- 0
ios/sdk/src/JavaScriptSandbox.m View File

@@ -0,0 +1,55 @@
1
+/*
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+@import JavaScriptCore;
18
+
19
+#import <React/RCTBridgeModule.h>
20
+
21
+
22
+@interface JavaScriptSandbox : NSObject<RCTBridgeModule>
23
+@end
24
+
25
+@implementation JavaScriptSandbox
26
+
27
+RCT_EXPORT_MODULE();
28
+
29
++ (BOOL)requiresMainQueueSetup {
30
+    return NO;
31
+}
32
+
33
+#pragma mark - Exported methods
34
+
35
+RCT_EXPORT_METHOD(evaluate:(NSString *)code
36
+                  resolve:(RCTPromiseResolveBlock)resolve
37
+                   reject:(RCTPromiseRejectBlock)reject) {
38
+    __block BOOL hasError = NO;
39
+    JSContext *ctx = [[JSContext alloc] init];
40
+    ctx.exceptionHandler = ^(JSContext *context, JSValue *exception) {
41
+        hasError = YES;
42
+        reject(@"evaluate", [exception toString], nil);
43
+    };
44
+    JSValue *ret = [ctx evaluateScript:code];
45
+    if (!hasError) {
46
+        NSString *result = [ret toString];
47
+        if (result == nil) {
48
+            reject(@"evaluate", @"Error in string coercion", nil);
49
+        } else {
50
+            resolve(result);
51
+        }
52
+    }
53
+}
54
+
55
+@end

react/features/base/lib-jitsi-meet/functions.js → react/features/base/lib-jitsi-meet/functions.any.js View File

@@ -1,12 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import { toState } from '../redux';
4
-import { loadScript } from '../util';
5
-
6 4
 import JitsiMeetJS from './_';
7
-import logger from './logger';
8
-
9
-declare var APP: Object;
10 5
 
11 6
 const JitsiConferenceErrors = JitsiMeetJS.errors.conference;
12 7
 const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
@@ -97,42 +92,3 @@ export function isFatalJitsiConnectionError(error: Object | string) {
97 92
             || error === JitsiConnectionErrors.OTHER_ERROR
98 93
             || error === JitsiConnectionErrors.SERVER_ERROR);
99 94
 }
100
-
101
-/**
102
- * Loads config.js from a specific remote server.
103
- *
104
- * @param {string} url - The URL to load.
105
- * @returns {Promise<Object>}
106
- */
107
-export function loadConfig(url: string): Promise<Object> {
108
-    let promise;
109
-
110
-    if (typeof APP === 'undefined') {
111
-        promise
112
-            = loadScript(url, 2.5 * 1000 /* Timeout in ms */)
113
-                .then(() => {
114
-                    const { config } = window;
115
-
116
-                    // We don't want to pollute the global scope.
117
-                    window.config = undefined;
118
-
119
-                    if (typeof config !== 'object') {
120
-                        throw new Error('window.config is not an object');
121
-                    }
122
-
123
-                    return config;
124
-                })
125
-                .catch(err => {
126
-                    logger.error(`Failed to load config from ${url}`, err);
127
-
128
-                    throw err;
129
-                });
130
-    } else {
131
-        // Return "the config.js file" from the global scope - that is how the
132
-        // Web app on both the client and the server was implemented before the
133
-        // React Native app was even conceived.
134
-        promise = Promise.resolve(window.config);
135
-    }
136
-
137
-    return promise;
138
-}

+ 36
- 0
react/features/base/lib-jitsi-meet/functions.native.js View File

@@ -0,0 +1,36 @@
1
+// @flow
2
+
3
+import { NativeModules } from 'react-native';
4
+
5
+import { loadScript } from '../util';
6
+import logger from './logger';
7
+
8
+export * from './functions.any';
9
+
10
+const { JavaScriptSandbox } = NativeModules;
11
+
12
+/**
13
+ * Loads config.js from a specific remote server.
14
+ *
15
+ * @param {string} url - The URL to load.
16
+ * @returns {Promise<Object>}
17
+ */
18
+export async function loadConfig(url: string): Promise<Object> {
19
+    try {
20
+        const configTxt = await loadScript(url, 2.5 * 1000 /* Timeout in ms */, true /* skipeval */);
21
+        const configJson = await JavaScriptSandbox.evaluate(`${configTxt}\nJSON.stringify(config);`);
22
+        const config = JSON.parse(configJson);
23
+
24
+        if (typeof config !== 'object') {
25
+            throw new Error('config is not an object');
26
+        }
27
+
28
+        logger.info(`Config loaded from ${url}`);
29
+
30
+        return config;
31
+    } catch (err) {
32
+        logger.error(`Failed to load config from ${url}`, err);
33
+
34
+        throw err;
35
+    }
36
+}

+ 16
- 0
react/features/base/lib-jitsi-meet/functions.web.js View File

@@ -0,0 +1,16 @@
1
+// @flow
2
+
3
+export * from './functions.any';
4
+
5
+/**
6
+ * Loads config.js from a specific remote server.
7
+ *
8
+ * @param {string} url - The URL to load.
9
+ * @returns {Promise<Object>}
10
+ */
11
+export async function loadConfig(url: string): Promise<Object> { // eslint-disable-line no-unused-vars
12
+    // Return "the config.js file" from the global scope - that is how the
13
+    // Web app on both the client and the server was implemented before the
14
+    // React Native app was even conceived.
15
+    return window.config;
16
+}

Loading…
Cancel
Save