Skip to content

Commit

Permalink
Support login request with leading or trailing whitespace from payload (
Browse files Browse the repository at this point in the history
#3496)

* trim whitespaces for login request
* simplify trimspace calls
  • Loading branch information
changchaishi authored Jan 23, 2025
1 parent b45e11c commit 31a5d64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func getLoginResponse(params authApi.LoginParams) (*models.LoginResponse, *Coded
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
lr := params.Body
// trim any leading and trailing whitespace from the login request
lr.AccessKey = strings.TrimSpace(lr.AccessKey)
lr.SecretKey = strings.TrimSpace(lr.SecretKey)
lr.Sts = strings.TrimSpace(lr.Sts)

clientIP := getClientIP(params.HTTPRequest)
client := GetConsoleHTTPClient(clientIP)
Expand Down
30 changes: 30 additions & 0 deletions integration/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ func TestLogout(t *testing.T) {
assert.Equal(response.StatusCode, 200)
}

func TestLoginExtraSpaces(t *testing.T) {
assert := assert.New(t)

client := &http.Client{
Timeout: 2 * time.Second,
}
requestData := map[string]string{
"accessKey": " minioadmin ",
"secretKey": "minioadmin",
}

requestDataJSON, _ := json.Marshal(requestData)

requestDataBody := bytes.NewReader(requestDataJSON)

request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody)
if err != nil {
log.Println(err)
return
}

request.Header.Add("Content-Type", "application/json")

response, err := client.Do(request)

assert.Equal(204, response.StatusCode, "Login request should succeed")
assert.NotNil(response, "Login response is nil")
assert.Nil(err, "Login errored out")
}

func TestBadLogin(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 31a5d64

Please sign in to comment.