Skip to content

Avoid common App Store and Google Play rejections. Open playbook and pre-submission guard with 117 documented rejection patterns and fixes, privacy manifest and Data safety checks, 95 tracked deadlines (EU AI Act, DSA, COPPA, age laws), and a skill for AI coding agents.

42.3 KB
Raw Blame
#!/usr/bin/env python3
"""Monitors the 17 mobile security domains in TRACKED_CATEGORIES
below, and generates repo-impact and migration tasks for each update."""

import os
import sys
import re
import argparse
import urllib.request
import xml.etree.ElementTree as ET
import json

# The 17 tracked mobile security requirement categories
TRACKED_CATEGORIES = [
    "secure storage",
    "Keychain",
    "Android Keystore",
    "biometric authentication",
    "certificate pinning",
    "jailbreak detection",
    "root detection",
    "SSL configuration",
    "backup rules",
    "exported activities",
    "intent filters",
    "deep links",
    "universal links",
    "app links",
    "authentication flows",
    "session handling",
    "token storage",
]

# Keywords used to classify incoming policy announcements/articles into the 17 categories
CATEGORY_KEYWORDS = {
    "secure storage": [
        "secure storage",
        "encryptedsharedpreferences",
        "encryptedfile",
        "sqlcipher",
        "sqlite",
        "room database",
        "local encryption",
    ],
    "Keychain": [
        "keychain",
        "ksecattraccessible",
        "ksecattraccessibleafterfirstunlockthisdeviceonly",
        "ksecattraccessiblewhenunlockedthisdeviceonly",
        "secitemadd",
    ],
    "Android Keystore": [
        "android keystore",
        "keygenparameterspec",
        "strongbox",
        "isinsidesecurehardware",
        "setisstrongboxbacked",
    ],
    "biometric authentication": [
        "biometric",
        "faceid",
        "touchid",
        "biometricprompt",
        "lacontext",
        "cryptoobject",
        "evaluatepolicy",
    ],
    "certificate pinning": [
        "certificate pinning",
        "public-key pinning",
        "spki",
        "nspinneddomains",
        "network_security_config",
        "certificatepinner",
    ],
    "jailbreak detection": [
        "jailbreak",
        "cydia",
        "sileo",
        "mobilesubstrate",
        "dyld",
        "jailbroken",
    ],
    "root detection": [
        "root detection",
        "magisk",
        "play integrity",
        "integritymanager",
        "test-keys",
        "su binary",
        "rooted",
    ],
    "SSL configuration": [
        "ssl configuration",
        "tls 1.3",
        "nsallowsarbitraryloads",
        "usescleartexttraffic",
        "cleartexttrafficpermitted",
    ],
    "backup rules": [
        "backup rules",
        "isexcludedfrombackup",
        "allowbackup",
        "dataextractionrules",
        "fullbackupcontent",
    ],
    "exported activities": [
        "exported activities",
        "exported=true",
        "exported=false",
        "android:exported",
    ],
    "intent filters": [
        "intent filters",
        "intent-filter",
        "getcallingpackage",
        "getcallingactivity",
    ],
    "deep links": [
        "deep links",
        "cfbundleurlschemes",
        "android:scheme",
        "url scheme",
    ],
    "universal links": [
        "universal links",
        "apple-app-site-association",
        "applinks:",
    ],
    "app links": [
        "app links",
        "assetlinks.json",
        "autoverify=true",
    ],
    "authentication flows": [
        "authentication flows",
        "oauth",
        "pkce",
        "aswebauthenticationsession",
        "custom tabs",
    ],
    "session handling": [
        "session handling",
        "session timeout",
        "background policy",
        "logout",
        "background blur",
    ],
    "token storage": [
        "token storage",
        "refresh token",
        "access token",
        "refreshtoken",
        "accesstoken",
    ],
}

