Skip to content

Commit 6e10fcf

Browse files
Jami CogswellJami Cogswell
authored andcommitted
added predicates in the AndroidManifest library and adjusted tests
1 parent 229324f commit 6e10fcf

File tree

8 files changed

+94
-13
lines changed

8 files changed

+94
-13
lines changed

java/ql/lib/semmle/code/xml/AndroidManifest.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ class AndroidManifestXmlFile extends XMLFile {
1818
* Gets the top-level `<manifest>` element in this Android manifest file.
1919
*/
2020
AndroidManifestXmlElement getManifestElement() { result = this.getAChild() }
21+
22+
/**
23+
* Holds if this Android manifest file is located in a build directory.
24+
*/
25+
predicate isInBuildDirectory() {
26+
exists(AndroidManifestXmlFile file |
27+
file = this.getFile() and
28+
file.getRelativePath().matches("%build%")
29+
)
30+
}
2131
}
2232

2333
/**
@@ -51,6 +61,23 @@ class AndroidApplicationXmlElement extends XMLElement {
5161
* Gets a component child element of this `<application>` element.
5262
*/
5363
AndroidComponentXmlElement getAComponentElement() { result = this.getAChild() }
64+
65+
/**
66+
* Holds if this application element has the attribute `android:debuggable` set to `true`.
67+
*/
68+
predicate isDebuggable() {
69+
exists(AndroidXmlAttribute attr |
70+
this.getAnAttribute() = attr and
71+
attr.getName() = "debuggable" and
72+
attr.getValue() = "true"
73+
)
74+
}
75+
76+
/**
77+
* Overrides the getFile() predicate of the XMLElement class to get the
78+
* AndroidManifest.xml file itself.
79+
*/
80+
override AndroidManifestXmlFile getFile() { result = super.getFile() }
5481
}
5582

5683
/**

java/ql/src/Security/CWE/CWE-489/DebuggableAttributeEnabled.ql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import java
1414
import semmle.code.xml.AndroidManifest
1515

16-
from AndroidXmlAttribute androidXmlAttr
16+
from AndroidApplicationXmlElement androidAppElem
1717
where
18-
androidXmlAttr.getName() = "debuggable" and
19-
androidXmlAttr.getValue() = "true" and
20-
not androidXmlAttr.getLocation().getFile().getRelativePath().matches("%build%")
21-
select androidXmlAttr, "The 'android:debuggable' attribute is enabled."
18+
androidAppElem.isDebuggable() and
19+
not androidAppElem.getFile().isInBuildDirectory()
20+
select androidAppElem.getAttribute("debuggable"), "The 'android:debuggable' attribute is enabled."

java/ql/test/query-tests/security/CWE-489/DebuggableAttributeEnabledTest.ql

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import java
22
import semmle.code.xml.AndroidManifest
33
import TestUtilities.InlineExpectationsTest
44

5-
class DebuggableAttributeTrueTest extends InlineExpectationsTest {
6-
DebuggableAttributeTrueTest() { this = "DebuggableAttributeEnabledTest" }
5+
class DebuggableAttributeEnabledTest extends InlineExpectationsTest {
6+
DebuggableAttributeEnabledTest() { this = "DebuggableAttributeEnabledTest" }
77

88
override string getARelevantTag() { result = "hasDebuggableAttributeEnabled" }
99

1010
override predicate hasActualResult(Location location, string element, string tag, string value) {
1111
tag = "hasDebuggableAttributeEnabled" and
12-
exists(AndroidXmlAttribute androidXmlAttr |
13-
androidXmlAttr.getName() = "debuggable" and
14-
androidXmlAttr.getValue() = "true" and
15-
not androidXmlAttr.getLocation().getFile().getRelativePath().matches("%build%")
12+
exists(AndroidApplicationXmlElement androidAppElem |
13+
androidAppElem.isDebuggable() and
14+
not androidAppElem.getFile().isInBuildDirectory()
1615
|
17-
androidXmlAttr.getLocation() = location and
18-
element = androidXmlAttr.toString() and
16+
androidAppElem.getAttribute("debuggable").getLocation() = location and
17+
element = androidAppElem.getAttribute("debuggable").toString() and
1918
value = ""
2019
)
2120
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.happybirthday">
5+
6+
<!-- $ hasDebuggableAttributeEnabled --> <application
7+
android:debuggable="true"
8+
android:allowBackup="true"
9+
android:dataExtractionRules="@xml/data_extraction_rules"
10+
android:fullBackupContent="@xml/backup_rules"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.HappyBirthday"
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.happybirthday">
5+
6+
<!-- Safe: manifest file located in build directory --> <application
7+
android:debuggable="true"
8+
android:allowBackup="true"
9+
android:dataExtractionRules="@xml/data_extraction_rules"
10+
android:fullBackupContent="@xml/backup_rules"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.HappyBirthday"
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>

0 commit comments

Comments
 (0)