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

SONARKT-569 Modify rule S4830: add support for WebViews #4673

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/header_names/allowed_framework_names.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* libxml2
// Java
* Android
* Android WebView
* Apache Commons
* Apache Commons
* Apache Commons Email
Expand All @@ -47,7 +48,6 @@
* Legacy Mongo Java API
* OkHttp
* Realm
* Java Cryptography Extension
* Apache HttpClient
* Couchbase
* SAX
Expand Down
44 changes: 44 additions & 0 deletions rules/S4830/kotlin/how-to-fix-it/android-webview.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
== How to fix it in Android WebView

=== Code examples

include::../../common/fix/code-rationale.adoc[]

The certificate validation gets disabled by overriding the `onReceivedSslError` method of the `WebViewClient` class with an implementation that calls `SslErrorHandler.proceed()` unconditionally, and that never calls `SslErrorHandler.cancel()`.

This means that a certificate initially rejected by the system will be accepted by the `WebViewClient`, regardless of its origin.

=== Noncompliant code example

[source,kotlin,diff-id=101,diff-type=noncompliant]
----
class MyWebViewClient : WebViewClient() {
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) =
handler.proceed() // Noncompliant
}
----

=== Compliant solution

You need to implement a validation of the server certificate received in the `SslErrorHandler` object, calling `proceed` and `cancel` appropriately.

[source,kotlin,diff-id=101,diff-type=compliant]
----
class MyWebViewClient : WebViewClient() {
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
if (error.certificate.isServerCertificateValid()) {
handler.proceed()
} else {
handler.cancel()
}
}

private fun SslCertificate.isServerCertificateValid(): Boolean {
// Implement the server certificate validation logic here ...
}
}
----

=== How does this work?

include::../../common/fix/validation.adoc[]
6 changes: 5 additions & 1 deletion rules/S4830/kotlin/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ include::../impact.adoc[]

include::how-to-fix-it/java-cryptography-extension.adoc[]

include::how-to-fix-it/android-webview.adoc[]

== Resources

include::../common/resources/standards-mobile.adoc[]

* https://wiki.sei.cmu.edu/confluence/display/java/MSC61-J.+Do+not+use+insecure+or+weak+cryptographic+algorithms
* CERT - https://wiki.sei.cmu.edu/confluence/display/java/MSC61-J.+Do+not+use+insecure+or+weak+cryptographic+algorithms
* Google Support - https://support.google.com/faqs/answer/7071387?hl=en[How to address WebView SSL Error Handler alerts in your apps]
* Android Documentation - https://developer.android.com/reference/android/webkit/WebViewClient?hl=en#onReceivedSslError(android.webkit.WebView,%20android.webkit.SslErrorHandler,%20android.net.http.SslError)[WebViewClient.onReceivedSslError] method
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also link https://support.google.com/faqs/answer/7071387?hl=en which is Google's official remediation


ifdef::env-github,rspecator-view[]

Expand Down