# Codebase signals (regex patterns) to find files affected by each of the 17 categories
CATEGORY_SIGNALS = {
    "secure storage": [
        r"EncryptedSharedPreferences",
        r"EncryptedFile",
        r"SQLCipher",
        r"completeFileProtection",
    ],
    "Keychain": [
        r"kSecAttrAccessible",
        r"Keychain",
        r"SecItemAdd",
    ],
    "Android Keystore": [
        r"AndroidKeyStore",
        r"KeyGenParameterSpec",
        r"setIsStrongBoxBacked",
        r"isInsideSecureHardware",
    ],
    "biometric authentication": [
        r"BiometricPrompt",
        r"LAContext",
        r"SecAccessControl",
        r"evaluatePolicy",
        r"CryptoObject",
    ],
    "certificate pinning": [
        r"NSPinnedDomains",
        r"network_security_config",
        r"CertificatePinner",
        r"pin-set",
    ],
    "jailbreak detection": [
        r"Cydia",
        r"Sileo",
        r"jailbreak",
        r"/Applications/Cydia.app",
        r"dyld",
    ],
    "root detection": [
        r"PlayIntegrity",
        r"IntegrityManager",
        r"test-keys",
        r"/system/bin/su",
    ],
    "SSL configuration": [
        r"NSAllowsArbitraryLoads",
        r"usesCleartextTraffic",
        r"cleartextTrafficPermitted",
    ],
    "backup rules": [
        r"isExcludedFromBackup",
        r"allowBackup",
        r"dataExtractionRules",
        r"fullBackupContent",
    ],
    "exported activities": [
        r"android:exported",
        r"exported=\"true\"",
        r"exported=\"false\"",
    ],
    "intent filters": [
        r"intent-filter",
        r"getCallingPackage",
        r"getCallingActivity",
    ],
    "deep links": [
        r"CFBundleURLSchemes",
        r"android:scheme",
        r"myapp://",
    ],
    "universal links": [
        r"apple-app-site-association",
        r"applinks:",
    ],
    "app links": [
        r"assetlinks.json",
        r"autoVerify=\"true\"",
    ],
    "authentication flows": [
        r"ASWebAuthenticationSession",
        r"CustomTabs",
        r"PKCE",
        r"oauth",
    ],
    "session handling": [
        r"session",
        r"logout",
        r"background",
        r"timeout",
    ],
    "token storage": [
        r"token",
        r"refreshToken",
        r"accessToken",
    ],
}

