Page MenuHomePhabricator

Migrate Action::checkCanExecute to use Authority with proper rigor
Open, Needs TriagePublic

Description

Current code:

Action.php
protected function checkCanExecute( User $user ) {
	$right = $this->getRestriction();
	if ( $right !== null ) {
		$errors = MediaWikiServices::getInstance()->getPermissionManager()
			->getPermissionErrors( $right, $user, $this->getTitle() );
		if ( count( $errors ) ) {
			throw new PermissionsError( $right, $errors );
		}
	}

This should use Authority, rather than PermissionManager::getPermissionErrors. The main issue is that PermissionManager::getPermissionErrors() is called with the default rigor, i.e. self::RIGOR_SECURE. This would correspond to Authority::authorizeWrite, but I don't think it's correct: by default (FormAction, FormlessAction), this code is executed on page view, which means we'd be making master queries and possibly incrementing throttle limiters on GET requests. My proposal is to use Authority::definitelyCan instead. I'm tagging CPT to confirm whether that's correct/wanted.

Event Timeline

Change 735065 had a related patch set uploaded (by D3r1ck01; author: Derick Alangi):

[mediawiki/core@master] actions: Update deprecated code in Actions::checkCanExecute()

https://gerrit.wikimedia.org/r/735065

Change 735070 had a related patch set uploaded (by D3r1ck01; author: Derick Alangi):

[mediawiki/core@master] actions: Migrate Action::checkCanExecute() to use Authority instead

https://gerrit.wikimedia.org/r/735070

I think you are right that per default, definitlyCan() is the correct check. This is especially true for ViewAction - are we really doing permission checks against master on page views? We shouldn't!

There are several other actions that are purely read actions, like InfoAction or HistoryAction.

However, many (most?) actions actually do perform writes of some kind. For these, we should call authorizeWrite(). Unless they do this explicitly later...

I think we need to have a look at the actual subclasses, and decide whether they need to call authorizeWrite(), and check whether they do it explicitly, or if we need to do it in checkCanExecute(). We also need to have a closer look at when checkCanExecute() is called - authorizeWrite() will (in the future) increment throttle counters, so it should only be called when the user is *actually* performing the action, not when preparing to perform the action (e.g. by loading a form). We can try to auto-detect (e.g. never call authorizeWrite on a GET request), but that seems a bit brittle.

There is one more difference between authorizeWrite and definitlyCan that we need to consider: since definitlyCan uses a db replica, the effect of blocks and protection may be delayed. Critical actions like editing, deleting, and moving need to be protected via authorizeWrite when migrating to Authority.

Change 735065 merged by jenkins-bot:

[mediawiki/core@master] actions: Update deprecated code in Actions::checkCanExecute()

https://gerrit.wikimedia.org/r/735065

I think you are right that per default, definitlyCan() is the correct check. This is especially true for ViewAction - are we really doing permission checks against master on page views? We shouldn't!

There are several other actions that are purely read actions, like InfoAction or HistoryAction.

However, many (most?) actions actually do perform writes of some kind. For these, we should call authorizeWrite(). Unless they do this explicitly later...

Whether an Action is going to perform writes should be inferable from the requiresWrite() method. So we may vary the permission check based on that, like ApiBase. However:

I think we need to have a look at the actual subclasses, and decide whether they need to call authorizeWrite(), and check whether they do it explicitly, or if we need to do it in checkCanExecute(). We also need to have a closer look at when checkCanExecute() is called - authorizeWrite() will (in the future) increment throttle counters, so it should only be called when the user is *actually* performing the action, not when preparing to perform the action (e.g. by loading a form).

That's what concerns me as well. authorizeWrite() should only be used close to the actual writes, not early. I think what we could do is to explicitly document checkCanExecute as a preliminary check only, used e.g. for showing the page, and asking the subclass to do the thorough permission checks (via authorizeWrite) right before performing the actual action. Then look at the various Action subclasses and ensure they comply with that.

There is one more difference between authorizeWrite and definitlyCan that we need to consider: since definitlyCan uses a db replica, the effect of blocks and protection may be delayed. Critical actions like editing, deleting, and moving need to be protected via authorizeWrite when migrating to Authority.

That's correct, but it would be addressed by my proposal above. If checkCanExecute is only for showing the page, I think we can tolerate a lagged replica, as long as the heavy checks using the DB master are performed later on.

Thank you for your insights @Daimona. I think I'm coming around to your original proposal: just always check definitelyCan(), and leave it to individual actions to call authorizeWrite() close to actual execution. Perhaps it would be good to convert relevant actions to authorizeWrite() first though, at least critical ones in core.

Fun fact: I just found that we have Action::requiresWrite() and Action::doesWrites(). One defaults to true, the other defaults to false. They are called in different places.

Thank you for your insights @Daimona. I think I'm coming around to your original proposal: just always check definitelyCan(), and leave it to individual actions to call authorizeWrite() close to actual execution. Perhaps it would be good to convert relevant actions to authorizeWrite() first though, at least critical ones in core.

Yeah, that's the plan although we'd probably have to find out how all existing actions are checking permissions.

Fun fact: I just found that we have Action::requiresWrite() and Action::doesWrites(). One defaults to true, the other defaults to false. They are called in different places.

Heh, I guess doesWrites() is the one we want...

Change 735070 abandoned by D3r1ck01:

[mediawiki/core@master] WIP: Migrate Action::checkCanExecute() to use Authority instead

Reason:

https://gerrit.wikimedia.org/r/735070