From cada22dfa6888e3e67bb54ecf6587a78e662d386 Mon Sep 17 00:00:00 2001 From: Zacgoose <107489668+Zacgoose@users.noreply.github.com> Date: Thu, 8 Jan 2026 08:35:24 +0800 Subject: [PATCH] Refactor config retrieval to use background messaging Updated content and detection rules manager scripts to retrieve configuration and branding data via chrome.runtime messaging to the background script, ensuring merged enterprise and local config is used. DetectionRulesManager now accepts a ConfigManager instance for improved config access. Fallbacks to local storage remain for robustness. --- scripts/background.js | 2 +- scripts/content.js | 108 +++++++++++++++++---- scripts/modules/detection-rules-manager.js | 14 ++- 3 files changed, 102 insertions(+), 22 deletions(-) diff --git a/scripts/background.js b/scripts/background.js index 8d50fe83..ada20995 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -288,7 +288,7 @@ class CheckBackground { constructor() { this.configManager = new ConfigManager(); this.policyManager = new PolicyManager(); - this.detectionRulesManager = new DetectionRulesManager(); + this.detectionRulesManager = new DetectionRulesManager(this.configManager); this.rogueAppsManager = new RogueAppsManager(); this.webhookManager = new WebhookManager(this.configManager); this.isInitialized = false; diff --git a/scripts/content.js b/scripts/content.js index 60a25222..7e21dd21 100644 --- a/scripts/content.js +++ b/scripts/content.js @@ -583,9 +583,25 @@ if (window.checkExtensionLoaded) { */ async function loadDeveloperConsoleLoggingSetting() { try { + // Request config from background to get merged enterprise + local config const config = await new Promise((resolve) => { - chrome.storage.local.get(["config"], (result) => { - resolve(result.config || {}); + chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => { + if (chrome.runtime.lastError) { + logger.log( + `[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}` + ); + // Fallback to local storage if background not available + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else if (!response || !response.success) { + // Fallback to local storage if response invalid + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else { + resolve(response.config); + } }); }); @@ -5580,12 +5596,27 @@ if (window.checkExtensionLoaded) { // Set flag to prevent DOM monitoring loops showingBanner = true; - // Fetch branding configuration (uniform pattern: storage only, like applyBrandingColors) + // Fetch branding configuration from background to get merged config const fetchBranding = () => new Promise((resolve) => { try { - chrome.storage.local.get(["brandingConfig"], (result) => { - resolve(result?.brandingConfig || {}); + chrome.runtime.sendMessage({ type: "GET_BRANDING_CONFIG" }, (response) => { + if (chrome.runtime.lastError) { + logger.log( + `[M365-Protection] Error getting branding from background: ${chrome.runtime.lastError.message}` + ); + // Fallback to local storage if background not available + chrome.storage.local.get(["brandingConfig"], (result) => { + resolve(result?.brandingConfig || {}); + }); + } else if (!response || !response.success) { + // Fallback to local storage if response invalid + chrome.storage.local.get(["brandingConfig"], (result) => { + resolve(result?.brandingConfig || {}); + }); + } else { + resolve(response.branding || {}); + } }); } catch (_) { resolve({}); @@ -5902,10 +5933,25 @@ if (window.checkExtensionLoaded) { validBadgeTimeoutId = null; } - // Load timeout configuration + // Load timeout configuration from background to get merged config const config = await new Promise((resolve) => { - chrome.storage.local.get(["config"], (result) => { - resolve(result.config || {}); + chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => { + if (chrome.runtime.lastError) { + logger.log( + `[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}` + ); + // Fallback to local storage if background not available + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else if (!response || !response.success) { + // Fallback to local storage if response invalid + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else { + resolve(response.config); + } }); }); @@ -6281,15 +6327,28 @@ if (window.checkExtensionLoaded) { return; } - // Get CIPP configuration from storage - const result = await new Promise((resolve) => { - chrome.storage.local.get(["config"], (result) => { - resolve(result.config || {}); + // Get CIPP configuration from background to get merged config + const config = await new Promise((resolve) => { + chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => { + if (chrome.runtime.lastError) { + logger.log( + `[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}` + ); + // Fallback to local storage if background not available + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else if (!response || !response.success) { + // Fallback to local storage if response invalid + chrome.storage.local.get(["config"], (result) => { + resolve(result.config || {}); + }); + } else { + resolve(response.config); + } }); }); - const config = result; - // Check if CIPP reporting is enabled and URL is configured if (!config.enableCippReporting || !config.cippServerUrl) { logger.debug("CIPP reporting disabled or no server URL configured"); @@ -6348,10 +6407,25 @@ if (window.checkExtensionLoaded) { */ async function applyBrandingColors() { try { - // Get branding configuration from storage + // Get branding configuration from background to get merged config const result = await new Promise((resolve) => { - chrome.storage.local.get(["brandingConfig"], (result) => { - resolve(result.brandingConfig || {}); + chrome.runtime.sendMessage({ type: "GET_BRANDING_CONFIG" }, (response) => { + if (chrome.runtime.lastError) { + logger.log( + `[M365-Protection] Error getting branding from background: ${chrome.runtime.lastError.message}` + ); + // Fallback to local storage if background not available + chrome.storage.local.get(["brandingConfig"], (result) => { + resolve(result?.brandingConfig || {}); + }); + } else if (!response || !response.success) { + // Fallback to local storage if response invalid + chrome.storage.local.get(["brandingConfig"], (result) => { + resolve(result?.brandingConfig || {}); + }); + } else { + resolve(response.branding || {}); + } }); }); diff --git a/scripts/modules/detection-rules-manager.js b/scripts/modules/detection-rules-manager.js index 97ad58e7..ad6d35d3 100644 --- a/scripts/modules/detection-rules-manager.js +++ b/scripts/modules/detection-rules-manager.js @@ -7,7 +7,7 @@ import { chrome, storage } from "../browser-polyfill.js"; import logger from "../utils/logger.js"; export class DetectionRulesManager { - constructor() { + constructor(configManager = null) { this.cachedRules = null; this.lastUpdate = 0; this.updateInterval = 24 * 60 * 60 * 1000; // Default: 24 hours @@ -16,6 +16,7 @@ export class DetectionRulesManager { this.remoteUrl = "https://raw.githubusercontent.com/CyberDrain/Check/refs/heads/main/rules/detection-rules.json"; this.config = null; + this.configManager = configManager; this.initialized = false; } @@ -53,9 +54,14 @@ export class DetectionRulesManager { async loadConfiguration() { try { - // Load from chrome storage to get user configuration - const result = await storage.local.get(["config"]); - this.config = result?.config || {}; + // Use ConfigManager if available to get merged configuration (enterprise + local) + if (this.configManager) { + this.config = await this.configManager.getConfig(); + } else { + // Fallback to direct storage access if ConfigManager is not available + const result = await storage.local.get(["config"]); + this.config = result?.config || {}; + } // Set remote URL from configuration or use default if (this.config.customRulesUrl) {