# Mock announcements covering the 17 mobile security categories
MOCK_ANNOUNCEMENTS = [
    {
        "id": "SEC-MOCK-STORAGE",
        "category": "secure storage",
        "title": "Secure Storage Update: Deprecating Insecure SharedPreferences and Plaintext Databases",
        "description": "Mobile applications must encrypt all sensitive localized data. Standard SharedPreferences and plain SQLite databases are declared insecure on rooted devices, requiring migration to SQLCipher or EncryptedSharedPreferences.",
        "link": "https://developer.android.com/topic/security/data",
        "pubDate": "Mon, 15 Jun 2026 10:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-KEYCHAIN",
        "category": "Keychain",
        "title": "iOS Keychain Security Enhancement: Enforcing ThisDeviceOnly Protection Classes",
        "description": "Apple developer security directives mandate migrating keychain items to kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly to prevent unauthorized backup restoration on secondary physical devices.",
        "link": "https://developer.apple.com/documentation/security/keychain_services",
        "pubDate": "Wed, 17 Jun 2026 11:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-KEYSTORE",
        "category": "Android Keystore",
        "title": "Android Keystore System: Mandatory Hardware-Backed Key Attestation",
        "description": "New security guidelines mandate hardware-backed (TEE or StrongBox) KeyGenParameterSpec initialization. Cryptographic keys used for financial or identity payload signing must enforce user authentication requirements.",
        "link": "https://developer.android.com/training/articles/keystore",
        "pubDate": "Fri, 19 Jun 2026 12:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-BIOMETRIC",
        "category": "biometric authentication",
        "title": "Biometric Authentication: Transitioning to Crypto-Backed Secure Biometric Verification",
        "description": "To prevent runtime instrumentation and hook-based bypasses of boolean returns, developers must wrap biometric prompts in a strong Keystore/Keychain CryptoObject or SecAccessControl structure.",
        "link": "https://developer.android.com/training/sign-in/biometric-auth",
        "pubDate": "Mon, 22 Jun 2026 09:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-PINNING",
        "category": "certificate pinning",
        "title": "Certificate Pinning Guidelines: Mandating Subject Public Key Info (SPKI) Pinning",
        "description": "Leaf-level certificate pinning is discouraged due to rotational outages. Best practices mandate pinning the Subject Public Key Info (SPKI) of the primary and backup intermediate certificate authorities.",
        "link": "https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning",
        "pubDate": "Wed, 24 Jun 2026 14:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-JAILBREAK",
        "category": "jailbreak detection",
        "title": "iOS Jailbreak Detection: Comprehensive Dynamic Linker and Sandbox Write Auditing",
        "description": "Static file existence checks are easily bypassed. Jailbreak detection must combine dynamic library (dyld) inspection, directory permission checking, and symlink integrity checks.",
        "link": "https://mas.owasp.org/MASVS/",
        "pubDate": "Fri, 26 Jun 2026 15:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-ROOT",
        "category": "root detection",
        "title": "Android Root Detection: Mandating Play Integrity API Attestation in Production",
        "description": "Local heuristic root checks are unreliable against modern zygisk masking. High-security apps must leverage the Play Integrity API with cryptographically verified backend tokens.",
        "link": "https://developer.android.com/google/play/integrity",
        "pubDate": "Mon, 29 Jun 2026 10:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-SSL",
        "category": "SSL configuration",
        "title": "SSL Configuration Policies: Global Enforcement of TLS 1.3 and Disabling Cleartext Traffic",
        "description": "Mobile app stores enforce secure networking by default. Cleartext HTTP traffic must be disabled globally, and minimum TLS versions must be set to TLS 1.2 or TLS 1.3.",
        "link": "https://developer.android.com/training/articles/security-config",
        "pubDate": "Wed, 01 Jul 2026 11:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-BACKUP",
        "category": "backup rules",
        "title": "Insecure Backup Controls: Configuring Data Extraction Rules and Backup Exclusions",
        "description": "By default, ADB backup transfers application local databases. Developers must declare allowBackup='false' or configure precise dataExtractionRules to exclude credentials.",
        "link": "https://developer.android.com/guide/topics/data/autobackup",
        "pubDate": "Fri, 03 Jul 2026 13:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-EXPORTED",
        "category": "exported activities",
        "title": "Exported Activities Security: Mandatory Explicit Export Controls on Android Components",
        "description": "Unintentionally exported activities expose internal application boundaries. Developers must set android:exported='false' for all internal classes to prevent intent injection.",
        "link": "https://developer.android.com/guide/components/activities/intro-activities",
        "pubDate": "Mon, 06 Jul 2026 14:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-INTENT",
        "category": "intent filters",
        "title": "Intent Filter Audits: Preventing Implicit Intent Spoofing and Component Hijacking",
        "description": "Implicit intent filters automatically expose components. To secure transitions, apps should use explicit class intents and enforce signature-level custom permissions.",
        "link": "https://developer.android.com/guide/components/intents-filters",
        "pubDate": "Wed, 08 Jul 2026 16:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-DEEP",
        "category": "deep links",
        "title": "Deep Link Security: Preventing Custom URL Scheme Hijacking and Parameter Injection",
        "description": "Custom URL schemes can be registered by other applications, leading to session hijacking. All parameters must be parsed, validated, and sanitized as untrusted inputs.",
        "link": "https://developer.android.com/training/app-links/deep-linking",
        "pubDate": "Fri, 10 Jul 2026 12:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-UNIVERSAL",
        "category": "universal links",
        "title": "Universal Links Verification: Secure Association via AASA Hosting on iOS",
        "description": "To prevent custom scheme hijacking, applications must adopt Universal Links verified by a secure apple-app-site-association file hosted on the target web domain.",
        "link": "https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html",
        "pubDate": "Mon, 13 Jul 2026 10:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-APP",
        "category": "app links",
        "title": "Android App Links Verification: Digitally Binding Web Domains via Digital Asset Links",
        "description": "Android App Links enable secure routing. The host domain must publish a digital assetlinks.json containing the application's unique signing key certificate fingerprint.",
        "link": "https://developer.android.com/training/app-links/verify-site-associations",
        "pubDate": "Wed, 15 Jul 2026 11:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-AUTH",
        "category": "authentication flows",
        "title": "Authentication Flows: Requiring OAuth 2.1 and PKCE to Secure Mobile Auth Tokens",
        "description": "Hardcoding client secrets inside mobile binaries is a critical risk. Modern mobile authentication must use Proof Key for Code Exchange (PKCE) over secure system browser custom tabs.",
        "link": "https://oauth.net/2/pkce/",
        "pubDate": "Fri, 17 Jul 2026 09:00:00 PDT",
    },
    {
        "id": "SEC-MOCK-SESSION",
        "category": "session handling",
        "title": "Session Handling Standards: Mandatory Server-Side Session Invalidation and Snapshot Protection",
        "description": "Client-side token deletion is insufficient. Applications must perform server-side session invalidation on logout and blur multitasking window snapshots to prevent data leakage.",
        "link": "https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html",
        "pubDate": "Mon, 20 Jul 2026 15:00:00 GMT",
    },
    {
        "id": "SEC-MOCK-TOKEN",
        "category": "token storage",
        "title": "Token Storage Policies: Securing Long-Lived Refresh Tokens and Session Credentials",
        "description": "Session and refresh tokens must be stored in encrypted enclaves (Keychain/Keystore) rather than local cache directories or standard plists. Access tokens must be short-lived.",
        "link": "https://mas.owasp.org/MASVS/",
        "pubDate": "Wed, 22 Jul 2026 13:00:00 PDT",
    },
]


