Exposed Dangerous Method or Function

The software provides an Applications Programming Interface (API) or similar interface for interaction with external actors, but the interface includes a dangerous method or function that is not properly restricted.


Description

This weakness can lead to a wide variety of resultant weaknesses, depending on the behavior of the exposed method. It can apply to any number of technologies and approaches, such as ActiveX controls, Java functions, IOCTLs, and so on.

The exposure can occur in a few different ways:

) The function/method was never intended to be exposed to outside actors.

) The function/method was only intended to be accessible to a limited set of actors, such as Internet-based access from a single web site.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

In the following Java example the method removeDatabase will delete the database with the name specified in the input parameter.

public void removeDatabase(String databaseName) {

  try {

    Statement stmt = conn.createStatement();
    stmt.execute("DROP DATABASE " + databaseName);


  } catch (SQLException ex) {...}

}

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.

private void removeDatabase(String databaseName) {

  try {

    Statement stmt = conn.createStatement();
    stmt.execute("DROP DATABASE " + databaseName);


  } catch (SQLException ex) {...}
  }

Example Two

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:

// Android
@Override
public 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;
    }
  }

}
// 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;

}

A call into native code can then be initiated by passing parameters within the URL:

window.location = examplescheme://method?parameter=value

Because the application does not check the source, a malicious website loaded within this WebView has the same access to the API as a trusted site.

Example Three

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:

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();
    }

  }

}

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.

For example, if the application has permission to send text messages:

<script>
  userInfoObject.getClass().forName('android.telephony.SmsManager').getMethod('getDefault',null).sendTextMessage(attackNumber, null, attackMessage, null, null);
</script>

This malicious script can use the userInfoObject object to load the SmsManager object and send arbitrary text messages to any recipient.

Example Four

After Android 4.2, only methods annotated with @JavascriptInterface are available in JavaScript, protecting usage of getClass() by default, as in this example:

final class JavaScriptInterface {

  JavaScriptInterface () { }

  @JavascriptInterface
  public String getUserInfo() {
    return currentUser.Info();
  }

}

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:

<script>
  var info = window.userInfoObject.getUserInfo();
  sendUserInfo(info);
</script>

This malicious code within an iframe is able to access the interface object and steal the user's data.

See Also

API / Function Errors

Weaknesses in this category are related to the use of built-in functions or external APIs.

SFP Secondary Cluster: Architecture

This category identifies Software Fault Patterns (SFPs) within the Architecture cluster.

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Weaknesses without Software Fault Patterns

CWE identifiers in this view are weaknesses that do not have associated Software Fault Patterns (SFPs), as covered by the CWE-888 view. As such, they represent gaps in...

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.