Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9421be6
WIP
gcp Aug 5, 2025
2b550e0
feat: add enterprise download security telemetry with MOZ_ENTERPRISE …
gcp Aug 22, 2025
8809673
feat: add enterprise download telemetry with policy controls
gcp Aug 22, 2025
c1320f0
chore: update Cargo.lock
gcp Aug 25, 2025
f2d2f34
feat: add TELEMETRY_ENDPOINT env var support for telemetry endpoints
gcp Aug 25, 2025
65a3b54
refactor: unify TELEMETRY_ENDPOINT handling across components
gcp Aug 25, 2025
f4ea991
refactor: use lazy loading for DownloadsTelemetry
gcp Aug 25, 2025
ce4144c
feat: prioritize TELEMETRY_ENDPOINT env var over localhost config
gcp Aug 27, 2025
a63c1a4
feat: add debug logging for download telemetry
gcp Aug 27, 2025
a2de78c
fix: Improve error handling and resource path resolution in downloads…
gcp Aug 27, 2025
14d45c0
feat: wrap file_downloaded telemetry in MOZ_ENTERPRISE ifdef
gcp Aug 27, 2025
3375a4b
fix: use MOZ_TELEMETRY_REPORTING instead of MOZILLA_OFFICIAL for tele…
gcp Aug 28, 2025
bab476a
feat: reset telemetry localhost port and expose MOZ_TELEMETRY_REPORTI…
gcp Sep 1, 2025
2ae0626
refactor: move download telemetry to dedicated enterprise ping
gcp Sep 2, 2025
7424422
fix: use PathUtils as global instead of importing as ES module
gcp Sep 2, 2025
b6ccae4
POC metric for page printed
Nov 13, 2025
46a4182
Update metric to have better data
Nov 13, 2025
df91afa
Put print telemetry behind #ifdef MOZ_ENTERPRISE
Nov 14, 2025
d69de77
Add and check prefs for metric
Nov 14, 2025
e19eb0e
Add content titles to print metric payload
Nov 14, 2025
dd5339f
Add existing add-on install metric to enterprise pings
Nov 17, 2025
ce37555
Include info section in enterprise ping so console can consume
Nov 20, 2025
c88d4ee
Allow enterprise prefix policies to be set via policy
Nov 20, 2025
d6ac473
Add metric for browsing a blocked URL
Nov 20, 2025
dfb579c
Remove debugging force-enable of download telemetry
Nov 21, 2025
761023b
Add policy and pref for telemetry of browsing blocked domains
Nov 21, 2025
f296748
Protect against telemetry exceptions for printing
Nov 21, 2025
4fb474d
Protect against telemetry exceptions during website filtering
Nov 21, 2025
d74c637
Add BlocklistDomainBrowsedTelemetry policy to schema
Nov 21, 2025
66b7494
Revert defunct changes to redirect telemetry
Nov 21, 2025
f67948c
Remove defunct cargo dependency on local glean
Nov 21, 2025
deec333
Remove obsolete pref
Nov 21, 2025
d0cda41
Remove trace log statements
Nov 21, 2025
ec73f3d
Address PR #152 review comments
gcp Nov 22, 2025
0ff3862
Rename and restructure download telemetry metrics
gcp Nov 22, 2025
0347d53
Add configurable FileLogging policy for download telemetry
gcp Nov 22, 2025
3c8f6af
Fix linting issues in download telemetry tests
gcp Nov 22, 2025
1726c8b
Enable enterprise download telemetry by default for local testing
gcp Nov 22, 2025
9f6d06f
Remove unused enterprise prefix from Preferences policy
gcp Nov 23, 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
39 changes: 36 additions & 3 deletions browser/components/downloads/DownloadsCommon.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
NetUtil: "resource://gre/modules/NetUtil.sys.mjs",
PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
DownloadsTelemetry:
"moz-src:///browser/components/downloads/DownloadsTelemetry.sys.mjs",
});

XPCOMUtils.defineLazyServiceGetters(lazy, {
Expand Down Expand Up @@ -900,7 +902,7 @@ DownloadsDataCtor.prototype = {
download.error?.becauseBlockedByReputationCheck ||
download.error?.becauseBlockedByContentAnalysis
) {
this._notifyDownloadEvent("error");
this._notifyDownloadEvent("error", { download });
}
},

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

Expand Down Expand Up @@ -996,11 +998,42 @@ 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."
);

