From 5b0bf08478a179a2371a8c2849675c8ab1252e22 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Wed, 4 Nov 2026 08:28:14 +0000 Subject: [PATCH] fix(mobile): a manifest rule counts only on an AndroidManifest.xml (#824) --- compliance-agent/src/pipeline/mobsfscan.rs | 58 +++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/compliance-agent/src/pipeline/mobsfscan.rs b/compliance-agent/src/pipeline/mobsfscan.rs index c64f472..bd9c546 100644 --- a/compliance-agent/src/pipeline/mobsfscan.rs +++ b/compliance-agent/src/pipeline/mobsfscan.rs @@ -258,8 +258,24 @@ struct Metadata { severity: String, } +/// Rule-id prefixes of mobsfscan's AndroidManifest rules. +const MANIFEST_RULE_PREFIXES: &[&str] = &["android_manifest_", "android_task_hijacking"]; + +/// Whether a match of `rule` at `file` can be real: a manifest rule only +/// applies to an `AndroidManifest.xml`. mobsfscan has reported one on another +/// XML file of the tree (`copyright.xml:1`, beside the same finding on the +/// real manifest), nondeterministically, in the nightly (#573). +fn applies_to(rule: &str, file: &str) -> bool { + let manifest_rule = MANIFEST_RULE_PREFIXES.iter().any(|p| rule.starts_with(p)); + !manifest_rule + || Path::new(file) + .file_name() + .is_some_and(|name| name.eq_ignore_ascii_case("AndroidManifest.xml")) +} + impl Report { - /// One finding per rule and location. + /// One finding per rule and location; a manifest rule's match outside an + /// `AndroidManifest.xml` is dropped ([`applies_to`]). fn findings(self, root: &Path, repo_id: &str) -> Vec { self.results .into_iter() @@ -267,6 +283,7 @@ impl Report { result .files .iter() + .filter(|m| applies_to(&rule, &m.file_path)) .map(|m| finding(root, repo_id, &rule, &result.metadata, m)) .collect::>() }) @@ -475,6 +492,45 @@ mod tests { ); } + #[test] + fn a_manifest_rule_counts_only_on_a_manifest() { + assert!(applies_to( + "android_manifest_missing_explicit_allow_backup", + "/tree/app/src/main/AndroidManifest.xml" + )); + assert!(!applies_to( + "android_manifest_missing_explicit_allow_backup", + "copyright.xml" + )); + assert!(!applies_to( + "android_task_hijacking2", + "res/values/strings.xml" + )); + // Code rules match wherever they match. + assert!(applies_to("android_kotlin_logging", "app/src/Main.kt")); + + let dir = tree(&[ + ("app/src/main/AndroidManifest.xml", MANIFEST), + ("spotless/copyright.xml", "\n"), + ]); + // The same manifest match, reported a second time on copyright.xml. + let mut json: serde_json::Value = serde_json::from_str(&report(&dir.0)).expect("json"); + let files = json["results"]["android_manifest_debugging_enabled"]["files"] + .as_array_mut() + .expect("files"); + let mut stray = files[0].clone(); + stray["file_path"] = format!("{}/spotless/copyright.xml", dir.0.display()).into(); + files.push(stray); + let json = json.to_string(); + let parsed: Report = serde_json::from_str(&json).expect("report"); + let findings = parsed.findings(&dir.0, "t"); + assert_eq!(findings.len(), 1); + assert_eq!( + findings[0].file_path.as_deref(), + Some("app/src/main/AndroidManifest.xml") + ); + } + #[test] fn a_setting_key_is_read_only_from_a_comparison() { assert_eq!(