2011-01-24 18:00:29 +01:00
|
|
|
<?php
|
|
|
|
|
2011-07-04 21:03:36 +02:00
|
|
|
/**
|
2012-04-18 23:25:27 +02:00
|
|
|
* @task status Method Status
|
2014-01-28 02:14:21 +01:00
|
|
|
* @task pager Paging Results
|
2011-07-04 21:03:36 +02:00
|
|
|
*/
|
2013-07-01 21:36:34 +02:00
|
|
|
abstract class ConduitAPIMethod
|
|
|
|
extends Phobject
|
|
|
|
implements PhabricatorPolicyInterface {
|
2011-01-24 18:00:29 +01:00
|
|
|
|
2012-04-18 23:25:27 +02:00
|
|
|
const METHOD_STATUS_STABLE = 'stable';
|
|
|
|
const METHOD_STATUS_UNSTABLE = 'unstable';
|
|
|
|
const METHOD_STATUS_DEPRECATED = 'deprecated';
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
abstract public function getMethodDescription();
|
|
|
|
abstract public function defineParamTypes();
|
|
|
|
abstract public function defineReturnType();
|
|
|
|
abstract public function defineErrorTypes();
|
|
|
|
abstract protected function execute(ConduitAPIRequest $request);
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
/**
|
|
|
|
* This is mostly for compatibility with
|
2013-10-14 23:35:47 +02:00
|
|
|
* @{class:PhabricatorCursorPagedPolicyAwareQuery}.
|
2013-07-01 21:36:34 +02:00
|
|
|
*/
|
|
|
|
public function getID() {
|
|
|
|
return $this->getAPIMethodName();
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:25:27 +02:00
|
|
|
/**
|
|
|
|
* Get the status for this method (e.g., stable, unstable or deprecated).
|
|
|
|
* Should return a METHOD_STATUS_* constant. By default, methods are
|
|
|
|
* "stable".
|
|
|
|
*
|
|
|
|
* @return const METHOD_STATUS_* constant.
|
|
|
|
* @task status
|
|
|
|
*/
|
|
|
|
public function getMethodStatus() {
|
|
|
|
return self::METHOD_STATUS_STABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional description to supplement the method status. In particular, if
|
|
|
|
* a method is deprecated, you can return a string here describing the reason
|
|
|
|
* for deprecation and stable alternatives.
|
|
|
|
*
|
|
|
|
* @return string|null Description of the method status, if available.
|
|
|
|
* @task status
|
|
|
|
*/
|
|
|
|
public function getMethodStatusDescription() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
public function getErrorDescription($error_code) {
|
|
|
|
return idx($this->defineErrorTypes(), $error_code, 'Unknown Error');
|
|
|
|
}
|
|
|
|
|
2012-02-21 23:28:05 +01:00
|
|
|
public function getRequiredScope() {
|
|
|
|
// by default, conduit methods are not accessible via OAuth
|
|
|
|
return PhabricatorOAuthServerScope::SCOPE_NOT_ACCESSIBLE;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
public function executeMethod(ConduitAPIRequest $request) {
|
|
|
|
return $this->execute($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAPIMethodName() {
|
|
|
|
return self::getAPIMethodNameFromClassName(get_class($this));
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
/**
|
|
|
|
* Return a key which sorts methods by application name, then method status,
|
|
|
|
* then method name.
|
|
|
|
*/
|
|
|
|
public function getSortOrder() {
|
|
|
|
$name = $this->getAPIMethodName();
|
|
|
|
|
|
|
|
$map = array(
|
|
|
|
ConduitAPIMethod::METHOD_STATUS_STABLE => 0,
|
|
|
|
ConduitAPIMethod::METHOD_STATUS_UNSTABLE => 1,
|
|
|
|
ConduitAPIMethod::METHOD_STATUS_DEPRECATED => 2,
|
|
|
|
);
|
|
|
|
$ord = idx($map, $this->getMethodStatus(), 0);
|
|
|
|
|
|
|
|
list($head, $tail) = explode('.', $name, 2);
|
|
|
|
|
|
|
|
return "{$head}.{$ord}.{$tail}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getApplicationName() {
|
|
|
|
return head(explode('.', $this->getAPIMethodName(), 2));
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
public static function getClassNameFromAPIMethodName($method_name) {
|
|
|
|
$method_fragment = str_replace('.', '_', $method_name);
|
|
|
|
return 'ConduitAPI_'.$method_fragment.'_Method';
|
|
|
|
}
|
|
|
|
|
2011-02-06 07:36:21 +01:00
|
|
|
public function shouldRequireAuthentication() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-22 22:47:52 +02:00
|
|
|
public function shouldAllowPublic() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Create AphrontWriteGuard, a backup mechanism for CSRF validation
Summary:
Provide a catchall mechanism to find unprotected writes.
- Depends on D758.
- Similar to WriteOnHTTPGet stuff from Facebook's stack.
- Since we have a small number of storage mechanisms and highly structured
read/write pathways, we can explicitly answer the question "is this page
performing a write?".
- Never allow writes without CSRF checks.
- This will probably break some things. That's fine: they're CSRF
vulnerabilities or weird edge cases that we can fix. But don't push to Facebook
for a few days unless you're prepared to deal with this.
- **>>> MEGADERP: All Conduit write APIs are currently vulnerable to CSRF!
<<<**
Test Plan:
- Ran some scripts that perform writes (scripts/search indexers), no issues.
- Performed normal CSRF submits.
- Added writes to an un-CSRF'd page, got an exception.
- Executed conduit methods.
- Did login/logout (this works because the logged-out user validates the
logged-out csrf "token").
- Did OAuth login.
- Did OAuth registration.
Reviewers: pedram, andrewjcg, erling, jungejason, tuomaspelkonen, aran,
codeblock
Commenters: pedram
CC: aran, epriestley, pedram
Differential Revision: 777
2011-08-03 20:49:27 +02:00
|
|
|
public function shouldAllowUnguardedWrites() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-13 15:09:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optionally, return a @{class:PhabricatorApplication} which this call is
|
|
|
|
* part of. The call will be disabled when the application is uninstalled.
|
|
|
|
*
|
|
|
|
* @return PhabricatorApplication|null Related application.
|
|
|
|
*/
|
|
|
|
public function getApplication() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
public static function getAPIMethodNameFromClassName($class_name) {
|
|
|
|
$match = null;
|
|
|
|
$is_valid = preg_match(
|
|
|
|
'/^ConduitAPI_(.*)_Method$/',
|
|
|
|
$class_name,
|
|
|
|
$match);
|
|
|
|
if (!$is_valid) {
|
|
|
|
throw new Exception(
|
|
|
|
"Parameter '{$class_name}' is not a valid Conduit API method class.");
|
|
|
|
}
|
|
|
|
$method_fragment = $match[1];
|
|
|
|
return str_replace('_', '.', $method_fragment);
|
|
|
|
}
|
|
|
|
|
2011-07-05 16:21:04 +02:00
|
|
|
protected function validateHost($host) {
|
|
|
|
if (!$host) {
|
|
|
|
// If the client doesn't send a host key, don't complain. We should in
|
|
|
|
// the future, but this change isn't severe enough to bump the protocol
|
|
|
|
// version.
|
|
|
|
|
|
|
|
// TODO: Remove this once the protocol version gets bumped past 2 (i.e.,
|
|
|
|
// require the host key be present and valid).
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-30 22:51:54 +02:00
|
|
|
// NOTE: Compare domains only so we aren't sensitive to port specification
|
|
|
|
// or omission.
|
|
|
|
|
2011-07-05 16:21:04 +02:00
|
|
|
$host = new PhutilURI($host);
|
2012-03-30 22:51:54 +02:00
|
|
|
$host = $host->getDomain();
|
|
|
|
|
|
|
|
$self = new PhutilURI(PhabricatorEnv::getURI('/'));
|
|
|
|
$self = $self->getDomain();
|
2011-07-05 16:21:04 +02:00
|
|
|
|
|
|
|
if ($self !== $host) {
|
|
|
|
throw new Exception(
|
|
|
|
"Your client is connecting to this install as '{$host}', but it is ".
|
|
|
|
"configured as '{$self}'. The client and server must use the exact ".
|
|
|
|
"same URI to identify the install. Edit your .arcconfig or ".
|
|
|
|
"phabricator/conf so they agree on the URI for the install.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:59:03 +02:00
|
|
|
protected function formatStringConstants($constants) {
|
|
|
|
foreach ($constants as $key => $value) {
|
|
|
|
$constants[$key] = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
$constants = implode(', ', $constants);
|
|
|
|
return 'string-constant<'.$constants.'>';
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
|
2014-01-28 02:14:21 +01:00
|
|
|
/* -( Paging Results )----------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task pager
|
|
|
|
*/
|
|
|
|
protected function getPagerParamTypes() {
|
|
|
|
return array(
|
|
|
|
'before' => 'optional string',
|
|
|
|
'after' => 'optional string',
|
|
|
|
'limit' => 'optional int (default = 100)',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task pager
|
|
|
|
*/
|
|
|
|
protected function newPager(ConduitAPIRequest $request) {
|
|
|
|
$limit = $request->getValue('limit', 100);
|
|
|
|
$limit = min(1000, $limit);
|
|
|
|
$limit = max(1, $limit);
|
|
|
|
|
|
|
|
$pager = id(new AphrontCursorPagerView())
|
|
|
|
->setPageSize($limit);
|
|
|
|
|
|
|
|
$before_id = $request->getValue('before');
|
|
|
|
if ($before_id !== null) {
|
|
|
|
$pager->setBeforeID($before_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$after_id = $request->getValue('after');
|
|
|
|
if ($after_id !== null) {
|
|
|
|
$pager->setAfterID($after_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task pager
|
|
|
|
*/
|
|
|
|
protected function addPagerResults(
|
|
|
|
array $results,
|
|
|
|
AphrontCursorPagerView $pager) {
|
|
|
|
|
|
|
|
$results['cursor'] = array(
|
|
|
|
'limit' => $pager->getPageSize(),
|
|
|
|
'after' => $pager->getNextPageID(),
|
2014-04-18 01:00:25 +02:00
|
|
|
'before' => $pager->getPrevPageID(),
|
2014-01-28 02:14:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
|
|
|
|
|
|
|
|
2013-10-14 23:35:47 +02:00
|
|
|
public function getPHID() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
2013-10-17 21:51:19 +02:00
|
|
|
// Application methods get application visibility; other methods get open
|
|
|
|
// visibility.
|
|
|
|
|
|
|
|
$application = $this->getApplication();
|
|
|
|
if ($application) {
|
|
|
|
return $application->getPolicy($capability);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PhabricatorPolicies::getMostOpenPolicy();
|
2013-07-01 21:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
|
2013-10-17 21:51:19 +02:00
|
|
|
if (!$this->shouldRequireAuthentication()) {
|
|
|
|
// Make unauthenticated methods univerally visible.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-07-01 21:36:34 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:43:41 +02:00
|
|
|
public function describeAutomaticCapability($capability) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:59:03 +02:00
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
}
|