Skip to content
Draft
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3b4a633
WIP
gcp Aug 5, 2025
e38d646
feat: add enterprise download security telemetry with MOZ_ENTERPRISE …
gcp Aug 22, 2025
ea2854a
feat: add enterprise download telemetry with policy controls
gcp Aug 22, 2025
e7f5908
chore: update Cargo.lock
gcp Aug 25, 2025
02f5632
feat: add TELEMETRY_ENDPOINT env var support for telemetry endpoints
gcp Aug 25, 2025
61dd192
refactor: unify TELEMETRY_ENDPOINT handling across components
gcp Aug 25, 2025
3205509
refactor: use lazy loading for DownloadsTelemetry
gcp Aug 25, 2025
4caf74b
feat: add localhost support for telemetry testing and debugging
gcp Aug 27, 2025
274abf1
feat: prioritize TELEMETRY_ENDPOINT env var over localhost config
gcp Aug 27, 2025
150fcc7
feat: add debug logging for download telemetry
gcp Aug 27, 2025
038925f
fix: Improve error handling and resource path resolution in downloads…
gcp Aug 27, 2025
47cea1a
feat: wrap file_downloaded telemetry in MOZ_ENTERPRISE ifdef
gcp Aug 27, 2025
e484c38
fix: use MOZ_TELEMETRY_REPORTING instead of MOZILLA_OFFICIAL for tele…
gcp Aug 28, 2025
b1f9540
feat: reset telemetry localhost port and expose MOZ_TELEMETRY_REPORTI…
gcp Sep 1, 2025
cc580c9
refactor: move download telemetry to dedicated enterprise ping
gcp Sep 2, 2025
ca80612
fix: use PathUtils as global instead of importing as ES module
gcp Sep 2, 2025
6997c65
POC metric for page printed
Nov 13, 2025
19b231e
Update metric to have better data
Nov 13, 2025
72a29ea
Put print telemetry behind #ifdef MOZ_ENTERPRISE
Nov 14, 2025
f824376
Add and check prefs for metric
Nov 14, 2025
384b4a4
Add content titles to print metric payload
Nov 14, 2025
e6a22fb
Add existing add-on install metric to enterprise pings
Nov 17, 2025
f98868d
Include info section in enterprise ping so console can consume
Nov 20, 2025
da7695d
Allow enterprise prefix policies to be set via policy
Nov 20, 2025
0bd4adb
Enterprise glean client
fiji-flo Nov 20, 2025
6ee43ad
Add metric for browsing a blocked URL
Nov 20, 2025
f3bf7a2
Remove debugging force-enable of download telemetry
Nov 21, 2025
65185b4
Add policy and pref for telemetry of browsing blocked domains
Nov 21, 2025
2090a56
Protect against telemetry exceptions for printing
Nov 21, 2025
eaaa96a
Protect against telemetry exceptions during website filtering
Nov 21, 2025
9b74d2f
Add BlocklistDomainBrowsedTelemetry policy to schema
Nov 21, 2025
9e4617a
Revert defunct changes to redirect telemetry
Nov 21, 2025
d72789d
Remove defunct cargo dependency on local glean
Nov 21, 2025
7da0388
Remove obsolete pref
Nov 21, 2025
4b6aecb
Remove trace log statements
Nov 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ oslog = { path = "build/rust/oslog" }
# Override terminal_size with a dummy crate that returns a fixed 80x25 terminal size.
terminal_size = { path = "build/rust/terminal_size" }

# Patch glean crate with local modifications for custom telemetry server URL.
glean = { path = "third_party/rust/glean" }

# Patch bitflags 1.0 to 2.0
bitflags = { path = "build/rust/bitflags" }

Expand Down
49 changes: 46 additions & 3 deletions browser/components/downloads/DownloadsCommon.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ DownloadsDataCtor.prototype = {
download.error?.becauseBlockedByReputationCheck ||
download.error?.becauseBlockedByContentAnalysis
) {
this._notifyDownloadEvent("error");
this._notifyDownloadEvent("error", { download });
}
},

Expand All @@ -927,7 +927,7 @@ DownloadsDataCtor.prototype = {
download.succeeded ||
(download.error && download.error.becauseBlocked)
) {
this._notifyDownloadEvent("finish");
this._notifyDownloadEvent("finish", { download });
}
}

