Compare commits

..

4 Commits

Author SHA1 Message Date
sagit 2afb1d275a Merge pull request #109 from Sagit-chu/opencode/playful-circuit
fix(backup): handle nullable tunnel export fields safely
2026-02-13 18:38:02 +08:00
sagit 880cd4cac5 fix(backup): handle nullable tunnel export fields safely 2026-02-13 10:34:58 +00:00
sagit a69a0f040b Merge branch 'main' into opencode/kind-planet 2026-02-13 17:48:27 +08:00
sagit cf6294a77d fix(backend): normalize strategy data and proxy ip parsing 2026-02-13 09:42:38 +00:00
8 changed files with 183 additions and 13 deletions
@@ -114,7 +114,7 @@ func (h *Handler) ensureTunnelPermission(userID int64, roleID int, tunnelID int6
func (h *Handler) getForwardRecord(forwardID int64) (*forwardRecord, error) {
row := h.repo.DB().QueryRow(`
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, strategy, status
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, COALESCE(strategy, 'fifo'), status
FROM forward WHERE id = ? LIMIT 1
`, forwardID)
var fr forwardRecord
@@ -152,7 +152,7 @@ func (h *Handler) getTunnelRecord(tunnelID int64) (*tunnelRecord, error) {
func (h *Handler) listForwardsByTunnel(tunnelID int64) ([]forwardRecord, error) {
rows, err := h.repo.DB().Query(`
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, strategy, status
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, COALESCE(strategy, 'fifo'), status
FROM forward
WHERE tunnel_id = ?
ORDER BY id ASC
+12 -2
View File
@@ -1406,7 +1406,7 @@ func parseIPLiteral(raw string) net.IP {
}
if ip := net.ParseIP(value); ip != nil {
return ip
return normalizeIPAddress(ip)
}
host, _, err := net.SplitHostPort(value)
@@ -1418,7 +1418,17 @@ func parseIPLiteral(raw string) net.IP {
if host == "" {
return nil
}
return net.ParseIP(host)
return normalizeIPAddress(net.ParseIP(host))
}
func normalizeIPAddress(ip net.IP) net.IP {
if ip == nil {
return nil
}
if v4 := ip.To4(); v4 != nil {
return v4
}
return ip.To16()
}
func isTrustedProxyIP(ip net.IP) bool {
@@ -567,6 +567,13 @@ func TestAuthPeerAllowedIPs(t *testing.T) {
xff: "198.51.100.20, 172.20.0.3",
wantAllowed: true,
},
{
name: "ipv4-mapped proxy xff allowed",
allowedIPs: "198.51.100.20",
remoteAddr: "[::ffff:172.20.0.3]:34567",
xff: "198.51.100.20, 172.20.0.3",
wantAllowed: true,
},
{
name: "non whitelisted ip denied",
allowedIPs: "203.0.113.10",
@@ -251,7 +251,7 @@ func (h *Handler) pauseForwardRecords(forwards []forwardRecord, now int64) {
func (h *Handler) listActiveForwardsByUser(userID int64) ([]forwardRecord, error) {
rows, err := h.repo.DB().Query(`
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, strategy, status
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, COALESCE(strategy, 'fifo'), status
FROM forward
WHERE user_id = ? AND status = 1
ORDER BY id ASC
@@ -266,7 +266,7 @@ func (h *Handler) listActiveForwardsByUser(userID int64) ([]forwardRecord, error
func (h *Handler) listActiveForwardsByUserTunnel(userID int64, tunnelID int64) ([]forwardRecord, error) {
rows, err := h.repo.DB().Query(`
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, strategy, status
SELECT id, user_id, user_name, name, tunnel_id, remote_addr, COALESCE(strategy, 'fifo'), status
FROM forward
WHERE user_id = ? AND tunnel_id = ? AND status = 1
ORDER BY id ASC
@@ -3035,8 +3035,8 @@ func replaceTunnelChainsTx(tx *store.Tx, tunnelID int64, req map[string]interfac
if nodeID <= 0 {
continue
}
_, err := tx.Exec(`INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol) VALUES(?, '1', ?, NULL, NULL, 0, ?)`,
tunnelID, nodeID, defaultString(asString(n["protocol"]), "tls"))
_, err := tx.Exec(`INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol) VALUES(?, '1', ?, NULL, ?, 0, ?)`,
tunnelID, nodeID, defaultString(asString(n["strategy"]), "round"), defaultString(asString(n["protocol"]), "tls"))
if err != nil {
return err
}
+72 -5
View File
@@ -727,7 +727,7 @@ func (r *Repository) ListForwards() ([]map[string]interface{}, error) {
}
rows, err := r.db.Query(`
SELECT f.id, f.user_id, f.user_name, f.name, f.tunnel_id, COALESCE(t.name, ''), f.remote_addr, f.strategy,
SELECT f.id, f.user_id, f.user_name, f.name, f.tunnel_id, COALESCE(t.name, ''), f.remote_addr, COALESCE(f.strategy, 'fifo'),
f.in_flow, f.out_flow, f.created_time, f.status, f.inx
FROM forward f
LEFT JOIN tunnel t ON t.id = f.tunnel_id
@@ -1305,7 +1305,7 @@ func bootstrapSchema(db *store.DB, schemaSQL, seedSQL string) error {
return nil
}
const currentSchemaVersion = 1
const currentSchemaVersion = 2
func getSchemaVersion(db *store.DB) int {
_, _ = db.Exec(`CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL DEFAULT 0)`)
@@ -1375,6 +1375,27 @@ func migrateSchema(db *store.DB) error {
}
}
normalizeStrategy := func(table, defaultValue string) error {
_, err := db.Exec(fmt.Sprintf("UPDATE %s SET strategy = ? WHERE strategy IS NULL", table), defaultValue)
if err != nil {
if isMissingTableError(db.Dialect(), err) {
return nil
}
return fmt.Errorf("normalize %s.strategy: %w", table, err)
}
return nil
}
if err := normalizeStrategy("forward", "fifo"); err != nil {
return err
}
if err := normalizeStrategy("chain_tunnel", "round"); err != nil {
return err
}
if err := normalizeStrategy("peer_share_runtime", "round"); err != nil {
return err
}
if db.Dialect() == store.DialectPostgres {
if err := ensurePostgresIDDefaults(db); err != nil {
return err
@@ -1545,6 +1566,17 @@ func isMissingColumnError(dialect store.Dialect, err error) bool {
return strings.Contains(msg, "no such column")
}
func isMissingTableError(dialect store.Dialect, err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
if dialect == store.DialectPostgres {
return strings.Contains(msg, "relation") && strings.Contains(msg, "does not exist")
}
return strings.Contains(msg, "no such table")
}
func (r *Repository) CreatePeerShare(share *PeerShare) error {
if r == nil || r.db == nil {
return errors.New("repository not initialized")
@@ -2298,13 +2330,25 @@ func (r *Repository) exportTunnels() ([]TunnelBackup, error) {
var tunnels []TunnelBackup
for rows.Next() {
var t TunnelBackup
var protocol sql.NullString
var updatedTime sql.NullInt64
var inIP sql.NullString
if err := rows.Scan(&t.ID, &t.Name, &t.TrafficRatio, &t.Type, &t.Protocol, &t.Flow, &t.CreatedTime, &t.UpdatedTime, &t.Status, &inIP, &t.Inx); err != nil {
var inx sql.NullInt64
if err := rows.Scan(&t.ID, &t.Name, &t.TrafficRatio, &t.Type, &protocol, &t.Flow, &t.CreatedTime, &updatedTime, &t.Status, &inIP, &inx); err != nil {
return nil, err
}
if protocol.Valid {
t.Protocol = protocol.String
}
if updatedTime.Valid {
t.UpdatedTime = updatedTime.Int64
}
if inIP.Valid {
t.InIP = inIP.String
}
if inx.Valid {
t.Inx = int(inx.Int64)
}
// Export chain tunnels
chainTunnels, err := r.exportChainTunnels(t.ID)
if err != nil {
@@ -2330,12 +2374,23 @@ func (r *Repository) exportChainTunnels(tunnelID int64) ([]ChainTunnelBackup, er
for rows.Next() {
var ct ChainTunnelBackup
var port sql.NullInt64
if err := rows.Scan(&ct.ID, &ct.TunnelID, &ct.ChainType, &ct.NodeID, &port, &ct.Strategy, &ct.Inx, &ct.Protocol); err != nil {
var strategy, protocol sql.NullString
var inx sql.NullInt64
if err := rows.Scan(&ct.ID, &ct.TunnelID, &ct.ChainType, &ct.NodeID, &port, &strategy, &inx, &protocol); err != nil {
return nil, err
}
if port.Valid {
ct.Port = int(port.Int64)
}
if strategy.Valid {
ct.Strategy = strategy.String
}
if inx.Valid {
ct.Inx = int(inx.Int64)
}
if protocol.Valid {
ct.Protocol = protocol.String
}
chainTunnels = append(chainTunnels, ct)
}
return chainTunnels, rows.Err()
@@ -2354,9 +2409,21 @@ func (r *Repository) exportForwards() ([]ForwardBackup, error) {
var forwards []ForwardBackup
for rows.Next() {
var f ForwardBackup
if err := rows.Scan(&f.ID, &f.UserID, &f.UserName, &f.Name, &f.TunnelID, &f.RemoteAddr, &f.Strategy, &f.InFlow, &f.OutFlow, &f.CreatedTime, &f.UpdatedTime, &f.Status, &f.Inx); err != nil {
var strategy sql.NullString
var updatedTime sql.NullInt64
var inx sql.NullInt64
if err := rows.Scan(&f.ID, &f.UserID, &f.UserName, &f.Name, &f.TunnelID, &f.RemoteAddr, &strategy, &f.InFlow, &f.OutFlow, &f.CreatedTime, &updatedTime, &f.Status, &inx); err != nil {
return nil, err
}
if strategy.Valid {
f.Strategy = strategy.String
}
if updatedTime.Valid {
f.UpdatedTime = updatedTime.Int64
}
if inx.Valid {
f.Inx = int(inx.Int64)
}
forwards = append(forwards, f)
}
return forwards, rows.Err()
@@ -310,6 +310,80 @@ func TestBackupExportImportRestoreContracts(t *testing.T) {
t.Fatalf("expected restored config value v3, got %+v", cfg)
}
})
t.Run("backup export tolerates nullable legacy tunnel chain fields", func(t *testing.T) {
now := time.Now().UnixMilli()
res, err := repo.DB().Exec(`
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, "legacy-null-chain", 1.0, 1, "tls", 1000, now, now, 1, nil, 1)
if err != nil {
t.Fatalf("seed tunnel for nullable chain export: %v", err)
}
tunnelID, err := res.LastInsertId()
if err != nil {
t.Fatalf("read tunnel id for nullable chain export: %v", err)
}
if _, err := repo.DB().Exec(`
INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol)
VALUES(?, ?, ?, ?, ?, ?, ?)
`, tunnelID, "1", 1, nil, nil, nil, nil); err != nil {
t.Fatalf("seed nullable chain_tunnel row: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/api/v1/backup/export", bytes.NewBufferString(`{"types":["tunnels"]}`))
req.Header.Set("Authorization", adminToken)
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.Code)
}
var payload struct {
Version string `json:"version"`
Tunnels []struct {
ID int64 `json:"id"`
ChainTunnels []struct {
Inx int `json:"inx"`
Strategy string `json:"strategy"`
Protocol string `json:"protocol"`
} `json:"chainTunnels"`
} `json:"tunnels"`
}
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
t.Fatalf("decode tunnels backup payload: %v", err)
}
if strings.TrimSpace(payload.Version) == "" {
t.Fatalf("expected backup payload version, got empty")
}
found := false
for _, tunnel := range payload.Tunnels {
if tunnel.ID != tunnelID {
continue
}
if len(tunnel.ChainTunnels) != 1 {
t.Fatalf("expected one chain tunnel for seeded tunnel %d, got %d", tunnelID, len(tunnel.ChainTunnels))
}
if tunnel.ChainTunnels[0].Inx != 0 {
t.Fatalf("expected nullable chain inx to export as 0, got %d", tunnel.ChainTunnels[0].Inx)
}
if tunnel.ChainTunnels[0].Strategy != "" {
t.Fatalf("expected nullable chain strategy to export as empty string, got %q", tunnel.ChainTunnels[0].Strategy)
}
if tunnel.ChainTunnels[0].Protocol != "" {
t.Fatalf("expected nullable chain protocol to export as empty string, got %q", tunnel.ChainTunnels[0].Protocol)
}
found = true
break
}
if !found {
t.Fatalf("expected seeded tunnel %d in backup export", tunnelID)
}
})
}
type backupExportPayload struct {
@@ -2,6 +2,7 @@ package contract_test
import (
"bytes"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -144,6 +145,17 @@ func TestTunnelUpdateAssignsChainPortsContract(t *testing.T) {
if outPort <= 0 {
t.Fatalf("expected out node port to be assigned, got %d", outPort)
}
var entryStrategy sql.NullString
if err := repo.DB().QueryRow(`SELECT strategy FROM chain_tunnel WHERE tunnel_id = ? AND chain_type = 1 LIMIT 1`, tunnelID).Scan(&entryStrategy); err != nil {
t.Fatalf("query entry strategy: %v", err)
}
if !entryStrategy.Valid || strings.TrimSpace(entryStrategy.String) == "" {
t.Fatalf("expected entry strategy to be non-null and non-empty")
}
if entryStrategy.String != "round" {
t.Fatalf("expected entry strategy round, got %q", entryStrategy.String)
}
}
func jsonInt(v int64) string {