Skip to content

Commit f8ccbcb

Browse files
Add qhelp
1 parent a2245bb commit f8ccbcb

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
If the <code>onReceivedSslError</code> method of an Android <code>WebViewClient</code> always calls <code>proceed</code> on the given <code>SslErrorHandler</code>, it trusts any certificate.
8+
This allows an attacker to perform a machine-in-the-middle attack against the application, therefore breaking any security Transport Layer Security (TLS) gives.
9+
</p>
10+
11+
<p>
12+
An attack might look like this:
13+
</p>
14+
15+
<ol>
16+
<li>The vulnerable application connects to <code>https://example.com</code>.</li>
17+
<li>The attacker intercepts this connection and presents a valid, self-signed certificate for <code>https://example.com</code>.</li>
18+
<li>The vulnerable application calls the <code>onReceivedSslError</code> method to check whether it should trust the certificate.</li>
19+
<li>The <code>onReceivedSslError</code> method of your <code>WebViewClient</code> calls <code>SslErrorHandler.proceed</code>.</li>
20+
<li>The vulnerable application accepts the certificate and proceeds with the connection since your <code>WevViewClient</code> trusted it by proceeding.</li>
21+
<li>The attacker can now read the data your application sends to <code>https://example.com</code> and/or alter its replies while the application thinks the connection is secure.</li>
22+
</ol>
23+
</overview>
24+
25+
<recommendation>
26+
<p>
27+
Do not use a call <code>SslerrorHandler.proceed</code> unconditonally.
28+
If you have to use a self-signed certificate, only accept that certificate, not all certificates.
29+
</p>
30+
31+
</recommendation>
32+
33+
<example>
34+
<p>
35+
In the first (bad) example, the <code>WebViewClient</code> trusts all certificates by always calling <code>SslErrorHandler.proceed</code>.
36+
In the second (good) example, only certificates signed by a certain public key are accepted.
37+
</p>
38+
<sample src="ImproperWebViewCertificateValidation.java" />
39+
</example>
40+
41+
<references>
42+
</references>
43+
</qhelp>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Bad extends WebViewClient {
2+
// BAD: All certificates are trusted.
3+
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { // $hasResult
4+
handler.proceed();
5+
}
6+
}
7+
8+
class Good extends WebViewClient {
9+
PublicKey myPubKey = ...;
10+
11+
// GOOD: Only certificates signed by a certain public key are trusted.
12+
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { // $hasResult
13+
try {
14+
X509Certificate cert = error.getCertificate().getX509Certificate();
15+
cert.verify(this.myPubKey);
16+
handler.proceed();
17+
}
18+
catch (CertificateException|NoSuchAlgorithmException|InvalidKeyException|NoSuchProviderException|SignatureException e) {
19+
handler.cancel();
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)