def scan_codebase_for_security_signals(start_dir="."):
    """Scans the codebase for files containing signals related to each of the 17 security categories."""
    matches = {cat: [] for cat in TRACKED_CATEGORIES}
    exclude_dirs = {
        "node_modules",
        "Pods",
        ".git",
        "build",
        "DerivedData",
        "vendor",
        ".dart_tool",
        "Carthage",
        "androidTest",
        "__tests__",
        "dist",
    }

    # Compile the signal patterns
    compiled_signals = {
        cat: [re.compile(pattern, re.IGNORECASE) for pattern in patterns]
        for cat, patterns in CATEGORY_SIGNALS.items()
    }

    for root, dirs, files in os.walk(start_dir):
        # Prune excluded directories in-place
        dirs[:] = [d for d in dirs if d not in exclude_dirs and not d.endswith("Tests")]

        for file in files:
            # Check applicable file types
            if not file.endswith(
                (
                    ".kt",
                    ".java",
                    ".xml",
                    ".gradle",
                    ".kts",
                    ".json",
                    ".js",
                    ".ts",
                    ".swift",
                    ".m",
                    ".h",
                    ".plist",
                    ".entitlements",
                    ".md",
                )
            ):
                continue

            filepath = os.path.join(root, file)
            # Skip monitor scripts to avoid self-referencing matches
            if "monitor-security" in file or "monitor-security-test" in file:
                continue

            try:
                with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
                    for i, line in enumerate(f, 1):
                        for cat, patterns in compiled_signals.items():
                            for pattern in patterns:
                                if pattern.search(line):
                                    matches[cat].append(
                                        {
                                            "file": filepath,
                                            "line_num": i,
                                            "content": line.strip()[:100],
                                            "matched_pattern": pattern.pattern,
                                        }
                                    )
                                    # Break to avoid duplicate entry for the same line and category
                                    break
            except Exception:
                pass
    return matches


def parse_rss_feed(url):
    """Fetches and parses live RSS or Atom XML feeds."""
    items = []
    try:
        req = urllib.request.Request(
            url, headers={"User-Agent": "Mozilla/5.0 (SecurityComplianceMonitor/1.0)"}
        )
        with urllib.request.urlopen(req, timeout=10) as response:
            xml_data = response.read()
            root = ET.fromstring(xml_data)

            def clean_tag(tag):
                return tag.split("}", 1)[1] if "}" in tag else tag

            for elem in root.iter():
                tag = clean_tag(elem.tag)
                if tag in ("item", "entry"):
                    title = ""
                    desc = ""
                    link = ""
                    pub_date = ""

                    for child in elem:
                        ctag = clean_tag(child.tag)
                        if ctag == "title":
                            title = child.text or ""
                        elif ctag in ("description", "summary", "content"):
                            desc = child.text or ""
                        elif ctag == "link":
                            link_val = child.get("href")
                            link = link_val if link_val else (child.text or "")
                        elif ctag in ("pubDate", "published", "updated"):
                            pub_date = child.text or ""

                    items.append(
                        {
                            "title": title.strip(),
                            "description": desc.strip() if desc else "",
                            "link": link.strip(),
                            "pubDate": pub_date.strip(),
                        }
                    )
    except Exception as e:
        print(f"Warning: Failed to fetch live feed {url}: {e}", file=sys.stderr)
    return items


def classify_announcements(announcements, keywords_filter=None):
    """Classifies incoming announcements into the 17 mobile security requirement categories."""
    classified_updates = []

    for ann in announcements:
        title = ann.get("title", "")
        desc = ann.get("description", "")
        text_to_search = (title + " " + desc).lower()

        # If keywords_filter is supplied, verify if any filter matches
        if keywords_filter:
            if not any(k.lower() in text_to_search for k in keywords_filter):
                continue

        # Match against categories
        matched_categories = []
        for cat, keywords in CATEGORY_KEYWORDS.items():
            for kw in keywords:
                if kw.lower() in text_to_search:
                    matched_categories.append(cat)
                    break  # Break keyword loop for this category

        # If a pre-set category exists on mock and no matched categories, use that category
        if not matched_categories and ann.get("category"):
            matched_categories.append(ann["category"])

        if matched_categories:
            for cat in matched_categories:
                classified_updates.append(
                    {
                        "id": ann.get("id", "SEC-UPDATE-" + str(hash(title))[:6]),
                        "category": cat,
                        "title": title,
                        "description": desc,
                        "link": ann.get("link", ""),
                        "pubDate": ann.get("pubDate", ""),
                    }
                )
    return classified_updates


