Skip to content
This repository was archived by the owner on May 5, 2026. It is now read-only.

Commit 0a1770c

Browse files
committed
fix: Validate email format before trying to do anything with it
1 parent fd9296a commit 0a1770c

4 files changed

Lines changed: 88 additions & 4 deletions

File tree

authme-core/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import fr.xephi.authme.message.MessageKey;
66
import fr.xephi.authme.process.Management;
77
import fr.xephi.authme.service.CommonService;
8+
import fr.xephi.authme.service.ValidationService;
89
import org.bukkit.entity.Player;
910

1011
import javax.inject.Inject;
@@ -24,10 +25,12 @@ public class ChangeEmailCommand extends PlayerCommand {
2425
@Inject
2526
private VerificationCodeManager codeManager;
2627

28+
@Inject
29+
private ValidationService validationService;
30+
2731
@Override
2832
public void runCommand(Player player, List<String> arguments) {
2933
final String playerName = player.getName();
30-
// Check if the user has been verified or not
3134
if (codeManager.isVerificationRequired(player)) {
3235
codeManager.codeExistOrGenerateNew(playerName);
3336
commonService.send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
@@ -36,6 +39,14 @@ public void runCommand(Player player, List<String> arguments) {
3639

3740
String playerMailOld = arguments.get(0);
3841
String playerMailNew = arguments.get(1);
42+
if (!validationService.validateEmail(playerMailOld)) {
43+
commonService.send(player, MessageKey.INVALID_OLD_EMAIL);
44+
return;
45+
}
46+
if (!validationService.validateEmail(playerMailNew)) {
47+
commonService.send(player, MessageKey.INVALID_NEW_EMAIL);
48+
return;
49+
}
3950
management.performChangeEmail(player, playerMailOld, playerMailNew);
4051
}
4152

authme-core/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import fr.xephi.authme.service.CommonService;
1313
import fr.xephi.authme.service.PasswordRecoveryService;
1414
import fr.xephi.authme.service.RecoveryCodeService;
15+
import fr.xephi.authme.service.ValidationService;
1516
import fr.xephi.authme.util.Utils;
1617
import org.bukkit.entity.Player;
1718

@@ -46,6 +47,9 @@ public class RecoverEmailCommand extends PlayerCommand {
4647
@Inject
4748
private BukkitService bukkitService;
4849

50+
@Inject
51+
private ValidationService validationService;
52+
4953
@Override
5054
protected void runCommand(Player player, List<String> arguments) {
5155
final String playerMail = arguments.get(0);
@@ -60,6 +64,10 @@ protected void runCommand(Player player, List<String> arguments) {
6064
commonService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
6165
return;
6266
}
67+
if (!validationService.validateEmail(playerMail)) {
68+
commonService.send(player, MessageKey.INVALID_EMAIL);
69+
return;
70+
}
6371

6472
DataSourceValue<String> emailResult = dataSource.getEmail(playerName);
6573
if (!emailResult.rowExists()) {

authme-core/src/test/java/fr/xephi/authme/command/executable/email/ChangeEmailCommandTest.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import fr.xephi.authme.message.Messages;
1010
import fr.xephi.authme.process.Management;
1111
import fr.xephi.authme.service.CommonService;
12+
import fr.xephi.authme.service.ValidationService;
1213
import org.bukkit.command.BlockCommandSender;
1314
import org.bukkit.command.CommandSender;
1415
import org.bukkit.entity.Player;
@@ -46,6 +47,9 @@ public class ChangeEmailCommandTest {
4647
@Mock
4748
private VerificationCodeManager codeManager;
4849

50+
@Mock
51+
private ValidationService validationService;
52+
4953
@Mock
5054
private Messages messages;
5155

@@ -83,13 +87,45 @@ public void shouldForwardData() {
8387
// given
8488
Player sender = initPlayerWithName("AmATest");
8589
given(codeManager.isVerificationRequired(sender)).willReturn(false);
90+
given(validationService.validateEmail("old_mail@example.org")).willReturn(true);
91+
given(validationService.validateEmail("new.mail@example.org")).willReturn(true);
92+
93+
// when
94+
command.executeCommand(sender, Arrays.asList("old_mail@example.org", "new.mail@example.org"));
95+
96+
// then
97+
verify(management).performChangeEmail(sender, "old_mail@example.org", "new.mail@example.org");
98+
}
99+
100+
@Test
101+
public void shouldFailForInvalidOldEmail() {
102+
// given
103+
Player sender = initPlayerWithName("AmATest");
104+
given(codeManager.isVerificationRequired(sender)).willReturn(false);
105+
given(validationService.validateEmail("notanemail")).willReturn(false);
86106

87107
// when
88-
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"));
108+
command.executeCommand(sender, Arrays.asList("notanemail", "new@example.org"));
89109

90110
// then
91-
verify(management).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
92-
verify(codeManager).isVerificationRequired(sender);
111+
verifyNoInteractions(management);
112+
verify(commonService).send(sender, MessageKey.INVALID_OLD_EMAIL);
113+
}
114+
115+
@Test
116+
public void shouldFailForInvalidNewEmail() {
117+
// given
118+
Player sender = initPlayerWithName("AmATest");
119+
given(codeManager.isVerificationRequired(sender)).willReturn(false);
120+
given(validationService.validateEmail("old@example.org")).willReturn(true);
121+
given(validationService.validateEmail("notanemail")).willReturn(false);
122+
123+
// when
124+
command.executeCommand(sender, Arrays.asList("old@example.org", "notanemail"));
125+
126+
// then
127+
verifyNoInteractions(management);
128+
verify(commonService).send(sender, MessageKey.INVALID_NEW_EMAIL);
93129
}
94130

95131
@Test

authme-core/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import fr.xephi.authme.service.CommonService;
1717
import fr.xephi.authme.service.PasswordRecoveryService;
1818
import fr.xephi.authme.service.RecoveryCodeService;
19+
import fr.xephi.authme.service.ValidationService;
1920
import fr.xephi.authme.settings.properties.SecuritySettings;
2021
import org.bukkit.entity.Player;
2122
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.BeforeEach;
2224
import org.junit.jupiter.api.Test;
2325
import org.mockito.Mock;
2426

@@ -30,6 +32,7 @@
3032
import static org.hamcrest.MatcherAssert.assertThat;
3133
import static org.mockito.ArgumentMatchers.anyString;
3234
import static org.mockito.BDDMockito.given;
35+
import static org.mockito.Mockito.lenient;
3336
import static org.mockito.Mockito.mock;
3437
import static org.mockito.Mockito.verify;
3538
import static org.mockito.Mockito.verifyNoInteractions;
@@ -70,6 +73,9 @@ public class RecoverEmailCommandTest {
7073
@Mock
7174
private BukkitService bukkitService;
7275

76+
@Mock
77+
private ValidationService validationService;
78+
7379
@Mock
7480
private Messages messages;
7581

@@ -83,6 +89,11 @@ public void initSettings() {
8389
given(commonService.getProperty(SecuritySettings.EMAIL_RECOVERY_COOLDOWN_SECONDS)).willReturn(40);
8490
}
8591

92+
@BeforeEach
93+
public void allowValidEmailsByDefault() {
94+
lenient().when(validationService.validateEmail(anyString())).thenReturn(true);
95+
}
96+
8697
@Test
8798
public void shouldHandleMissingMailProperties() {
8899
// given
@@ -115,6 +126,24 @@ public void shouldShowErrorForAuthenticatedUser() {
115126
verify(commonService).send(sender, MessageKey.ALREADY_LOGGED_IN_ERROR);
116127
}
117128

129+
@Test
130+
public void shouldRejectInvalidEmailFormat() {
131+
// given
132+
String name = "SomePlayer";
133+
Player sender = mock(Player.class);
134+
given(sender.getName()).willReturn(name);
135+
given(emailService.hasAllInformation()).willReturn(true);
136+
given(playerCache.isAuthenticated(name)).willReturn(false);
137+
given(validationService.validateEmail("notanemail")).willReturn(false);
138+
139+
// when
140+
command.executeCommand(sender, Collections.singletonList("notanemail"));
141+
142+
// then
143+
verifyNoInteractions(dataSource);
144+
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
145+
}
146+
118147
@Test
119148
public void shouldShowRegisterMessageForUnregisteredPlayer() {
120149
// given

0 commit comments

Comments
 (0)