Skip to content

Commit eb90b1b

Browse files
committed
allow cleartext and introduce preference option disabling HTTP by default but making it configurable by user. for all the users who are in LAN/VPN. closes #2966
1 parent 3f13d55 commit eb90b1b

9 files changed

Lines changed: 51 additions & 16 deletions

File tree

src/Kp2aBusinessLogic/Io/WebDavFileStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace keepass2android.Io
1919
#if !NoNet && !EXCLUDE_JAVAFILESTORAGE
2020
public class WebDavFileStorage: JavaFileStorage
2121
{
22-
public WebDavFileStorage(IKp2aApp app) : base(new Keepass2android.Javafilestorage.WebDavStorage(app.CertificateErrorHandler), app)
22+
public WebDavFileStorage(IKp2aApp app, Context appContext) : base(new Keepass2android.Javafilestorage.WebDavStorage(app.CertificateErrorHandler, appContext), app)
2323
{
2424
}
2525

src/java/JavaFileStorage/app/src/main/java/keepass2android/javafilestorage/WebDavStorage.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55

6-
import android.net.Uri;
76
import android.os.Bundle;
87
import android.preference.PreferenceManager;
98
import android.util.Log;
@@ -15,7 +14,11 @@
1514
import com.burgstaller.okhttp.digest.CachingAuthenticator;
1615
import com.burgstaller.okhttp.digest.DigestAuthenticator;
1716

17+
import okhttp3.Interceptor;
18+
import okhttp3.Response;
19+
import okhttp3.Request;
1820
import java.io.FileNotFoundException;
21+
import java.io.IOException;
1922
import java.io.InputStream;
2023
import java.io.StringReader;
2124
import java.io.UnsupportedEncodingException;
@@ -38,25 +41,43 @@
3841
import javax.net.ssl.TrustManagerFactory;
3942
import javax.net.ssl.X509TrustManager;
4043

41-
import keepass2android.javafilestorage.ConnectionInfo;
4244
import keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier;
4345
import keepass2android.javafilestorage.webdav.DecoratedTrustManager;
4446
import keepass2android.javafilestorage.webdav.PropfindXmlParser;
4547
import keepass2android.javafilestorage.webdav.WebDavUtil;
4648
import okhttp3.MediaType;
4749
import okhttp3.OkHttpClient;
48-
import okhttp3.Request;
4950
import okhttp3.RequestBody;
50-
import okhttp3.Response;
5151
import okhttp3.internal.tls.OkHostnameVerifier;
5252

5353
public class WebDavStorage extends JavaFileStorageBase {
5454

55+
56+
public class CleartextBlockInterceptor implements Interceptor {
57+
private final boolean permitCleartext;
58+
59+
public CleartextBlockInterceptor(Context context) {
60+
this.permitCleartext = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("permit_cleartext_traffic", false);
61+
}
62+
63+
@Override
64+
public Response intercept(Chain chain) throws IOException {
65+
Request request = chain.request();
66+
67+
if (!permitCleartext && request.url().scheme().equals("http")) {
68+
throw new IOException("Cleartext HTTP is disabled by user preference. Go to app settings/File handling if you really want to use HTTP.");
69+
}
70+
71+
return chain.proceed(request);
72+
}
73+
}
74+
5575
private final ICertificateErrorHandler mCertificateErrorHandler;
5676
private Context appContext;
5777

58-
public WebDavStorage(ICertificateErrorHandler certificateErrorHandler)
78+
public WebDavStorage(ICertificateErrorHandler certificateErrorHandler, Context appContext)
5979
{
80+
this.appContext = appContext;
6081

6182
mCertificateErrorHandler = certificateErrorHandler;
6283
}
@@ -174,6 +195,7 @@ private OkHttpClient getClient(ConnectionInfo ci) throws NoSuchAlgorithmExceptio
174195
builder.readTimeout(25, TimeUnit.SECONDS);
175196
builder.writeTimeout(25, TimeUnit.SECONDS);
176197
}
198+
builder.addInterceptor(new CleartextBlockInterceptor(appContext));
177199

178200
OkHttpClient client = builder.build();
179201

@@ -514,7 +536,6 @@ public void onActivityResult(FileStorageSetupActivity activity,
514536

515537
@Override
516538
public void prepareFileUsage(Context appContext, String path) {
517-
this.appContext = appContext;
518539

519540
}
520541

src/keepass2android-app/FileSelectHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private void ShowHttpDialog(Activity activity, Util.FileSelectedHandler onStartB
319319
View dlgContents = activity.LayoutInflater.Inflate(Resource.Layout.httpcredentials, null);
320320
if (!defaultPath.EndsWith(_schemeSeparator))
321321
{
322-
var webdavStorage = new Keepass2android.Javafilestorage.WebDavStorage(App.Kp2a.CertificateErrorHandler);
322+
var webdavStorage = new Keepass2android.Javafilestorage.WebDavStorage(App.Kp2a.CertificateErrorHandler, App.Context);
323323
var connInfo = webdavStorage.SplitStringToConnectionInfo(defaultPath);
324324
dlgContents.FindViewById<EditText>(Resource.Id.http_url).Text = connInfo.Url;
325325
dlgContents.FindViewById<EditText>(Resource.Id.http_user).Text = connInfo.Username;
@@ -339,7 +339,7 @@ private void ShowHttpDialog(Activity activity, Util.FileSelectedHandler onStartB
339339
string scheme = defaultPath.Substring(0, defaultPath.IndexOf(_schemeSeparator, StringComparison.Ordinal));
340340
if (host.Contains(_schemeSeparator) == false)
341341
host = scheme + _schemeSeparator + host;
342-
string httpPath = new Keepass2android.Javafilestorage.WebDavStorage(null).BuildFullPath(host, user,
342+
string httpPath = new Keepass2android.Javafilestorage.WebDavStorage(null, App.Context).BuildFullPath(host, user,
343343
password);
344344
onStartBrowse(httpPath);
345345
});
@@ -518,7 +518,7 @@ private void ShowOwncloudDialog(Activity activity, Util.FileSelectedHandler onSt
518518
string scheme = defaultPath.Substring(0,defaultPath.IndexOf(_schemeSeparator, StringComparison.Ordinal));
519519
if (host.Contains(_schemeSeparator) == false)
520520
host = scheme + _schemeSeparator + host;
521-
string httpPath = new Keepass2android.Javafilestorage.WebDavStorage(null).BuildFullPath(WebDavFileStorage.Owncloud2Webdav(host, subtype == "owncloud" ? WebDavFileStorage.owncloudPrefix : WebDavFileStorage.nextcloudPrefix), user,
521+
string httpPath = new Keepass2android.Javafilestorage.WebDavStorage(null, App.Context).BuildFullPath(WebDavFileStorage.Owncloud2Webdav(host, subtype == "owncloud" ? WebDavFileStorage.owncloudPrefix : WebDavFileStorage.nextcloudPrefix), user,
522522
password);
523523
onStartBrowse(httpPath);
524524
});

src/keepass2android-app/Resources/values/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@
125125
<string name="QuickUnlockIconHidden16_key">QuickUnlockIconHidden16_key</string>
126126
<string name="QuickUnlockBlockedWhenDeviceNotSecure_key">QuickUnlockBlockedWhenDeviceNotSecure_key</string>
127127

128+
<!-- Note: this key is used in WebDavStorage.java -->
129+
<string name="cleartextTrafficPermitted_key">permit_cleartext_traffic</string>
130+
128131
<string name="UsageCount_key">UsageCount</string>
129132
<string name="LastInfoVersionCode_key">LastInfoVersion</string>
130133

src/keepass2android-app/Resources/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@
731731
<string name="EntryChannel_desc">Notification to simplify access to the currently selected entry.</string>
732732
<string name="CloseDbAfterFailedAttempts">Close database after three failed biometric unlock attempts.</string>
733733
<string name="WarnFingerprintInvalidated">Warning! Biometric authentication can be invalidated by Android, e.g. after adding a new fingerprint in your device settings. Make sure you always know how to unlock with your master password!</string>
734+
<string name="cleartextTrafficPermitted_title">Allow clear-text network traffic in WebDav</string>
735+
<string name="cleartextTrafficPermitted_summary">HTTP connections are not secure. Only enable this if you have other security measures in place.</string>
734736

735737
<string-array name="ChangeLog_1_13">
736738
<item>Improved password quality estimation by considering most popular passwords.</item>

src/keepass2android-app/Resources/xml/network_security_config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<network-security-config>
3-
<base-config>
3+
<base-config cleartextTrafficPermitted="true">
44
<trust-anchors>
55
<certificates src="system" />
66
<certificates src="user" />

src/keepass2android-app/Resources/xml/pref_app_file_handling.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
android:defaultValue="@bool/PreloadDatabaseEnabled_default"
5353
android:title="@string/PreloadDatabaseEnabled_title"
5454
android:key="@string/PreloadDatabaseEnabled_key" />
55+
56+
<CheckBoxPreference
57+
android:enabled="true"
58+
android:persistent="true"
59+
android:summary="@string/cleartextTrafficPermitted_summary"
60+
android:defaultValue="false"
61+
android:title="@string/cleartextTrafficPermitted_title"
62+
android:key="@string/cleartextTrafficPermitted_key" />
5563
<CheckBoxPreference
5664
android:enabled="true"
5765
android:persistent="true"
@@ -80,5 +88,4 @@
8088
android:defaultValue="true"
8189
android:title="@string/CheckForDuplicateUuids_title"
8290
android:key="@string/CheckForDuplicateUuids_key" />
83-
8491
</PreferenceScreen>

src/keepass2android-app/app/App.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ public IEnumerable<IFileStorage> FileStorages
847847
new OneDrive2AppFolderFileStorage(),
848848
new SftpFileStorage(LocaleManager.LocalizedAppContext, this, IsFtpDebugEnabled()),
849849
new NetFtpFileStorage(LocaleManager.LocalizedAppContext, this, IsFtpDebugEnabled),
850-
new WebDavFileStorage(this),
850+
new WebDavFileStorage(this, App.Context),
851851
new PCloudFileStorage(LocaleManager.LocalizedAppContext, this),
852852
new PCloudFileStorageAll(LocaleManager.LocalizedAppContext, this),
853853
new MegaFileStorage(App.Context),

src/keepass2android-app/settings/AppSettingsActivity.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,6 @@ private void OnUseOfflineCacheChanged(object sender, Preference.PreferenceChange
641641
builder.SetCancelable(false);
642642
Dialog dialog = builder.Create();
643643
dialog.Show();
644-
645-
646644
}
647645
}
648646

@@ -654,7 +652,11 @@ public override void OnCreate(Bundle? savedInstanceState)
654652
Preference cachingPreference = FindPreference(GetString(Resource.String.UseOfflineCache_key));
655653
cachingPreference.PreferenceChange += OnUseOfflineCacheChanged;
656654

657-
655+
#if EXCLUDE_JAVAFILESTORAGE || NoNet
656+
// hide network related preference in NoNet variant:
657+
Preference cleartextPreference = FindPreference(GetString(Resource.String.cleartextTrafficPermitted_key));
658+
cleartextPreference.Visible = false;
659+
#endif
658660

659661
}
660662
}

0 commit comments

Comments
 (0)