def generate_pull_request_draft(updates, scan_results):
    """Generates a draft of a pull request complying with the exact 15 required sections."""
    citations_list = []
    affected_files_set = set()
    migration_steps = []
    impl_checklist = []
    risk_assessment = []
    processed_categories = set()

    for idx, u in enumerate(updates, 1):
        cat = u["category"]
        citations_list.append(
            f"- **{cat}**: [{u['title']}]({u['link']}) (Published: {u['pubDate']})"
        )

        # Pull affected files
        files = scan_results.get(cat, [])
        if files:
            for f in files:
                affected_files_set.add(f["file"])

        if cat in processed_categories:
            continue
        processed_categories.add(cat)

        # Category-specific migration details
        if cat == "secure storage":
            migration_steps.append(
                f"- **{cat}**: Migrate sensitive localized storage from plaintext UserDefaults/SharedPreferences to Jetpack EncryptedSharedPreferences (Android) or iOS Keychain."
            )
            impl_checklist.append(
                "- [ ] Replace plain SharedPreferences calls with EncryptedSharedPreferences."
            )
            risk_assessment.append(
                f"- *{cat}*: Extraction of user session credentials from the file system on compromised or backed-up devices."
            )
        elif cat == "Keychain":
            migration_steps.append(
                f"- **{cat}**: Audit and enforce `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly` on all newly added iOS Keychain entries."
            )
            impl_checklist.append(
                "- [ ] Configure kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly on iOS Keychain items."
            )
            risk_assessment.append(
                f"- *{cat}*: Unauthorized keychain migration to other physical devices during system backups."
            )
        elif cat == "Android Keystore":
            migration_steps.append(
                f"- **{cat}**: Initialize KeyGenParameterSpec with hardware-backed StrongBox protection and enforce biometric user authentication."
            )
            impl_checklist.append(
                "- [ ] Configure KeyGenParameterSpec with StrongBox-backed hardware parameters."
            )
            risk_assessment.append(
                f"- *{cat}*: Extraction of cryptographic keys from memory if the key is not hardware-enclave isolated."
            )
        elif cat == "biometric authentication":
            migration_steps.append(
                f"- **{cat}**: Secure biometric auth with a Keystore CryptoObject rather than rely on vulnerable runtime boolean returns."
            )
            impl_checklist.append(
                "- [ ] Implement CryptoObject-backed BiometricPrompt authentication."
            )
            risk_assessment.append(
                f"- *{cat}*: Runtime bypass using hooking engines like Frida if the biometric check merely checks a return value."
            )
        elif cat == "certificate pinning":
            migration_steps.append(
                f"- **{cat}**: Pin Subject Public Key Info (SPKI) hashes in network security configs instead of leaf certificates."
            )
            impl_checklist.append(
                "- [ ] Configure SPKI hashes in network_security_config.xml and NSPinnedDomains in Info.plist."
            )
            risk_assessment.append(
                f"- *{cat}*: Traffic interception or server spoofing if trust anchors are compromised."
            )
        elif cat == "jailbreak detection":
            migration_steps.append(
                f"- **{cat}**: Implement multi-layered jailbreak audits covering file paths, directory permissions, and dynamic linker library loading."
            )
            impl_checklist.append(
                "- [ ] Add multi-layered jailbreak detection heuristic checks on iOS."
            )
            risk_assessment.append(
                f"- *{cat}*: Execution on heavily compromised platforms exposing client-side secure boundaries."
            )
        elif cat == "root detection":
            migration_steps.append(
                f"- **{cat}**: Integrate Google Play Integrity API and implement backend token validation to detect rooted/compromised environments."
            )
            impl_checklist.append(
                "- [ ] Integrate Google Play Integrity verification workflows."
            )
            risk_assessment.append(
                f"- *{cat}*: Bypassed client-side heuristic checks due to advanced rooting bypass frameworks."
            )
        elif cat == "SSL configuration":
            migration_steps.append(
                f"- **{cat}**: Disable cleartext HTTP traffic globally in the manifest and configuration files, enforcing TLS 1.2+."
            )
            impl_checklist.append(
                "- [ ] Disable usesCleartextTraffic in AndroidManifest.xml and verify ATS in Info.plist."
            )
            risk_assessment.append(
                f"- *{cat}*: Credential sniffing or traffic modification over unencrypted HTTP channels."
            )
        elif cat == "backup rules":
            migration_steps.append(
                f"- **{cat}**: Configure precise data extraction rules or set allowBackup to false to block database leaks."
            )
            impl_checklist.append(
                "- [ ] Configure dataExtractionRules to exclude credentials and local SQLite databases."
            )
            risk_assessment.append(
                f"- *{cat}*: Extraction of private sandboxed files via standard ADB backup extractions."
            )
        elif cat == "exported activities":
            migration_steps.append(
                f"- **{cat}**: Review AndroidManifest.xml; enforce exported='false' on all internal components."
            )
            impl_checklist.append(
                "- [ ] Set android:exported=false for all non-launcher activities."
            )
            risk_assessment.append(
                f"- *{cat}*: External apps launching internal flows to bypass authentication states."
            )
        elif cat == "intent filters":
            migration_steps.append(
                f"- **{cat}**: Protect implicit intent filters using custom signature-level permissions."
            )
            impl_checklist.append(
                "- [ ] Enforce signature-level permissions on any exported intent filters."
            )
            risk_assessment.append(
                f"- *{cat}*: Interception, spoofing, or hijacking of implicit intent components by other apps."
            )
        elif cat == "deep links":
            migration_steps.append(
                f"- **{cat}**: Sanitize all incoming deep link parameters and avoid using them for sensitive operations."
            )
            impl_checklist.append(
                "- [ ] Add strict input sanitization on deep link parameter parsers."
            )
            risk_assessment.append(
                f"- *{cat}*: Parameter injection or cross-site scripting-like exploits within web rendering modules."
            )
        elif cat == "universal links":
            migration_steps.append(
                f"- **{cat}**: Implement verified Universal Links with a valid apple-app-site-association file to secure routing."
            )
            impl_checklist.append(
                "- [ ] Host a secure apple-app-site-association file at the target web domain."
            )
            risk_assessment.append(
                f"- *{cat}*: Custom URL scheme hijacking if another app registers the same custom link protocol."
            )
        elif cat == "app links":
            migration_steps.append(
                f"- **{cat}**: Implement verified Android App Links with a digitally signed assetlinks.json file on the host domain."
            )
            impl_checklist.append(
                "- [ ] Publish the digital assetlinks.json with the correct signing certificate fingerprint."
            )
            risk_assessment.append(
                f"- *{cat}*: Platform disambiguation dialogues and custom scheme hijacking on Android."
            )
        elif cat == "authentication flows":
            migration_steps.append(
                f"- **{cat}**: Implement Proof Key for Code Exchange (PKCE) over secure system browsers (Custom Tabs / ASWebAuthenticationSession)."
            )
            impl_checklist.append(
                "- [ ] Configure OAuth 2.1 client with PKCE challenge/verifier code generation."
            )
            risk_assessment.append(
                f"- *{cat}*: Interception of authorization codes and leakage of client credentials inside source code."
            )
        elif cat == "session handling":
            migration_steps.append(
                f"- **{cat}**: Perform complete server-side session invalidation on logout and blur background app snapshot views."
            )
            impl_checklist.append(
                "- [ ] Add background multitasking blur window transitions to protect user data from snapshots."
            )
            risk_assessment.append(
                f"- *{cat}*: Leaking sensitive UI layouts inside system multitasking views or session hijacking due to orphan backend sessions."
            )
        elif cat == "token storage":
            migration_steps.append(
                f"- **{cat}**: Isolate refresh tokens inside a secure hardware-backed database vault or encrypted preferences."
            )
            impl_checklist.append(
                "- [ ] Save access and refresh tokens inside encrypted vaults with short-lived access periods."
            )
            risk_assessment.append(
                f"- *{cat}*: Loss of user account custody if refresh tokens leak from persistent cache storage."
            )

    citations_str = "\n".join(citations_list)

    if affected_files_set:
        affected_files_str = "\n".join(
            f"- `{f}`" for f in sorted(list(affected_files_set))
        )
    else:
        affected_files_str = "- *No specific files containing matching category patterns were automatically detected. (Perform manual review of configuration variables).* "

    migration_steps_str = "\n".join(migration_steps)
    impl_checklist_str = "\n".join(impl_checklist)
    risk_assessment_str = "\n".join(risk_assessment)

    pr_template = f"""# PULL REQUEST DRAFT: Mobile Security Requirements Compliance Update

## 1. Summary
This pull request brings the application into complete compliance with all monitored mobile security requirements. It addresses secure storage, hardware keystore backing, certificate pinning, backup configurations, exported activity boundaries, and secure deep linking to mitigate critical platform vulnerabilities.

## 2. Background
Mobile platforms are subjected to extensive reverse engineering and dynamic analysis. Hardcoded secrets, unencrypted cache databases, insecure backups, and unvalidated deep links pose serious security risks and conflict with App Store and Google Play privacy/security requirements.

## 3. Regulatory change
- **Mobile Platform Security Frameworks**: Alignment with modern OWASP MASVS (Mobile Application Security Verification Standard) guidelines.
- **Privacy and Data Protection**: Mandatory isolation of sensitive user tokens inside hardware-backed containers (TEE/StrongBox/Secure Enclave) and enforcement of strict backup extraction exclusions.

## 4. Official citations
{citations_str}

## 5. Affected files
{affected_files_str}

## 6. Risk assessment
{risk_assessment_str}
- **Overall Standing**: High risk of credential harvesting and data exposure on rooted/compromised platforms if insecure storage or fallback methods are used.

## 7. Migration steps
{migration_steps_str}

## 8. Backward compatibility
All proposed security upgrades are fully backward-compatible. Hardware-backed features (StrongBox/Secure Enclave) automatically fallback gracefully to software-backed key generation or standard OS keychain on legacy devices without causing application crashes.

## 9. Implementation checklist
{impl_checklist_str}
- [ ] Re-run the automated compliance guard checks locally.

## 10. Testing checklist
- [ ] Verify that secure storage databases (SQLCipher/EncryptedSharedPreferences) mount successfully.
- [ ] Simulate device background transitions and confirm the UI multitask preview blurs correctly.
- [ ] Test the logout workflow and verify that the local session is completely purged and server sessions are invalidated.
- [ ] Verify certificate pinning SPKI hashes block connections when an untrusted proxy is active.

## 11. Documentation checklist
- [ ] Update `docs/SECURITY-POLICY-MIGRATION.md` with the completed checklists.
- [ ] Document the backup exclusion rules and network security configuration settings in the development guidelines.

## 12. Compliance impact
- **OWASP MASVS Aligned**: Ensures the repository satisfies the L1 and L2 security controls.
- **Account Protection**: Mitigates compliance strikes, securing our publishing credentials.
- **User Safety**: Prevents session theft and data leakage, protecting user trust.

## 13. Breaking changes
- Standard ADB backups will no longer pull application databases, which may impact legacy developer debugging flows.
- Unencrypted SharedPreferences are migrated to EncryptedSharedPreferences, resetting localized user configurations during the update.

## 14. Review checklist
- [ ] Code is 100% free of emojis or graphical symbols in comments and files.
- [ ] Encryption keys are hardware-backed and never printed in diagnostic log buffers.
- [ ] Activities and intent-filters are closed by default unless strictly required.

## 15. Approver recommendations
Verify that the production certificate authority (CA) SPKI hashes match the values declared in the network configuration files. Confirm that all background transitions and logout handlers destroy cached memory references to sensitive token vectors.
"""
    return pr_template


