Merge: * SB/PC: limit the number of hashes in request to 4

Close #1697

* commit '0ee3505e1f0cc3df78651f2f9cb393c5ff3df26e':
  * SB/PC: limit the number of hashes in request to 4
This commit is contained in:
Simon Zolin 2020-06-26 11:21:09 +03:00
commit 7a77dadab0
2 changed files with 19 additions and 0 deletions

View File

@ -146,6 +146,12 @@ func TestEtcHostsMatching(t *testing.T) {
// SAFE BROWSING
func TestSafeBrowsingHash(t *testing.T) {
q, hashes := hostnameToHashParam("1.2.3.4.5.6")
assert.Equal(t, "0132d0fa.b5413b4e.5fa067c1.e7f6c011.", q)
assert.Equal(t, 4, len(hashes))
}
func TestSafeBrowsing(t *testing.T) {
d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Close()

View File

@ -157,6 +157,7 @@ func (d *Dnsfilter) checkSafeSearch(host string) (Result, error) {
}
// for each dot, hash it and add it to string
// The maximum is 4 components: "a.b.c.d"
func hostnameToHashParam(host string) (string, map[string]bool) {
var hashparam bytes.Buffer
hashes := map[string]bool{}
@ -166,6 +167,18 @@ func hostnameToHashParam(host string) (string, map[string]bool) {
tld = ""
}
curhost := host
nDots := 0
for i := len(curhost) - 1; i >= 0; i-- {
if curhost[i] == '.' {
nDots++
if nDots == 4 {
curhost = curhost[i+1:] // "xxx.a.b.c.d" -> "a.b.c.d"
break
}
}
}
for {
if curhost == "" {
// we've reached end of string