fix(df): keep npm and Yarn cafile settings from distrusting the proxy

Description

When a user points npm, pnpm or Yarn at their company CA with a cafile, that setting replaces the CA bundle glab gives the package manager. It then rejects glab's inspection proxy, and the install fails. It fails closed, so nothing gets past the firewall, but nothing installs either.

Example

A developer behind a company's TLS-inspecting proxy has npm_config_cafile=/etc/corp/ca.pem set, so npm trusts the company CA. They run glab dependency-firewall npm install left-pad.

On main:

  1. glab starts its inspection proxy and writes its CA bundle: glab's CA and the system CAs.
  2. glab gives npm NODE_EXTRA_CA_CERTS=<bundle> and points its proxy settings at glab.
  3. npm reads npm_config_cafile and passes /etc/corp/ca.pem to Node as the complete CA list. Node then ignores NODE_EXTRA_CA_CERTS, so glab's CA is gone.
  4. npm rejects glab's proxy certificate with SELF_SIGNED_CERT_IN_CHAIN, and nothing installs.
  5. Even if npm trusted glab, glab's own connection to the registry, through the company proxy, wouldn't trust /etc/corp/ca.pem. glab doesn't read npm_config_cafile.

With this MR:

  1. glab merges /etc/corp/ca.pem into its bundle, which then holds the company CA, glab's CA and the system CAs.
  2. glab sets npm_config_cafile and NPM_CONFIG_CAFILE to that bundle, empties NPM_CONFIG_CA, and removes other spellings of these variables. Environment variables outrank every .npmrc, so the bundle wins wherever the user's cafile was set.
  3. npm trusts glab's proxy, and glab checks the package against the policy.
  4. glab's registry connection trusts the file in npm_config_cafile, so it gets through the company proxy, and the package installs.
  5. Install scripts that read npm's cafile get the same bundle, so they trust the company CA and public hosts.

What glab does

main This MR
What npm, pnpm and Yarn get NODE_EXTRA_CA_CERTS=<bundle> The same, plus npm_config_cafile and NPM_CONFIG_CAFILE pointing at the bundle. npm and pnpm also get an empty NPM_CONFIG_CA. Yarn 1 gets YARN_CAFILE=<bundle>, but only if the user set YARN_CAFILE. Other spellings of these variables are removed
Yarn Berry plugin Empties the per-host networkSettings Also points httpsCaFilePath (Yarn 4) or caFilePath (Yarn 3) at the bundle, if the user set one. Berry rejects these variables on versions that lack them, so they can't go in the environment
What the bundle holds The user's NODE_EXTRA_CA_CERTS file, glab's CA and the system CAs Also the files in npm_config_cafile, NPM_CONFIG_CAFILE, YARN_CAFILE, YARN_HTTPS_CA_FILE_PATH and YARN_CA_FILE_PATH, in any spelling
What glab's own registry connections trust System CAs, the ca_cert setting, and SSL_CERT_FILE, REQUESTS_CA_BUNDLE, PIP_CERT, TWINE_CERT, NODE_EXTRA_CA_CERTS, BUNDLE_SSL_CA_CERT and CARGO_HTTP_CAINFO, exact names only Also the five npm and Yarn variables above, in any spelling

glab reads these variables in any case, as npm and Yarn do, so it scans the environment itself rather than using os.LookupEnv, which only matches exact names. The help text lists them, and docs/source/ is regenerated.

These still fail closed:

  • pnpm 11 and later, which this MR doesn't cover.
  • A cafile in Yarn 1's .yarnrc without YARN_CAFILE. Overriding it needs YARN_CAFILE, which Berry rejects.
  • A per-registry //host/:ca in pnpm's .npmrc.
  • A CA named only in .npmrc or .yarnrc(.yml), for registry connections. Set glab's ca_cert to it.

Trade-off

A user who set cafile to limit npm to their company CA now has npm trust the company CA, glab's CA and the system CAs. npm's own requests all go to glab's proxy, so in practice this matters only for install scripts that connect directly. The CA bundle for every package manager has worked this way since !4514 (merged).

Split from !4281 (closed) (closed), with the Poetry fix in !4503 (merged) and the Bundler fix in !4504 (merged), both now merged.

Related to gitlab-org&24327

How has this been tested?

Unit tests and before/after runs behind a corporate proxy

Unit tests in internal/dependencyfirewall/pm:

  • TestManagerExistingBundleVars, TestManagerEnvironAndCATrust, TestNPMCATrust and TestRunPassesRunInputsToSetup.
  • TestRunMergesUserCAIntoOverriddenVar (npm, pnpm and Yarn) and TestRunTrustsNPMAndYarnCAVarsInAnyCase.
  • TestRunPointsNPMCafileAtBundle, TestRunDropsInheritedNPMInlineCA, TestRunReplacesInheritedYarnCafileInAnyCase, TestRunMergesYarnBerryCAFileIntoBundle and TestYarnSetupSetsYarnCafileOnlyWhenInherited.
  • TestWriteCABundleSkipsEmptyVariable, TestWriteCABundleMergesEverySpelling, TestUpstreamRootCAsMatchesNPMAndYarnNamesInAnyCase and TestUpstreamRootCAsMatchesOtherNamesExactly.

