Skip to content

Commit 6adedcd

Browse files
authored
Fix query parameter validations (#52)
1 parent e6e343d commit 6adedcd

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

handlers/getStatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var initTime = time.Now()
15-
var version = "1.14.0"
15+
var version = "1.14.1"
1616

1717
func getStatus(w http.ResponseWriter, r *http.Request) {
1818
json.NewEncoder(w).Encode(map[string]interface{}{

validators/ValidateAdventurerNameQueryParam.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,43 @@ import (
88

99
// The naming policies in BDO are fucked up
1010
// This function only checks the length and allowed symbols
11+
// searchType: 1 for character, 2 for family
1112
func ValidateAdventurerNameQueryParam(query []string, region string, searchType string) (name string, ok bool, errorMessage string) {
1213
if 1 > len(query) {
1314
return "", false, "Adventurer name is missing from request"
1415
}
1516

17+
// Length bounds have been found for each region by trial and error,
18+
// they're wider than the game says it allows
1619
minLength := map[string]int{
17-
"EU1": 2,
20+
"EU1": 3,
1821
"EU2": 1,
1922
"KR1": 2,
20-
"KR2": 1,
21-
"NA1": 2,
23+
"KR2": 2,
24+
"NA1": 3,
2225
"NA2": 1,
2326
"SA1": 2,
2427
"SA2": 2,
2528
}[region+searchType]
29+
maxLength := map[string]int{
30+
"EU1": 19,
31+
"EU2": 16,
32+
"KR1": 10,
33+
"KR2": 10,
34+
"NA1": 28,
35+
"NA2": 24,
36+
"SA1": 18,
37+
"SA2": 16,
38+
}[region+searchType]
2639

2740
name = strings.ToLower(query[0])
2841

29-
if len(name) < minLength || len(name) > 16 {
30-
return name, false, fmt.Sprintf("Adventurer name should be between %v and 16 symbols long", minLength)
42+
if len(name) < minLength {
43+
return name, false, fmt.Sprintf("Adventurer name on %v should be at least %v symbols long", region, minLength)
44+
}
45+
46+
if len(name) > maxLength {
47+
return name, false, fmt.Sprintf("Adventurer name on %v should be at most %v symbols long", region, maxLength)
3148
}
3249

3350
// Returns false for allowed characters

validators/ValidateAdventurerNameQueryParam_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func TestValidateAdventurerNameQueryParam(t *testing.T) {
1717
{input: []string{"Name1", "Name2"}, region: "EU", searchType: "1", expectedName: "name1", expectedOk: true, expectedMessage: ""},
1818
{input: []string{"고대신"}, region: "EU", searchType: "1", expectedName: "고대신", expectedOk: true, expectedMessage: ""},
1919

20-
{input: []string{""}, region: "EU", searchType: "1", expectedName: "", expectedOk: false, expectedMessage: "Adventurer name should be between 2 and 16 symbols long"},
20+
{input: []string{""}, region: "EU", searchType: "1", expectedName: "", expectedOk: false, expectedMessage: "Adventurer name on EU should be at least 3 symbols long"},
2121
{input: []string{"With Spaces"}, region: "EU", searchType: "1", expectedName: "with spaces", expectedOk: false, expectedMessage: "Adventurer name contains a forbidden symbol at position 5: ' '"},
22-
{input: []string{"AdventurerNameTooLong12345"}, region: "EU", searchType: "1", expectedName: "adventurernametoolong12345", expectedOk: false, expectedMessage: "Adventurer name should be between 2 and 16 symbols long"},
22+
{input: []string{"AdventurerNameTooLong12345"}, region: "EU", searchType: "1", expectedName: "adventurernametoolong12345", expectedOk: false, expectedMessage: "Adventurer name on EU should be at most 19 symbols long"},
2323
{input: []string{"Name$"}, region: "EU", searchType: "1", expectedName: "name$", expectedOk: false, expectedMessage: "Adventurer name contains a forbidden symbol at position 5: '$'"},
2424
{input: []string{}, region: "EU", searchType: "1", expectedName: "", expectedOk: false, expectedMessage: "Adventurer name is missing from request"},
2525

26-
{input: []string{""}, region: "SA", searchType: "2", expectedName: "", expectedOk: false, expectedMessage: "Adventurer name should be between 2 and 16 symbols long"},
26+
{input: []string{""}, region: "SA", searchType: "2", expectedName: "", expectedOk: false, expectedMessage: "Adventurer name on SA should be at least 2 symbols long"},
2727
{input: []string{"Ad"}, region: "SA", searchType: "2", expectedName: "ad", expectedOk: true, expectedMessage: ""},
2828
}
2929

validators/ValidateGuildNameQueryParam.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ func ValidateGuildNameQueryParam(query []string) (guildName string, ok bool, err
1616

1717
guildName = strings.ToLower(query[0])
1818

19-
if len(guildName) < 2 {
20-
return guildName, false, "Guild name can't be shorter than 2 symbols"
19+
if len(guildName) < 3 {
20+
return guildName, false, "Guild name can't be shorter than 3 symbols"
21+
}
22+
23+
if len(guildName) > 16 {
24+
return guildName, false, "Guild name can't be longer than 16 symbols"
2125
}
2226

2327
// Returns false for allowed characters

validators/ValidateGuildNameQueryParam_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ func TestValidateGuildNameQueryParam(t *testing.T) {
1515
{input: []string{"MyGuild"}, expectedName: "myguild", expectedOk: true, expectedMessage: ""},
1616
{input: []string{"고대신"}, expectedName: "고대신", expectedOk: true, expectedMessage: ""}, // Guild name with Korean characters
1717

18-
{input: []string{""}, expectedName: "", expectedOk: false, expectedMessage: "Guild name can't be shorter than 2 symbols"},
19-
{input: []string{"A Guild With Spaces"}, expectedName: "a guild with spaces", expectedOk: false, expectedMessage: "Guild name contains a forbidden symbol at position 2: ' '"},
18+
{input: []string{""}, expectedName: "", expectedOk: false, expectedMessage: "Guild name can't be shorter than 3 symbols"},
19+
{input: []string{"With Spaces"}, expectedName: "with spaces", expectedOk: false, expectedMessage: "Guild name contains a forbidden symbol at position 5: ' '"},
2020
{input: []string{"Some$"}, expectedName: "some$", expectedOk: false, expectedMessage: "Guild name contains a forbidden symbol at position 5: '$'"},
21-
{input: []string{"x"}, expectedName: "x", expectedOk: false, expectedMessage: "Guild name can't be shorter than 2 symbols"},
21+
{input: []string{"x"}, expectedName: "x", expectedOk: false, expectedMessage: "Guild name can't be shorter than 3 symbols"},
2222
{input: []string{}, expectedName: "", expectedOk: false, expectedMessage: "Guild name is missing from request"},
23+
{input: []string{"GuildNameThatIsWayTooLong"}, expectedName: "guildnamethatiswaytoolong", expectedOk: false, expectedMessage: "Guild name can't be longer than 16 symbols"},
2324
}
2425

2526
for _, test := range tests {

0 commit comments

Comments
 (0)