Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
Route Office files through the engine first; harden WebViews against network
Chethan616528661cf43a5
diff --git a/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt b/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.ktindex 543dfbb..a2471e9 100644--- a/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt+++ b/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt@@ -28,7 +28,9 @@ object HtmlToPdfConverter {fun convertHtml(context: Context, html: String, outputUri: Uri, onDone: (Boolean) -> Unit) {val webView = WebView(context)webView.settings.javaScriptEnabled = false- finishOnLoad(webView, context, outputUri, settleMs = 250, onDone)+ // User-supplied HTML must not fetch remote images/trackers (foss builds hold INTERNET).+ OfflineWebContent.harden(webView)+ finishOnLoad(webView, context, outputUri, settleMs = 250, offline = true, onDone)webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)}@@ -41,13 +43,26 @@ object HtmlToPdfConverter {useWideViewPort = true}// JS-heavy pages need a longer settle before the layout is stable enough to snapshot.- finishOnLoad(webView, context, outputUri, settleMs = 900, onDone)+ finishOnLoad(webView, context, outputUri, settleMs = 900, offline = false, onDone)webView.loadUrl(url)}- private fun finishOnLoad(webView: WebView, context: Context, outputUri: Uri, settleMs: Long, onDone: (Boolean) -> Unit) {+ private fun finishOnLoad(+ webView: WebView,+ context: Context,+ outputUri: Uri,+ settleMs: Long,+ offline: Boolean,+ onDone: (Boolean) -> Unit+ ) {var handled = falsewebView.webViewClient = object : WebViewClient() {+ override fun shouldInterceptRequest(+ view: WebView,+ request: android.webkit.WebResourceRequest+ ): android.webkit.WebResourceResponse? =+ if (offline) OfflineWebContent.intercept(request) else null+override fun onPageFinished(view: WebView, url: String?) {if (handled) returnview.postDelayed({diff --git a/app/src/main/java/com/chethan616/clearpdf/util/OfflineWebContent.kt b/app/src/main/java/com/chethan616/clearpdf/util/OfflineWebContent.ktnew file mode 100644index 0000000..f1bde07--- /dev/null+++ b/app/src/main/java/com/chethan616/clearpdf/util/OfflineWebContent.kt@@ -0,0 +1,30 @@+package com.chethan616.clearpdf.util++import android.webkit.WebResourceRequest+import android.webkit.WebResourceResponse+import android.webkit.WebView+import java.io.ByteArrayInputStream++/**+ * Keeps a WebView that renders local content from reaching the network, independently of the+ * app's permissions (the foss flavor holds INTERNET for the optional Office engine download).+ *+ * Two layers: [WebView.getSettings] `blockNetworkLoads`, plus [intercept] from+ * `shouldInterceptRequest`, which answers every request that is not bundled/inline content with+ * an empty 403 so it never leaves the device.+ */+internal object OfflineWebContent {++ private val allowedPrefixes = listOf("file:///android_asset/", "data:", "blob:", "about:")++ fun harden(view: WebView) {+ view.settings.blockNetworkLoads = true+ view.settings.allowContentAccess = false+ }++ fun intercept(request: WebResourceRequest): WebResourceResponse? {+ val url = request.url?.toString().orEmpty()+ if (allowedPrefixes.any { url.startsWith(it) }) return null+ return WebResourceResponse("text/plain", "utf-8", 403, "Blocked", emptyMap(), ByteArrayInputStream(ByteArray(0)))+ }+}diff --git a/app/src/main/java/com/chethan616/clearpdf/utils/DocxWebRenderer.kt b/app/src/main/java/com/chethan616/clearpdf/utils/DocxWebRenderer.ktindex 38ffbdf..800fb13 100644--- a/app/src/main/java/com/chethan616/clearpdf/utils/DocxWebRenderer.kt+++ b/app/src/main/java/com/chethan616/clearpdf/utils/DocxWebRenderer.kt@@ -39,8 +39,10 @@ import java.util.zip.ZipInputStream* thing the phone already has: the browser engine.** Offline: the page is loaded from `file:///android_asset`, network loads are blocked on the- * WebView, and the app declares no `INTERNET` permission at all — so the OS would refuse a network- * request even if a script attempted one.+ * WebView, and `shouldInterceptRequest` answers anything that is not a bundled asset or inline+ * data with a local 403 ([com.chethan616.clearpdf.util.OfflineWebContent]). The play flavor also+ * declares no `INTERNET` permission; the foss flavor does (only for the optional Office engine+ * download), which is why the WebView itself must never rely on the permission being absent.** There are two ways out of the WebView, tried in order:* 1. **The print framework.** It honours the CSS page breaks the host page puts on each section,@@ -112,8 +114,8 @@ internal object DocxWebRenderer {val view = WebView(context)webView = viewview.settings.javaScriptEnabled = true- // Redundant with the missing INTERNET permission, and kept anyway: two independent- // guarantees that a document can never phone home.+ // Independent of the INTERNET permission (present in the foss flavor): together with+ // shouldInterceptRequest below, a document can never phone home.view.settings.blockNetworkLoads = trueview.settings.allowFileAccess = true@@ -151,7 +153,16 @@ internal object DocxWebRenderer {"AndroidDocx")+ com.chethan616.clearpdf.util.OfflineWebContent.harden(view)view.webViewClient = object : WebViewClient() {+ // Third guarantee: anything that is not a bundled asset or inline data is+ // answered locally with a 403, even in builds that hold INTERNET (foss).+ override fun shouldInterceptRequest(+ view: WebView,+ request: android.webkit.WebResourceRequest+ ): android.webkit.WebResourceResponse? =+ com.chethan616.clearpdf.util.OfflineWebContent.intercept(request)+override fun onPageFinished(view: WebView, url: String) {val base64 = Base64.encodeToString(docxBytes, Base64.NO_WRAP)view.evaluateJavascript("renderDocx('$base64')", null)diff --git a/app/src/main/java/com/chethan616/clearpdf/utils/UniversalDocumentConverter.kt b/app/src/main/java/com/chethan616/clearpdf/utils/UniversalDocumentConverter.ktindex cfc1f50..96e7b67 100644--- a/app/src/main/java/com/chethan616/clearpdf/utils/UniversalDocumentConverter.kt+++ b/app/src/main/java/com/chethan616/clearpdf/utils/UniversalDocumentConverter.kt@@ -45,6 +45,9 @@ object UniversalDocumentConverter {fun convertToPdf(context: Context, sourceUri: Uri): Uri {val mimeType = context.contentResolver.getType(sourceUri) ?: ""val name = getFileName(context, sourceUri).lowercase()+ // Optional Office engine (powered by LibreOffice): used first when installed and enabled.+ // Any failure returns null and the built-in renderers below take over, unchanged.+ com.chethan616.clearpdf.office.OfficeEngine.tryConvert(context, sourceUri, name)?.let { return Uri.fromFile(it) }return when {mimeType.startsWith("image/") || name.endsWithAny(".png", ".jpg", ".jpeg", ".webp", ".bmp", ".heic") ->convertImageToPdf(context, sourceUri)