mirror of
https://github.com/elyby/chrly.git
synced 2025-01-03 18:51:49 +05:30
#1: Disallow to query invalid Mojang usernames
This commit is contained in:
parent
b1e18d0d01
commit
96af45b2a1
@ -1,6 +1,7 @@
|
|||||||
package queue
|
package queue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -15,6 +16,9 @@ var forever = func() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://help.mojang.com/customer/portal/articles/928638
|
||||||
|
var allowedUsernamesRegex = regexp.MustCompile(`^[\w_]{3,16}$`)
|
||||||
|
|
||||||
type JobsQueue struct {
|
type JobsQueue struct {
|
||||||
Storage Storage
|
Storage Storage
|
||||||
|
|
||||||
@ -31,6 +35,14 @@ func (ctx *JobsQueue) GetTexturesForUsername(username string) chan *mojang.Signe
|
|||||||
})
|
})
|
||||||
|
|
||||||
responseChan := make(chan *mojang.SignedTexturesResponse)
|
responseChan := make(chan *mojang.SignedTexturesResponse)
|
||||||
|
if !allowedUsernamesRegex.MatchString(username) {
|
||||||
|
go func() {
|
||||||
|
responseChan <- nil
|
||||||
|
close(responseChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return responseChan
|
||||||
|
}
|
||||||
|
|
||||||
cachedResult := ctx.Storage.Get(username)
|
cachedResult := ctx.Storage.Get(username)
|
||||||
if cachedResult != nil {
|
if cachedResult != nil {
|
||||||
|
@ -3,6 +3,7 @@ package queue
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/elyby/chrly/api/mojang"
|
"github.com/elyby/chrly/api/mojang"
|
||||||
@ -251,6 +252,11 @@ func (suite *QueueTestSuite) TestHandle429ResponseWhenRequestingUsersTextures()
|
|||||||
suite.Assert().Nil(<-resultChan)
|
suite.Assert().Nil(<-resultChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *QueueTestSuite) TestReceiveTexturesForNotAllowedMojangUsername() {
|
||||||
|
resultChan := suite.Queue.GetTexturesForUsername("Not allowed")
|
||||||
|
suite.Assert().Nil(<-resultChan)
|
||||||
|
}
|
||||||
|
|
||||||
func TestJobsQueueSuite(t *testing.T) {
|
func TestJobsQueueSuite(t *testing.T) {
|
||||||
suite.Run(t, new(QueueTestSuite))
|
suite.Run(t, new(QueueTestSuite))
|
||||||
}
|
}
|
||||||
@ -259,7 +265,7 @@ func TestJobsQueueSuite(t *testing.T) {
|
|||||||
func randStr(len int) string {
|
func randStr(len int) string {
|
||||||
buff := make([]byte, len)
|
buff := make([]byte, len)
|
||||||
_, _ = rand.Read(buff)
|
_, _ = rand.Read(buff)
|
||||||
str := base64.StdEncoding.EncodeToString(buff)
|
str := strings.ReplaceAll(base64.URLEncoding.EncodeToString(buff), "-", "_")
|
||||||
|
|
||||||
// Base 64 can be longer than len
|
// Base 64 can be longer than len
|
||||||
return str[:len]
|
return str[:len]
|
||||||
|
Loading…
Reference in New Issue
Block a user