fix(db): strip NUL bytes from SNMP strings before PostgreSQL insert
Made-with: Cursor
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// pgText убирает U+0000: PostgreSQL text/json не принимают нулевой байт в UTF-8 (SQLSTATE 22021).
|
||||
func pgText(s string) string {
|
||||
return strings.ReplaceAll(s, "\x00", "")
|
||||
}
|
||||
|
||||
type PostgresStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
@@ -61,8 +66,8 @@ func (s *PostgresStore) CreateScan(req CreateScanRequest) (ScanJob, error) {
|
||||
}
|
||||
|
||||
var snmpID any
|
||||
if job.SNMPCredentialsID != "" {
|
||||
snmpID = job.SNMPCredentialsID
|
||||
if v := pgText(job.SNMPCredentialsID); v != "" {
|
||||
snmpID = v
|
||||
}
|
||||
|
||||
query := `
|
||||
@@ -74,7 +79,7 @@ insert into scan_jobs (
|
||||
|
||||
_, err = s.db.ExecContext(context.Background(), query,
|
||||
job.ID,
|
||||
job.Name,
|
||||
pgText(job.Name),
|
||||
job.Status,
|
||||
string(cidrsJSON),
|
||||
string(excludeJSON),
|
||||
@@ -375,10 +380,10 @@ values ($1, $2, $3, $4, $5, $6, $7, $8)`
|
||||
scanID,
|
||||
result.IP,
|
||||
result.Success,
|
||||
result.SysName,
|
||||
result.SysDescr,
|
||||
result.SysObjectID,
|
||||
result.Error,
|
||||
pgText(result.SysName),
|
||||
pgText(result.SysDescr),
|
||||
pgText(result.SysObjectID),
|
||||
pgText(result.Error),
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
@@ -433,10 +438,10 @@ values ($1, $2, $3, $4, $5, $6, $7)`
|
||||
query,
|
||||
scanID,
|
||||
result.IP,
|
||||
result.LocalPortNum,
|
||||
result.RemoteChassisID,
|
||||
result.RemotePortID,
|
||||
result.RemoteSysName,
|
||||
pgText(result.LocalPortNum),
|
||||
pgText(result.RemoteChassisID),
|
||||
pgText(result.RemotePortID),
|
||||
pgText(result.RemoteSysName),
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
@@ -487,9 +492,9 @@ values ($1, $2, $3, $4, $5, $6, $7)`
|
||||
scanID,
|
||||
result.IP,
|
||||
result.IfIndex,
|
||||
result.IfDescr,
|
||||
result.IfName,
|
||||
result.IfOperStatus,
|
||||
pgText(result.IfDescr),
|
||||
pgText(result.IfName),
|
||||
pgText(result.IfOperStatus),
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
@@ -589,7 +594,7 @@ values ($1, $2, $3, $4, $5, $6, $7, $8)`
|
||||
result.IfIndex,
|
||||
result.BridgePort,
|
||||
result.Vlan,
|
||||
result.MAC,
|
||||
pgText(result.MAC),
|
||||
learnedIP,
|
||||
result.CheckedAt,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user