go test over internal/dependencyfirewall/... and internal/commands/df/... and golangci-lint pass.

Before and after, run on this branch (46194a283) against main at its base (7ea9c2ebc), on macOS with npm 11.17.0 (Node.js 24.19.0), pnpm 10.34.9, Yarn 1.22.22 and Yarn 4.18.1. The package managers ran behind a TLS-intercepting corporate proxy (mitmproxy in Docker), with its CA set as the company CA in the package manager's own setting. glab trusted that proxy through GLAB_CA_CERT, except in the npm_config_cafile case. "Corporate proxy" counts the requests that proxy logged during the run with this branch.

Case main This branch Corporate proxy
npm, cafile in .npmrc SELF_SIGNED_CERT_IN_CHAIN, exit 1 added 1 package 2
npm, npm_config_cafile only, no GLAB_CA_CERT SELF_SIGNED_CERT_IN_CHAIN, exit 1 added 1 package 2
pnpm 10.34.9, cafile in .npmrc Hangs with no output Done in 1.4s using pnpm v10.34.9 2
Yarn 1.22.22, YARN_CAFILE error Error: self-signed certificate in certificate chain, exit 1 success Saved 1 new dependency. 2 to 4
Yarn 4.18.1, httpsCaFilePath in .yarnrc.yml YN0001: RequestError: self-signed certificate in certificate chain, exit 1 YN0000: · Done 2
No CA settings; a postinstall script connects to registry.npmjs.org directly with npm's cafile npm_config_cafile unset; status 200 164 certificates, glab's CA and the system's; status 200 n/a

Without glab, npm with the .npmrc cafile installs through the corporate proxy, so the setup itself works.

On !4281 (closed)'s last head (bdff30ccc), the same changes also passed these on Linux in Docker, with npm 10.9.9: NPM_CONFIG_CAFILE, npm_config_cafile plus an inline npm_config_ca, Yarn 1 with a .npmrc cafile, and Yarn 4 with YARN_HTTPS_CA_FILE_PATH.

From the same runs on !4281 (closed), these still fail closed:

  • pnpm 11.28.12 with a .npmrc cafile, and a cafile in Yarn 1's .yarnrc, both fail with a TLS error.
  • With the company CA only in .npmrc and no GLAB_CA_CERT, glab prints warning: dependency firewall request to registry registry.npmjs.org:443 failed: tls: failed to verify certificate: x509: certificate signed by unknown authority.

And these don't reproduce, so they install on main too:

  • Yarn 3.8.7 doesn't apply caFilePath to requests through a proxy.
  • pnpm 10.34.9 doesn't apply a per-registry :cafile under glab.
Manual testing steps (bash or zsh)

These steps compare this branch with main at the branch's base. glab, npm, pnpm and Yarn run on your machine, and mitmproxy runs in Docker as a TLS-intercepting corporate proxy on port 8081. pnpm 10.34.9, Yarn 1.22.22 and Yarn 4.18.1 come from corepack, with its files kept in the scratch directory. Run the steps from the root of a checkout of this branch, with Docker and Node.js 20 to 24 (which ship corepack), in a new terminal tab: they set a fake token and proxy settings. Each step is one command, even when it spans several lines.

Case Steps main This branch
A 39–40 npm with cafile in .npmrc, without glab: installs Same
B/C 41–42 npm, cafile in .npmrc: SELF_SIGNED_CERT_IN_CHAIN Installs
D 43–49 npm, npm_config_cafile only, no GLAB_CA_CERT: fails Installs; glab's registry connection trusts the file too
E 50–52 pnpm 10, cafile in .npmrc: hangs Installs
F 53–57 Yarn 1, YARN_CAFILE: self-signed certificate in certificate chain Installs
G 58–60 Yarn 4, httpsCaFilePath: YN0001: RequestError Installs
H 61–63 No CA settings; an install script connects directly with npm's cafile: 200 200, with the system CAs in the file

1. Name a scratch directory

T=~/df-4505-test

2. Name this checkout

W=$(git rev-parse --show-toplevel)

3. Create the scratch directories

mkdir -p $T/mitm $T/bin $T/npm $T/npm-env $T/pnpm $T/yarn1 $T/yarn4 $T/probe

4. Build this branch

make -C $W build

5. Keep this branch's binary

cp $W/bin/glab $T/glab-branch

6. Check out main at this branch's base

