Audit changes

This commit is contained in:
jdl
2026-06-14 08:15:00 +02:00
parent 52ea1a8d42
commit fa182eca76
13 changed files with 29 additions and 15 deletions

View File

@@ -27,7 +27,7 @@ type API struct {
} }
func New(dbPath string) (*API, error) { func New(dbPath string) (*API, error) {
sqlDB, err := sql.Open("sqlite3", dbPath+"?_journal=WAL") sqlDB, err := sql.Open("sqlite3", dbPath+"?_journal=WAL&_foreign_keys=on")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -92,7 +92,7 @@ func Peer_Validate(p *Peer) error {
return errs.ErrInvalidPort return errs.ErrInvalidPort
} }
if len(p.Name) == 0 { if len(p.Name) == 0 || len(p.Name) > 63 {
return errs.ErrInvalidPeerName return errs.ErrInvalidPeerName
} }
for _, c := range p.Name { for _, c := range p.Name {

View File

@@ -1,7 +1,5 @@
package db package db
import "database/sql"
func Peer_ListAll(tx TX, networkID int64) ([]*Peer, error) { func Peer_ListAll(tx TX, networkID int64) ([]*Peer, error) {
const query = Peer_SelectQuery + ` WHERE NetworkID=? ORDER BY PeerIP ASC` const query = Peer_SelectQuery + ` WHERE NetworkID=? ORDER BY PeerIP ASC`
return Peer_List(tx, query, networkID) return Peer_List(tx, query, networkID)
@@ -14,8 +12,8 @@ func Peer_GetByAPIKey(tx TX, apiKey string) (*Peer, error) {
apiKey) apiKey)
} }
func Network_HasPeers(db *sql.DB, networkID int64) (exists bool, err error) { func Network_HasPeers(tx TX, networkID int64) (exists bool, err error) {
const query = "SELECT EXISTS(SELECT 1 FROM peers WHERE NetworkID=?)" const query = "SELECT EXISTS(SELECT 1 FROM peers WHERE NetworkID=?)"
err = db.QueryRow(query, networkID).Scan(&exists) err = tx.QueryRow(query, networkID).Scan(&exists)
return exists, err return exists, err
} }

View File

@@ -21,5 +21,6 @@ CREATE TABLE peers (
WGPubKey BLOB NOT NULL, WGPubKey BLOB NOT NULL,
SignPubKey BLOB NOT NULL, SignPubKey BLOB NOT NULL,
UNIQUE(NetworkID, Name), UNIQUE(NetworkID, Name),
PRIMARY KEY(NetworkID, PeerIP) PRIMARY KEY(NetworkID, PeerIP),
FOREIGN KEY(NetworkID) REFERENCES networks(NetworkID)
) WITHOUT ROWID; ) WITHOUT ROWID;

View File

@@ -19,7 +19,7 @@ func (a *App) setCookie(w http.ResponseWriter, name, value string) {
Secure: !a.insecure, Secure: !a.insecure,
SameSite: http.SameSiteStrictMode, SameSite: http.SameSiteStrictMode,
HttpOnly: true, HttpOnly: true,
MaxAge: 86400 * 21, MaxAge: 86400 * 365 * 10,
}) })
} }

View File

@@ -18,6 +18,7 @@ func (app *App) handlePub(pattern string, fn handlerFunc) {
s := app.api.Session_Get(sessionID) s := app.api.Session_Get(sessionID)
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
r.Body = http.MaxBytesReader(w, r.Body, 128*1024)
r.ParseMultipartForm(64 * 1024) r.ParseMultipartForm(64 * 1024)
} else { } else {
r.ParseForm() r.ParseForm()

View File

@@ -65,14 +65,18 @@ func Unmarshal(buf [Size]byte) (Ping, error) {
p := Ping{ p := Ping{
PingTS: int64(binary.BigEndian.Uint64(buf[1:9])), PingTS: int64(binary.BigEndian.Uint64(buf[1:9])),
} }
if addr := netip.AddrFrom4([4]byte(buf[9:13])); !addr.IsUnspecified() {
if addr := netip.AddrFrom4([4]byte(buf[9:13])); !addr.IsUnspecified() && addr.Is4() {
p.SrcV4 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[13:15])) p.SrcV4 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[13:15]))
} }
if addr := netip.AddrFrom16([16]byte(buf[15:31])); !addr.IsUnspecified() {
if addr := netip.AddrFrom16([16]byte(buf[15:31])); !addr.IsUnspecified() && addr.Is6() {
p.SrcV6 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[31:33])) p.SrcV6 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[31:33]))
} }
if addr := netip.AddrFrom16([16]byte(buf[33:49])).Unmap(); !addr.IsUnspecified() { if addr := netip.AddrFrom16([16]byte(buf[33:49])).Unmap(); !addr.IsUnspecified() {
p.Dst = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[49:51])) p.Dst = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[49:51]))
} }
return p, nil return p, nil
} }

