coredns/plugin/file/reload.go
Yong Tang c6709d930f
Fix security scans by cleaning up file path (#5185)
While performing security scans there were several
issue raised as G304 (CWE-22): Potential file inclusion via variable.
As some files path are taken from user input, it is possible the
filepath passed by user may have unintended effect if not properly formed.
This fix add Clean to remove the security warning and address some
potential issue.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
2022-02-14 11:24:21 -05:00

69 lines
1.5 KiB
Go

package file
import (
"os"
"path/filepath"
"time"
"github.com/coredns/coredns/plugin/transfer"
)
// Reload reloads a zone when it is changed on disk. If z.ReloadInterval is zero, no reloading will be done.
func (z *Zone) Reload(t *transfer.Transfer) error {
if z.ReloadInterval == 0 {
return nil
}
tick := time.NewTicker(z.ReloadInterval)
go func() {
for {
select {
case <-tick.C:
zFile := z.File()
reader, err := os.Open(filepath.Clean(zFile))
if err != nil {
log.Errorf("Failed to open zone %q in %q: %v", z.origin, zFile, err)
continue
}
serial := z.SOASerialIfDefined()
zone, err := Parse(reader, z.origin, zFile, serial)
reader.Close()
if err != nil {
if _, ok := err.(*serialErr); !ok {
log.Errorf("Parsing zone %q: %v", z.origin, err)
}
continue
}
// copy elements we need
z.Lock()
z.Apex = zone.Apex
z.Tree = zone.Tree
z.Unlock()
log.Infof("Successfully reloaded zone %q in %q with %d SOA serial", z.origin, zFile, z.Apex.SOA.Serial)
if t != nil {
if err := t.Notify(z.origin); err != nil {
log.Warningf("Failed sending notifies: %s", err)
}
}
case <-z.reloadShutdown:
tick.Stop()
return
}
}
}()
return nil
}
// SOASerialIfDefined returns the SOA's serial if the zone has a SOA record in the Apex, or -1 otherwise.
func (z *Zone) SOASerialIfDefined() int64 {
z.RLock()
defer z.RUnlock()
if z.Apex.SOA != nil {
return int64(z.Apex.SOA.Serial)
}
return -1
}