git -C $W worktree add --detach $T/main "$(git -C $W merge-base HEAD origin/main)"

7. Build main

make -C $T/main build

8. Keep main's binary

cp $T/main/bin/glab $T/glab-main

9. Start the corporate proxy

docker run -d --name df-4505-corp -p 127.0.0.1:8081:8080 \
  -v $T/mitm:/home/mitmproxy/.mitmproxy mitmproxy/mitmproxy mitmdump

10. Wait for the proxy to create its CA

sleep 3

11. Name the corporate CA. The ls on the next step prints its path; if it doesn't, wait a second and run it again.

CA=$T/mitm/mitmproxy-ca-cert.pem

12. Check that the CA exists

ls $CA

13. Keep corepack's and Yarn's own files in the scratch directory

export COREPACK_HOME=$T/corepack COREPACK_ENABLE_DOWNLOAD_PROMPT=0 YARN_GLOBAL_FOLDER=$T/yarn-global

14. Create pnpm and Yarn shims in the scratch directory

corepack enable --install-directory $T/bin pnpm yarn

15. Put the shims first on PATH

export PATH=$T/bin:$PATH

16. Create the npm project

printf '{"name":"df-npm","version":"1.0.0","private":true}\n' > $T/npm/package.json

17. Point npm at the corporate CA in the project's .npmrc

printf 'cafile=%s\n' $CA > $T/npm/.npmrc

18. Create a second npm project, without .npmrc

cp $T/npm/package.json $T/npm-env/package.json

19. Create the pnpm project, pinned to pnpm 10

printf '{"name":"df-pnpm","version":"1.0.0","private":true,"packageManager":"pnpm@10.34.9"}\n' > $T/pnpm/package.json

20. Point pnpm at the corporate CA

cp $T/npm/.npmrc $T/pnpm/.npmrc

21. Create the Yarn 1 project

printf '{"name":"df-yarn1","version":"1.0.0","private":true,"packageManager":"yarn@1.22.22"}\n' > $T/yarn1/package.json

22. Create the Yarn 4 project

printf '{"name":"df-yarn4","version":"1.0.0","private":true,"packageManager":"yarn@4.18.1"}\n' > $T/yarn4/package.json

23. Point Yarn 4 at the corporate CA in .yarnrc.yml

printf 'httpsCaFilePath: %s\n' $CA > $T/yarn4/.yarnrc.yml

24. Create the probe project. Its postinstall script connects to registry.npmjs.org directly, without the proxy, using npm's cafile, like an install script that downloads a binary.

printf '{"name":"df-probe","version":"1.0.0","private":true,"dependencies":{"left-pad":"1.3.0"},"scripts":{"postinstall":"node probe.js"}}\n' > $T/probe/package.json

25. Write the probe script

printf '%s\n' 'const f=process.env.npm_config_cafile;const ca=f?require("fs").readFileSync(f):undefined;console.log("PROBE npm_config_cafile "+(f?"has "+(ca.toString().match(/BEGIN CERTIFICATE/g)||[]).length+" certificates":"unset"));require("https").get({host:"registry.npmjs.org",path:"/left-pad",ca,agent:false},r=>{console.log("PROBE direct connection status "+r.statusCode);r.resume()}).on("error",e=>console.log("PROBE direct connection error "+(e.code||e.message)))' > $T/probe/probe.js

26. Go into the pnpm project. Its packageManager field picks pnpm 10.34.9.

cd $T/pnpm

27. Download pnpm and check its version. Prints 10.34.9.

pnpm --version

28. Go into the Yarn 1 project

cd $T/yarn1

29. Download Yarn 1 and check its version. Prints 1.22.22.

yarn --version

30. Go into the Yarn 4 project

cd $T/yarn4

31. Download Yarn 4 and check its version. Prints 4.18.1.

yarn --version

32. Clear CA, proxy and package-manager settings from your shell

unset NODE_EXTRA_CA_CERTS SSL_CERT_FILE GLAB_CA_CERT HTTP_PROXY http_proxy https_proxy ALL_PROXY NO_PROXY no_proxy CI GITLAB_CI \
  npm_config_cafile NPM_CONFIG_CAFILE npm_config_ca NPM_CONFIG_CA YARN_CAFILE YARN_HTTPS_CA_FILE_PATH YARN_CA_FILE_PATH

33. Send traffic through the corporate proxy

export HTTPS_PROXY=http://127.0.0.1:8081

34. Let glab allow every package without calling GitLab

export GLAB_DF_FAKE_DEFAULT=allow GITLAB_REPO=gitlab.com/example/df-proxy-test GITLAB_TOKEN=fake

35. Keep your own glab settings out of the test, and let glab trust the corporate proxy

export GLAB_CONFIG_DIR=$T/glab-config GLAB_SEND_TELEMETRY=false GLAB_CHECK_UPDATE=false GLAB_CA_CERT=$CA