View File

@@ -61,7 +61,7 @@ func (a *App) devPromote(p *Peer) {
if ep.IsValid() { if ep.IsValid() {
log.Printf("PROMOTED: %s - %s @ %s", p.Name, p.VPNIP.String(), p.WGEndpoint().String()) log.Printf("PROMOTED: %s - %s @ %s", p.Name, p.VPNIP.String(), p.WGEndpoint().String())
} else { } else {
log.Printf("PROMOTED: %s - %s (no IP)", p.Name, p.VPNIP.String()) log.Printf("DIRECT: %s - %s (waiting for handshake)", p.Name, p.VPNIP.String())
} }
devRetry(p.VPNIP, "Promote", func() error { return a.dev.Promote(p.PubKey(), p.VPNIP) }) devRetry(p.VPNIP, "Promote", func() error { return a.dev.Promote(p.PubKey(), p.VPNIP) })
p.State = StateDirect p.State = StateDirect

View File

@@ -89,7 +89,7 @@ func (hp *HubPoller) poll() {
return return
} }
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(io.LimitReader(resp.Body, 128*1024))
if err != nil { if err != nil {
log.Printf("[HubPoller] read body: %v", err) log.Printf("[HubPoller] read body: %v", err)
return return

View File

@@ -15,7 +15,7 @@ func (a *App) onMulticastDiscovery(pkt multicast.Packet) {
vpnIP := netip.AddrFrom4(octets) vpnIP := netip.AddrFrom4(octets)
peer, ok := a.peersByIP[vpnIP] peer, ok := a.peersByIP[vpnIP]
if !ok { if !ok || peer.IsPublic {
return return
} }

View File

@@ -58,10 +58,12 @@ func (a *App) onPing(e PingEvent) {
peer.UpdateEndpoints(e.ping.SrcV4, e.ping.SrcV6) peer.UpdateEndpoints(e.ping.SrcV4, e.ping.SrcV6)
} }
var cgnatPrefix = netip.MustParsePrefix("100.64.0.0/10")
func addrIsRoutable(addrPort netip.AddrPort) bool { func addrIsRoutable(addrPort netip.AddrPort) bool {
if addrPort.Port() == 0 { if addrPort.Port() == 0 {
return false return false
} }
addr := addrPort.Addr() addr := addrPort.Addr()
return addr.IsGlobalUnicast() && !addr.IsPrivate() return addr.IsGlobalUnicast() && !addr.IsPrivate() && !cgnatPrefix.Contains(addr)
} }

View File

@@ -39,6 +39,7 @@ func (a *App) onTick() {
case StateRelayed: case StateRelayed:
// If we have an ep to probe, add it. // If we have an ep to probe, add it.
if ep := p.PreferredEndpoint(); ep.IsValid() { if ep := p.PreferredEndpoint(); ep.IsValid() {
p.ProbeStart = time.Now()
a.devAddProbe(p, ep) a.devAddProbe(p, ep)
} }
@@ -50,6 +51,12 @@ func (a *App) onTick() {
} else if ep := p.PreferredEndpoint(); ep.IsValid() && ep != p.WGEndpoint() { } else if ep := p.PreferredEndpoint(); ep.IsValid() && ep != p.WGEndpoint() {
// Update the probe address if it's changed. // Update the probe address if it's changed.
a.devAddProbe(p, ep) a.devAddProbe(p, ep)
} else if time.Since(p.ProbeStart) > 8*wginterface.ProbeKeepalive {
// Give up probing if we haven't been able to handshake.
p.EndpointV4 = netip.AddrPort{}
p.EndpointV6 = netip.AddrPort{}
p.EndpointLAN = netip.AddrPort{}
a.devAddPeer(p)
} }
case StateDirect: case StateDirect:

View File

@@ -27,8 +27,9 @@ type Peer struct {
EndpointV4 netip.AddrPort // Reported IPv4 endpoint. EndpointV4 netip.AddrPort // Reported IPv4 endpoint.
EndpointV6 netip.AddrPort // Reported IPv6 endpoint. EndpointV6 netip.AddrPort // Reported IPv6 endpoint.
EndpointLAN netip.AddrPort // Discovered via multicast. EndpointLAN netip.AddrPort // Discovered via multicast.
EndpointWG netip.AddrPort // Current wireguard port. EndpointWG netip.AddrPort // Current wireguard endpoint.
RTT time.Duration // Round-trip time. RTT time.Duration // Round-trip time.
ProbeStart time.Time // When we started probing.
State PeerState // Current routing state; updated on each devXxx call. State PeerState // Current routing state; updated on each devXxx call.
Role control.Role // Role in relation to the local application. Role control.Role // Role in relation to the local application.
SignPubKey [32]byte // nacl/sign public key for verifying multicast beacons. SignPubKey [32]byte // nacl/sign public key for verifying multicast beacons.