Skip to content

Commit

Permalink
respect npm_config in server, add debug logs for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Jul 15, 2019
1 parent d3cc16a commit 14b12ff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
44 changes: 41 additions & 3 deletions packages/server/lib/util/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,75 @@
import _ from 'lodash'
import debugModule from 'debug'
import os from 'os'
import { getWindowsProxy } from './get-windows-proxy'

const debug = debugModule('cypress:server:util:proxy')

const falsyEnv = (v) => {
return v === 'false' || v === '0' || !v
}

const copyLowercaseEnvToUppercase = (name: string) => {
// uppercase environment variables are used throughout Cypress and dependencies
// but users sometimes supply these vars as lowercase
const lowerEnv = process.env[name.toLowerCase()]

if (lowerEnv) {
debug('overriding uppercase env var with lowercase %o', { name })
process.env[name.toUpperCase()] = lowerEnv
}
}

const normalizeEnvironmentProxy = () => {
if (process.env.HTTP_PROXY === 'false' || process.env.HTTP_PROXY === '0' || !process.env.HTTP_PROXY) {
if (falsyEnv(process.env.HTTP_PROXY)) {
debug('HTTP_PROXY is falsy, disabling HTTP_PROXY')
delete process.env.HTTP_PROXY
}

if (!process.env.HTTPS_PROXY && process.env.HTTP_PROXY) {
// request library will use HTTP_PROXY as a fallback for HTTPS urls, but
// proxy-from-env will not, so let's just force it to fall back like this
debug('setting HTTPS_PROXY to HTTP_PROXY since it does not exist')
process.env.HTTPS_PROXY = process.env.HTTP_PROXY
}

if (!process.env.hasOwnProperty('NO_PROXY')) {
// don't proxy localhost, to match Chrome's default behavior and user expectation
debug('setting default NO_PROXY of `localhost`')
process.env.NO_PROXY = 'localhost'
}

debug('normalized proxy environment variables %o', _.pick(process.env, [
'NO_PROXY', 'HTTP_PROXY', 'HTTPS_PROXY',
]))
}

const mergeNpmProxyVars = () => {
// copy npm's `proxy` and `https-proxy` config if they are set
// https://github.com/cypress-io/cypress/pull/4705
[
['npm_config_proxy', 'HTTP_PROXY'],
['npm_config_https_proxy', 'HTTPS_PROXY'],
].forEach(([from, to]) => {
if (!falsyEnv(process.env[from]) && _.isUndefined(process.env[to])) {
debug('using npm\'s %s as %s', from, to)
process.env[to] = process.env[from]
}
})
}

export const loadSystemProxySettings = () => {
['NO_PROXY', 'HTTP_PROXY', 'HTTPS_PROXY'].forEach(copyLowercaseEnvToUppercase)
debug('found proxy environment variables %o', _.pick(process.env, [
'NO_PROXY', 'HTTP_PROXY', 'HTTPS_PROXY',
'no_proxy', 'http_proxy', 'https_proxy',
'npm_config_proxy', 'npm_config_https_proxy',
]))

;['NO_PROXY', 'HTTP_PROXY', 'HTTPS_PROXY'].forEach(copyLowercaseEnvToUppercase)

mergeNpmProxyVars()

if (typeof process.env.HTTP_PROXY !== 'undefined') {
if (!_.isUndefined(process.env.HTTP_PROXY)) {
normalizeEnvironmentProxy()

return
Expand Down
26 changes: 26 additions & 0 deletions packages/server/test/unit/args_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,29 @@ describe "lib/util/args", ->
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq process.env.HTTP_PROXY
expect(options.proxyBypassList).to.eq process.env.NO_PROXY

it "can use npm_config_proxy", ->
process.env.npm_config_proxy = "http://foo-bar.baz:123"
expect(process.env.HTTP_PROXY).to.be.undefined

options = @setup()

expect(process.env.HTTP_PROXY).to.eq "http://foo-bar.baz:123"
expect(process.env.HTTPS_PROXY).to.eq "http://foo-bar.baz:123"
expect(process.env.NO_PROXY).to.eq "localhost"
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq process.env.HTTP_PROXY
expect(options.proxyBypassList).to.eq process.env.NO_PROXY

it "can override npm_config_proxy with falsy HTTP_PROXY", ->
process.env.npm_config_proxy = "http://foo-bar.baz:123"
process.env.HTTP_PROXY = ""

options = @setup()

expect(process.env.HTTP_PROXY).to.be.undefined
expect(process.env.HTTPS_PROXY).to.be.undefined
expect(process.env.NO_PROXY).to.eq "localhost"
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq process.env.HTTP_PROXY
expect(options.proxyBypassList).to.be.undefined

0 comments on commit 14b12ff

Please sign in to comment.