From a03033ef150b352536813bebdcdcf449f7533949 Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Mon, 30 Jan 2023 20:15:18 +0100 Subject: [PATCH] [MM-48385] Validate against a list of auth services (#22154) --- app/imports/import_validators.go | 27 +++++++++++++++++++++++++++ app/imports/import_validators_test.go | 3 +-- i18n/en.json | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/imports/import_validators.go b/app/imports/import_validators.go index 15ec9f5bbe..1d21148210 100644 --- a/app/imports/import_validators.go +++ b/app/imports/import_validators.go @@ -231,6 +231,10 @@ func ValidateUserImportData(data *UserImportData) *model.AppError { return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_and_service_dependency.error", nil, "", http.StatusBadRequest) } + if appErr := validateAuthService(data.AuthService); appErr != nil { + return appErr + } + if data.Password != nil && *data.Password == "" { return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest) } @@ -312,6 +316,29 @@ func ValidateUserImportData(data *UserImportData) *model.AppError { return nil } +var validAuthServices = []string{ + "", + model.UserAuthServiceEmail, + model.UserAuthServiceGitlab, + model.UserAuthServiceSaml, + model.UserAuthServiceLdap, + model.ServiceGoogle, + model.ServiceOffice365, +} + +func validateAuthService(authService *string) *model.AppError { + if authService == nil { + return nil + } + for _, valid := range validAuthServices { + if *authService == valid { + return nil + } + } + + return model.NewAppError("BulkImport", "app.import.validate_user_teams_import_data.invalid_auth_service.error", map[string]any{"AuthService": *authService}, "", http.StatusBadRequest) +} + func ValidateUserTeamsImportData(data *[]UserTeamImportData) *model.AppError { if data == nil { return nil diff --git a/app/imports/import_validators_test.go b/app/imports/import_validators_test.go index 205d58b1a6..681e5c8656 100644 --- a/app/imports/import_validators_test.go +++ b/app/imports/import_validators_test.go @@ -646,7 +646,7 @@ func TestImportValidateUserAuth(t *testing.T) { }{ {nil, nil, true}, {ptrStr(""), ptrStr(""), true}, - {ptrStr("foo"), ptrStr("foo"), true}, + {ptrStr("foo"), ptrStr("foo"), false}, {nil, ptrStr(""), true}, {ptrStr(""), nil, true}, @@ -669,7 +669,6 @@ func TestImportValidateUserAuth(t *testing.T) { require.Nil(t, err, fmt.Sprintf("authService: %v, authData: %v", test.authService, test.authData)) } else { require.NotNil(t, err, fmt.Sprintf("authService: %v, authData: %v", test.authService, test.authData)) - require.Equal(t, "app.import.validate_user_import_data.auth_data_and_service_dependency.error", err.Id) } } diff --git a/i18n/en.json b/i18n/en.json index f9afb2ad80..d19140cbe4 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -5723,6 +5723,10 @@ "id": "app.import.validate_user_import_data.username_missing.error", "translation": "Missing require user property: username." }, + { + "id": "app.import.validate_user_teams_import_data.invalid_auth_service.error", + "translation": "Invalid auth service: {{.AuthService}}" + }, { "id": "app.import.validate_user_teams_import_data.invalid_roles.error", "translation": "Invalid roles for User's Team Membership."