Skip to content

Commit 4dfdd0d

Browse files
author
epriestley
committedOct 1, 2013
Treat invalid policies as broadly similar to "no one"
Summary: Ref T3903. Ref T603. We currently overreact to invalid policies. Instead: - For non-omnipotent users, just reject the viewer. - For omnipotent users, we already shortcircuit and permit the viewer. - Formalize and add test coverage for these behaviors. Also clean up some strings. The practical effect of this is that setting an object to an invalid policy (either intentionally or accidentally) doesn't break callers who are querying it. Test Plan: - Created a Legalpad document and set view policy to "asldkfnaslkdfna". - Verified this policy behaved as though it were "no one". - Added, executed unit tests. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603, T3903 Differential Revision: https://secure.phabricator.com/D7185
1 parent ca85c45 commit 4dfdd0d

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed
 

‎src/applications/policy/__tests__/PhabricatorPolicyTestCase.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function testNoOnePolicy() {
8282
'admin' => false,
8383
),
8484
'No One Policy');
85-
8685
}
8786

8887

@@ -172,6 +171,46 @@ public function testOmnipotence() {
172171
}
173172

174173

174+
/**
175+
* Test that invalid policies reject viewers of all types.
176+
*/
177+
public function testRejectInvalidPolicy() {
178+
$invalid_policy = "the duck goes quack";
179+
$object = $this->buildObject($invalid_policy);
180+
181+
$this->expectVisibility(
182+
$object = $this->buildObject($invalid_policy),
183+
array(
184+
'public' => false,
185+
'user' => false,
186+
'admin' => false,
187+
),
188+
'Invalid Policy');
189+
}
190+
191+
192+
/**
193+
* An omnipotent user should be able to see even objects with invalid
194+
* policies.
195+
*/
196+
public function testInvalidPolicyVisibleByOmnipotentUser() {
197+
$invalid_policy = "the cow goes moo";
198+
$object = $this->buildObject($invalid_policy);
199+
200+
$results = array(
201+
$object,
202+
);
203+
204+
$query = new PhabricatorPolicyAwareTestQuery();
205+
$query->setResults($results);
206+
$query->setViewer(PhabricatorUser::getOmnipotentUser());
207+
208+
$this->assertEqual(
209+
1,
210+
count($query->execute()));
211+
}
212+
213+
175214
/**
176215
* Test an object for visibility across multiple user specifications.
177216
*/

‎src/applications/policy/filter/PhabricatorPolicyFilter.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ private function checkCapability(
225225
$this->rejectObject($object, $policy, $capability);
226226
}
227227
} else {
228-
throw new Exception("Object has unknown policy '{$policy}'!");
228+
// Reject objects with unknown policies.
229+
$this->rejectObject($object, false, $capability);
229230
}
230231
}
231232

@@ -241,11 +242,22 @@ private function rejectImpossiblePolicy(
241242
return;
242243
}
243244

244-
// TODO: clean this up
245-
$verb = $capability;
245+
switch ($capability) {
246+
case PhabricatorPolicyCapability::CAN_VIEW:
247+
$message = pht("This object has an impossible view policy.");
248+
break;
249+
case PhabricatorPolicyCapability::CAN_EDIT:
250+
$message = pht("This object has an impossible edit policy.");
251+
break;
252+
case PhabricatorPolicyCapability::CAN_JOIN:
253+
$message = pht("This object has an impossible join policy.");
254+
break;
255+
default:
256+
$message = pht("This object has an impossible policy.");
257+
break;
258+
}
246259

247-
throw new PhabricatorPolicyException(
248-
"This object has an impossible {$verb} policy.");
260+
throw new PhabricatorPolicyException($message);
249261
}
250262

251263
public function rejectObject(
@@ -350,9 +362,8 @@ public function rejectObject(
350362
$handle->getFullName());
351363
break;
352364
}
353-
354365
} else {
355-
$who = pht("This object has an unknown policy setting.");
366+
$who = pht("This object has an unknown or invalid policy setting.");
356367
}
357368
break;
358369
}

0 commit comments

Comments
 (0)
Failed to load comments.