fix(df): keep package-manager CA settings from distrusting the inspection proxy

Description

When a user points their package manager at their company CA, that setting replaces the CA bundle glab gives the package manager. The package manager then rejects glab's inspection proxy, and the install fails. It fails closed, so nothing gets past the firewall, but nothing installs either. This MR fixes three such settings, one commit each:

  • npm, pnpm 10 and earlier, and Yarn.
    • For npm, pnpm and Yarn 1, glab sets npm_config_cafile and NPM_CONFIG_CAFILE to its bundle, which outranks every .npmrc, and merges the user's file into it. For npm and pnpm, it also empties NPM_CONFIG_CA.
    • Yarn 1 also gets YARN_CAFILE when the user sets it.
    • For Yarn Berry, the firewall plugin points httpsCaFilePath (Yarn 4) or caFilePath (Yarn 3) at the bundle. Berry rejects those variables on versions that lack them, so they can't be set in the environment.
  • Poetry. glab sets PIP_CERT to its bundle and merges the user's into it, as it already does for pip and pipenv.
  • Bundler. An ssl_ca_cert in the project's .bundle/config outranks the environment.
    • glab points BUNDLE_APP_CONFIG at a temporary copy of that directory. The copy's CA file holds the user's CA and glab's bundle.
    • After the run, glab writes Bundler's own changes back to the user's config, with the user's CA line restored.

Registry connections also trust the files the new variables name. The help text lists the new variables, and docs/source/ is regenerated.

These still fail closed:

  • pnpm 11 and later, whose own CA settings are handled separately.
  • 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.
  • bundle install --gemfile naming another project.
  • A CA named only in a config file, for registry connections. Set glab's ca_cert to it.

With no user CA set, the bundle holds only glab's CA. A tool that connects directly, without the proxy, using npm's cafile or PIP_CERT therefore loses the public roots. A follow-up MR adds the system roots to the bundle.

Related to gitlab-org/gitlab#629431
Related to gitlab-org&24327

Part of the corporate-proxy stack. !4280 (merged) adds an anchors argument to upstreamRootCAs, so whichever of the two lands second must pass nil in this MR's three new calls (managers_test.go, upstreamtrust_test.go).

How has this been tested?

Unit tests and before/after runs behind a corporate proxy

Unit tests in internal/dependencyfirewall/pm, each observed failing before the fix:

  • TestManagerExistingBundleVars, TestManagerEnvironAndCATrust, TestNPMCATrust and TestRunPassesRunInputsToSetup.
  • TestRunMergesUserCAIntoOverriddenVar, for poetry PIP_CERT, the npm, pnpm and yarn npm_config_cafile/NPM_CONFIG_CAFILE, and yarn YARN_CAFILE.
  • TestRunPointsNPMCafileAtBundle, TestRunDropsInheritedNPMInlineCA, TestRunReplacesInheritedYarnCafileInAnyCase, TestRunMergesYarnBerryCAFileIntoBundle and TestYarnSetupSetsYarnCafileOnlyWhenInherited.
  • bundle_test.go, which covers:
    • the config-line parser;
    • finding the local config from a subdirectory and through BUNDLE_GEMFILE and BUNDLE_APP_CONFIG;
    • writing Bundler's changes back;
    • the no-op cases, an unreadable CA, and Run end to end.

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

Before and after: glab was built from main (2b5d4bcb9) and from this MR (bdff30ccc), and run with real package managers in Docker. Every run went through a TLS-intercepting corporate proxy (mitmproxy). The proxy's CA was set as the company CA in the package manager's own setting, plus GLAB_CA_CERT where only a config file named it. "Corporate proxy" counts the requests that proxy logged during the run with this MR.

Case main This MR Corporate proxy
npm 10.9.9, npm_config_cafile SELF_SIGNED_CERT_IN_CHAIN added 1 package 2
npm, .npmrc cafile SELF_SIGNED_CERT_IN_CHAIN added 1 package 2
npm, npm_config_cafile plus an inline npm_config_ca SELF_SIGNED_CERT_IN_CHAIN added 1 package 2
npm, NPM_CONFIG_CAFILE SELF_SIGNED_CERT_IN_CHAIN added 1 package 2
pnpm 10.34.9, .npmrc cafile Hangs with no output Done 2
Yarn 1.22.22, YARN_CAFILE self-signed certificate in certificate chain success Saved 1 new dependency 2
Yarn 1.22.22, .npmrc cafile self-signed certificate in certificate chain success Saved 1 new dependency 2
Yarn 4.18.1, httpsCaFilePath in .yarnrc.yml YN0001: RequestError: self-signed certificate in certificate chain Done 2
Yarn 4.18.1, YARN_HTTPS_CA_FILE_PATH YN0001: RequestError: self-signed certificate in certificate chain Done 2
Poetry 1.8.5, poetry run pip install six with PIP_CERT CERTIFICATE_VERIFY_FAILED Successfully installed six-1.17.0 3
Poetry 2.5.1, poetry run pip install six with PIP_CERT CERTIFICATE_VERIFY_FAILED Successfully installed six-1.17.0 3
Bundler 2.5.22, local ssl_ca_cert, project root Could not verify the SSL certificate, exit 17 Bundle complete! 3
Bundler, local ssl_ca_cert, from a subdirectory Could not verify the SSL certificate, exit 17 Bundle complete! 3

Bundler's local config, with this MR:

  • It keeps the user's CA line and gains BUNDLE_PATH when Bundler writes it during the run, and no temporary directory is left.
  • A symlinked .bundle/config stays a symlink.
  • It is byte-identical after kill -9 of glab and Bundler mid-install. The temporary copy then stays in /tmp.

Still failing closed, with this MR:

  • These fail with a TLS error: pnpm 11.28.12 with a .npmrc cafile, a cafile in Yarn 1's .yarnrc, and bundle install --gemfile naming another project.
  • 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.

Cases that don't reproduce:

  • 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.
  • A plain poetry add doesn't read PIP_CERT, with Poetry 1.8.5's legacy installer either.

All of these install on main too.

A direct connection that uses npm's cafile, with no user CA set, fails with UNABLE_TO_GET_ISSUER_CERT_LOCALLY, where main succeeds. That's the follow-up above.

Edited by Arpit Gogia

Merge request reports

Loading
Loading