// If this is a finished download, record enterprise telemetry if available
if (aType === "finish" && download && download.succeeded) {
try {
if (
typeof lazy.DownloadsTelemetry !== "undefined" &&
lazy.DownloadsTelemetry &&
typeof lazy.DownloadsTelemetry.recordFileDownloaded === "function"
) {
lazy.DownloadsTelemetry.recordFileDownloaded(download);
} else {
console.warn(
`[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);
}
}
}

// Show the panel in the most recent browser window, if present.
let browserWin = lazy.BrowserWindowTracker.getTopWindow({
private: this._isPrivate,
Expand Down
264 changes: 264 additions & 0 deletions browser/components/downloads/DownloadsTelemetry.enterprise.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/* 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 information collected. In the enterprise policies.json file:
*
* {
* "policies": {
* "DownloadTelemetry": {
* "Enabled": true,
* "UrlLogging": "full",
* "FileLogging": "full"
* }
* }
* }
*
* Configuration Options:
* - Enabled (boolean): Enable/disable download telemetry collection
* - UrlLogging (string): URL logging level with values:
* - "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
* - FileLogging (string): File information logging level with values:
* - "full" (default): Collect filename, extension, and MIME type
* - "metadata": Collect only extension and MIME type (no filename)
* - "none": Do not collect any file information
*
* SECURITY CONSIDERATIONS:
* =======================
*
* - "full" mode provides maximum visibility for security analysis but may
* contain sensitive information in URL paths/parameters or filenames
* - "domain" and "metadata" modes balance security monitoring with privacy
* - "none" modes disable 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"
);

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
Copy link
Contributor

Choose a reason for hiding this comment

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

we default to enabled by default? ok

Copy link
Contributor Author

@gcp gcp Nov 24, 2025

Choose a reason for hiding this comment

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

Mostly for debugging, can flip the default later on. We did enable the other stuff by default.

CrowdStrike knows your downloads anyway :P

);
},

/**
* 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
},

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

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

return "full"; // Default to full file info 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;
}
},

/**
* Processes file information based on the configured logging policy.
*
* @param {string} filename - The filename (basename)
* @param {string} extension - The file extension
* @param {string} mimeType - The MIME type
* @returns {object} Object with filename, extension, and mime_type based on policy
*/
_processFileInfo(filename, extension, mimeType) {
const policy = this._getFileLoggingPolicy();

switch (policy) {
case "none":
return {
filename: "",
extension: "",
mime_type: "",
};

case "metadata":
// Only log extension and MIME type, no filename
return {
filename: "",
extension: extension || "",
mime_type: mimeType || "",
};

case "full":
default:
return {
filename: filename || "",
extension: extension || "",
mime_type: mimeType || "",
};
}
},

/**
* Records a telemetry event for a completed file download.
*
* @param {object} download - The Download object containing download information
*/
recordFileDownloaded(download) {
// Check if telemetry is enabled via enterprise policy
const isEnabled = this._isEnabled();
if (!isEnabled) {
return;
}

try {
// Extract filename from target path
let filename = null;
if (download.target?.path) {
try {
// PathUtils is available as a global WebIDL binding in this context.
filename = PathUtils.filename(download.target.path);
} catch (pathErr) {
console.warn(
`[DownloadsTelemetryEnterprise] PathUtils failed, falling back to split:`,
pathErr
);
const parts = download.target.path.split(/[/\\]/);
filename = parts[parts.length - 1] || "";
}
}

// Extract file extension
let extension = null;
if (filename) {
const lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex > 0) {
extension = filename.substring(lastDotIndex + 1).toLowerCase();
}
}

// Use provided content type; avoid MIME service lookup when unavailable.
let mimeType = download.contentType || "";

// Process file information based on enterprise policy configuration
const fileInfo = this._processFileInfo(filename, extension, mimeType);

// Process source URL based on enterprise policy configuration
let sourceUrl = this._processSourceUrl(download.source?.url);

// Get file size
let sizeBytes = download.target?.size;
if (typeof sizeBytes !== "number" || sizeBytes < 0) {
sizeBytes = null;
}

const telemetryData = {
filename: fileInfo.filename,
extension: fileInfo.extension,
mime_type: fileInfo.mime_type,
size_bytes: sizeBytes,
source_url: sourceUrl || "",
is_private: download.source?.isPrivate || false,
};

// Record the Glean event
Glean.downloads.downloadCompleted.record(telemetryData);

// Submit the enterprise ping
GleanPings.enterprise.submit();
} 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
);
}
}
},
};
33 changes: 33 additions & 0 deletions browser/components/downloads/DownloadsTelemetry.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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/. */

/**
* Shim module for Downloads Telemetry.
*
* This module provides a stable import path for downloads telemetry functionality.
* The actual implementation is conditionally provided at build time:
* - In MOZ_ENTERPRISE builds: Full enterprise telemetry implementation
* - In regular builds: No-op implementation (enterprise code completely absent)
*/

let DownloadsTelemetryImpl;

try {
// Attempt to import enterprise implementation (only available in MOZ_ENTERPRISE builds)
const { DownloadsTelemetryEnterprise } = ChromeUtils.importESModule(
"moz-src:///browser/components/downloads/DownloadsTelemetry.enterprise.sys.mjs"
);
DownloadsTelemetryImpl = DownloadsTelemetryEnterprise;
} catch (ex) {
console.warn(
"[DownloadsTelemetry] Enterprise implementation not available, using no-op shim. Error:",
ex.message
);
// Enterprise implementation not available, use no-op shim
DownloadsTelemetryImpl = {
recordFileDownloaded: _download => {},
};
}

export const DownloadsTelemetry = DownloadsTelemetryImpl;
Loading
Loading