Skip to content

Commit 7a74773

Browse files
authored
chore: convert chrome_policy_check to TypeScript (#33625)
1 parent f652605 commit 7a74773

4 files changed

Lines changed: 115 additions & 123 deletions

File tree

packages/server/lib/modes/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as env from '../util/env'
1919
import trash from '../util/trash'
2020
import { id as randomId } from '../util/random'
2121
import * as system from '../util/system'
22-
import chromePolicyCheck from '../util/chrome_policy_check'
22+
import { run as runChromePolicyCheck } from '../util/chrome_policy_check'
2323
import type { SpecWithRelativeRoot, SpecFile, TestingType, OpenProjectLaunchOpts, FoundBrowser, BrowserVideoController, VideoRecording, ProcessOptions, ProtocolManagerShape, AutomationCommands } from '@packages/types'
2424
import type { Cfg, ProjectBase } from '../project-base'
2525
import type { Browser } from '../browsers/types'
@@ -1136,7 +1136,7 @@ async function ready (options: ReadyOptions) {
11361136
}
11371137

11381138
if (browser.family === 'chromium') {
1139-
chromePolicyCheck.run(onWarning)
1139+
runChromePolicyCheck(onWarning)
11401140
}
11411141

11421142
async function runAllSpecs ({ beforeSpecRun, afterSpecRun, runUrl, parallel }: { beforeSpecRun?: BeforeSpecRun, afterSpecRun?: AfterSpecRun, runUrl?: string, parallel?: boolean }) {

packages/server/lib/util/chrome_policy_check.js

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import _ from 'lodash'
2+
3+
import Debug from 'debug'
4+
import * as errors from '../errors'
5+
import os from 'os'
6+
import { enumerateValues, HKEY } from 'registry-js'
7+
import type { RegistryValue } from 'registry-js'
8+
9+
const debug = Debug('cypress:server:chrome_policy_check')
10+
// https://chromeenterprise.google/policies/#Proxy
11+
// https://chromeenterprise.google/policies/#ProxySettings
12+
const BAD_PROXY_POLICY_NAMES = [
13+
'ProxySettings',
14+
'ProxyMode',
15+
'ProxyServerMode',
16+
'ProxyServer',
17+
'ProxyPacUrl',
18+
'ProxyBypassList',
19+
]
20+
21+
// https://chromeenterprise.google/policies/#Extensions
22+
const BAD_EXTENSION_POLICY_NAMES = [
23+
'ExtensionInstallBlacklist',
24+
'ExtensionInstallWhitelist',
25+
'ExtensionInstallForcelist',
26+
'ExtensionInstallSources',
27+
'ExtensionAllowedTypes',
28+
'ExtensionAllowInsecureUpdates',
29+
'ExtensionSettings',
30+
'UninstallBlacklistedExtensions',
31+
]
32+
33+
const POLICY_KEYS: string[] = [
34+
'Software\\Policies\\Google\\Chrome',
35+
'Software\\Policies\\Google\\Chromium',
36+
]
37+
38+
const POLICY_HKEYS: HKEY[] = [
39+
HKEY.HKEY_LOCAL_MACHINE,
40+
HKEY.HKEY_CURRENT_USER,
41+
]
42+
43+
type RegistryValueWithPath = RegistryValue & { fullPath: string }
44+
45+
function warnIfPolicyMatches (policyNames: string[], allPolicies: RegistryValueWithPath[], warningName: Parameters<typeof errors.get>[0], cb: (err: Error) => void) {
46+
const matchedPolicyPaths = policyNames
47+
.map((policyName) => _.find(allPolicies, { name: policyName })?.fullPath)
48+
.filter((path): path is string => Boolean(path))
49+
50+
if (!matchedPolicyPaths.length) {
51+
return
52+
}
53+
54+
cb(errors.get(warningName, matchedPolicyPaths))
55+
}
56+
57+
export function getRunner ({ enumerateValues }: { enumerateValues: (hkey: HKEY, key: string) => ReadonlyArray<RegistryValue> }) {
58+
function getAllPolicies (): RegistryValueWithPath[] {
59+
return _.flattenDeep(
60+
POLICY_KEYS.map((key) => {
61+
return POLICY_HKEYS.map((hkey) => {
62+
return enumerateValues(hkey, key)
63+
.map((value): RegistryValueWithPath => ({
64+
...value,
65+
fullPath: `${hkey}\\${key}\\${value.name}`,
66+
}))
67+
})
68+
}),
69+
)
70+
}
71+
72+
return function run (cb: (err: Error) => void) {
73+
try {
74+
debug('running chrome policy check')
75+
76+
const policies = getAllPolicies()
77+
const badPolicyNames = _.concat(BAD_PROXY_POLICY_NAMES, BAD_EXTENSION_POLICY_NAMES)
78+
79+
debug('received policies %o', { policies, badPolicyNames })
80+
81+
warnIfPolicyMatches(badPolicyNames, policies, 'BAD_POLICY_WARNING', cb)
82+
} catch (err) {
83+
debug('error running policy check %o', { err })
84+
}
85+
}
86+
}
87+
88+
export function run (cb: (err: Error) => void): void {
89+
if (os.platform() !== 'win32') {
90+
return _.noop(cb)
91+
}
92+
93+
/**
94+
* Only check on Windows. While it is possible for macOS/Linux to have preferences set that
95+
* override Cypress's settings, it's never been reported as an issue and would require more
96+
* native extensions to support checking.
97+
* https://github.com/cypress-io/cypress/issues/4391
98+
*/
99+
const runner = getRunner({
100+
enumerateValues,
101+
})
102+
103+
return runner(cb)
104+
}

packages/server/test/unit/util/chrome_policy_check.js renamed to packages/server/test/unit/util/chrome_policy_check_spec.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const stripAnsi = require('strip-ansi')
1+
import _ from 'lodash'
2+
import { stripIndent } from 'common-tags'
3+
import stripAnsi from 'strip-ansi'
4+
import { getRunner } from '../../../lib/util/chrome_policy_check'
25

3-
require('../../spec_helper')
4-
5-
const _ = require('lodash')
6-
const { stripIndent } = require('common-tags')
7-
const chromePolicyCheck = require(`../../../lib/util/chrome_policy_check`)
6+
import '../../spec_helper'
87

98
describe('lib/util/chrome_policy_check', () => {
10-
context('.getRunner returns a function', () => {
9+
describe('.getRunner returns a function', () => {
1110
it('calls callback with an error if policies are found', () => {
12-
const run = chromePolicyCheck.getRunner({
11+
const run = getRunner({
1312
enumerateValues (hkey, key) {
1413
// mock a registry with a couple of policies
1514
return _.get({
@@ -46,7 +45,7 @@ For more information, see https://on.cypress.io/bad-browser-policy\
4645
})
4746

4847
it('does not call callback if no policies are found', () => {
49-
const run = chromePolicyCheck.getRunner({
48+
const run = getRunner({
5049
enumerateValues: _.constant([]),
5150
})
5251

@@ -58,7 +57,7 @@ For more information, see https://on.cypress.io/bad-browser-policy\
5857
})
5958

6059
it('fails silently if enumerateValues throws', () => {
61-
const run = chromePolicyCheck.getRunner({
60+
const run = getRunner({
6261
enumerateValues () {
6362
throw new Error('blah')
6463
},

0 commit comments

Comments
 (0)