Modern Apps git

← Modern-Apps.git

general: guard library dependency boundaries in ./check (#884)

Commit
110dc93a90e5e5e3745be40047280e283273e8b3
Author
Vayun Mathur <[email protected]>
When
2026-10-31 19:42 UTC (1 month ago)
Parents
a62fd6c
#884 traced PDF Reader's network permission to libraries depending on
permission-declaring libraries (:library:ui -> :library:network,
:library:image's own INTERNET, etc.). The earlier #884 commits removed
those edges; this stops them from coming back.

New root task verifyLibraryBoundaries (also run by ./check):
- Library modules are found by plugin (common-conventions-library or
  -kmp), not by directory, so app modules under games/ or cast/ (which
  may depend on libraries freely) are not mistaken for libraries, and
  :library, :auto:protocol and :library:everysync-core are covered.
- Fails if any library depends, directly or transitively, on a library
  whose manifest declares any <uses-permission>, or on one of the known
  permission modules (:library:network, :library:map,
  :library:downloadservice). No exceptions.
- Fails on any library->library edge outside an explicit, commented
  allowlist of the remaining permission-free edges (mostly -> :library
  base, plus ui-shell consumers, ocr->ml, e2ee-p2p consumers), so a new
  edge has to be reviewed before it lands.
- Configuration-cache safe: captures only literals; file IO happens at
  execution time.

Also deletes library/image's HttpFetcher.kt. It imported NetworkClient,
which :library:image stopped depending on in 07388aa7a (apps now supply
remote fetchers through ImageLoader.Builder.remote), so it was dead code
that no longer compiled. Each module's AGENTS.md Deps line now says why
its remaining edge is permission-free.

Verified: verifyLibraryBoundaries passes (26 library modules). Negative
test: re-adding :library:ui -> :library:network fails with the direct
edge plus the transitive tv/avatar/carhost paths.
:library:image/:room/:biometric compileDebugKotlin green.

Diff

build.gradle.kts
diff --git a/build.gradle.kts b/build.gradle.ktsindex 8e91a05017..22f6ae2f40 100644--- a/build.gradle.kts+++ b/build.gradle.kts@@ -170,6 +170,159 @@ tasks.register("checkMetadata") {     } } +// Guards issue #884: no library/SDK module may depend on another library+// module that declares Android permissions, or those permissions silently+// merge into every consuming app (pdf gained network access this way). A+// second list pins the remaining permission-free lib→lib edges, so a new+// edge (permission-free today, permission-declaring tomorrow) fails until+// it is reviewed and documented here.+// Runs standalone (`./gradlew verifyLibraryBoundaries`) and from ./check.+// Configuration-cache compatible: the action captures only string literals+// (project root path + both lists) and does all file IO at execution time.+tasks.register("verifyLibraryBoundaries") {+    group = "verification"+    description = "Fails if a library/SDK module depends on another library module " ++        "that declares Android permissions (directly or transitively), or on a " ++        "library module outside the documented permission-free edge list."+    val projectRoot = rootDir+    // Permission-declaring modules: nothing in library/* or sdk/* may depend on+    // these (directly or transitively). Declared here rather than inferred so a+    // module that newly gains a permission fails loudly instead of silently+    // widening the blast radius. No exceptions: map and downloadservice declare+    // INTERNET themselves exactly so they need no edge into network.+    val permissionDeclaringModules = setOf(+        ":library:network",+        ":library:map",+        ":library:downloadservice",+    )+    // The only permitted lib→lib edges (all permission-free; each is justified+    // in its module's AGENTS.md Deps line):+    // - many modules → :library (shared nav/DTO/Room base; declares nothing)+    // - :library:room → :library (DbBackupCodec SPI)+    // - :library:biometric → :library (DatabaseHelper keystore)+    // - :library:work → :library (reminder notification resources)+    // - :library:widgets → :library (Log facade only)+    // - :library:downloadservice → :library (DataStoreUtils)+    // - :library:ui → :library (shell + nav base)+    // - :library:avatar → :library:ui, :library:carhost → :library:ui,+    //   :library:tv → :library:ui (Compose shell; ui is permission-free)+    // - :library:tv → :library (base DTOs alongside the ui shell)+    // - :library:ocr → :library:ml (on-device OCR runs on the ml runtime)+    // - :sdk:controller → :library:e2ee-p2p, :cast:protocol → :library:e2ee-p2p+    //   (Hkdf key schedule + E2EE handshake; e2ee-p2p declares nothing)+    // - :library → :sdk:games (achievement contract in the shared base)+    // - :auto:protocol → :library (shared DTOs)+    // - :library:everysync-core → :library:e2ee-p2p (E2EE sync transport)+    // - :sdk:openassistant → (none)+    val allowedLibraryEdges = setOf(+        ":library" to ":sdk:games",+        ":auto:protocol" to ":library",+        ":library:everysync-core" to ":library:e2ee-p2p",+        ":library:avatar" to ":library:ui",+        ":library:biometric" to ":library",+        ":library:carhost" to ":library:ui",+        ":library:downloadservice" to ":library",+        ":library:ocr" to ":library:ml",+        ":library:room" to ":library",+        ":library:tv" to ":library",+        ":library:tv" to ":library:ui",+        ":library:ui" to ":library",+        ":library:widgets" to ":library",+        ":library:work" to ":library",+        ":sdk:controller" to ":library:e2ee-p2p",+        ":cast:protocol" to ":library:e2ee-p2p",+    )+    doLast {+        val projectRef = Regex("""project\(["'](:[A-Za-z0-9:_\-]+)["']\)""")+        val permissionRef = Regex("""<uses-permission[^>]*android:name="([^"]+)"""")+        val manifestOf = { moduleDir: File ->+            File(moduleDir, "src/main/AndroidManifest.xml").takeIf { it.isFile }+        }+        val permissionsOf = { moduleDir: File ->+            val manifest = manifestOf(moduleDir)+            if (manifest == null) {+                emptySet()+            } else {+                permissionRef.findAll(manifest.readText()).map { it.groupValues[1] }.toSet()+            }+        }+        // Library modules = anything applying the library/KMP convention plugin+        // (apps apply common-conventions-app and may depend on libraries freely).+        // aosp-stubs is compile-only; log is added to every library by the+        // convention plugin and declares nothing.+        val skipDirs = setOf(".git", ".gradle", ".kotlin", "build", "build-logic", "analysis",+            "personal", "target", "third_party", "node_modules", "scripts", "firmware")+        val libraryPlugin = Regex("""id\("common-conventions-(library|kmp)"\)""")+        val libraryDirs = projectRoot.walkTopDown()+            .maxDepth(3)+            .onEnter { it == projectRoot || it.name !in skipDirs }+            .filter { it.isDirectory && it != projectRoot }+            .filter {+                val build = File(it, "build.gradle.kts")+                build.isFile && libraryPlugin.containsMatchIn(build.readText())+            }+            .filter {+                val rel = it.relativeTo(projectRoot).invariantSeparatorsPath+                rel != "library/aosp-stubs" && rel != "library/log"+            }+            .toList()+        val gradlePathOf = { dir: File ->+            ":" + dir.relativeTo(projectRoot).invariantSeparatorsPath.replace('/', ':')+        }+        val dirOf = { gradlePath: String ->+            File(projectRoot, gradlePath.trimStart(':').replace(':', '/'))+        }+        val directDeps = libraryDirs.associate { dir ->+            val text = File(dir, "build.gradle.kts").readText()+            gradlePathOf(dir) to projectRef.findAll(text).map { it.groupValues[1] }.toSet()+        }+        val violations = mutableListOf<String>()+        for ((module, deps) in directDeps) {+            val seen = mutableSetOf<String>()+            val queue = ArrayDeque(deps)+            while (queue.isNotEmpty()) {+                val dep = queue.removeFirst()+                if (!seen.add(dep)) continue+                val depDir = dirOf(dep)+                if (!depDir.isDirectory) continue+                val perms = permissionsOf(depDir)+                // Belt and braces: the declared set fails fast on known+                // permission modules, while the manifest scan catches a module+                // that newly gains a permission without updating the set.+                if (dep in permissionDeclaringModules || perms.isNotEmpty()) {+                    val why = if (perms.isNotEmpty()) {+                        "which declares " + perms.sorted().joinToString(", ")+                    } else {+                        "which is a permission-declaring module"+                    }+                    violations += "$module depends on $dep, $why"+                }+                directDeps[dep]?.let { queue.addAll(it) }+            }+            for (dep in deps) {+                if ((module to dep) !in allowedLibraryEdges) {+                    violations += "$module depends on $dep, which is not in the " ++                        "documented permission-free edge list — review and document " ++                        "it in verifyLibraryBoundaries (root build.gradle.kts) first"+                }+            }+        }+        violations.forEach { println("verifyLibraryBoundaries: VIOLATION: $it") }+        if (violations.isNotEmpty()) {+            throw GradleException(+                "verifyLibraryBoundaries: ${violations.size} permission-leaking " ++                    "library edge(s). Move the permission-needing code into the " ++                    "consuming app (or the permission-declaring module) instead of " ++                    "depending across libraries — see issue #884.",+            )+        }+        println(+            "verifyLibraryBoundaries: ${libraryDirs.size} library module(s) checked, " ++                "no permission-leaking edges.",+        )+    }+}+ // Jacoco XML summaries land under build/reports/coverage after unit or instrumented runs, // while manual runs drop .ec files under build/outputs/coverage-manual. This task only reads // existing outputs, so it never triggers test execution.
check
diff --git a/check b/checkindex dd1e9ddaff..88e19fe9f2 100755--- a/check+++ b/check@@ -93,6 +93,9 @@ for mod in "${MODS[@]}"; do   # cargo include_bytes!s the SPIR-V this task writes to build/spirv.   [[ -d "$ROOT/$mod/src/main/rust/shaders" ]] && TASKS+=(":$gmod:compileShaders") done+# Guards issue #884 (no library may depend on a permission-declaring library).+# Root task, so add once however many modules are checked.+TASKS+=(":verifyLibraryBoundaries") echo "Modules (${#MODS[@]}): ${MODS[*]}" echo "Gradle tasks: ${TASKS[*]}" echo ""
library/avatar/AGENTS.md
diff --git a/library/avatar/AGENTS.md b/library/avatar/AGENTS.mdindex 6d1929c638..1452e37be4 100644--- a/library/avatar/AGENTS.md+++ b/library/avatar/AGENTS.md@@ -7,7 +7,7 @@ _Install: dev by default (library, no launcher)._ - Gradle: :library:avatar / dir: library/avatar/ - Package roots present: `com.vayunmathur.library.avatar` (bridge), `...avatar.components` (surface/composable), `...avatar.yuu` (pose contract) - Entry files: YuuAvatarNative.kt (JNI bridge), components/YuuAvatar3D.kt (hero composable)-- Deps: :library:ui, :library:log, androidx.lifecycle.runtime.compose+- Deps: :library:ui (permission-free since #884), :library:log, androidx.lifecycle.runtime.compose - Rust: yes (`src/main/rust/`, crate `yuu_avatar`, glTF + skinning) - Metadata: none (library, no listing) 
library/biometric/AGENTS.md
diff --git a/library/biometric/AGENTS.md b/library/biometric/AGENTS.mdindex 6dc00aca32..b16611c47f 100644--- a/library/biometric/AGENTS.md+++ b/library/biometric/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:biometric / dir: library/biometric/ - Package roots present: (single-purpose crypto helper — one package) - Entry files: BiometricDatabaseHelper.kt (no MainActivity/Route — library)-- Deps: :library+- Deps: :library (permission-free; extends base's DatabaseHelper keystore, #884) - Metadata: metadata_data/library-biometric.md (absent — libraries have none by design) - Rust: no / screenshotTest: no 
library/biometric/build.gradle.kts
diff --git a/library/biometric/build.gradle.kts b/library/biometric/build.gradle.ktsindex 08aab20da5..849bc0bc28 100644--- a/library/biometric/build.gradle.kts+++ b/library/biometric/build.gradle.kts@@ -3,6 +3,8 @@ plugins { }  dependencies {+    // BiometricDatabaseHelper extends the DatabaseHelper keystore base from+    // :library, so this edge stays: both modules are permission-free (#884).     implementation(project(":library"))     implementation(libs.androidx.biometric)     implementation(libs.androidx.fragment.ktx)
library/carhost/AGENTS.md
diff --git a/library/carhost/AGENTS.md b/library/carhost/AGENTS.mdindex 1761e17ee2..b4bc1604f6 100644--- a/library/carhost/AGENTS.md+++ b/library/carhost/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:carhost / dir: library/carhost/ - Package roots present: (car template host — one package) - Entry files: CarTemplateView.kt (no MainActivity/Route — library)-- Deps: (verify in build.gradle.kts before adding)+- Deps: :library:ui (permission-free since #884) - Metadata: metadata_data/library-carhost.md (absent — libraries have none by design) - Rust: no / screenshotTest: no 
library/image/src/main/java/com/vayunmathur/library/image/fetchers/HttpFetcher.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/fetchers/HttpFetcher.kt b/library/image/src/main/java/com/vayunmathur/library/image/fetchers/HttpFetcher.ktdeleted file mode 100644index 46b470153f..0000000000--- a/library/image/src/main/java/com/vayunmathur/library/image/fetchers/HttpFetcher.kt+++ /dev/null@@ -1,29 +0,0 @@-package com.vayunmathur.library.image.fetchers--import android.content.Context-import android.net.Uri-import com.vayunmathur.library.network.NetworkClient-import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.withContext--class HttpFetcher : Fetcher {-    override suspend fun fetch(data: Any?, context: Context): FetchResult? {-        val url: String = when (data) {-            is String -> data-            is Uri -> {-                val s = data.scheme-                if (s == "http" || s == "https") data.toString() else return null-            }-            else -> return null-        }-        if (!url.startsWith("http://") && !url.startsWith("https://")) return null--        return withContext(Dispatchers.IO) {-            val resp = NetworkClient.execute(url)-            if (!resp.isSuccess) {-                throw java.io.IOException("HTTP ${resp.status} for $url")-            }-            FetchResult.Bytes(resp.bytes)-        }-    }-}
library/ocr/AGENTS.md
diff --git a/library/ocr/AGENTS.md b/library/ocr/AGENTS.mdindex 7de701a54a..f98d90e7cb 100644--- a/library/ocr/AGENTS.md+++ b/library/ocr/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:ocr / dir: library/ocr/ - Package roots present: (on-device OCR — one package) - Entry files: OcrEngine.kt (no MainActivity/Route — library)-- Deps: :library:ml+- Deps: :library:ml (both permission-free; the OCR pipeline runs on the ml runtime, #884) - Metadata: metadata_data/library-ocr.md (absent — libraries have none by design) - Rust: no / screenshotTest: no 
library/room/AGENTS.md
diff --git a/library/room/AGENTS.md b/library/room/AGENTS.mdindex f850c6e3f4..6e663236ae 100644--- a/library/room/AGENTS.md+++ b/library/room/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:room / dir: library/room/ - Package roots present: (pure-Kotlin Room3 + SQLCipher wiring — single package) - Entry files: RoomRepository.kt (no MainActivity/Route — library)-- Deps: :library+- Deps: :library (permission-free; SqlCipherDbCodec implements base's DbBackupCodec, #884) - Metadata: metadata_data/library-room.md (absent — libraries have none by design) - Rust: no / screenshotTest: no 
library/room/build.gradle.kts
diff --git a/library/room/build.gradle.kts b/library/room/build.gradle.ktsindex 2d50289c70..1c7218c856 100644--- a/library/room/build.gradle.kts+++ b/library/room/build.gradle.kts@@ -4,6 +4,8 @@ plugins { }  dependencies {+    // SqlCipherDbCodec implements the DbBackupCodec interface from :library, so+    // this edge stays: both modules are permission-free (documented in #884).     implementation(project(":library"))      // room + SQLCipher. The 2 MB libsqlcipher.so lives here, so only apps that
library/widgets/AGENTS.md
diff --git a/library/widgets/AGENTS.md b/library/widgets/AGENTS.mdindex 884923b002..32a3c0e013 100644--- a/library/widgets/AGENTS.md+++ b/library/widgets/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:widgets / dir: library/widgets/ - Package roots present: (Glance widget bases — one package) - Entry files: MaWidget.kt + MaWidgetReceiver.kt (no MainActivity/Route — library)-- Deps: :library+- Deps: :library (permission-free; only the Log facade is imported, #884) - Metadata: metadata_data/library-widgets.md (absent — libraries have none by design) - Rust: no / screenshotTest: no 
library/work/AGENTS.md
diff --git a/library/work/AGENTS.md b/library/work/AGENTS.mdindex 2cc7da1822..00c400c152 100644--- a/library/work/AGENTS.md+++ b/library/work/AGENTS.md@@ -6,7 +6,7 @@ - Gradle: :library:work / dir: library/work/ - Package roots present: (WorkManager helpers — one package) - Entry files: Work.kt (no MainActivity/Route — library)-- Deps: :library+- Deps: :library (permission-free; daily-reminder notification resources come from base, #884) - Metadata: metadata_data/library-work.md (absent — libraries have none by design) - Rust: no / screenshotTest: no