SIMULATED_NOTICE = [
    "",
    "> **Simulated output, not live announcements.** This file was generated from sample",
    "> announcements (the built-in set, the default, or a file passed with `--mock`). The titles,",
    "> publish dates, and descriptions below are examples that show the shape of a migration",
    "> report, not real publications. Only the linked official documentation URLs are real.",
    "> This monitor has no live feed (Android security bulletins publish no RSS), so check the",
    "> linked official pages before treating anything here as an actual requirement.",
    "",
]


def update_documentation_report(updates, output_filepath, is_simulated=False):
    """Overwrites or updates the migration report in docs/SECURITY-POLICY-MIGRATION.md."""
    lines = [
        "<!-- SECURITY_POLICY_MONITOR_START -->",
    ]
    if is_simulated:
        lines.extend(SIMULATED_NOTICE)
    lines.extend([
        "# Mobile Security Requirements Policy Migration & Report",
        "",
        "This report is continuously generated and updated by `scripts/monitor-security.py` to track compliance areas.",
        "",
        "## Monitored Security Requirements Update Log",
        "",
    ])

    for idx, u in enumerate(updates, 1):
        lines.append(f"### {idx}. [{u['category']}] {u['title']}")
        lines.append(f"- **Published Date**: {u['pubDate']}")
        lines.append(f"- **Official Resource**: [{u['link']}]({u['link']})")
        lines.append(f"- **Description**: {u['description']}")
        lines.append("")

    lines.append("## Automated Migration Recommendations & Implementation Tasks")
    lines.append("")

    processed_doc_cats = set()
    for u in updates:
        cat = u["category"]
        if cat in processed_doc_cats:
            continue
        processed_doc_cats.add(cat)
        lines.append(f"### Tasks for {cat}")
        lines.append(
            "- **Regulatory Impact**: High priority. Security audit mandates action."
        )

        if cat == "secure storage":
            lines.append(
                "- [ ] **Task 1**: Update standard preferences to EncryptedSharedPreferences on Android."
            )
            lines.append(
                "- [ ] **Task 2**: Test local database encryption with SQLCipher."
            )
        elif cat == "Keychain":
            lines.append(
                "- [ ] **Task 1**: Configure Keychain accessibility to kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly."
            )
        elif cat == "Android Keystore":
            lines.append(
                "- [ ] **Task 1**: Enforce StrongBox and check KeyInfo.isInsideSecureHardware() on key creation."
            )
        elif cat == "biometric authentication":
            lines.append(
                "- [ ] **Task 1**: Integrate Keystore CryptoObject-backed BiometricPrompt."
            )
        elif cat == "certificate pinning":
            lines.append(
                "- [ ] **Task 1**: Populate SPKI pins inside network_security_config.xml."
            )
        else:
            lines.append(
                f"- [ ] **Task**: Verify that all security criteria for {cat} are checked and handled."
            )
        lines.append("")

    lines.append("<!-- SECURITY_POLICY_MONITOR_END -->")

    try:
        with open(output_filepath, "w", encoding="utf-8") as f:
            f.write("\n".join(lines))
        print(f"Security documentation updated successfully at: {output_filepath}")
    except Exception as e:
        print(f"Error writing documentation to {output_filepath}: {e}", file=sys.stderr)


