Fix five findings from a security assessment of the plugin

The assessment looked for three things: whether students can reach each
other's bookings, whether payment settings can be dodged, and whether the
plugin opens a way into the rest of the install. The student-isolation and
payment paths held up. These are what did not.

- The front-end login form told WordPress not to work out whether the site
  was secure, so on HTTPS every student's session cookie was issued without
  the Secure flag. wp_signon() only derives it from is_ssl() when the second
  argument is left at its default; an explicit false reads like "no
  preference" and is not.

- The update check took whatever download URL the release API returned and
  handed it to core, which unpacks it over the installed plugin. The package
  must now be https on git.unsupervised.ca exactly, compared on the parsed
  host so a lookalike name cannot pass.

- Uninstalling dropped 2 of 14 tables and left the Stripe secret and webhook
  signing key in wp_options. Removal is now a choice made in advance on
  Access -> Plugin removal: records are kept unless the owner opts in (with a
  typed confirmation), while credentials and the borrowed core registration
  settings go every time.

- Open registration switches on the site-wide users_can_register and makes
  Student the default role, arming any other signup form on the site to mint
  students who could book and be billed immediately. The pending state is now
  decided once, on user_register, rather than by whichever form created the
  account.

- Cancel and withdraw answered "not yours" differently from "does not exist",
  which let a signed-in student enumerate the studio's bookings. Both now
  give the same 404.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-09-05 11:55:53 -03:00
