-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
erver: fixes #442 implements blacklisting hosts with config
- Loading branch information
1 parent
9ab2855
commit 08ada59
Showing
6 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) -> | ||
## 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
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") |