Skip to content

Commit

Permalink
safesearch: https req
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Apr 9, 2024
1 parent 6f36ebc commit a665e72
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/filtering/safesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (d *DNSFilter) checkSafeSearch(
) (res Result, err error) {
if !setts.ProtectionEnabled ||
!setts.SafeSearchEnabled ||
(qtype != dns.TypeA && qtype != dns.TypeAAAA) {
(qtype != dns.TypeA && qtype != dns.TypeAAAA && qtype != dns.TypeHTTPS) {
return Result{}, nil
}

Expand Down
7 changes: 5 additions & 2 deletions internal/filtering/safesearch/safesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ func (ss *Default) CheckHost(host string, qtype rules.RRType) (res filtering.Res
ss.log(log.DEBUG, "lookup for %q finished in %s", host, time.Since(start))
}()

if qtype != dns.TypeA && qtype != dns.TypeAAAA {
return filtering.Result{}, fmt.Errorf("unsupported question type %s", dns.Type(qtype))
switch qtype {
case dns.TypeA, dns.TypeAAAA, dns.TypeHTTPS:
// Go on.
default:
return filtering.Result{}, nil
}

// Check cache. Return cached result if it was found
Expand Down
71 changes: 40 additions & 31 deletions internal/filtering/safesearch/safesearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,54 @@ func TestDefault_CheckHost_yandex(t *testing.T) {
ss, err := safesearch.NewDefault(conf, "", testCacheSize, testCacheTTL)
require.NoError(t, err)

// Check host for each domain.
for _, host := range []string{
hosts := []string{
"yandex.ru",
"yAndeX.ru",
"YANdex.COM",
"yandex.by",
"yandex.kz",
"www.yandex.com",
} {
var res filtering.Result
res, err = ss.CheckHost(host, testQType)
require.NoError(t, err)

assert.True(t, res.IsFiltered)

require.Len(t, res.Rules, 1)

assert.Equal(t, yandexIP, res.Rules[0].IP)
assert.Equal(t, rulelist.URLFilterIDSafeSearch, res.Rules[0].FilterListID)
}
}

func TestDefault_CheckHost_yandexAAAA(t *testing.T) {
conf := testConf
ss, err := safesearch.NewDefault(conf, "", testCacheSize, testCacheTTL)
require.NoError(t, err)

res, err := ss.CheckHost("www.yandex.ru", dns.TypeAAAA)
require.NoError(t, err)

assert.True(t, res.IsFiltered)

// TODO(a.garipov): Currently, the safe-search filter returns a single rule
// with a nil IP address. This isn't really necessary and should be changed
// once the TODO in [safesearch.Default.newResult] is resolved.
require.Len(t, res.Rules, 1)

assert.Empty(t, res.Rules[0].IP)
assert.Equal(t, rulelist.URLFilterIDSafeSearch, res.Rules[0].FilterListID)
testCases := []struct {
name string
qt uint16
want netip.Addr
}{{
name: "a",
qt: dns.TypeA,
want: yandexIP,
}, {
name: "aaaa",
qt: dns.TypeAAAA,
want: netip.Addr{},
}, {
name: "https",
qt: dns.TypeHTTPS,
want: netip.Addr{},
}}

for _, tc := range testCases {
for _, host := range hosts {
t.Run(tc.name+"_"+host, func(t *testing.T) {
// Check host for each domain.
var res filtering.Result
res, err = ss.CheckHost(host, tc.qt)
require.NoError(t, err)

assert.True(t, res.IsFiltered)

// TODO(a.garipov): In case of AAAA and HTTPS requests, the
// safe-search filter returns a single rule with a nil IP
// address. This isn't really necessary and should be changed
// once the TODO in [safesearch.Default.newResult] is resolved.
require.Len(t, res.Rules, 1)

assert.Equal(t, tc.want, res.Rules[0].IP)
assert.Equal(t, rulelist.URLFilterIDSafeSearch, res.Rules[0].FilterListID)
})
}
}
}

func TestDefault_CheckHost_google(t *testing.T) {
Expand Down

0 comments on commit a665e72

Please sign in to comment.