Skip to content

Commit 34ca801

Browse files
ZacgooseJohnDuprey
authored andcommitted
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.
1 parent a92bdcc commit 34ca801

File tree

3 files changed

+102
-22
lines changed

3 files changed

+102
-22
lines changed

scripts/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class CheckBackground {
288288
constructor() {
289289
this.configManager = new ConfigManager();
290290
this.policyManager = new PolicyManager();
291-
this.detectionRulesManager = new DetectionRulesManager();
291+
this.detectionRulesManager = new DetectionRulesManager(this.configManager);
292292
this.rogueAppsManager = new RogueAppsManager();
293293
this.webhookManager = new WebhookManager(this.configManager);
294294
this.isInitialized = false;

scripts/content.js

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,25 @@ if (window.checkExtensionLoaded) {
583583
*/
584584
async function loadDeveloperConsoleLoggingSetting() {
585585
try {
586+
// Request config from background to get merged enterprise + local config
586587
const config = await new Promise((resolve) => {
587-
chrome.storage.local.get(["config"], (result) => {
588-
resolve(result.config || {});
588+
chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => {
589+
if (chrome.runtime.lastError) {
590+
logger.log(
591+
`[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}`
592+
);
593+
// Fallback to local storage if background not available
594+
chrome.storage.local.get(["config"], (result) => {
595+
resolve(result.config || {});
596+
});
597+
} else if (!response || !response.success) {
598+
// Fallback to local storage if response invalid
599+
chrome.storage.local.get(["config"], (result) => {
600+
resolve(result.config || {});
601+
});
602+
} else {
603+
resolve(response.config);
604+
}
589605
});
590606
});
591607

@@ -5580,12 +5596,27 @@ if (window.checkExtensionLoaded) {
55805596
// Set flag to prevent DOM monitoring loops
55815597
showingBanner = true;
55825598

5583-
// Fetch branding configuration (uniform pattern: storage only, like applyBrandingColors)
5599+
// Fetch branding configuration from background to get merged config
55845600
const fetchBranding = () =>
55855601
new Promise((resolve) => {
55865602
try {
5587-
chrome.storage.local.get(["brandingConfig"], (result) => {
5588-
resolve(result?.brandingConfig || {});
5603+
chrome.runtime.sendMessage({ type: "GET_BRANDING_CONFIG" }, (response) => {
5604+
if (chrome.runtime.lastError) {
5605+
logger.log(
5606+
`[M365-Protection] Error getting branding from background: ${chrome.runtime.lastError.message}`
5607+
);
5608+
// Fallback to local storage if background not available
5609+
chrome.storage.local.get(["brandingConfig"], (result) => {
5610+
resolve(result?.brandingConfig || {});
5611+
});
5612+
} else if (!response || !response.success) {
5613+
// Fallback to local storage if response invalid
5614+
chrome.storage.local.get(["brandingConfig"], (result) => {
5615+
resolve(result?.brandingConfig || {});
5616+
});
5617+
} else {
5618+
resolve(response.branding || {});
5619+
}
55895620
});
55905621
} catch (_) {
55915622
resolve({});
@@ -5902,10 +5933,25 @@ if (window.checkExtensionLoaded) {
59025933
validBadgeTimeoutId = null;
59035934
}
59045935

5905-
// Load timeout configuration
5936+
// Load timeout configuration from background to get merged config
59065937
const config = await new Promise((resolve) => {
5907-
chrome.storage.local.get(["config"], (result) => {
5908-
resolve(result.config || {});
5938+
chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => {
5939+
if (chrome.runtime.lastError) {
5940+
logger.log(
5941+
`[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}`
5942+
);
5943+
// Fallback to local storage if background not available
5944+
chrome.storage.local.get(["config"], (result) => {
5945+
resolve(result.config || {});
5946+
});
5947+
} else if (!response || !response.success) {
5948+
// Fallback to local storage if response invalid
5949+
chrome.storage.local.get(["config"], (result) => {
5950+
resolve(result.config || {});
5951+
});
5952+
} else {
5953+
resolve(response.config);
5954+
}
59095955
});
59105956
});
59115957

@@ -6281,15 +6327,28 @@ if (window.checkExtensionLoaded) {
62816327
return;
62826328
}
62836329

6284-
// Get CIPP configuration from storage
6285-
const result = await new Promise((resolve) => {
6286-
chrome.storage.local.get(["config"], (result) => {
6287-
resolve(result.config || {});
6330+
// Get CIPP configuration from background to get merged config
6331+
const config = await new Promise((resolve) => {
6332+
chrome.runtime.sendMessage({ type: "GET_CONFIG" }, (response) => {
6333+
if (chrome.runtime.lastError) {
6334+
logger.log(
6335+
`[M365-Protection] Error getting config from background: ${chrome.runtime.lastError.message}`
6336+
);
6337+
// Fallback to local storage if background not available
6338+
chrome.storage.local.get(["config"], (result) => {
6339+
resolve(result.config || {});
6340+
});
6341+
} else if (!response || !response.success) {
6342+
// Fallback to local storage if response invalid
6343+
chrome.storage.local.get(["config"], (result) => {
6344+
resolve(result.config || {});
6345+
});
6346+
} else {
6347+
resolve(response.config);
6348+
}
62886349
});
62896350
});
62906351

6291-
const config = result;
6292-
62936352
// Check if CIPP reporting is enabled and URL is configured
62946353
if (!config.enableCippReporting || !config.cippServerUrl) {
62956354
logger.debug("CIPP reporting disabled or no server URL configured");
@@ -6348,10 +6407,25 @@ if (window.checkExtensionLoaded) {
63486407
*/
63496408
async function applyBrandingColors() {
63506409
try {
6351-
// Get branding configuration from storage
6410+
// Get branding configuration from background to get merged config
63526411
const result = await new Promise((resolve) => {
6353-
chrome.storage.local.get(["brandingConfig"], (result) => {
6354-
resolve(result.brandingConfig || {});
6412+
chrome.runtime.sendMessage({ type: "GET_BRANDING_CONFIG" }, (response) => {
6413+
if (chrome.runtime.lastError) {
6414+
logger.log(
6415+
`[M365-Protection] Error getting branding from background: ${chrome.runtime.lastError.message}`
6416+
);
6417+
// Fallback to local storage if background not available
6418+
chrome.storage.local.get(["brandingConfig"], (result) => {
6419+
resolve(result?.brandingConfig || {});
6420+
});
6421+
} else if (!response || !response.success) {
6422+
// Fallback to local storage if response invalid
6423+
chrome.storage.local.get(["brandingConfig"], (result) => {
6424+
resolve(result?.brandingConfig || {});
6425+
});
6426+
} else {
6427+
resolve(response.branding || {});
6428+
}
63556429
});
63566430
});
63576431

scripts/modules/detection-rules-manager.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { chrome, storage } from "../browser-polyfill.js";
77
import logger from "../utils/logger.js";
88

99
export class DetectionRulesManager {
10-
constructor() {
10+
constructor(configManager = null) {
1111
this.cachedRules = null;
1212
this.lastUpdate = 0;
1313
this.updateInterval = 24 * 60 * 60 * 1000; // Default: 24 hours
@@ -16,6 +16,7 @@ export class DetectionRulesManager {
1616
this.remoteUrl =
1717
"https://raw.githubusercontent.com/CyberDrain/Check/refs/heads/main/rules/detection-rules.json";
1818
this.config = null;
19+
this.configManager = configManager;
1920
this.initialized = false;
2021
}
2122

@@ -53,9 +54,14 @@ export class DetectionRulesManager {
5354

5455
async loadConfiguration() {
5556
try {
56-
// Load from chrome storage to get user configuration
57-
const result = await storage.local.get(["config"]);
58-
this.config = result?.config || {};
57+
// Use ConfigManager if available to get merged configuration (enterprise + local)
58+
if (this.configManager) {
59+
this.config = await this.configManager.getConfig();
60+
} else {
61+
// Fallback to direct storage access if ConfigManager is not available
62+
const result = await storage.local.get(["config"]);
63+
this.config = result?.config || {};
64+
}
5965

6066
// Set remote URL from configuration or use default
6167
if (this.config.customRulesUrl) {

0 commit comments

Comments
 (0)