Expand Down Expand Up @@ -996,11 +996,47 @@ DownloadsDataCtor.prototype = {
* true (default) - open the downloads panel.
* false - only show an indicator notification.
*/
_notifyDownloadEvent(aType, { openDownloadsListOnStart = true } = {}) {
_notifyDownloadEvent(aType, { openDownloadsListOnStart = true, download = null } = {}) {
DownloadsCommon.log(
"Attempting to notify that a new download has started or finished."
);

// DEBUG: Enhanced logging for download telemetry debugging
console.log(`[DownloadsCommon] _notifyDownloadEvent called with aType: ${aType}, download: ${download ? 'present' : 'null'}, succeeded: ${download?.succeeded}`);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the console.log debugging as part of the PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that can all be reduced to the minimum, a lot of this was tracing to see where things were going.


// If this is a finished download, record enterprise telemetry if available
if (aType === "finish" && download && download.succeeded) {
console.log(`[DownloadsCommon] Download finished successfully, attempting to record telemetry`);
console.log(`[DownloadsCommon] Download details - target: ${download.target?.path}, source: ${download.source?.url}, contentType: ${download.contentType}, size: ${download.target?.size}`);

try {
console.log(`[DownloadsCommon] DownloadsTelemetry type: ${typeof lazy.DownloadsTelemetry}, exists: ${lazy.DownloadsTelemetry ? 'yes' : 'no'}`);
console.log(`[DownloadsCommon] recordFileDownloaded type: ${typeof lazy.DownloadsTelemetry?.recordFileDownloaded}`);

if (
typeof lazy.DownloadsTelemetry !== "undefined" &&
lazy.DownloadsTelemetry &&
typeof lazy.DownloadsTelemetry.recordFileDownloaded === "function"
) {
console.log(`[DownloadsCommon] Calling DownloadsTelemetry.recordFileDownloaded`);
lazy.DownloadsTelemetry.recordFileDownloaded(download);
console.log(`[DownloadsCommon] DownloadsTelemetry.recordFileDownloaded completed`);
} else {
console.log(`[DownloadsCommon] DownloadsTelemetry not available or not a function`);
}
} catch (e) {
console.error(`[DownloadsCommon] Error recording download telemetry:`, e);
try {
ChromeUtils.reportError(e);
} catch (reportEx) {
// ChromeUtils.reportError may not be available in all contexts
console.error(`[DownloadsCommon] Could not report error:`, reportEx);
}
}
} else {
console.log(`[DownloadsCommon] Skipping telemetry recording - aType: ${aType}, succeeded: ${download?.succeeded}`);
}

// Show the panel in the most recent browser window, if present.
let browserWin = lazy.BrowserWindowTracker.getTopWindow({
private: this._isPrivate,
Expand Down Expand Up @@ -1068,6 +1104,13 @@ ChromeUtils.defineLazyGetter(lazy, "DownloadsData", function () {
return new DownloadsDataCtor();
});

// Telemetry helper for downloads. Lazy import so we don't require Glean in
// contexts where it's not available.
ChromeUtils.defineESModuleGetters(lazy, {
DownloadsTelemetry:
"moz-src:///browser/components/downloads/DownloadsTelemetry.sys.mjs",
});

// DownloadsViewPrototype

/**
Expand Down
268 changes: 268 additions & 0 deletions browser/components/downloads/DownloadsTelemetry.enterprise.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* Enterprise Downloads Telemetry Implementation
*
* This module is only included in MOZ_ENTERPRISE builds and provides
* security telemetry for completed downloads.
*
* ENTERPRISE POLICY CONFIGURATION:
* ================================
*
* The telemetry collection can be configured via enterprise policy to control
* the level of URL information collected. In the enterprise policies.json file:
*
* {
* "policies": {
* "DownloadTelemetry": {
* "Enabled": true,
* "UrlLogging": "full"
* }
* }
* }
*
* Configuration Options:
* - Enabled (boolean): Enable/disable download telemetry collection
* - UrlLogging (string): URL logging level with values:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this wants FileLogging as well, where there is an intermediate level that just logs the extension and MIME type.

* - "full" (default): Collect complete download URLs including paths and parameters
* - "domain": Collect only the hostname portion of URLs
* - "none": Do not collect any URL information
*
* SECURITY CONSIDERATIONS:
* =======================
*
* - "full" mode provides maximum visibility for security analysis but may
* contain sensitive information in URL paths/parameters
* - "domain" mode balances security monitoring with privacy by limiting
* collection to hostnames only
* - "none" mode disables URL collection entirely for high-privacy environments
*
* The default "full" mode is appropriate for most enterprise environments where
* comprehensive security monitoring is prioritized.
*/

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyServiceGetter(
lazy,
"gMIMEService",
"@mozilla.org/mime;1",
"nsIMIMEService"
);

// PathUtils will be imported lazily when needed

export const DownloadsTelemetryEnterprise = {
/**
* Checks if download telemetry is enabled via enterprise policy.
*
* @returns {boolean} True if telemetry should be collected
*/
_isEnabled() {
return Services.prefs.getBoolPref(
"browser.download.enterprise.telemetry.enabled",
true
);
},

/**
* Gets the configured URL logging level from enterprise policy preferences.
*
* @returns {string} One of: "full", "domain", "none"
*/
_getUrlLoggingPolicy() {
const urlLogging = Services.prefs.getCharPref(
"browser.download.enterprise.telemetry.urlLogging",
"full"
);

// Validate policy value
if (["full", "domain", "none"].includes(urlLogging)) {
return urlLogging;
}

return "full"; // Default to full URL for enterprise environments
},

/**
* Processes the source URL based on the configured logging policy.
*
* @param {string} sourceUrl - The original download URL
* @returns {string|null} Processed URL or null based on policy
*/
_processSourceUrl(sourceUrl) {
if (!sourceUrl) {
return null;
}

const policy = this._getUrlLoggingPolicy();

switch (policy) {
case "none":
return null;

case "domain":
try {
const url = new URL(sourceUrl);
return url.hostname || null;
} catch (ex) {
return null;
}

case "full":
default:
return sourceUrl;
}
},

/**
* Records a telemetry event for a completed file download.
*
* @param {object} download - The Download object containing download information
*/
recordFileDownloaded(download) {
console.log("[DownloadsTelemetryEnterprise] recordFileDownloaded called");

// DEBUG: Force enable telemetry for debugging purposes
console.log(
"[DownloadsTelemetryEnterprise] DEBUG: Force enabling telemetry for debugging"
);
try {
Services.prefs.setBoolPref(
"browser.download.enterprise.telemetry.enabled",
true
);
Services.prefs.setCharPref(
"browser.download.enterprise.telemetry.urlLogging",
"full"
);
console.log(
"[DownloadsTelemetryEnterprise] DEBUG: Telemetry preferences set"
);
} catch (e) {
console.error(
"[DownloadsTelemetryEnterprise] DEBUG: Failed to set prefs:",
e
);
}

// Check if telemetry is enabled via enterprise policy
const isEnabled = this._isEnabled();
console.log(
`[DownloadsTelemetryEnterprise] Telemetry enabled: ${isEnabled}`
);
console.log(
`[DownloadsTelemetryEnterprise] Checking pref: browser.download.enterprise.telemetry.enabled = ${Services.prefs.getBoolPref("browser.download.enterprise.telemetry.enabled", false)}`
);

if (!isEnabled) {
console.log(
"[DownloadsTelemetryEnterprise] Telemetry disabled, not recording"
);
return;
}

try {
console.log(
"[DownloadsTelemetryEnterprise] Processing download for telemetry"
);

// Extract filename from target path
let filename = null;
if (download.target?.path) {
filename = PathUtils.filename(download.target.path);
}
console.log(`[DownloadsTelemetryEnterprise] Filename: ${filename}`);

// Extract file extension
let extension = null;
if (filename) {
const lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex > 0) {
extension = filename.substring(lastDotIndex + 1).toLowerCase();
}
}
console.log(`[DownloadsTelemetryEnterprise] Extension: ${extension}`);

// Get MIME type with fallback to extension-based detection
let mimeType = download.contentType || null;
if (!mimeType && extension) {
try {
mimeType = lazy.gMIMEService.getTypeFromExtension(extension);
} catch (ex) {
// MIME service failed, leave null
console.log(
`[DownloadsTelemetryEnterprise] MIME service failed: ${ex.message}`
);
}
}
console.log(`[DownloadsTelemetryEnterprise] MIME type: ${mimeType}`);

// Process source URL based on enterprise policy configuration
let sourceUrl = this._processSourceUrl(download.source?.url);
const urlPolicy = this._getUrlLoggingPolicy();
console.log(
`[DownloadsTelemetryEnterprise] URL policy: ${urlPolicy}, processed URL: ${sourceUrl}`
);

// Get file size
let sizeBytes = download.target?.size;
if (typeof sizeBytes !== "number" || sizeBytes < 0) {
sizeBytes = null;
}
console.log(`[DownloadsTelemetryEnterprise] File size: ${sizeBytes}`);

const telemetryData = {
filename: filename || "",
extension: extension || "",
mime_type: mimeType || "",
size_bytes: sizeBytes,
source_url: sourceUrl || "",
};

console.log(
`[DownloadsTelemetryEnterprise] Recording Glean event with data:`,
telemetryData
);

// Record the Glean event
console.log(
`[DownloadsTelemetryEnterprise] Glean object available: ${typeof Glean !== "undefined"}`
);
console.log(
`[DownloadsTelemetryEnterprise] Glean.downloads available: ${typeof Glean?.downloads !== "undefined"}`
);
console.log(
`[DownloadsTelemetryEnterprise] Glean.downloads.fileDownloaded available: ${typeof Glean?.downloads?.fileDownloaded !== "undefined"}`
);

Glean.downloads.fileDownloaded.record(telemetryData);
console.log(
`[DownloadsTelemetryEnterprise] Glean event recorded successfully`
);

// Submit the enterprise ping
GleanPings.enterprise.submit();
console.log(`[DownloadsTelemetryEnterprise] Enterprise ping submitted`);
} catch (ex) {
// Silently fail - telemetry errors should not break downloads
console.error(
`[DownloadsTelemetryEnterprise] Download telemetry recording failed:`,
ex
);
try {
ChromeUtils.reportError(`Download telemetry recording failed: ${ex}`);
} catch (reportEx) {
// ChromeUtils.reportError may not be available in all contexts
console.error(
`[DownloadsTelemetryEnterprise] Could not report error:`,
reportEx
);
}
}
},
};
Loading