co-authored by Claude Opus 5
parent 74df3f5ba8
commit 1847159e31
28 changed files with 1246 additions and 50 deletions
+105
View File
@@ -6,6 +6,7 @@ namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\AccessSettings;
use Unsupervised\Schedular\Tests\Unit\TestCase;
use Unsupervised\Schedular\Uninstaller;
class AccessSettingsTest extends TestCase
{
@@ -31,4 +32,108 @@ class AccessSettingsTest extends TestCase
self::assertFalse($settings->adminsAreStudioAdmins());
self::assertTrue($settings->adminsAreInstructors());
}
/**
* Render the page with the given options stored and the given form posted,
* returning the options as they end up.
*
* @param array<string, mixed> $options
* @param array<string, mixed> $post
* @return array<string, mixed>
*/
private function submit(array $options, array $post): array
{
// A regular closure, not an arrow fn: arrow functions capture by value,
// so reads after the save would still see the options as they were.
Functions\when('get_option')->alias(
static function (string $name, mixed $default = false) use (&$options): mixed {
return $options[$name] ?? $default;
}
);
Functions\when('update_option')->alias(
static function (string $name, mixed $value) use (&$options): bool {
$options[$name] = $value;
return true;
}
);
Functions\when('current_user_can')->justReturn(true);
Functions\when('check_admin_referer')->justReturn(true);
Functions\when('wp_unslash')->returnArg();
// The typed confirmation is read through sanitize_key, so "Delete",
// " delete " and "DELETE" all arrive here as the same word.
Functions\when('sanitize_key')->alias(
static fn($v): string => (string) preg_replace('/[^a-z0-9_\-]/', '', strtolower((string) $v))
);
Functions\when('wp_nonce_field')->justReturn('');
Functions\when('submit_button')->justReturn('');
$_POST = $post + ['usc_action' => 'save'];
ob_start();
(new AccessSettings())->renderPage();
ob_end_clean();
$_POST = [];
return $options;
}
/**
* Erasing the studio's records cannot be undone, and a checkbox is one stray
* click. The tick alone must not be enough.
*/
public function testTickingDataRemovalWithoutTypingTheWordDoesNotEnableIt(): void
{
$options = $this->submit(
[Uninstaller::OPT_DELETE_DATA => '0'],
['delete_data' => '1', 'grant_studio' => '1']
);
self::assertSame('0', $options[Uninstaller::OPT_DELETE_DATA]);
// The rest of the page still saved: a mistyped confirmation must not
// silently swallow a capability change made in the same submit.
self::assertSame('1', $options[AccessSettings::OPT_GRANT_STUDIO]);
}
public function testTickAndTypedConfirmationTogetherEnableIt(): void
{
$options = $this->submit(
[Uninstaller::OPT_DELETE_DATA => '0'],
['delete_data' => '1', 'delete_data_confirm' => ' DELETE ']
);
self::assertSame('1', $options[Uninstaller::OPT_DELETE_DATA]);
}
public function testANearMissDoesNotCount(): void
{
$options = $this->submit(
[Uninstaller::OPT_DELETE_DATA => '0'],
['delete_data' => '1', 'delete_data_confirm' => 'delete everything']
);
self::assertSame('0', $options[Uninstaller::OPT_DELETE_DATA]);
}
/**
* Once it is on, saving the page for some other reason must not turn it off
* — nor demand the word again for a setting already made.
*/
public function testSavingOtherSettingsLeavesAnEnabledChoiceAlone(): void
{
$options = $this->submit(
[Uninstaller::OPT_DELETE_DATA => '1'],
['delete_data' => '1', 'grant_instructor' => '1']
);
self::assertSame('1', $options[Uninstaller::OPT_DELETE_DATA]);
}
public function testUntickingTurnsItOffWithNoCeremony(): void
{
$options = $this->submit([Uninstaller::OPT_DELETE_DATA => '1'], ['grant_studio' => '1']);
self::assertSame('0', $options[Uninstaller::OPT_DELETE_DATA]);
}
}
+34 -1
View File
@@ -19,7 +19,7 @@ class LoginPageTest extends TestCase
protected function tearDown(): void
{
unset($_POST['us_login']);
unset($_POST['us_login'], $_POST['log'], $_POST['pwd']);
parent::tearDown();
}
@@ -91,4 +91,37 @@ class LoginPageTest extends TestCase
self::assertSame('https://example.com/book/', $this->page->bookingUrl(9));
}
/**
* wp_signon()'s second argument decides whether the auth cookie carries the
* Secure flag, and *only* its default (the empty string) makes it work that
* out from is_ssl(). Passing an explicit false — which reads like "no
* preference" and is not — issues the plain, non-Secure cookie on an HTTPS
* site, leaving every student's session to leak over the first http://
* request to the domain. So the call must pass the credentials and nothing
* else, which is what this asserts: a second argument of any kind fails it.
*/
public function testSignOnDoesNotOverrideWordPressSecureCookieDetection(): void
{
$_POST['us_login'] = '1';
$_POST['log'] = '[email protected]';
$_POST['pwd'] = 'hunter2';
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->justReturn('https://example.com/login/');
Functions\when('sanitize_url')->returnArg();
Functions\when('sanitize_user')->returnArg();
Functions\when('wp_unslash')->returnArg();
Functions\when('wp_nonce_field')->justReturn('');
Functions\when('check_admin_referer')->justReturn(true);
// The failure branch, so the render returns instead of redirecting and
// exiting the test process.
Functions\when('is_wp_error')->justReturn(true);
Functions\expect('wp_signon')->once()->with(\Mockery::type('array'))->andReturn(null);
$html = $this->page->render([]);
self::assertStringContainsString('Invalid username or password.', $html);
}
}
@@ -115,4 +115,62 @@ class RegistrationLoginGateTest extends TestCase
self::assertSame($allcaps, $result);
}
/**
* Describe a user to the `user_register` guard: which role they hold, and
* whether whoever created them is studio staff.
*/
private function stubSignup(string $role, bool $byStaff): void
{
$user = Mockery::mock(\WP_User::class);
$user->roles = [$role];
Functions\when('get_userdata')->justReturn($user);
Functions\when('current_user_can')->justReturn($byStaff);
}
public function testStudentCreatedByAnUnknownSignupFormIsHeldForApproval(): void
{
// Open registration switches the site's own users_can_register on and
// makes Student the default role, so any other signup form on the site
// now mints students. They must not arrive able to book.
$this->stubSignup(RoleManager::STUDENT, byStaff: false);
Functions\expect('update_user_meta')->once()
->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1');
// Counted as confirmed: nobody asked them to confirm, and there is no
// token for them to answer with, so blocking the login would strand them.
Functions\expect('update_user_meta')->once()
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED, '1');
(new RegistrationLoginGate())->holdUnknownSignup(7);
}
public function testStudentCreatedByStaffIsLeftActive(): void
{
// An administrator adding a student from wp-admin could have approved
// them in the next click; making them do so is ceremony.
$this->stubSignup(RoleManager::STUDENT, byStaff: true);
Functions\expect('update_user_meta')->never();
(new RegistrationLoginGate())->holdUnknownSignup(7);
}
public function testNonStudentSignupIsNotHeld(): void
{
$this->stubSignup(RoleManager::INSTRUCTOR, byStaff: false);
Functions\expect('update_user_meta')->never();
(new RegistrationLoginGate())->holdUnknownSignup(7);
}
public function testUnknownUserIdIsIgnored(): void
{
Functions\when('get_userdata')->justReturn(false);
Functions\expect('update_user_meta')->never();
(new RegistrationLoginGate())->holdUnknownSignup(0);
}
}
+36
View File
@@ -9,6 +9,7 @@ use Unsupervised\Schedular\Auth\Invite;
use Unsupervised\Schedular\Auth\InviteRepository;
use Unsupervised\Schedular\Auth\RegistrationMailer;
use Unsupervised\Schedular\Auth\RegistrationPage;
use Unsupervised\Schedular\Auth\RegistrationStatus;
use Unsupervised\Schedular\GroupClass\GroupAccessRepository;
use Unsupervised\Schedular\Guardian\GuardianService;
use Unsupervised\Schedular\Payment\StudioSettings;
@@ -29,6 +30,9 @@ class RegistrationPageTest extends TestCase
/** @var array<string, mixed> */
private array $ctx;
/** @var list<string> User meta keys cleared during the submit under test. */
private array $clearedMeta = [];
protected function setUp(): void
{
parent::setUp();
@@ -52,6 +56,20 @@ class RegistrationPageTest extends TestCase
Functions\when('wp_enqueue_script')->justReturn(null);
Functions\when('wp_localize_script')->justReturn(true);
// Both success branches clear pending meta on the account they just
// created — the invited student is approved outright, the self-signup is
// marked unconfirmed. Recorded rather than counted so a test can say
// which, without every other test having to expect the calls.
$this->clearedMeta = [];
$cleared = &$this->clearedMeta;
Functions\when('delete_user_meta')->alias(
static function (int $userId, string $key) use (&$cleared): bool {
$cleared[] = $key;
return true;
}
);
$invites = Mockery::mock(InviteRepository::class);
$policies = Mockery::mock(PolicyRepository::class);
$questions = Mockery::mock(QuestionRepository::class);
@@ -149,6 +167,24 @@ class RegistrationPageTest extends TestCase
self::assertSame('invite', $this->submit($invite, false));
}
/**
* The registration gate holds every student account created by an
* unauthenticated request, which is what a signup is — so the invite branch
* has to say that this one is different. Without it an invited student is
* logged straight in and then told they cannot book.
*/
public function testInvitedStudentIsApprovedRatherThanLeftAwaitingApproval(): void
{
$_POST = [ 'password' => 'thistle-marrow-42', 'display_name' => 'Ada', 'birth_year' => '1990' ];
$this->stubInviteSuccess();
$invite = new Invite(email: '[email protected]', token: 'hash');
self::assertSame('invite', $this->submit($invite, false));
self::assertContains(RegistrationStatus::META_AWAITING_APPROVAL, $this->clearedMeta);
}
public function testInviteAcceptanceLinksClassGrantForTheEmail(): void
{
$_POST = [ 'password' => 'thistle-marrow-42', 'display_name' => 'Ada', 'birth_year' => '1990' ];
@@ -22,6 +22,12 @@ class RegistrationStatusTest extends TestCase
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_CONFIRM_EXPIRES, \Mockery::type('string'));
// Explicitly unconfirmed: the account may already have been held (and so
// counted as confirmed) by the user_register guard, and this signup did
// ask for a confirmation, so it has to be waited for.
Functions\expect('delete_user_meta')
->once()
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED);
self::assertSame('rawtoken', RegistrationStatus::markPending(7));
}
@@ -63,10 +69,34 @@ class RegistrationStatusTest extends TestCase
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_AUTO_APPROVE, '1');
Functions\expect('delete_user_meta')
->once()
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED);
self::assertSame('rawtoken', RegistrationStatus::markPending(7, true));
}
/**
* The hold put on a student account that turned up without going through the
* studio's signup form. Awaiting approval — so the booking capability is
* withheld — but counted as email-confirmed, because no confirmation was ever
* asked for and there is no token to answer with: blocking the login instead
* would strand the account with no way forward.
*/
public function testHoldMarksAwaitingApprovalWithoutDemandingAConfirmationThatWasNeverSent(): void
{
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1');
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED, '1');
// No token is issued, so nothing is generated and nothing can be spent.
Functions\expect('wp_generate_password')->never();
RegistrationStatus::hold(7);
}
public function testIsAutoApproveReadsMeta(): void
{
Functions\when('get_user_meta')->alias(static function (int $id, string $key) {