Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 0bac0d4

Browse files
committed
Bug 1944680 - lock all ContentAnalysis prefs r=dlp-reviewers,handyman
Differential Revision: https://phabricator.services.mozilla.com/D236562
1 parent 05a8bb1 commit 0bac0d4

File tree

2 files changed

+130
-57
lines changed

2 files changed

+130
-57
lines changed

browser/components/enterprisepolicies/Policies.sys.mjs

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,13 @@ export var Policies = {
509509

510510
ContentAnalysis: {
511511
onBeforeAddons(manager, param) {
512-
if ("PipePathName" in param) {
513-
setAndLockPref(
514-
"browser.contentanalysis.pipe_path_name",
515-
param.PipePathName
516-
);
517-
}
512+
// For security reasons, all of the Content Analysis related prefs should be locked in
513+
// this method, even if the values aren't specified in Enterprise Policies.
514+
setPrefIfPresentAndLock(
515+
param,
516+
"PipePathName",
517+
"browser.contentanalysis.pipe_path_name"
518+
);
518519
if ("AgentTimeout" in param) {
519520
if (!Number.isInteger(param.AgentTimeout)) {
520521
lazy.log.error(
@@ -526,28 +527,29 @@ export var Policies = {
526527
param.AgentTimeout
527528
);
528529
}
530+
} else {
531+
Services.prefs.lockPref("browser.contentanalysis.agent_timeout");
529532
}
530-
if ("AllowUrlRegexList" in param) {
531-
setAndLockPref(
532-
"browser.contentanalysis.allow_url_regex_list",
533-
param.AllowUrlRegexList
534-
);
535-
}
536-
if ("DenyUrlRegexList" in param) {
537-
setAndLockPref(
538-
"browser.contentanalysis.deny_url_regex_list",
539-
param.DenyUrlRegexList
540-
);
541-
}
542-
if ("AgentName" in param) {
543-
setAndLockPref("browser.contentanalysis.agent_name", param.AgentName);
544-
}
545-
if ("ClientSignature" in param) {
546-
setAndLockPref(
547-
"browser.contentanalysis.client_signature",
548-
param.ClientSignature
549-
);
550-
}
533+
setPrefIfPresentAndLock(
534+
param,
535+
"AllowUrlRegexList",
536+
"browser.contentanalysis.allow_url_regex_list"
537+
);
538+
setPrefIfPresentAndLock(
539+
param,
540+
"DenyUrlRegexList",
541+
"browser.contentanalysis.deny_url_regex_list"
542+
);
543+
setPrefIfPresentAndLock(
544+
param,
545+
"AgentName",
546+
"browser.contentanalysis.agent_name"
547+
);
548+
setPrefIfPresentAndLock(
549+
param,
550+
"ClientSignature",
551+
"browser.contentanalysis.client_signature"
552+
);
551553
if ("DefaultResult" in param) {
552554
if (
553555
!Number.isInteger(param.DefaultResult) ||
@@ -563,6 +565,8 @@ export var Policies = {
563565
param.DefaultResult
564566
);
565567
}
568+
} else {
569+
Services.prefs.lockPref("browser.contentanalysis.default_result");
566570
}
567571
let boolPrefs = [
568572
["IsPerUser", "is_per_user"],
@@ -575,6 +579,8 @@ export var Policies = {
575579
`browser.contentanalysis.${pref[1]}`,
576580
!!param[pref[0]]
577581
);
582+
} else {
583+
Services.prefs.lockPref(`browser.contentanalysis.${pref[1]}`);
578584
}
579585
}
580586
let interceptionPointPrefs = [
@@ -585,8 +591,6 @@ export var Policies = {
585591
];
586592
if ("InterceptionPoints" in param) {
587593
for (let pref of interceptionPointPrefs) {
588-
// Need to set and lock this value even if the enterprise
589-
// policy isn't set so users can't change it
590594
let value = true;
591595
if (pref[0] in param.InterceptionPoints) {
592596
if ("Enabled" in param.InterceptionPoints[pref[0]]) {
@@ -598,6 +602,12 @@ export var Policies = {
598602
value
599603
);
600604
}
605+
} else {
606+
for (let pref of interceptionPointPrefs) {
607+
Services.prefs.lockPref(
608+
`browser.contentanalysis.interception_point.${pref[1]}.enabled`
609+
);
610+
}
601611
}
602612
if ("Enabled" in param) {
603613
let enabled = !!param.Enabled;
@@ -606,6 +616,10 @@ export var Policies = {
606616
Ci.nsIContentAnalysis
607617
);
608618
ca.isSetByEnterprisePolicy = true;
619+
} else {
620+
// Probably not strictly necessary, but let's lock everything
621+
// to be consistent.
622+
Services.prefs.lockPref("browser.contentanalysis.enabled");
609623
}
610624
},
611625
},
@@ -2652,6 +2666,28 @@ export function setAndLockPref(prefName, prefValue) {
26522666
PoliciesUtils.setDefaultPref(prefName, prefValue, true);
26532667
}
26542668

2669+
/**
2670+
*
2671+
* setPrefIfPresentAndLock
2672+
*
2673+
* Sets the pref to the value param[paramKey] if that exists. Either
2674+
* way, the pref is locked.
2675+
*
2676+
* @param {object} param
2677+
* Object with pref values
2678+
* @param {string} paramKey
2679+
* The key to look up the value in param
2680+
* @param {string} prefName
2681+
* The pref to be changed
2682+
*/
2683+
function setPrefIfPresentAndLock(param, paramKey, prefName) {
2684+
if (paramKey in param) {
2685+
setAndLockPref(prefName, param[paramKey]);
2686+
} else {
2687+
Services.prefs.lockPref(prefName);
2688+
}
2689+
}
2690+
26552691
/**
26562692
* setDefaultPref
26572693
*

toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ const { EnterprisePolicyTesting, PoliciesPrefTracker } =
1616
"resource://testing-common/EnterprisePolicyTesting.sys.mjs"
1717
);
1818

19-
const kEnabledPref = "enabled";
20-
const kPipeNamePref = "pipe_path_name";
21-
const kTimeoutPref = "agent_timeout";
22-
const kAllowUrlPref = "allow_url_regex_list";
23-
const kDenyUrlPref = "deny_url_regex_list";
24-
const kAgentNamePref = "agent_name";
25-
const kClientSignaturePref = "client_signature";
26-
const kPerUserPref = "is_per_user";
27-
const kShowBlockedPref = "show_blocked_result";
28-
const kDefaultResultPref = "default_result";
29-
const kBypassForSameTabOperationsPref = "bypass_for_same_tab_operations";
19+
const kIndividualPrefs = new Map([
20+
["Enabled", "enabled"],
21+
["PipeName", "pipe_path_name"],
22+
["Timeout", "agent_timeout"],
23+
["AllowUrl", "allow_url_regex_list"],
24+
["DenyUrl", "deny_url_regex_list"],
25+
["AgentName", "agent_name"],
26+
["ClientSignature", "client_signature"],
27+
["PerUser", "is_per_user"],
28+
["ShowBlocked", "show_blocked_result"],
29+
["DefaultResult", "default_result"],
30+
["BypassForSameTab", "bypass_for_same_tab_operations"],
31+
]);
32+
function getIndividualPrefName(name) {
33+
is(
34+
kIndividualPrefs.has(name),
35+
true,
36+
`"${name}" passed to getIndividualPrefName() is valid`
37+
);
38+
return `browser.contentanalysis.${kIndividualPrefs.get(name)}`;
39+
}
3040
const kInterceptionPoints = [
3141
"clipboard",
3242
"drag_and_drop",
@@ -43,7 +53,7 @@ add_task(async function test_ca_active() {
4353
ok(!ca.isActive, "CA is inactive when pref and cmd line arg are missing");
4454

4555
// Set the pref without enterprise policy. CA should not be active.
46-
Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, true);
56+
Services.prefs.setBoolPref(getIndividualPrefName("Enabled"), true);
4757
ok(
4858
!ca.isActive,
4959
"CA is inactive when pref is set but cmd line arg is missing"
@@ -83,7 +93,38 @@ add_task(async function test_ca_active() {
8393
);
8494
}
8595

86-
Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, false);
96+
Services.prefs.setBoolPref(getIndividualPrefName("Enabled"), false);
97+
PoliciesPrefTracker.stop();
98+
});
99+
100+
add_task(async function test_ca_enterprise_config() {
101+
PoliciesPrefTracker.start();
102+
await EnterprisePolicyTesting.setupPolicyEngineWithJson({
103+
policies: {
104+
ContentAnalysis: {
105+
Enabled: true,
106+
},
107+
},
108+
});
109+
110+
for (let individualPref of kIndividualPrefs.values()) {
111+
is(
112+
Services.prefs.prefIsLocked("browser.contentanalysis." + individualPref),
113+
true,
114+
`${individualPref} should be locked`
115+
);
116+
}
117+
118+
for (let interceptionPoint of kInterceptionPoints) {
119+
is(
120+
Services.prefs.prefIsLocked(
121+
`browser.contentanalysis.interception_point.${interceptionPoint}.enabled`
122+
),
123+
true,
124+
`${interceptionPoint} enabled should be locked`
125+
);
126+
}
127+
87128
PoliciesPrefTracker.stop();
88129
});
89130

@@ -126,56 +167,52 @@ add_task(async function test_ca_enterprise_config() {
126167
});
127168

128169
is(
129-
Services.prefs.getStringPref("browser.contentanalysis." + kPipeNamePref),
170+
Services.prefs.getStringPref(getIndividualPrefName("PipeName")),
130171
"abc",
131172
"pipe name match"
132173
);
133174
is(
134-
Services.prefs.getIntPref("browser.contentanalysis." + kTimeoutPref),
175+
Services.prefs.getIntPref(getIndividualPrefName("Timeout")),
135176
99,
136177
"timeout match"
137178
);
138179
is(
139-
Services.prefs.getStringPref("browser.contentanalysis." + kAllowUrlPref),
180+
Services.prefs.getStringPref(getIndividualPrefName("AllowUrl")),
140181
string1,
141182
"allow urls match"
142183
);
143184
is(
144-
Services.prefs.getStringPref("browser.contentanalysis." + kDenyUrlPref),
185+
Services.prefs.getStringPref(getIndividualPrefName("DenyUrl")),
145186
string2,
146187
"deny urls match"
147188
);
148189
is(
149-
Services.prefs.getStringPref("browser.contentanalysis." + kAgentNamePref),
190+
Services.prefs.getStringPref(getIndividualPrefName("AgentName")),
150191
string3,
151192
"agent names match"
152193
);
153194
is(
154-
Services.prefs.getStringPref(
155-
"browser.contentanalysis." + kClientSignaturePref
156-
),
195+
Services.prefs.getStringPref(getIndividualPrefName("ClientSignature")),
157196
string4,
158197
"client signatures match"
159198
);
160199
is(
161-
Services.prefs.getBoolPref("browser.contentanalysis." + kPerUserPref),
200+
Services.prefs.getBoolPref(getIndividualPrefName("PerUser")),
162201
true,
163202
"per user match"
164203
);
165204
is(
166-
Services.prefs.getBoolPref("browser.contentanalysis." + kShowBlockedPref),
205+
Services.prefs.getBoolPref(getIndividualPrefName("ShowBlocked")),
167206
false,
168207
"show blocked match"
169208
);
170209
is(
171-
Services.prefs.getIntPref("browser.contentanalysis." + kDefaultResultPref),
210+
Services.prefs.getIntPref(getIndividualPrefName("DefaultResult")),
172211
1,
173212
"default result match"
174213
);
175214
is(
176-
Services.prefs.getBoolPref(
177-
"browser.contentanalysis." + kBypassForSameTabOperationsPref
178-
),
215+
Services.prefs.getBoolPref(getIndividualPrefName("BypassForSameTab")),
179216
true,
180217
"bypass for same tab operations match"
181218
);
@@ -201,7 +238,7 @@ add_task(async function test_cleanup() {
201238
// the policy and do not get cleared if there is no ContentAnalysis
202239
// element - reset them manually here.
203240
ca.isSetByEnterprisePolicy = false;
204-
Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, false);
241+
Services.prefs.setBoolPref(getIndividualPrefName("Enabled"), false);
205242
for (let interceptionPoint of kInterceptionPoints) {
206243
Services.prefs.setBoolPref(
207244
`browser.contentanalysis.interception_point.${interceptionPoint}.enabled`,

0 commit comments

Comments
 (0)