Skip to content

Commit

Permalink
fix version header parsing for tasmota
Browse files Browse the repository at this point in the history
Fix #35
vooon committed Jan 18, 2024
1 parent 40b2c3a commit 79bfd67
Showing 5 changed files with 83 additions and 7 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -6,17 +6,21 @@ require (
github.com/alecthomas/kong v0.8.1
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/gommon v0.4.2
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY
github.com/alecthomas/kong v0.8.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
@@ -16,7 +17,9 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
@@ -33,4 +36,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 3 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/labstack/echo/v4"
@@ -18,8 +17,6 @@ import (
"github.com/vooon/esp-ota-server/assets"
)

//go:generate rice embed-go

type server struct {
config Config
templates *template.Template
@@ -99,10 +96,9 @@ func (s server) getBinaryFile(c echo.Context) error {

sendFile := true
if vok {
vmap := map[string]string{}
for _, kv := range strings.Split(version[0], " ") {
n := strings.SplitN(kv, ":", 2)
vmap[n[0]] = n[1]
vmap, err := parseVersionHeader(version)
if err != nil {
return err
}

c.Logger().Printj(log.JSON{
32 changes: 32 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package server

import (
"fmt"
"strings"
)

const unknownVersionKey = "ver"

func parseVersionHeader(versionHdr []string) (map[string]string, error) {
vmap := make(map[string]string)

for _, ver := range versionHdr {
// version header doesn't have key:value pairs
if !strings.Contains(ver, ":") {
vmap[unknownVersionKey] = ver
continue
}

for _, kv := range strings.Split(ver, " ") {
n := strings.SplitN(kv, ":", 2)

if len(n) != 2 {
return nil, fmt.Errorf("failed to parse version: %s", kv)
}

vmap[n[0]] = n[1]
}
}

return vmap, nil
}
38 changes: 38 additions & 0 deletions server/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseVersionHeader(t *testing.T) {

testCases := []struct {
name string
hdrs []string
expected map[string]string
expectedErrMsg string
}{
{"nil", nil, map[string]string{}, ""},
{"kv", []string{"fw:1.2.3 hv:1.0"}, map[string]string{"fw": "1.2.3", "hv": "1.0"}, ""},
{"tasmota", []string{"13.3.0.3(tasmota-4M)"}, map[string]string{"ver": "13.3.0.3(tasmota-4M)"}, ""}, // see #35
{"multiple-kv", []string{"fw:1.2.3", "hv:1.0"}, map[string]string{"fw": "1.2.3", "hv": "1.0"}, ""}, // not actually sent by esp, just check corner case
{"malicious-space", []string{"foo:1.2.3 bar"}, nil, "failed to parse version:"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)

vmap, err := parseVersionHeader(tc.hdrs)
if tc.expectedErrMsg == "" {
assert.NoError(err)
assert.Equal(tc.expected, vmap)
} else {
assert.ErrorContains(err, tc.expectedErrMsg)
}

})
}
}

0 comments on commit 79bfd67

Please sign in to comment.