Skip to content

Commit f0e9b76

Browse files
Apply suggestions from code review
Co-authored-by: Felicity Chapman <[email protected]>
1 parent 65b6c16 commit f0e9b76

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
lgtm,codescanning
2-
* Two new queries, "Android Fragment injection" (`java/android/fragment-injection`) and "Android Fragment injection in PreferenceActivity" (`java/android/fragment-injection-preference-activity`) have been added.
3-
These queries find exported Android Activities that instantiate and host Fragments created from user-provided data, which can lead to access control bypass and exposes the application to unintended effects.
2+
* Two new queries, "Android fragment injection" (`java/android/fragment-injection`) and "Android fragment injection in PreferenceActivity" (`java/android/fragment-injection-preference-activity`) have been added.
3+
These queries find exported Android activities that instantiate and host fragments created from user-provided data. Such activities are vulnerable to access control bypass and expose the Android application to unintended effects.

java/ql/lib/semmle/code/java/frameworks/android/Fragment.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** Provides classes and predicates related to Android Fragments. */
1+
/** Provides classes and predicates to track Android fragments. */
22

33
import java
44

java/ql/lib/semmle/code/java/security/FragmentInjection.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class IsValidFragmentMethod extends Method {
3535

3636
/**
3737
* A sink for Fragment injection vulnerabilities,
38-
* that is, method calls that dynamically add Fragments to Activities.
38+
* that is, method calls that dynamically add fragments to activities.
3939
*/
4040
abstract class FragmentInjectionSink extends DataFlow::Node { }
4141

java/ql/lib/semmle/code/java/security/FragmentInjectionQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import semmle.code.java.security.FragmentInjection
77

88
/**
99
* A taint-tracking configuration for unsafe user input
10-
* that is used to create Android Fragments dinamically.
10+
* that is used to create Android fragments dynamically.
1111
*/
1212
class FragmentInjectionTaintConf extends TaintTracking::Configuration {
1313
FragmentInjectionTaintConf() { this = "FragmentInjectionTaintConf" }

java/ql/src/Security/CWE/CWE-470/FragmentInjection.inc.qhelp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,43 @@
33

44
<overview>
55
<p>
6-
Fragments are reusable parts of an Android application's user interface.
7-
Even though a Fragment controls its own lifecycle and layout and handles its input events,
8-
it cannot exist on its own: it must be hosted either by an Activity or another fragment.
9-
This means that a Fragment will be accessible by third-party applications (that is, exported)
10-
only if its hosting Activity is itself exported.
6+
When fragments are instantiated with externally provided names, this exposes any exported activity that dynamically
7+
creates and hosts the fragment to fragment injection. A malicious application could provide the
8+
name of an arbitrary fragment, even one not designed to be externally accessible, and inject it into the activity.
9+
Thus, effectively bypassing access controls and exposing the application to unintended effects.
1110
</p>
1211
<p>
13-
If an exported Activity dinamically creates and hosts Fragments instantiated with externally
14-
provided names, a malicious application could provide the name of an arbitrary Fragment, even
15-
one not designed to be externally accessible, and inject it into the Activity, effectively
16-
bypassing access controls and exposing the application to unintended effects.
12+
Fragments are reusable parts of an Android application's user interface.
13+
Even though a fragment controls its own lifecycle and layout, and handles its input events,
14+
it cannot exist on its own: it must be hosted either by an activity or another fragment.
15+
This means that, normally, a fragment will be accessible by third-party applications (that is, exported)
16+
only if its hosting activity is itself exported.
1717
</p>
1818
</overview>
1919

2020
<recommendation>
2121
<p>
22-
In general, do not instantiate classes (including Fragments) with user-provided names
23-
without proper validation.
22+
In general, do not instantiate classes (including fragments) with user-provided names
23+
unless the name has been properly validated.
2424

25-
Also, if an exported Activity is extending the <code>PreferenceActivity</code> class, make sure that
25+
Also, if an exported activity is extending the <code>PreferenceActivity</code> class, make sure that
2626
the <code>isValidFragment</code> method is overriden and only returns <code>true</code> when the provided
27-
<code>fragmentName</code> points to an intended Fragment.
27+
<code>fragmentName</code> points to an intended fragment.
2828
</p>
2929
</recommendation>
3030

3131
<example>
3232
<p>
3333
The following example shows two cases: in the first one, untrusted data is used to instantiate and
34-
add a Fragment to an Activity, while in the second one, a Fragment is safely added with a static name.
34+
add a fragment to an activity, while in the second one, a fragment is safely added with a static name.
3535
</p>
3636
<sample src="FragmentInjection.java" />
3737

3838
<p>
39-
The next example shows two Activities extending <code>PreferenceActivity</code>. The first one overrides
40-
<code>isValidFragment</code>, but it wrongly returns <code>true</code> inconditionally. The second Activity
41-
correctly overrides <code>isValidFragment</code> to only return <code>true</code> when <code>fragmentName</code>
42-
is a trusted Fragment name.
39+
The next example shows two activities that extend <code>PreferenceActivity</code>. The first activity overrides
40+
<code>isValidFragment</code>, but it wrongly returns <code>true</code> unconditionally. The second activity
41+
correctly overrides <code>isValidFragment</code> so that it only returns <code>true</code> when <code>fragmentName</code>
42+
is a trusted fragment name.
4343
</p>
4444
<sample src="FragmentInjectionInPreferenceActivity.java" />
4545
</example>

java/ql/src/Security/CWE/CWE-470/FragmentInjection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class MyActivity extends FragmentActivity {
44
protected void onCreate(Bundle savedInstance) {
55
try {
66
super.onCreate(savedInstance);
7-
// BAD: Fragment instantiated from user input
7+
// BAD: Fragment instantiated from user input without validation
88
{
99
String fName = getIntent().getStringExtra("fragmentName");
1010
getFragmentManager().beginTransaction().replace(com.android.internal.R.id.prefs,

java/ql/src/Security/CWE/CWE-470/FragmentInjection.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* @name Android Fragment injection
3-
* @description Instantiating an Android Fragment from a user-provided value
4-
* may lead to Fragment injection.
2+
* @name Android fragment injection
3+
* @description Instantiating an Android fragment from a user-provided value
4+
* may allow a malicious application to bypass access controls, exposing the application to unintended effects.
55
* @kind path-problem
66
* @problem.severity error
77
* @security-severity 9.8

java/ql/src/Security/CWE/CWE-470/FragmentInjectionInPreferenceActivity.ql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @name Android Fragment injection in PreferenceActivity
3-
* @description An insecure implementation of the isValidFragment method
4-
* of the PreferenceActivity class may lead to Fragment injection.
2+
* @name Android fragment injection in PreferenceActivity
3+
* @description An insecure implementation of the 'isValidFragment' method
4+
* of the 'PreferenceActivity' class may allow a malicious application to bypass access controls,
5+
* exposing the application to unintended effects.
56
* @kind problem
67
* @problem.severity error
78
* @security-severity 9.8

0 commit comments

Comments
 (0)