AccountValidationTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. use PHPUnit\Framework\Attributes\Group;
  4. /**
  5. * Account forms reject invalid emails, weak/wrong passwords, and injection payloads.
  6. */
  7. #[Group('integration')]
  8. class AccountValidationTest extends BxDolAccountTestCase
  9. {
  10. static public function providerForRejectedCreateEmails(): array
  11. {
  12. return [
  13. 'empty' => [''],
  14. 'no-at' => ['not-an-email'],
  15. 'double-at' => ['a@b@c.com'],
  16. 'leading-dot' => ['.user@example.com'],
  17. 'underscore-domain' => ['user@exam_ple.com'],
  18. 'sql-or' => ["admin@example.com' OR '1'='1"],
  19. 'sql-comment' => ["' OR 1=1 --"],
  20. 'sql-union' => ["x' UNION SELECT password FROM sys_accounts --@example.com"],
  21. 'sql-drop' => ["'; DROP TABLE sys_accounts; --"],
  22. 'xss' => ['<script>alert(1)</script>@example.com'],
  23. ];
  24. }
  25. static public function providerForRejectedCreatePasswords(): array
  26. {
  27. return [
  28. 'empty' => [''],
  29. 'too-short' => ['Ab1'],
  30. 'no-digit' => ['Password'],
  31. 'no-upper' => ['password1'],
  32. 'no-lower' => ['PASSWORD1'],
  33. 'sql-or' => ["' OR '1'='1"],
  34. 'sql-comment' => ["' OR 1=1 --"],
  35. ];
  36. }
  37. static public function providerForRejectedCreateNames(): array
  38. {
  39. return [
  40. 'empty' => [''],
  41. 'at-prefix' => ['@admin'],
  42. ];
  43. }
  44. static public function providerForMaliciousNames(): array
  45. {
  46. return [
  47. 'sql-or' => ["' OR '1'='1"],
  48. 'sql-drop' => ["'; DROP TABLE sys_accounts; --"],
  49. 'sql-delete' => ['1; DELETE FROM sys_accounts --'],
  50. 'xss-script' => ['<script>alert(1)</script>'],
  51. 'xss-img' => ['<img src=x onerror=alert(1)>'],
  52. ];
  53. }
  54. static public function providerForRejectedPasswordsCurrent(): array
  55. {
  56. return [
  57. 'wrong' => ['not-the-password'],
  58. 'empty' => [''],
  59. 'sql-or' => ["' OR '1'='1"],
  60. 'sql-comment' => ["' OR 1=1 --"],
  61. 'sql-drop' => ["'; DROP TABLE sys_accounts; --"],
  62. ];
  63. }
  64. #[DataProvider('providerForRejectedCreateEmails')]
  65. public function testCreateFormRejectsInvalidEmail(string $sEmail): void
  66. {
  67. $this->bxBecomeGuest();
  68. $sName = 'lifecycle-' . bin2hex(random_bytes(4));
  69. $oForm = $this->bxSubmitCreateForm($sName, $sEmail, 'UnaTest1a');
  70. $this->bxAssertFormRejected($oForm, 'email');
  71. $iCreatedId = $this->bxAccountIdByName($sName);
  72. if ($iCreatedId)
  73. $this->_iCreatedAccountId = $iCreatedId;
  74. $this->assertSame(0, $iCreatedId);
  75. $this->bxAssertAccountsTableIntact();
  76. }
  77. #[DataProvider('providerForRejectedCreatePasswords')]
  78. public function testCreateFormRejectsInvalidPassword(string $sPassword): void
  79. {
  80. $this->bxBecomeGuest();
  81. $sEmail = 'lifecycle-' . bin2hex(random_bytes(4)) . '@example.com';
  82. $oForm = $this->bxSubmitCreateForm('Lifecycle User', $sEmail, $sPassword);
  83. $this->bxAssertFormRejected($oForm, 'password');
  84. $this->assertSame(0, $this->bxAccountIdByEmail($sEmail));
  85. $this->bxAssertAccountsTableIntact();
  86. }
  87. #[DataProvider('providerForRejectedCreateNames')]
  88. public function testCreateFormRejectsInvalidName(string $sName): void
  89. {
  90. $this->bxBecomeGuest();
  91. $sEmail = 'lifecycle-' . bin2hex(random_bytes(4)) . '@example.com';
  92. $oForm = $this->bxSubmitCreateForm($sName, $sEmail, 'UnaTest1a');
  93. $this->bxAssertFormRejected($oForm, 'name');
  94. $this->assertSame(0, $this->bxAccountIdByEmail($sEmail));
  95. $this->bxAssertAccountsTableIntact();
  96. }
  97. public function testCreateFormRejectsDuplicateEmail(): void
  98. {
  99. $this->bxBecomeGuest();
  100. $sEmail = $this->bxTestEmail('admin');
  101. $iExistingId = $this->bxAccountIdByEmail($sEmail);
  102. $this->assertGreaterThan(0, $iExistingId);
  103. $oForm = $this->bxSubmitCreateForm('Lifecycle User', $sEmail, 'UnaTest1a');
  104. $this->bxAssertFormRejected($oForm, 'email');
  105. $this->assertSame($iExistingId, $this->bxAccountIdByEmail($sEmail));
  106. $this->bxAssertAccountsTableIntact();
  107. }
  108. #[DataProvider('providerForMaliciousNames')]
  109. public function testCreateFormTreatsMaliciousNameAsData(string $sName): void
  110. {
  111. $this->bxBecomeGuest();
  112. $sEmail = 'lifecycle-' . bin2hex(random_bytes(4)) . '@example.com';
  113. $oForm = $this->bxSubmitCreateForm($sName, $sEmail, 'UnaTest1a');
  114. if (!$oForm->isSubmittedAndValid()) {
  115. $this->assertSame(0, $this->bxAccountIdByEmail($sEmail));
  116. $this->bxAssertAccountsTableIntact();
  117. return;
  118. }
  119. $this->_aCreatedEmails[] = $sEmail;
  120. $iAccountId = $oForm->insert(['email_confirmed' => 0]);
  121. $this->assertNotFalse($iAccountId);
  122. $this->_iCreatedAccountId = (int)$iAccountId;
  123. (new BxTemplAccountForms())->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  124. $this->assertSame((int)$iAccountId, $this->bxAccountIdByEmail($sEmail));
  125. $this->bxAssertAccountsTableIntact();
  126. $oAccount = BxDolAccount::getInstance($iAccountId, true);
  127. $this->assertInstanceOf(BxDolAccount::class, $oAccount);
  128. $this->assertSame($sEmail, $oAccount->getEmail());
  129. $this->assertStringNotContainsString('<script', strtolower($oAccount->getDisplayName()));
  130. }
  131. #[DataProvider('providerForRejectedCreateEmails')]
  132. public function testEmailFormRejectsInvalidEmail(string $sEmail): void
  133. {
  134. $aCreated = $this->bxCreateLifecycleAccount();
  135. $sResult = $this->bxSubmitEmailForm($aCreated['id'], $sEmail, $aCreated['password']);
  136. $this->assertIsString($sResult);
  137. $this->assertStringNotContainsString(_t('_sys_account_settings_email_successfully_submitted'), $sResult);
  138. $oAccount = BxDolAccount::getInstance($aCreated['id'], true);
  139. $this->assertSame($aCreated['email'], $oAccount->getEmail());
  140. $this->assertSame($aCreated['id'], $this->bxAccountIdByEmail($aCreated['email']));
  141. $this->bxAssertAccountsTableIntact();
  142. }
  143. #[DataProvider('providerForRejectedPasswordsCurrent')]
  144. public function testEmailFormRejectsWrongPassword(string $sPassword): void
  145. {
  146. $aCreated = $this->bxCreateLifecycleAccount();
  147. $sEmailNew = 'lifecycle-' . bin2hex(random_bytes(4)) . '-new@example.com';
  148. $sResult = $this->bxSubmitEmailForm($aCreated['id'], $sEmailNew, $sPassword);
  149. $this->assertIsString($sResult);
  150. $this->assertStringNotContainsString(_t('_sys_account_settings_email_successfully_submitted'), $sResult);
  151. $oAccount = BxDolAccount::getInstance($aCreated['id'], true);
  152. $this->assertSame($aCreated['email'], $oAccount->getEmail());
  153. $this->assertSame(0, $this->bxAccountIdByEmail($sEmailNew));
  154. $this->bxAssertAccountsTableIntact();
  155. }
  156. public function testEmailFormRejectsDuplicateEmail(): void
  157. {
  158. $aCreated = $this->bxCreateLifecycleAccount();
  159. $sResult = $this->bxSubmitEmailForm($aCreated['id'], $this->bxTestEmail('admin'), $aCreated['password']);
  160. $this->assertIsString($sResult);
  161. $this->assertStringNotContainsString(_t('_sys_account_settings_email_successfully_submitted'), $sResult);
  162. $oAccount = BxDolAccount::getInstance($aCreated['id'], true);
  163. $this->assertSame($aCreated['email'], $oAccount->getEmail());
  164. $this->bxAssertAccountsTableIntact();
  165. }
  166. #[DataProvider('providerForRejectedCreateNames')]
  167. public function testInfoFormRejectsInvalidName(string $sName): void
  168. {
  169. $aCreated = $this->bxCreateLifecycleAccount();
  170. $sResult = $this->bxSubmitInfoForm($aCreated['id'], $sName);
  171. $this->assertIsString($sResult);
  172. $this->assertStringNotContainsString(_t('_sys_account_settings_info_successfully_submitted'), $sResult);
  173. $oAccount = BxDolAccount::getInstance($aCreated['id'], true);
  174. $this->assertSame($aCreated['name'], $oAccount->getDisplayName());
  175. $this->bxAssertAccountsTableIntact();
  176. }
  177. #[DataProvider('providerForMaliciousNames')]
  178. public function testInfoFormTreatsMaliciousNameAsData(string $sName): void
  179. {
  180. $aCreated = $this->bxCreateLifecycleAccount();
  181. $sResult = $this->bxSubmitInfoForm($aCreated['id'], $sName);
  182. $oAccount = BxDolAccount::getInstance($aCreated['id'], true);
  183. if (!str_contains((string)$sResult, _t('_sys_account_settings_info_successfully_submitted'))) {
  184. $this->assertSame($aCreated['name'], $oAccount->getDisplayName());
  185. $this->bxAssertAccountsTableIntact();
  186. return;
  187. }
  188. $this->assertSame($aCreated['email'], $oAccount->getEmail());
  189. $this->assertStringNotContainsString('<script', strtolower($oAccount->getDisplayName()));
  190. $this->bxAssertAccountsTableIntact();
  191. }
  192. #[DataProvider('providerForRejectedPasswordsCurrent')]
  193. public function testDeleteFormRejectsWrongPassword(string $sPassword): void
  194. {
  195. $aCreated = $this->bxCreateLifecycleAccount();
  196. $oForm = $this->bxSubmitDeleteForm($aCreated['account'], $sPassword);
  197. $this->bxAssertFormRejected($oForm, 'password_current');
  198. $this->assertInstanceOf(BxDolAccount::class, BxDolAccount::getInstance($aCreated['id'], true));
  199. $this->bxAssertAccountsTableIntact();
  200. }
  201. public function testDeleteFormRejectsMissingConfirm(): void
  202. {
  203. $aCreated = $this->bxCreateLifecycleAccount();
  204. $oForm = $this->bxSubmitDeleteForm($aCreated['account'], $aCreated['password'], '');
  205. $this->bxAssertFormRejected($oForm, 'delete_confirm');
  206. $this->assertInstanceOf(BxDolAccount::class, BxDolAccount::getInstance($aCreated['id'], true));
  207. $this->bxAssertAccountsTableIntact();
  208. }
  209. }