Skip to content

Commit 4aed1a1

Browse files
Add test cases; fix handling of recievers declared through xml
1 parent 87f26bf commit 4aed1a1

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class SystemActionName extends Top {
7272

7373
/** Gets the name of the system intent that this expression or attriute represents. */
7474
string getName() { result = name }
75+
76+
override string toString() {
77+
result =
78+
[this.(StringLiteral).toString(), this.(FieldRead).toString(), this.(XMLAttribute).toString()]
79+
}
7580
}
7681

7782
/** A call to `Context.registerReceiver` */
@@ -140,10 +145,10 @@ predicate xmlUnverifiedSystemReceiver(
140145
filter.hasName("intent-filter") and
141146
action.hasName("action") and
142147
filter = rec.getAChild() and
143-
action = rec.getAChild() and
148+
action = filter.getAChild() and
144149
ormty = orm.getDeclaringType() and
145-
rec.getAttribute("android:name").getValue() = ["." + ormty.getName(), ormty.getQualifiedName()] and
146-
action.getAttribute("android:name") = sa
150+
rec.getAttribute("name").getValue() = ["." + ormty.getName(), ormty.getQualifiedName()] and
151+
action.getAttribute("name") = sa
147152
)
148153
}
149154

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test">
2+
<application>
3+
<receiver android:name=".BootReceiverXml">
4+
<intent-filter>
5+
<action android:name="android.intent.action.BOOT_COMPLETED" />
6+
</intent-filter>
7+
</receiver>
8+
</application>
9+
</manifest>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test;
2+
import android.content.Intent;
3+
import android.content.Context;
4+
import android.content.BroadcastReceiver;
5+
6+
class BootReceiverXml extends BroadcastReceiver {
7+
void doStuff(Intent intent) {}
8+
9+
@Override
10+
public void onReceive(Context ctx, Intent intent) { // $hasResult
11+
doStuff(intent);
12+
}
13+
}

java/ql/test/query-tests/security/CWE-925/ImproperIntentVerification.expected

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java
2+
import semmle.code.java.security.ImproperIntentVerificationQuery
3+
import TestUtilities.InlineExpectationsTest
4+
5+
class HasFlowTest extends InlineExpectationsTest {
6+
HasFlowTest() { this = "HasFlowTest" }
7+
8+
override string getARelevantTag() { result = "hasResult" }
9+
10+
override predicate hasActualResult(Location location, string element, string tag, string value) {
11+
tag = "hasResult" and
12+
exists(Method orm | unverifiedSystemReceiver(_, orm, _) |
13+
orm.getLocation() = location and
14+
element = orm.toString() and
15+
value = ""
16+
)
17+
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test;
2+
import android.content.Intent;
3+
import android.content.IntentFilter;
4+
import android.content.Context;
5+
import android.content.BroadcastReceiver;
6+
7+
class ImproperIntentVerificationTest {
8+
static void doStuff(Intent intent) {}
9+
10+
class ShutdownBroadcastReceiver extends BroadcastReceiver {
11+
@Override
12+
public void onReceive(Context ctx, Intent intent) { // $hasResult
13+
doStuff(intent);
14+
}
15+
}
16+
17+
class ShutdownBroadcastReceiverSafe extends BroadcastReceiver {
18+
@Override
19+
public void onReceive(Context ctx, Intent intent) {
20+
if (!intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
21+
return;
22+
}
23+
doStuff(intent);
24+
}
25+
}
26+
27+
void test(Context c) {
28+
c.registerReceiver(new ShutdownBroadcastReceiver(), new IntentFilter(Intent.ACTION_SHUTDOWN));
29+
c.registerReceiver(new ShutdownBroadcastReceiverSafe(), new IntentFilter(Intent.ACTION_SHUTDOWN));
30+
}
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// semmle-extractor-options: --javac-args -cp ${testdir}/../../../stubs/google-android-9.0.0

0 commit comments

Comments
 (0)