plugin/rewrite: fix domain length validation (#2995)
* unit test * fix domain length validation * code optimization * remove unit test
This commit is contained in:
parent
a5c405f6d7
commit
69a2397bf5
2 changed files with 3 additions and 19 deletions
|
@ -9,8 +9,6 @@ import (
|
||||||
|
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
"github.com/coredns/coredns/request"
|
"github.com/coredns/coredns/request"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type exactNameRule struct {
|
type exactNameRule struct {
|
||||||
|
@ -264,19 +262,6 @@ func (rule *substringNameRule) GetResponseRule() ResponseRule { return ResponseR
|
||||||
// GetResponseRule return a rule to rewrite the response with.
|
// GetResponseRule return a rule to rewrite the response with.
|
||||||
func (rule *regexNameRule) GetResponseRule() ResponseRule { return rule.ResponseRule }
|
func (rule *regexNameRule) GetResponseRule() ResponseRule { return rule.ResponseRule }
|
||||||
|
|
||||||
// validName returns true if s is valid domain name and shorter than 256 characters.
|
|
||||||
func validName(s string) bool {
|
|
||||||
_, ok := dns.IsDomainName(s)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if len(dns.Name(s).String()) > 255 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// hasClosingDot return true if s has a closing dot at the end.
|
// hasClosingDot return true if s has a closing dot at the end.
|
||||||
func hasClosingDot(s string) bool {
|
func hasClosingDot(s string) bool {
|
||||||
if strings.HasSuffix(s, ".") {
|
if strings.HasSuffix(s, ".") {
|
||||||
|
|
|
@ -44,11 +44,10 @@ func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
|
||||||
for _, rule := range rw.Rules {
|
for _, rule := range rw.Rules {
|
||||||
switch result := rule.Rewrite(ctx, state); result {
|
switch result := rule.Rewrite(ctx, state); result {
|
||||||
case RewriteDone:
|
case RewriteDone:
|
||||||
if !validName(state.Req.Question[0].Name) {
|
if _, ok := dns.IsDomainName(state.Req.Question[0].Name); !ok {
|
||||||
x := state.Req.Question[0].Name
|
err := fmt.Errorf("invalid name after rewrite: %s", state.Req.Question[0].Name)
|
||||||
log.Errorf("Invalid name after rewrite: %s", x)
|
|
||||||
state.Req.Question[0] = wr.originalQuestion
|
state.Req.Question[0] = wr.originalQuestion
|
||||||
return dns.RcodeServerFailure, fmt.Errorf("invalid name after rewrite: %s", x)
|
return dns.RcodeServerFailure, err
|
||||||
}
|
}
|
||||||
respRule := rule.GetResponseRule()
|
respRule := rule.GetResponseRule()
|
||||||
if respRule.Active {
|
if respRule.Active {
|
||||||
|
|
Loading…
Add table
Reference in a new issue