36. Keep the package managers' caches in the scratch directory, and make failures quick

export npm_config_cache=$T/npm-cache npm_config_store_dir=$T/pnpm-store npm_config_cache_dir=$T/pnpm-cache \
  npm_config_fetch_retries=0 npm_config_audit=false npm_config_fund=false npm_config_update_notifier=false \
  YARN_CACHE_FOLDER=$T/yarn-cache YARN_ENABLE_GLOBAL_CACHE=0 YARN_ENABLE_TELEMETRY=0 COREPACK_ENABLE_NETWORK=0

37. Define corp. It counts the requests the corporate proxy has logged.

corp() { docker logs df-4505-corp 2>&1 | grep -c -E '(GET|HEAD) https://'; }

38. Define try. It deletes the project's installed packages, lockfiles and caches, so every install downloads again, then runs the command and prints its exit code and the number of requests the corporate proxy saw.

try() {
  local n=$(corp)
  rm -rf node_modules package-lock.json pnpm-lock.yaml yarn.lock .pnp.cjs .pnp.loader.mjs .yarn \
    $T/npm-cache $T/pnpm-store $T/pnpm-cache $T/yarn-cache $T/yarn-global
  "$@"
  local rc=$?
  echo "exit=$rc, corporate proxy: +$(( $(corp) - n )) requests"
}

39. A: go into the npm project

cd $T/npm

40. A: install without glab. Expect added 1 package and exit=0, corporate proxy: +2 requests.

try npm install left-pad

41. B: install through main. Expect npm error code SELF_SIGNED_CERT_IN_CHAIN and exit=1, corporate proxy: +0 requests. npm's cafile replaces Node's CAs, so main's NODE_EXTRA_CA_CERTS is ignored.

try $T/glab-main df npm install left-pad

42. C: install through this branch. Expect added 1 package and exit=0, corporate proxy: +2 requests.

try $T/glab-branch df npm install left-pad

43. D: go into the project without .npmrc

cd $T/npm-env

44. D: stop glab trusting the corporate proxy through GLAB_CA_CERT

unset GLAB_CA_CERT

45. D: point npm at the corporate CA with npm_config_cafile only

export npm_config_cafile=$CA

46. D: install through main. Expect SELF_SIGNED_CERT_IN_CHAIN and exit=1, corporate proxy: +0 requests.

try $T/glab-main df npm install left-pad

47. D: install through this branch. Expect added 1 package and exit=0, corporate proxy: +2 requests. glab's own registry connection now trusts the file in npm_config_cafile.

try $T/glab-branch df npm install left-pad

48. D: drop npm_config_cafile

unset npm_config_cafile

49. D: let glab trust the corporate proxy again

export GLAB_CA_CERT=$CA

50. E: go into the pnpm project

cd $T/pnpm

51. E: install through main. pnpm hangs without printing anything. After about 30 seconds, stop it with Control+C.

try $T/glab-main df pnpm add left-pad

52. E: install through this branch. Expect Done in …s using pnpm v10.34.9 and exit=0, corporate proxy: +2 requests.

try $T/glab-branch df pnpm add left-pad

53. F: go into the Yarn 1 project

cd $T/yarn1

54. F: point Yarn 1 at the corporate CA with YARN_CAFILE

export YARN_CAFILE=$CA

55. F: install through main. Expect error Error: self-signed certificate in certificate chain and exit=1, corporate proxy: +0 requests.

try $T/glab-main df yarn add left-pad

56. F: install through this branch. Expect success Saved 1 new dependency. and exit=0, with 2 or more requests through the corporate proxy.

try $T/glab-branch df yarn add left-pad

57. F: drop YARN_CAFILE

unset YARN_CAFILE

58. G: go into the Yarn 4 project

cd $T/yarn4

59. G: install through main. Expect YN0001: RequestError: self-signed certificate in certificate chain and exit=1, corporate proxy: +0 requests.

try $T/glab-main df yarn add left-pad

60. G: install through this branch. Expect YN0000: · Done and exit=0, corporate proxy: +2 requests.

try $T/glab-branch df yarn add left-pad

61. H: go into the probe project

cd $T/probe

62. H: install through main. Expect PROBE npm_config_cafile unset, PROBE direct connection status 200 and exit=0.

try $T/glab-main df npm install

63. H: install through this branch. Expect PROBE npm_config_cafile has N certificates, where N is glab's CA plus your system's CAs (164 on macOS), then PROBE direct connection status 200 and exit=0.

try $T/glab-branch df npm install

64. Leave the scratch directory

cd ~

65. Stop the corporate proxy

docker rm -f df-4505-corp

66. Remove the main checkout

git -C $W worktree remove --force $T/main

67. Delete the scratch directory

rm -rf $T
Edited by Arpit Gogia

Merge request reports

Loading
Loading