Add server-registry store methods with record-ID validation (step 1)
New IStore methods for a per-server registry (GetServers, GetServerByID, CreateServer, DeleteServer, GetServerSettings/SaveServerSettings, GetServerHashes/SaveServerHashes), all additive - existing single-server methods untouched. Adds util.ValidateRecordID/ValidateInterfaceName and applies them to every new method taking a server ID, closing a path- traversal gap before serverID is ever driven by user input (flagged by a security review pass).
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -165,6 +166,31 @@ func ValidateServerAddresses(cidrs []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// recordIDPattern restricts identifiers that end up as scribble/jsondb
|
||||
// record keys (and therefore as filesystem path components) to a safe,
|
||||
// predictable character set - no "/", "..", or other path metacharacters.
|
||||
var recordIDPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,64}$`)
|
||||
|
||||
// ValidateRecordID validates an identifier before it is used as a jsondb
|
||||
// record key (i.e. a filename component under the db directory), to
|
||||
// prevent path traversal / arbitrary file writes via a crafted ID.
|
||||
func ValidateRecordID(id string) bool {
|
||||
return recordIDPattern.MatchString(id)
|
||||
}
|
||||
|
||||
// interfaceNamePattern enforces Linux's IFNAMSIZ limit (16 bytes including
|
||||
// the trailing NUL, so 15 usable characters) and a safe character set for
|
||||
// any string that may later be passed as a network interface name to
|
||||
// external tools (wg-quick, systemctl unit names, etc.).
|
||||
var interfaceNamePattern = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,15}$`)
|
||||
|
||||
// ValidateInterfaceName validates a WireGuard interface name before it is
|
||||
// stored or ever used as an argument to exec'd tools such as wg-quick or
|
||||
// systemctl, to prevent command/argument injection and invalid interfaces.
|
||||
func ValidateInterfaceName(name string) bool {
|
||||
return interfaceNamePattern.MatchString(name)
|
||||
}
|
||||
|
||||
// ValidateIPAddress to validate the IPv4 and IPv6 address
|
||||
func ValidateIPAddress(ip string) bool {
|
||||
if net.ParseIP(ip) == nil {
|
||||
|
||||
Reference in New Issue
Block a user