Skip to content

Commit

Permalink
Add whitelistHosts option.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Mar 13, 2018
1 parent 1251fce commit 5664efb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cli/schema/cypress.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
"default": null,
"description": "A String or Array of hosts that you wish to block traffic for. Please read the notes for examples on using this https://on.cypress.io/configuration#blacklistHosts"
},
"whitelistHosts": {
"type": ["string", "array"],
"items": {
"type": "string"
},
"default": null,
"description": "A String or Array of hosts that you wish to allow traffic for. Please read the notes for examples on using this https://on.cypress.io/configuration#whitelistHosts"
},
"modifyObstructiveCode": {
"type": "boolean",
"default": true,
Expand Down
8 changes: 7 additions & 1 deletion packages/server/lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ configKeys = toWords """
testFiles execTimeout
trashAssetsBeforeHeadlessRuns pageLoadTimeout
blacklistHosts requestTimeout
userAgent responseTimeout
whitelistHosts responseTimeout
userAgent
viewportWidth
viewportHeight
videoRecording
Expand Down Expand Up @@ -87,6 +88,7 @@ defaults = {
reporter: "spec"
reporterOptions: null
blacklistHosts: null
whitelistHosts: null
clientRoute: "/__/"
xhrRoute: "/xhrs/"
socketIoRoute: "/__socket.io"
Expand Down Expand Up @@ -130,6 +132,7 @@ validationRules = {
animationDistanceThreshold: v.isNumber
baseUrl: v.isFullyQualifiedUrl
blacklistHosts: v.isStringOrArrayOfStrings
whitelistHosts: v.isStringOrArrayOfStrings
modifyObstructiveCode: v.isBoolean
chromeWebSecurity: v.isBoolean
defaultCommandTimeout: v.isNumber
Expand Down Expand Up @@ -245,6 +248,9 @@ module.exports = {
if blacklistHosts = config.blacklistHosts
config.blacklistHosts = toArrayFromPipes(blacklistHosts)

if whitelistHosts = config.whitelistHosts
config.whitelistHosts = toArrayFromPipes(whitelistHosts)

## when headless
if config.isTextTerminal
## dont ever watch for file changes
Expand Down
14 changes: 14 additions & 0 deletions packages/server/lib/controllers/proxy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ module.exports = {

return res.status(503).end()

## if we have white listed hosts
if wlh = config.whitelistHosts
## and url does not match any of our whitelisted hosts
if not matched = blacklist.matches(req.proxiedUrl, wlh)
## then bail and return with 503
## and set a custom header
res.set("x-cypress-not-matched-whitelisted-host", '1')

debug("blacklisting request %o as not on whitelist", {
url: req.proxiedUrl
})

return res.status(503).end()

thr = through (d) -> @queue(d)

@getHttpContent(thr, req, res, remoteState, config, request)
Expand Down
7 changes: 6 additions & 1 deletion packages/server/lib/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Server

createServer: (app, config, request) ->
new Promise (resolve, reject) =>
{port, fileServerFolder, socketIoRoute, baseUrl, blacklistHosts} = config
{port, fileServerFolder, socketIoRoute, baseUrl, blacklistHosts, whitelistHosts} = config

@_server = http.createServer(app)
@_wsProxy = httpProxy.createProxyServer()
Expand Down Expand Up @@ -190,6 +190,11 @@ class Server

debug("HTTPS request #{urlToCheck} matches blacklist?", isMatching)

if whitelistHosts and not isMatching
isMatching = not blacklist.matches(urlToCheck, whitelistHosts)

debug("HTTPS request #{urlToCheck} does not match whitelist?", isMatching)

## make a direct connection only if
## our req url does not match the origin policy
## which is the superDomain + port
Expand Down
6 changes: 3 additions & 3 deletions packages/server/lib/util/blacklist.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ stripProtocolAndDefaultPorts = (urlToCheck) ->
## else return the host
return host

matches = (urlToCheck, blacklistHosts) ->
matches = (urlToCheck, listHosts) ->
## normalize into flat array
blacklistHosts = [].concat(blacklistHosts)
listHosts = [].concat(listHosts)

urlToCheck = stripProtocolAndDefaultPorts(urlToCheck)

Expand All @@ -27,7 +27,7 @@ matches = (urlToCheck, blacklistHosts) ->
## to see if any match
minimatch(urlToCheck, hostMatcher)

_.find(blacklistHosts, matchUrl)
_.find(listHosts, matchUrl)


module.exports = {
Expand Down

0 comments on commit 5664efb

Please sign in to comment.