Skip to content

Commit

Permalink
move checkQuerySign
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed May 31, 2024
1 parent 6ae207e commit af435ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
38 changes: 33 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,37 @@ func (cr *Cluster) getRecordMiddleWare() utils.MiddleWareFunc {
}
}

func (cr *Cluster) checkQuerySign(req *http.Request, hash string, secret string) bool {
if config.Advanced.SkipSignatureCheck {
return true
}
query := req.Query()

Check failure on line 322 in handler.go

View workflow job for this annotation

GitHub Actions / test

req.Query undefined (type *http.Request has no field or method Query)

Check failure on line 322 in handler.go

View workflow job for this annotation

GitHub Actions / test

req.Query undefined (type *http.Request has no field or method Query)
sign, e := query.Get("s"), query.Get("e")
if len(sign) == 0 || len(e) == 0 {
return false
}
before, err := strconv.ParseInt(e, 36, 64)
if err != nil {
return false
}
if time.Now().UnixMilli() > before {
return false
}
hs := crypto.SHA1.New()
io.WriteString(hs, secret)

Check failure on line 335 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io

Check failure on line 335 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io
io.WriteString(hs, hash)

Check failure on line 336 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io

Check failure on line 336 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io
io.WriteString(hs, e)

Check failure on line 337 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io

Check failure on line 337 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: io
var (
buf [20]byte
sbuf [27]byte
)
base64.RawURLEncoding.Encode(sbuf[:], hs.Sum(buf[:0]))

Check failure on line 342 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: base64

Check failure on line 342 in handler.go

View workflow job for this annotation

GitHub Actions / test

undefined: base64
if (string)(sbuf[:]) != sign {
return false
}
return true
}

var emptyHashes = func() (hashes map[string]struct{}) {
hashMethods := []crypto.Hash{
crypto.MD5, crypto.SHA1,
Expand All @@ -329,9 +360,6 @@ var emptyHashes = func() (hashes map[string]struct{}) {

var HeaderXPoweredBy = fmt.Sprintf("go-openbmclapi/%s; url=https://github.com/LiterMC/go-openbmclapi", build.BuildVersion)

var accessedTeapotMux sync.RWMutex
var accessedTeapot = make(map[string]struct{})

//go:embed robots.txt
var robotTxtContent string

Expand All @@ -357,7 +385,7 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

query := req.URL.Query()

Check failure on line 387 in handler.go

View workflow job for this annotation

GitHub Actions / test

query declared and not used

Check failure on line 387 in handler.go

View workflow job for this annotation

GitHub Actions / test

query declared and not used
if !checkQuerySign(hash, cr.clusterSecret, query) {
if !cr.checkQuerySign(req, hash, cr.clusterSecret) {
http.Error(rw, "Cannot verify signature", http.StatusForbidden)
return
}
Expand All @@ -379,7 +407,7 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

query := req.URL.Query()

Check failure on line 409 in handler.go

View workflow job for this annotation

GitHub Actions / test

query declared and not used

Check failure on line 409 in handler.go

View workflow job for this annotation

GitHub Actions / test

query declared and not used
if !checkQuerySign(u.Path, cr.clusterSecret, query) {
if !cr.checkQuerySign(req, u.Path, cr.clusterSecret) {
http.Error(rw, "Cannot verify signature", http.StatusForbidden)
return
}
Expand Down
27 changes: 0 additions & 27 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,6 @@ func copyFile(src, dst string, mode os.FileMode) (err error) {
return
}

func checkQuerySign(hash string, secret string, query url.Values) bool {
if config.Advanced.SkipSignatureCheck {
return true
}
sign, e := query.Get("s"), query.Get("e")
if len(sign) == 0 || len(e) == 0 {
return false
}
before, err := strconv.ParseInt(e, 36, 64)
if err != nil {
return false
}
hs := crypto.SHA1.New()
io.WriteString(hs, secret)
io.WriteString(hs, hash)
io.WriteString(hs, e)
var (
buf [20]byte
sbuf [27]byte
)
base64.RawURLEncoding.Encode(sbuf[:], hs.Sum(buf[:0]))
if (string)(sbuf[:]) != sign {
return false
}
return time.Now().UnixMilli() < before
}

type RedirectError struct {
Redirects []*url.URL
Err error
Expand Down

0 comments on commit af435ad

Please sign in to comment.