def main():
    parser = argparse.ArgumentParser(
        description="Monitor all Mobile Security Requirements"
    )
    parser.add_argument(
        "--live", action="store_true", help="No live security feed exists (Android security bulletins publish no RSS), so this still uses the sample announcements and labels the report as simulated"
    )
    parser.add_argument(
        "--mock",
        type=str,
        help="Path to custom mock announcements JSON file, or 'inline' to use default mock data",
    )
    parser.add_argument(
        "--keywords",
        type=str,
        help="Optional comma-separated keywords to filter updates",
    )
    parser.add_argument(
        "--dir", type=str, default=".", help="Codebase directory to scan"
    )
    parser.add_argument(
        "--output-docs",
        type=str,
        default=None,
        help="Filepath to write migration tasks and logs",
    )
    parser.add_argument(
        "--pr-output",
        type=str,
        default=None,
        help="Filepath to save the drafted PR",
    )

    args = parser.parse_args()

    # 1. Gather announcements
    announcements = []

    if args.live:
        print("No live security feed exists (Android security bulletins publish no RSS); using sample announcements.")
        # Android Security Bulletins publish no RSS feed. The canonical page is
        # https://source.android.com/docs/security/bulletin/asb-overview (checked live).

    # Fallback to mock data if live has no updates or mock is explicitly requested
    used_mock = False
    if args.mock or (not args.live and not args.mock) or not announcements:
        used_mock = True
        print("Data. sample announcements built into this script, not live news.")
        print(
            "Using comprehensive mock Security policy updates for compliance scanning..."
        )
        if args.mock and args.mock != "inline" and os.path.exists(args.mock):
            try:
                with open(args.mock, "r") as f:
                    announcements.extend(json.load(f))
            except Exception as e:
                print(
                    f"Failed to read mock file {args.mock}: {e}, using default mock dataset instead.",
                    file=sys.stderr,
                )
                announcements.extend(MOCK_ANNOUNCEMENTS)
        else:
            announcements.extend(MOCK_ANNOUNCEMENTS)
    else:
        print("Data. live feeds, fetched just now.")

    # 2. Classify updates into the 17 required categories
    keywords_filter = (
        [k.strip() for k in args.keywords.split(",")] if args.keywords else None
    )
    classified_updates = classify_announcements(announcements, keywords_filter)

    if not classified_updates:
        print("No classified updates matched the current filters.")
        sys.exit(0)

    print(
        f"Monitored and classified {len(classified_updates)} security requirement updates:"
    )
    for idx, u in enumerate(classified_updates, 1):
        print(f" {idx}. [{u['category']}] {u['title']}")

    # 3. Scan the codebase for signals related to these categories
    print(f"Scanning codebase under '{args.dir}' for security integration signals...")
    scan_results = scan_codebase_for_security_signals(args.dir)

    total_matches = sum(len(matches) for matches in scan_results.values())
    print(f"Found {total_matches} signal matches in code.")

    # 4. Write/Update documentation
    if args.output_docs:
        os.makedirs(os.path.dirname(args.output_docs) or ".", exist_ok=True)
        update_documentation_report(classified_updates, args.output_docs, is_simulated=used_mock)
    else:
        print("No file written. Pass --output-docs <path> to save this report.")

    # 5. Generate Pull Request draft
    pr_draft = generate_pull_request_draft(classified_updates, scan_results)
    if used_mock:
        pr_draft = "\n".join(SIMULATED_NOTICE[1:]) + "\n" + pr_draft

    if args.pr_output:
        try:
            os.makedirs(os.path.dirname(args.pr_output) or ".", exist_ok=True)
            with open(args.pr_output, "w", encoding="utf-8") as f:
                f.write(pr_draft)
            print(f"PR draft written successfully to: {args.pr_output}")
        except Exception as e:
            print(f"Failed to write PR draft to {args.pr_output}: {e}", file=sys.stderr)
    else:
        print("No PR draft written. Pass --pr-output <path> to save it.")


if __name__ == "__main__":
    main()