In the following Java example the method removeDatabase will delete the database with the name specified in the input parameter.
The method in this example is declared public and therefore is exposed to any class in the application. Deleting a database should be considered a critical operation within an application and access to this potentially dangerous method should be restricted. Within Java this can be accomplished simply by declaring the method private thereby exposing it only to the enclosing class as in the following example.
BadJava
public void removeDatabase(String databaseName) { try { Statement stmt = conn.createStatement();stmt.execute("DROP DATABASE " + databaseName); } catch (SQLException ex) {...} }
In the following Java example the method removeDatabase will delete the database with the name specified in the input parameter.
The method in this example is declared public and therefore is exposed to any class in the application. Deleting a database should be considered a critical operation within an application and access to this potentially dangerous method should be restricted. Within Java this can be accomplished simply by declaring the method private thereby exposing it only to the enclosing class as in the following example.
GoodJava
private void removeDatabase(String databaseName) { try { Statement stmt = conn.createStatement();stmt.execute("DROP DATABASE " + databaseName); } catch (SQLException ex) {...}}
These Android and iOS applications intercept URL loading within a WebView and perform special actions if a particular URL scheme is used, thus allowing the Javascript within the WebView to communicate with the application:
A call into native code can then be initiated by passing parameters within the URL:
BadJava
// Android @Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url){ if (url.substring(0,14).equalsIgnoreCase("examplescheme:")){if(url.substring(14,25).equalsIgnoreCase("getUserInfo")){writeDataToView(view, UserData);return false;}else{return true;}} }
These Android and iOS applications intercept URL loading within a WebView and perform special actions if a particular URL scheme is used, thus allowing the Javascript within the WebView to communicate with the application:
A call into native code can then be initiated by passing parameters within the URL:
BadObjective-C
// iOS -(BOOL) webView:(UIWebView *)exWebView shouldStartLoadWithRequest:(NSURLRequest *)exRequest navigationType:(UIWebViewNavigationType)exNavigationType{ NSURL *URL = [exRequest URL];if ([[URL scheme] isEqualToString:@"exampleScheme"]){ NSString *functionString = [URL resourceSpecifier];if ([functionString hasPrefix:@"specialFunction"]){ // Make data available back in webview. UIWebView *webView = [self writeDataToView:[URL query]]; }return NO; }return YES; }
These Android and iOS applications intercept URL loading within a WebView and perform special actions if a particular URL scheme is used, thus allowing the Javascript within the WebView to communicate with the application:
A call into native code can then be initiated by passing parameters within the URL:
AttackJavaScript
window.location = examplescheme://method?parameter=value
This application uses a WebView to display websites, and creates a Javascript interface to a Java object to allow enhanced functionality on a trusted website:
Before Android 4.2 all methods, including inherited ones, are exposed to Javascript when using addJavascriptInterface(). This means that a malicious website loaded within this WebView can use reflection to acquire a reference to arbitrary Java objects. This will allow the website code to perform any action the parent application is authorized to.
BadJava
public class WebViewGUI extends Activity { WebView mainWebView; public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mainWebView = new WebView(this);mainWebView.getSettings().setJavaScriptEnabled(true);mainWebView.addJavascriptInterface(new JavaScriptInterface(), "userInfoObject");mainWebView.loadUrl("file:///android_asset/www/index.html");setContentView(mainWebView);} final class JavaScriptInterface { JavaScriptInterface () {} public String getUserInfo() {return currentUser.Info();} } }
This application uses a WebView to display websites, and creates a Javascript interface to a Java object to allow enhanced functionality on a trusted website:
Before Android 4.2 all methods, including inherited ones, are exposed to Javascript when using addJavascriptInterface(). This means that a malicious website loaded within this WebView can use reflection to acquire a reference to arbitrary Java objects. This will allow the website code to perform any action the parent application is authorized to.
AttackJavaScript
<script>userInfoObject.getClass().forName('android.telephony.SmsManager').getMethod('getDefault',null).sendTextMessage(attackNumber, null, attackMessage, null, null);</script>
After Android 4.2, only methods annotated with @JavascriptInterface are available in JavaScript, protecting usage of getClass() by default, as in this example:
This code is not vulnerable to the above attack, but still may expose user info to malicious pages loaded in the WebView. Even malicious iframes loaded within a trusted page may access the exposed interface:
BadJava
final class JavaScriptInterface { JavaScriptInterface () { } @JavascriptInterfacepublic String getUserInfo() {return currentUser.Info();} }
After Android 4.2, only methods annotated with @JavascriptInterface are available in JavaScript, protecting usage of getClass() by default, as in this example:
This code is not vulnerable to the above attack, but still may expose user info to malicious pages loaded in the WebView. Even malicious iframes loaded within a trusted page may access the exposed interface:
AttackJavaScript
<script>var info = window.userInfoObject.getUserInfo();sendUserInfo(info);</script>