forked from jitsi/jitsi-meet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
172 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
android/sdk/src/main/java/org/jitsi/meet/sdk/JavaScriptSandboxModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright @ 2019-present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jitsi.meet.sdk; | ||
|
||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
import com.squareup.duktape.Duktape; | ||
|
||
@ReactModule(name = JavaScriptSandboxModule.NAME) | ||
class JavaScriptSandboxModule extends ReactContextBaseJavaModule { | ||
public static final String NAME = "JavaScriptSandbox"; | ||
|
||
public JavaScriptSandboxModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
/** | ||
* Evaluates the given code in a Duktape VM. | ||
* @param code - The code that needs to evaluated. | ||
* @param promise - Resolved with the output in case of success or rejected with an exception | ||
* in case of failure. | ||
*/ | ||
@ReactMethod | ||
public void evaluate(String code, Promise promise) { | ||
Duktape vm = Duktape.create(); | ||
try { | ||
Object res = vm.evaluate(code); | ||
promise.resolve(res.toString()); | ||
} catch (Throwable tr) { | ||
promise.reject(tr); | ||
} finally { | ||
vm.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright @ 2019-present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@import JavaScriptCore; | ||
|
||
#import <React/RCTBridgeModule.h> | ||
|
||
|
||
@interface JavaScriptSandbox : NSObject<RCTBridgeModule> | ||
@end | ||
|
||
@implementation JavaScriptSandbox | ||
|
||
RCT_EXPORT_MODULE(); | ||
|
||
+ (BOOL)requiresMainQueueSetup { | ||
return NO; | ||
} | ||
|
||
#pragma mark - Exported methods | ||
|
||
RCT_EXPORT_METHOD(evaluate:(NSString *)code | ||
resolve:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) { | ||
__block BOOL hasError = NO; | ||
JSContext *ctx = [[JSContext alloc] init]; | ||
ctx.exceptionHandler = ^(JSContext *context, JSValue *exception) { | ||
hasError = YES; | ||
reject(@"evaluate", [exception toString], nil); | ||
}; | ||
JSValue *ret = [ctx evaluateScript:code]; | ||
if (!hasError) { | ||
NSString *result = [ret toString]; | ||
if (result == nil) { | ||
reject(@"evaluate", @"Error in string coercion", nil); | ||
} else { | ||
resolve(result); | ||
} | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @flow | ||
|
||
import { NativeModules } from 'react-native'; | ||
|
||
import { loadScript } from '../util'; | ||
import logger from './logger'; | ||
|
||
export * from './functions.any'; | ||
|
||
const { JavaScriptSandbox } = NativeModules; | ||
|
||
/** | ||
* Loads config.js from a specific remote server. | ||
* | ||
* @param {string} url - The URL to load. | ||
* @returns {Promise<Object>} | ||
*/ | ||
export async function loadConfig(url: string): Promise<Object> { | ||
try { | ||
const configTxt = await loadScript(url, 2.5 * 1000 /* Timeout in ms */, true /* skipeval */); | ||
const configJson = await JavaScriptSandbox.evaluate(`${configTxt}\nJSON.stringify(config);`); | ||
const config = JSON.parse(configJson); | ||
|
||
if (typeof config !== 'object') { | ||
throw new Error('config is not an object'); | ||
} | ||
|
||
logger.info(`Config loaded from ${url}`); | ||
|
||
return config; | ||
} catch (err) { | ||
logger.error(`Failed to load config from ${url}`, err); | ||
|
||
throw err; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @flow | ||
|
||
export * from './functions.any'; | ||
|
||
/** | ||
* Loads config.js from a specific remote server. | ||
* | ||
* @param {string} url - The URL to load. | ||
* @returns {Promise<Object>} | ||
*/ | ||
export async function loadConfig(url: string): Promise<Object> { // eslint-disable-line no-unused-vars | ||
// Return "the config.js file" from the global scope - that is how the | ||
// Web app on both the client and the server was implemented before the | ||
// React Native app was even conceived. | ||
return window.config; | ||
} |