local_user_login_enumeration_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package api
  2. import (
  3. "context"
  4. "testing"
  5. "connectrpc.com/connect"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  9. apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
  10. config "github.com/OliveTin/OliveTin/internal/config"
  11. )
  12. func newEnumerationTestClient(t *testing.T) apiv1connect.OliveTinApiServiceClient {
  13. t.Helper()
  14. hash, err := createHash("correct horse")
  15. require.NoError(t, err)
  16. cfg := config.DefaultConfig()
  17. cfg.SetDir(t.TempDir())
  18. cfg.AuthLocalUsers.Enabled = true
  19. cfg.AuthLocalUsers.Users = []*config.LocalUser{
  20. {Username: "alice", Password: hash},
  21. {Username: "apikeyonly", ApiKey: "k"},
  22. }
  23. ts, client := getNewTestServerAndClient(cfg)
  24. t.Cleanup(ts.Close)
  25. return client
  26. }
  27. func loginAttempt(client apiv1connect.OliveTinApiServiceClient, username string, password string) (*connect.Response[apiv1.LocalUserLoginResponse], error) {
  28. return client.LocalUserLogin(context.Background(), connect.NewRequest(&apiv1.LocalUserLoginRequest{
  29. Username: username,
  30. Password: password,
  31. }))
  32. }
  33. func fillArgon2Slots(t *testing.T) {
  34. t.Helper()
  35. for range argon2MaxConcurrent {
  36. argon2Sem <- struct{}{}
  37. }
  38. t.Cleanup(func() {
  39. for range argon2MaxConcurrent {
  40. <-argon2Sem
  41. }
  42. })
  43. }
  44. // Not parallel: this test holds every Argon2 slot while it runs.
  45. func TestLocalUserLoginIsBusyForEveryUsernameWhenArgon2IsBusy(t *testing.T) {
  46. client := newEnumerationTestClient(t)
  47. _, err := getDummyPasswordHash()
  48. require.NoError(t, err)
  49. fillArgon2Slots(t)
  50. for _, username := range []string{"alice", "nosuchuser", "apikeyonly"} {
  51. _, err := loginAttempt(client, username, "wrong password")
  52. require.Error(t, err, username)
  53. assert.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err), "username %q must get the same busy error as every other username", username)
  54. }
  55. }
  56. func TestLocalUserLoginFailsTheSameForUnknownAndPasswordlessUsers(t *testing.T) {
  57. client := newEnumerationTestClient(t)
  58. for _, username := range []string{"alice", "nosuchuser", "apikeyonly"} {
  59. resp, err := loginAttempt(client, username, "wrong password")
  60. require.NoError(t, err, username)
  61. assert.False(t, resp.Msg.GetSuccess(), username)
  62. assert.Empty(t, resp.Header().Values("Set-Cookie"), username)
  63. }
  64. }
  65. func TestLocalUserLoginRejectsDummyPasswordForUnknownAndPasswordlessUsers(t *testing.T) {
  66. client := newEnumerationTestClient(t)
  67. _, err := getDummyPasswordHash()
  68. require.NoError(t, err)
  69. require.NotEmpty(t, dummyPassword)
  70. for _, username := range []string{"nosuchuser", "apikeyonly"} {
  71. resp, err := loginAttempt(client, username, dummyPassword)
  72. require.NoError(t, err, username)
  73. assert.False(t, resp.Msg.GetSuccess(), "the dummy password must never log anyone in")
  74. }
  75. }
  76. func TestLocalUserLoginStillAcceptsTheRightPassword(t *testing.T) {
  77. client := newEnumerationTestClient(t)
  78. resp, err := loginAttempt(client, "alice", "correct horse")
  79. require.NoError(t, err)
  80. assert.True(t, resp.Msg.GetSuccess())
  81. }