]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/ImportExportActivity.java
Restrict the File Provider to a subfolder of the cache directory. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / ImportExportActivity.java
index fe4f99a02044be07b1dce82cd19fe6a9763b1778..4c48b42631d560c606e53e655861b028a27d0532 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2018-2022 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -105,6 +105,7 @@ public class ImportExportActivity extends AppCompatActivity {
     Button importExportButton;
 
     // Define the class variables.
+    private File fileProviderDirectory;
     private boolean openKeychainInstalled;
     private File temporaryPgpEncryptedImportFile;
     private File temporaryPreEncryptedExportFile;
@@ -123,9 +124,6 @@ public class ImportExportActivity extends AppCompatActivity {
             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
         }
 
-        // Set the theme.
-        setTheme(R.style.PrivacyBrowser);
-
         // Run the default commands.
         super.onCreate(savedInstanceState);
 
@@ -502,6 +500,13 @@ public class ImportExportActivity extends AppCompatActivity {
                     //noinspection ResultOfMethodCallIgnored
                     temporaryPgpEncryptedImportFile.delete();
                 }
+
+                // Delete the file provider directory if it exists.
+                if (fileProviderDirectory.exists()) {
+                    //noinspection ResultOfMethodCallIgnored
+                    fileProviderDirectory.delete();
+                }
+
                 break;
 
             case OPENPGP_EXPORT_RESULT_CODE:
@@ -510,6 +515,13 @@ public class ImportExportActivity extends AppCompatActivity {
                     //noinspection ResultOfMethodCallIgnored
                     temporaryPreEncryptedExportFile.delete();
                 }
+
+                // Delete the file provider directory if it exists.
+                if (fileProviderDirectory.exists()) {
+                    //noinspection ResultOfMethodCallIgnored
+                    fileProviderDirectory.delete();
+                }
+
                 break;
         }
     }
@@ -531,6 +543,7 @@ public class ImportExportActivity extends AppCompatActivity {
                 case NO_ENCRYPTION:
                     try {
                         // Get an input stream for the file name.
+                        // A file may be opened directly once the minimum API >= 29.  <https://developer.android.com/reference/kotlin/android/content/ContentResolver#openfile>
                         InputStream inputStream = getContentResolver().openInputStream(Uri.parse(fileNameString));
 
                         // Import the unencrypted file.
@@ -659,8 +672,15 @@ public class ImportExportActivity extends AppCompatActivity {
 
                 case OPENPGP_ENCRYPTION:
                     try {
+                        // Get a handle for the file provider directory.
+                        fileProviderDirectory = new File(getApplicationContext().getCacheDir() + "/" + getString(R.string.file_provider_directory));
+
+                        // Create the file provider directory.  Any errors will be handled by the catch statement below.
+                        //noinspection ResultOfMethodCallIgnored
+                        fileProviderDirectory.mkdir();
+
                         // Set the temporary PGP encrypted import file.
-                        temporaryPgpEncryptedImportFile = File.createTempFile("temporary_pgp_encrypted_import_file", null, getApplicationContext().getCacheDir());
+                        temporaryPgpEncryptedImportFile = File.createTempFile("temporary_pgp_encrypted_import_file", null, fileProviderDirectory);
 
                         // Create a temporary PGP encrypted import file output stream.
                         FileOutputStream temporaryPgpEncryptedImportFileOutputStream = new FileOutputStream(temporaryPgpEncryptedImportFile);
@@ -724,6 +744,7 @@ public class ImportExportActivity extends AppCompatActivity {
 
                     try {
                         // Get the export file output stream.
+                        // A file may be opened directly once the minimum API >= 29.  <https://developer.android.com/reference/kotlin/android/content/ContentResolver#openfile>
                         OutputStream exportFileOutputStream = getContentResolver().openOutputStream(Uri.parse(noEncryptionFileNameString));
 
                         // Export the unencrypted file.
@@ -859,8 +880,15 @@ public class ImportExportActivity extends AppCompatActivity {
 
                 case OPENPGP_ENCRYPTION:
                     try {
+                        // Get a handle for the file provider directory.
+                        fileProviderDirectory = new File(getApplicationContext().getCacheDir() + "/" + getString(R.string.file_provider_directory));
+
+                        // Create the file provider directory.  Any errors will be handled by the catch statement below.
+                        //noinspection ResultOfMethodCallIgnored
+                        fileProviderDirectory.mkdir();
+
                         // Set the temporary pre-encrypted export file.
-                        temporaryPreEncryptedExportFile = new File(getApplicationContext().getCacheDir() + "/" + getString(R.string.settings) + " " + BuildConfig.VERSION_NAME + ".pbs");
+                        temporaryPreEncryptedExportFile = new File(fileProviderDirectory + "/" + getString(R.string.settings) + " " + BuildConfig.VERSION_NAME + ".pbs");
 
                         // Delete the temporary pre-encrypted export file if it already exists.
                         if (temporaryPreEncryptedExportFile.exists()) {
@@ -933,4 +961,4 @@ public class ImportExportActivity extends AppCompatActivity {
         // Restart Privacy Browser after 150 milliseconds to allow enough time for the preferences to be saved.
         restartHandler.postDelayed(restartRunnable, 150);
     }
-}
\ No newline at end of file
+}