Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Improve Android app detection #16914

Merged
merged 7 commits into from
Jul 16, 2024

Conversation

owen-mc
Copy link
Contributor

@owen-mc owen-mc commented Jul 5, 2024

This change improves the detection of Android apps in the CodeQL Java standard library. It moves the detection of Android apps to one place and updates the detection to be more accurate. Previously we just required the presence of a AndroidManifest.xml file anywhere in the project. This PR improves that in two ways:

  • the AndroidManifest.xml file must define an activity, service or content provider (so it corresponds to an android application rather than a library);
  • we only consider files in an android application if they are under a folder that contains such an AndroidManifest.xml.

These are the results for "Android missing certificate pinning" which are removed by this change. I have spot-checked them and they seemed valid.

@owen-mc owen-mc force-pushed the java/android-app-detection branch from 240a2b0 to b10b016 Compare July 6, 2024 23:09
@owen-mc owen-mc force-pushed the java/android-app-detection branch from b10b016 to e2a6358 Compare July 6, 2024 23:25
@owen-mc owen-mc marked this pull request as ready for review July 7, 2024 22:09
@owen-mc owen-mc requested a review from a team as a code owner July 7, 2024 22:09
smowton
smowton previously approved these changes Jul 12, 2024
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested tidying, otherwise looks good to me

---
category: deprecated
---
* The predicate `isAndroid` from the module `semmle.code.java.security.AndroidCertificatePinningQuery` has been deprecated. Use `semmle.code.java.frameworks.android.Android::inAndroidApplication(File file)` instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The predicate `isAndroid` from the module `semmle.code.java.security.AndroidCertificatePinningQuery` has been deprecated. Use `semmle.code.java.frameworks.android.Android::inAndroidApplication(File file)` instead.
* The predicate `isAndroid` from the module `semmle.code.java.security.AndroidCertificatePinningQuery` has been deprecated. Use `semmle.code.java.frameworks.android.Android::inAndroidApplication(File)` instead.

Comment on lines 8 to 26
/**
* There is an android manifest file which defines an activity, service or
* content provider (so it corresponds to an android application rather than a
* library), and `file` is in a subfolder of the folder that contains it.
*/
predicate inAndroidApplication(File file) {
file.isSourceFile() and
exists(AndroidComponentXmlElement acxe, AndroidManifestXmlFile amxf |
amxf.getManifestElement().getApplicationElement().getAComponentElement() = acxe and
(
acxe instanceof AndroidActivityXmlElement or
acxe instanceof AndroidServiceXmlElement or
acxe instanceof AndroidProviderXmlElement
)
|
file.getParentContainer+() = amxf.getParentContainer()
)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* There is an android manifest file which defines an activity, service or
* content provider (so it corresponds to an android application rather than a
* library), and `file` is in a subfolder of the folder that contains it.
*/
predicate inAndroidApplication(File file) {
file.isSourceFile() and
exists(AndroidComponentXmlElement acxe, AndroidManifestXmlFile amxf |
amxf.getManifestElement().getApplicationElement().getAComponentElement() = acxe and
(
acxe instanceof AndroidActivityXmlElement or
acxe instanceof AndroidServiceXmlElement or
acxe instanceof AndroidProviderXmlElement
)
|
file.getParentContainer+() = amxf.getParentContainer()
)
}
/**
* Holds if `amxf` defines at least one activity, service or contest provider,
* and so it corresponds to an android application rather than a library.
*/
private predicate definesAndroidApplication(AndroidManifestXmlFile amxf) {
exists(AndroidComponentXmlElement acxe |
amxf.getManifestElement().getApplicationElement().getAComponentElement() = acxe and
(
acxe instanceof AndroidActivityXmlElement or
acxe instanceof AndroidServiceXmlElement or
acxe instanceof AndroidProviderXmlElement
)
)
}
/**
* Holds if in `file`'s directory or some parent directory there is an `AndroidManifestXmlFile`
* that defines at least one activity, service or contest provider, suggesting this file is
* part of an android application.
*/
predicate inAndroidApplication(File file) {
file.isSourceFile() and
exists(AndroidManifestXmlFile amxf, Folder amxfDir |
definesAndroidApplication(amxf) and amxfDir = amxf.getParentContainer()
|
file.getParentContainer+() = amxfDir
)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(rewords the comment to use Holds if, and splits it into two predicates)

@owen-mc
Copy link
Contributor Author

owen-mc commented Jul 13, 2024

@smowton Thanks, I've accepted those, except that I made definesAndroidApplication a (public) member predicate of AndroidManifestXmlFile. I feel like that functionality might be useful in future, and belongs more in that class.

smowton
smowton previously approved these changes Jul 15, 2024
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (one typo)

java/ql/lib/semmle/code/xml/AndroidManifest.qll Outdated Show resolved Hide resolved
Co-authored-by: Chris Smowton <smowton@github.com>
@owen-mc owen-mc merged commit e2356d9 into github:main Jul 16, 2024
18 checks passed
@owen-mc owen-mc deleted the java/android-app-detection branch July 16, 2024 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants