Java


JAVA.IO.INJ.FRAGMENT : Fragment Injection (Java)

Summary

Tainted data might flow into a fragment execution.

Properties

Class Name Fragment Injection (Java)
Significance security
Mnemonic JAVA.IO.INJ.FRAGMENT
Categories
CWE CWE:470 Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')
OWASP-2017 OWASP-2017:A1 Injection
  OWASP-2017:A5 Broken access control
OWASP-2021 OWASP-2021:A1 Broken access control
  OWASP-2021:A3 Injection
Availability Available for Java only.

Android Only. Warnings of this class will only be reported in Android code: that is, code that uses the Android API.

Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Fragment Injection (Java)"

Example

// UncontrolledFragment1.java 
package com.juliasoft.julia.tests.checks.useOfUncontrolledExternalData;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.webkit.WebView;

public class UncontrolledFragment1 extends PreferenceActivity  // Missing isValidFragment Override (Java) warning issued here
{
  WebView mWebView;
      
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
      final Fragment f = Fragment.instantiate(this, initialFragment, null);
      final FragmentTransaction transaction = getFragmentManager().beginTransaction();
      transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
      transaction.replace(11, f);
      transaction.commitAllowingStateLoss();                   // Fragment Injection (Java) warning issued here
  }
}
// UncontrolledFragment2.java 
package com.juliasoft.julia.tests.checks.useOfUncontrolledExternalData;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.webkit.WebView;

public class UncontrolledFragment2 extends PreferenceActivity
{
  WebView mWebView;
      
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
      if(isValidFragment(initialFragment)) {
          final Fragment f = Fragment.instantiate(this, initialFragment, null);
          final FragmentTransaction transaction = getFragmentManager().beginTransaction();
          transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
          transaction.replace(11, f);
          transaction.commitAllowingStateLoss();
      }
  }

  @Override
  protected boolean isValidFragment(String fragmentName) {
      return true;                                             // Ineffective Cleansing of Fragment Taint (Java) warning issued here
  }
}

The warnings in this example are all relative to external fragments and their validation. The external fragments without or with a wrong validation could lead to security issues. As described in the Android API documentation for isValidFragment(), subclasses of PreferenceActivity should override the isValidFragment() method and verify that the given fragment is a valid type to be attached to this activity.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.