-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 442 - blacklisting hosts with config.json #1062
Changes from 2 commits
08ada59
d1318f1
68b7f47
4a7584f
7c3ec65
a943914
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
_ = require("lodash") | ||
url = require("url") | ||
minimatch = require("minimatch") | ||
|
||
DEFAULT_PORTS = ["443", "80"] | ||
|
||
stripProtocolAndDefaultPorts = (urlToCheck) -> | ||
## grab host which is 'hostname:port' only | ||
{ host, hostname, port } = url.parse(urlToCheck) | ||
|
||
## if we have a default port for 80 or 443 | ||
## then just return the hostname | ||
if port in DEFAULT_PORTS | ||
return hostname | ||
|
||
## else return the host | ||
return host | ||
|
||
matches = (urlToCheck, blacklistHosts) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, what does this return? needs a jsdoc block I feel, because this is so reusable |
||
## normalize into flat array | ||
blacklistHosts = [].concat(blacklistHosts) | ||
|
||
urlToCheck = stripProtocolAndDefaultPorts(urlToCheck) | ||
|
||
matchUrl = (hostMatcher) -> | ||
## use minimatch against the url | ||
## to see if any match | ||
minimatch(urlToCheck, hostMatcher) | ||
|
||
_.find(blacklistHosts, matchUrl) | ||
|
||
|
||
module.exports = { | ||
matches | ||
|
||
stripProtocolAndDefaultPorts | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2610,6 +2610,90 @@ describe "Routes", -> | |
|
||
expect(res.body).to.include("Cypress errored attempting to make an http request to this url") | ||
|
||
context "blacklisted hosts", -> | ||
beforeEach -> | ||
@setup({ | ||
config: { | ||
blacklistHosts: [ | ||
"*.google.com" | ||
"shop.apple.com" | ||
"cypress.io" | ||
"localhost:6666" | ||
"*adwords.com" | ||
] | ||
} | ||
}) | ||
|
||
it "returns 503 and custom headers for all hosts", -> | ||
expectedHeader = (res, val) -> | ||
expect(res.headers["x-cypress-matched-blacklisted-host"]).to.eq(val) | ||
|
||
Promise.all([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also use https://github.com/bahmutov/snap-shot-it#data-driven-testing to make the test simpler here |
||
@rp("https://mail.google.com/f") | ||
@rp("http://shop.apple.com/o") | ||
@rp("https://cypress.io") | ||
@rp("https://localhost:6666/o") | ||
@rp("https://some.adwords.com") | ||
]) | ||
.spread (responses...) -> | ||
_.every responses, (res) -> | ||
expect(res.statusCode).to.eq(503) | ||
expect(res.body).to.be.empty | ||
|
||
expectedHeader(responses[0], "*.google.com") | ||
expectedHeader(responses[1], "shop.apple.com") | ||
expectedHeader(responses[2], "cypress.io") | ||
expectedHeader(responses[3], "localhost:6666") | ||
expectedHeader(responses[4], "*adwords.com") | ||
|
||
context "extra http request headers", -> | ||
beforeEach -> | ||
@setup({ | ||
initialUrl: "http://localhost:3333/" | ||
config: { | ||
extraRequestHeaders: { | ||
"x-foo-bar": "baz" | ||
"some": "override" | ||
} | ||
} | ||
}) | ||
|
||
it "sets and overrides extra request headers", -> | ||
nock("http://localhost:3333") | ||
.matchHeader("x-foo-bar", "baz") | ||
.matchHeader("some", "override") | ||
.matchHeader("another", "one") | ||
.get("/headers") | ||
.reply(200) | ||
|
||
@rp({ | ||
url: "http://localhost:3333/headers" | ||
headers: { | ||
some: "thing" | ||
another: "one" | ||
} | ||
}) | ||
.then (res) -> | ||
expect(res.statusCode).to.eq(200) | ||
|
||
it "does not set extra request headers for domains not matching", -> | ||
# nock("http://localhost:3322") | ||
# .matchHeader("some", "thing") | ||
# .matchHeader("another", "one") | ||
# .get("/headers") | ||
# .reply(200) | ||
|
||
@rp({ | ||
# url: "https://mail.google.com/" | ||
url: "https://localhost:3322/headers" | ||
headers: { | ||
some: "thing" | ||
another: "one" | ||
} | ||
}) | ||
.then (res) -> | ||
expect(res.statusCode).to.eq(200) | ||
|
||
context "POST *", -> | ||
beforeEach -> | ||
@setup("http://localhost:8000") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require("../spec_helper") | ||
|
||
blacklist = require("#{root}lib/util/blacklist") | ||
|
||
hosts = [ | ||
"*.google.com" | ||
"shop.apple.com" | ||
"localhost:6666" | ||
"adwords.com" | ||
"*yahoo.com" | ||
] | ||
|
||
matchesStr = (url, host, val) -> | ||
m = blacklist.matches(url, host) | ||
expect(!!m).to.eq(val, "url: '#{url}' did not pass") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohhh, the |
||
|
||
matchesArray = (url, val) -> | ||
m = blacklist.matches(url, hosts) | ||
expect(!!m).to.eq(val, "url: '#{url}' did not pass") | ||
|
||
matchesHost = (url, host) -> | ||
expect(blacklist.matches(url, hosts)).to.eq(host) | ||
|
||
describe "lib/util/blacklist", -> | ||
it "handles hosts, ports, wildcards", -> | ||
matchesArray("https://mail.google.com/foo", true) | ||
matchesArray("https://shop.apple.com/bar", true) | ||
matchesArray("http://localhost:6666/", true) | ||
matchesArray("https://localhost:6666/", true) | ||
matchesArray("https://adwords.com:443/", true) | ||
matchesArray("http://adwords.com:80/quux", true) | ||
matchesArray("https://yahoo.com:443/asdf", true) | ||
matchesArray("http://mail.yahoo.com:443/asdf", true) | ||
|
||
matchesArray("https://buy.adwords.com:443/", false) | ||
matchesArray("http://localhost:66667", false) | ||
matchesArray("http://mac.apple.com/", false) | ||
|
||
matchesStr("https://localhost:6666/foo", "localhost:6666", true) | ||
|
||
it "returns the matched host", -> | ||
matchesHost("https://shop.apple.com:443/foo", "shop.apple.com") | ||
matchesHost("http://mail.yahoo.com:80/bar", "*yahoo.com") | ||
matchesHost("https://localhost:6666/bar", "localhost:6666") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string or array of strings here