Skip to content

Commit fd462bc

Browse files
committed
fix(ajax): enforce related-record visibility permissions
1 parent b10f4dc commit fd462bc

7 files changed

Lines changed: 189 additions & 28 deletions

File tree

‎ajax/getDataItemJobOrders.php‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
$interface = new SecureAJAXInterface();
1515

16-
if (!$interface->isRequiredIDValid('dataItemID'))
16+
if (is_array($_REQUEST['dataItemID'] ?? null) || !$interface->isRequiredIDValid('dataItemID', false))
1717
{
1818
$interface->outputXMLErrorPage(-1, 'Invalid data item ID.');
1919
die();
2020
}
2121

22-
if (!$interface->isRequiredIDValid('dataItemType'))
22+
if (is_array($_REQUEST['dataItemType'] ?? null) || !$interface->isRequiredIDValid('dataItemType', false))
2323
{
2424
$interface->outputXMLErrorPage(-1, 'Invalid data item type.');
2525
die();
@@ -31,18 +31,21 @@
3131
switch ($dataItemType)
3232
{
3333
case DATA_ITEM_CANDIDATE:
34-
include_once(LEGACY_ROOT . '/lib/Candidates.php');
35-
$dataItem = new Candidates();
34+
include_once(LEGACY_ROOT . '/lib/CandidateAuthorization.php');
35+
$parentPermission = 'candidates.show';
36+
$dataItemClass = Candidates::class;
3637
break;
3738

3839
case DATA_ITEM_COMPANY:
3940
include_once(LEGACY_ROOT . '/lib/Companies.php');
40-
$dataItem = new Companies();
41+
$parentPermission = 'companies.show';
42+
$dataItemClass = Companies::class;
4143
break;
4244

4345
case DATA_ITEM_CONTACT:
4446
include_once(LEGACY_ROOT . '/lib/Contacts.php');
45-
$dataItem = new Contacts();
47+
$parentPermission = 'contacts.show';
48+
$dataItemClass = Contacts::class;
4649
break;
4750

4851
default:
@@ -51,7 +54,19 @@
5154
break;
5255
}
5356

54-
$jobOrdersArray = $dataItem->getJobOrdersArray($dataItemID);
57+
$session = $_SESSION['CATS'];
58+
if ($session->getAccessLevel($parentPermission) < ACCESS_LEVEL_READ ||
59+
$session->getAccessLevel('joborders.show') < ACCESS_LEVEL_READ ||
60+
($dataItemClass === Candidates::class && !CandidateAuthorization::canAccessCandidate($dataItemID)))
61+
{
62+
$interface->outputXMLErrorPage(-2, 'No job orders data.');
63+
die();
64+
}
65+
66+
$dataItem = new $dataItemClass();
67+
$jobOrdersArray = $dataItem->getJobOrdersArray(
68+
$dataItemID, $session->getAccessLevel('joborders.hidden') >= ACCESS_LEVEL_SA
69+
);
5570

5671
if (empty($jobOrdersArray))
5772
{

‎ajax/replaceTemplateTags.php‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@
33

44
include_once(LEGACY_ROOT . '/lib/EmailTemplates.php');
55
include_once(LEGACY_ROOT . '/lib/DateUtility.php');
6-
include_once(LEGACY_ROOT . '/lib/Candidates.php');
6+
include_once(LEGACY_ROOT . '/lib/CandidateAuthorization.php');
77

88
$interface = new SecureAJAXInterface();
99

10-
if (!$interface->isRequiredIDValid('candidateID', false))
10+
if (is_array($_REQUEST['candidateID'] ?? null) || !$interface->isRequiredIDValid('candidateID', false))
1111
{
1212
$interface->outputXMLErrorPage(-1, 'Invalid candidate ID.');
1313
die();
1414
}
1515

16-
$candidateID = $_REQUEST['candidateID'];
17-
$templateText = $_REQUEST['templateText'];
18-
19-
/* Get an array of the company's location data. */
20-
$candidates = new Candidates();
21-
$candidateData = $candidates->get($candidateID);
22-
$emailTemplates = new EmailTemplates();
23-
$emailTemplateText = $emailTemplates->replaceVariables($templateText);
24-
25-
if (empty($candidateData))
16+
if ($_SESSION['CATS']->getAccessLevel('candidates.show') < ACCESS_LEVEL_READ ||
17+
!CandidateAuthorization::canAccessCandidate($_REQUEST['candidateID'], $candidateData))
2618
{
2719
$interface->outputXMLErrorPage(-2, 'No candidate data.');
2820
die();
2921
}
3022

23+
$emailTemplates = new EmailTemplates();
24+
$emailTemplateText = $emailTemplates->replaceVariables($_REQUEST['templateText']);
25+
3126
$stringsToFind = array(
3227
'%CANDOWNER%',
3328
'%CANDFIRSTNAME%',

‎lib/Candidates.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,15 @@ public function getResume($attachmentID)
919919
* for the specified candidate ID.
920920
*
921921
* @param integer Candidate ID,
922+
* @param boolean Whether to include administratively hidden job orders
922923
* @return array Multi-dimensional associative result set array of
923924
* job orders data, or array() if no records were returned.
924925
*/
925-
public function getJobOrdersArray($candidateID)
926+
public function getJobOrdersArray($candidateID, $allowAdministrativeHidden = false)
926927
{
928+
$adminHiddenCriterion = $allowAdministrativeHidden
929+
? ''
930+
: 'AND (joborder.is_admin_hidden <> 1 OR joborder.is_admin_hidden IS NULL)';
927931
$sql = sprintf(
928932
"SELECT
929933
joborder.joborder_id AS jobOrderID,
@@ -937,9 +941,11 @@ public function getJobOrdersArray($candidateID)
937941
ON joborder.joborder_id = candidate_joborder.joborder_id
938942
WHERE
939943
candidate_joborder.candidate_id = %s
944+
%s
940945
ORDER BY
941946
title ASC",
942-
$this->_db->makeQueryInteger($candidateID)
947+
$this->_db->makeQueryInteger($candidateID),
948+
$adminHiddenCriterion
943949
);
944950

945951
return $this->_db->getAllAssoc($sql);

‎lib/Companies.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,24 +562,30 @@ public function getContactsArray($companyID)
562562
* for the specified company ID.
563563
*
564564
* @param integer Company ID
565+
* @param boolean Whether to include administratively hidden job orders
565566
* @return array Job Orders data
566567
*/
567-
public function getJobOrdersArray($companyID)
568+
public function getJobOrdersArray($companyID, $allowAdministrativeHidden = false)
568569
{
570+
$adminHiddenCriterion = $allowAdministrativeHidden
571+
? ''
572+
: 'AND (joborder.is_admin_hidden <> 1 OR joborder.is_admin_hidden IS NULL)';
569573
$sql = sprintf(
570574
"SELECT
571575
joborder.joborder_id AS jobOrderID,
572576
joborder.title AS title,
573577
company.name AS companyName
574578
FROM
575579
joborder
576-
LEFT JOIN company
580+
INNER JOIN company
577581
ON joborder.company_id = company.company_id
578582
WHERE
579583
joborder.company_id = %s
584+
%s
580585
ORDER BY
581586
title ASC",
582-
$this->_db->makeQueryInteger($companyID)
587+
$this->_db->makeQueryInteger($companyID),
588+
$adminHiddenCriterion
583589
);
584590

585591
return $this->_db->getAllAssoc($sql);

‎lib/Contacts.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,14 @@ public function updateModified($contactID)
706706
* for the specified contact ID.
707707
*
708708
* @param integer contact ID
709+
* @param boolean Whether to include administratively hidden job orders
709710
* @return array job orders data
710711
*/
711-
public function getJobOrdersArray($contactID)
712+
public function getJobOrdersArray($contactID, $allowAdministrativeHidden = false)
712713
{
714+
$adminHiddenCriterion = $allowAdministrativeHidden
715+
? ''
716+
: 'AND (joborder.is_admin_hidden <> 1 OR joborder.is_admin_hidden IS NULL)';
713717
$sql = sprintf(
714718
"SELECT
715719
joborder.joborder_id AS jobOrderID,
@@ -724,11 +728,13 @@ public function getJobOrdersArray($contactID)
724728
ON company.company_id = contact.company_id
725729
WHERE
726730
contact.contact_id = %s
731+
%s
727732
ORDER BY
728733
isAssigned DESC,
729734
title ASC",
730735
$this->_db->makeQueryInteger($contactID),
731-
$this->_db->makeQueryInteger($contactID)
736+
$this->_db->makeQueryInteger($contactID),
737+
$adminHiddenCriterion
732738
);
733739

734740
return $this->_db->getAllAssoc($sql);

‎src/OpenCATS/Tests/Support/runAuthorizationAjax.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public function getAssoc($sql = null)
7575
public function getAllAssoc($sql = null)
7676
{
7777
$this->reads[] = $sql;
78-
return $this->scenario['rows'] ?? array();
78+
$rows = $this->scenario['rows'] ?? array();
79+
if ($this->scenario['endpoint'] === 'getDataItemJobOrders' && str_contains($sql, 'joborder.is_admin_hidden <> 1'))
80+
{
81+
$rows = array_filter($rows, fn($row) => $row['isAdminHidden'] != 1);
82+
}
83+
return $rows;
7984
}
8085
}
8186

@@ -148,7 +153,8 @@ class ACL_SETUP
148153
require $script;
149154
return;
150155
}
151-
if (!in_array($endpoint, array('editActivity', 'deleteActivity', 'setCandidateJobOrderRating', 'getPipelineDetails', 'getPipelineJobOrder'), true))
156+
if (!in_array($endpoint, array('editActivity', 'deleteActivity', 'setCandidateJobOrderRating', 'getPipelineDetails', 'getPipelineJobOrder',
157+
'getDataItemJobOrders', 'replaceTemplateTags'), true))
152158
{
153159
throw new InvalidArgumentException('Unsupported test endpoint.');
154160
}

‎src/OpenCATS/Tests/UnitTests/ActivityPipelineAuthorizationTest.php‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,131 @@ public function testPipelineDetailsCannotLeakHiddenCandidateNotes($admin): void
210210
if ($admin) self::assertStringContainsString('Private notes', $result['output']);
211211
else self::assertStringNotContainsString('Private notes', $result['output']);
212212
}
213+
214+
public static function associationReaders(): array
215+
{
216+
return array(
217+
'candidate read denied' => array(DATA_ITEM_CANDIDATE, 'candidates.show', false, false, false),
218+
'company read denied' => array(DATA_ITEM_COMPANY, 'companies.show', false, false, false),
219+
'contact read denied' => array(DATA_ITEM_CONTACT, 'contacts.show', false, false, false),
220+
'job order read denied' => array(DATA_ITEM_COMPANY, 'joborders.show', false, false, false),
221+
'hidden candidate denied' => array(DATA_ITEM_CANDIDATE, 'candidates.hidden', true, false, false),
222+
'hidden candidate allowed to administrator' => array(DATA_ITEM_CANDIDATE, null, true, true, true),
223+
'visible candidate' => array(DATA_ITEM_CANDIDATE, null, false, false, true),
224+
'company associations' => array(DATA_ITEM_COMPANY, null, false, false, true),
225+
'contact associations' => array(DATA_ITEM_CONTACT, null, false, false, true),
226+
'administrator without hidden job order permission' => array(DATA_ITEM_CONTACT, 'joborders.hidden', false, true, true)
227+
);
228+
}
229+
230+
#[DataProvider('associationReaders')]
231+
public function testRelatedJobOrdersMatchRecordVisibility($type, $deniedPermission, $hiddenParent, $admin, $allowed): void
232+
{
233+
$result = $this->request(array(
234+
'endpoint' => 'getDataItemJobOrders',
235+
'request' => array('dataItemID' => '7', 'dataItemType' => (string) $type),
236+
'acl' => $deniedPermission === null ? array() : array($deniedPermission => 0),
237+
'level' => $admin ? ACCESS_LEVEL_SA : ACCESS_LEVEL_READ,
238+
'parent' => array('isAdminHidden' => (int) $hiddenParent),
239+
'rows' => array(
240+
array('jobOrderID' => 8, 'title' => 'Assigned visible', 'companyName' => 'Example & Sons',
241+
'isAssigned' => $type === DATA_ITEM_CONTACT ? '1' : null, 'isAdminHidden' => 0),
242+
array('jobOrderID' => 9, 'title' => 'Private title', 'companyName' => 'Private company',
243+
'isAssigned' => '0', 'isAdminHidden' => 1),
244+
array('jobOrderID' => 10, 'title' => 'Visible legacy', 'companyName' => 'Example & Sons',
245+
'isAssigned' => '0', 'isAdminHidden' => null)
246+
)
247+
));
248+
self::assertStringContainsString($allowed ? '<errorcode>0</errorcode>' : '<errorcode>-2</errorcode>', $result['output']);
249+
self::assertSame(array(), $result['writes']);
250+
if (!$allowed)
251+
{
252+
self::assertStringNotContainsString('<joborder>', $result['output']);
253+
self::assertStringNotContainsString('Private', $result['output']);
254+
self::assertCount($hiddenParent ? 1 : 0, $result['reads']);
255+
return;
256+
}
257+
258+
$xml = simplexml_load_string($result['output']);
259+
$includeHidden = $admin && $deniedPermission !== 'joborders.hidden';
260+
$ids = array_map('strval', $xml->xpath('/data/joborder/id'));
261+
self::assertSame($includeHidden ? array('8', '9', '10') : array('8', '10'), $ids);
262+
self::assertSame('Example & Sons', (string) $xml->joborder[0]->companyname);
263+
if (!$includeHidden)
264+
{
265+
self::assertStringNotContainsString('Private', $result['output']);
266+
}
267+
$sql = end($result['reads']);
268+
$visibility = 'joborder.is_admin_hidden <> 1 OR joborder.is_admin_hidden IS NULL';
269+
self::assertSame(!$includeHidden, str_contains($sql, $visibility));
270+
if ($type === DATA_ITEM_CONTACT)
271+
{
272+
self::assertMatchesRegularExpression('/ORDER BY\s+isAssigned DESC,\s+title ASC/', $sql);
273+
self::assertSame('1', (string) $xml->joborder[0]->assigned);
274+
self::assertSame('0', (string) $xml->joborder[count($ids) - 1]->assigned);
275+
}
276+
else
277+
{
278+
self::assertMatchesRegularExpression('/ORDER BY\s+title ASC/', $sql);
279+
self::assertSame('0', (string) $xml->joborder[0]->assigned);
280+
}
281+
}
282+
283+
public static function invalidReadRequests(): array
284+
{
285+
return array(
286+
'unauthenticated association' => array('getDataItemJobOrders', array('dataItemID' => '7', 'dataItemType' => '100'), false),
287+
'invalid parent ID' => array('getDataItemJobOrders', array('dataItemID' => 'abc', 'dataItemType' => '100'), true),
288+
'unsupported parent type' => array('getDataItemJobOrders', array('dataItemID' => '7', 'dataItemType' => '999'), true),
289+
'array parent ID' => array('getDataItemJobOrders', array('dataItemID' => array('7'), 'dataItemType' => '100'), true),
290+
'unauthenticated preview' => array('replaceTemplateTags', array('candidateID' => '7'), false),
291+
'missing candidate ID' => array('replaceTemplateTags', array(), true),
292+
'zero candidate ID' => array('replaceTemplateTags', array('candidateID' => '0'), true)
293+
);
294+
}
295+
296+
#[DataProvider('invalidReadRequests')]
297+
public function testInvalidRelatedRecordRequestsFailBeforeDatabaseAccess($endpoint, $request, $loggedIn): void
298+
{
299+
$result = $this->request(array('endpoint' => $endpoint, 'request' => $request, 'loggedIn' => $loggedIn));
300+
self::assertStringContainsString('<errorcode>-1</errorcode>', $result['output']);
301+
self::assertSame(array(), $result['reads']);
302+
self::assertSame(array(), $result['writes']);
303+
}
304+
305+
public static function templateReaders(): array
306+
{
307+
return array(
308+
'visible candidate' => array(0, ACCESS_LEVEL_READ, array(), true),
309+
'candidate read denied' => array(0, ACCESS_LEVEL_EDIT, array('candidates.show' => 0), false),
310+
'hidden candidate denied' => array(1, ACCESS_LEVEL_EDIT, array(), false),
311+
'hidden candidate granted' => array(1, ACCESS_LEVEL_SA, array(), true),
312+
'missing candidate' => array(null, ACCESS_LEVEL_READ, array(), false)
313+
);
314+
}
315+
316+
#[DataProvider('templateReaders')]
317+
public function testTemplatePreviewRequiresCandidateAccess($hidden, $level, $acl, $allowed): void
318+
{
319+
$result = $this->request(array(
320+
'endpoint' => 'replaceTemplateTags', 'level' => $level, 'acl' => $acl,
321+
'request' => array('candidateID' => '7',
322+
'templateText' => '%CANDFIRSTNAME% / %CANDFULLNAME% / %CANDOWNER% / %USERMAIL% / <em>Hi & bye</em>'),
323+
'parent' => $hidden === null ? array() : array('isAdminHidden' => $hidden, 'firstName' => 'Sample',
324+
'candidateFullName' => 'Sample Candidate', 'ownerFullName' => 'Record Owner')
325+
));
326+
self::assertSame(array(), $result['writes']);
327+
self::assertCount($acl ? 0 : 1, $result['reads']);
328+
$xml = simplexml_load_string($result['output']);
329+
self::assertSame($allowed ? '0' : '-2', (string) $xml->errorcode);
330+
if ($allowed)
331+
{
332+
self::assertSame('Sample / Sample Candidate / Record Owner / <a href="mailto:"></a> / <em>Hi & bye</em>',
333+
(string) $xml->text);
334+
return;
335+
}
336+
self::assertFalse(isset($xml->text));
337+
self::assertStringNotContainsString('Sample', $result['output']);
338+
self::assertStringNotContainsString('Record Owner', $result['output']);
339+
}
213340
}

0 commit comments

Comments
 (0)