Tasmota: mask password in error messages (#32659)

This commit is contained in:
Michael Geers 2026-08-09 11:22:04 +02:00 • committed by GitHub
parent d09a952cba
commit 27aca97bab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -43,7 +43,7 @@ func NewConnection(uri, user, password string, channels []int, cache time.Durati
used[c] = true
}
log := util.NewLogger("tasmota")
log := util.NewLogger("tasmota").Redact(user, password)
c := &Connection{
Helper: request.NewHelper(log),
uri: util.DefaultScheme(strings.TrimRight(uri, "/"), "http"),
@ -55,30 +55,35 @@ func NewConnection(uri, user, password string, channels []int, cache time.Durati
c.Client.Transport = request.NewTripper(log, transport.Insecure())
c.statusSnsG = util.ResettableCached(func() (StatusSNSResponse, error) {
parameters := url.Values{
"user": {c.user},
"password": {c.password},
"cmnd": {"Status 8"},
}
var res StatusSNSResponse
err := c.GetJSON(fmt.Sprintf("%s/cm?%s", c.uri, parameters.Encode()), &res)
err := c.cmd("Status 8", &res)
return res, err
}, cache)
c.statusStsG = util.ResettableCached(func() (StatusSTSResponse, error) {
parameters := url.Values{
"user": {c.user},
"password": {c.password},
"cmnd": {"Status 0"},
}
var res StatusSTSResponse
err := c.GetJSON(fmt.Sprintf("%s/cm?%s", c.uri, parameters.Encode()), &res)
err := c.cmd("Status 0", &res)
return res, err
}, cache)
return c, nil
}
// cmd executes a Tasmota command, masking the password in returned errors
func (c *Connection) cmd(cmnd string, res any) error {
parameters := url.Values{
"user": {c.user},
"password": {c.password},
"cmnd": {cmnd},
}
err := c.GetJSON(fmt.Sprintf("%s/cm?%s", c.uri, parameters.Encode()), res)
if err != nil && c.password != "" {
return errors.New(strings.ReplaceAll(err.Error(), url.QueryEscape(c.password), "***"))
}
return err
}
// channelExists checks the existence of the configured relay channel interface
func (c *Connection) RelayExists() error {
res, err := c.statusStsG.Get()
@ -123,14 +128,8 @@ func (c *Connection) Enable(enable bool) error {
cmd = fmt.Sprintf("Power%d on", channel)
}
parameters := url.Values{
"user": {c.user},
"password": {c.password},
"cmnd": {cmd},
}
var res PowerResponse
if err := c.GetJSON(fmt.Sprintf("%s/cm?%s", c.uri, parameters.Encode()), &res); err != nil {
if err := c.cmd(cmd, &res); err != nil {
return err
}