Skip to content

Commit b10f4dc

Browse files
committed
fix(attachments): enforce backup access permissions
1 parent 71469f0 commit b10f4dc

11 files changed

Lines changed: 326 additions & 10 deletions

File tree

‎docker/docker-compose-test.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55
ports:
66
- "80:80"
77
- "443:443"
8+
volumes:
9+
- ./nginx-opencats.conf:/etc/nginx/conf.d/basic.conf:ro
810
volumes_from:
911
- opencatsdata
1012

‎docker/docker-compose.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55
ports:
66
- "80:80"
77
- "443:443"
8+
volumes:
9+
- ./nginx-opencats.conf:/etc/nginx/conf.d/basic.conf:ro
810
volumes_from:
911
- opencatsdata
1012

‎docker/nginx-opencats.conf‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Preserve the bundled site's basic configuration.
2+
resolver 127.0.0.11 valid=30s;
3+
include conf.d/directive-only/x-ua-compatible.conf;
4+
include conf.d/directive-only/extra-security.conf;
5+
include conf.d/directive-only/ssl.conf;
6+
include conf.d/directive-only/cross-domain-insecure.conf;
7+
include conf.d/location/expires.conf;
8+
include conf.d/location/cross-domain-fonts.conf;
9+
10+
# Serve archives only through the attachment controller. Backup progress polling
11+
# still uses the existing progress.txt file.
12+
location ~* /attachments/(.*/)?catsbackup\.(bak|sql(\.[0-9]+)?)$ {
13+
return 403;
14+
}

‎lib/Attachments.php‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ public function delete($attachmentID, $removeFile = true)
267267
$sql = sprintf(
268268
"SELECT
269269
directory_name AS directoryName,
270-
stored_filename AS storedFileName
270+
stored_filename AS storedFileName,
271+
content_type AS contentType
271272
FROM
272273
attachment
273274
WHERE
@@ -276,6 +277,11 @@ public function delete($attachmentID, $removeFile = true)
276277
);
277278
$rs = $this->_db->getAssoc($sql);
278279

280+
if (($rs['contentType'] ?? null) === 'catsbackup' && !self::canDeleteBackup())
281+
{
282+
return false;
283+
}
284+
279285
$this->forceRemoteDeleteAttachment($attachmentID);
280286

281287
$sql = sprintf(
@@ -327,6 +333,11 @@ public function delete($attachmentID, $removeFile = true)
327333
*/
328334
public function deleteAll($dataItemType, $dataItemID, $criteria = '')
329335
{
336+
if (!self::canDeleteBackup())
337+
{
338+
$criteria .= " AND (content_type IS NULL OR content_type <> 'catsbackup')";
339+
}
340+
330341
/* Get all attachment IDs for the Data Item. */
331342
$sql = sprintf(
332343
"SELECT
@@ -355,7 +366,7 @@ public function deleteAll($dataItemType, $dataItemID, $criteria = '')
355366
{
356367
/* Sanity check. Don't delete the whole attachments directory. */
357368
$directoryName = trim($row['directoryName']);
358-
if (empty($directoryName) || $directoryName = '.')
369+
if (empty($directoryName) || $directoryName === '.')
359370
{
360371
continue;
361372
}
@@ -384,6 +395,12 @@ public function deleteAll($dataItemType, $dataItemID, $criteria = '')
384395
$this->_db->query($sql);
385396
}
386397

398+
private static function canDeleteBackup()
399+
{
400+
return isset($_SESSION['CATS']) &&
401+
$_SESSION['CATS']->getAccessLevel('settings.deleteBackup') >= ACCESS_LEVEL_SA;
402+
}
403+
387404
/**
388405
* Removes all profile image attachments for the specified Data Item ID.
389406
*
@@ -446,11 +463,18 @@ public function setDirectoryName($attachmentID, $directoryName)
446463
*
447464
* @param flag Data Item type.
448465
* @param integer Data Item ID.
466+
* @param bool|null Whether to select only backups, exclude them, or include both.
449467
* @return array Multi-dimensional associative array of attachments data, or
450468
* array() if no records are present.
451469
*/
452-
public function getAll($dataItemType, $dataItemID)
470+
public function getAll($dataItemType, $dataItemID, ?bool $backups = null)
453471
{
472+
$criteria = match ($backups) {
473+
true => " AND content_type = 'catsbackup'",
474+
false => " AND (content_type IS NULL OR content_type <> 'catsbackup')",
475+
null => ''
476+
};
477+
454478
$sql = sprintf(
455479
"SELECT
456480
IF(ISNULL(text), 0, 1) AS hasText,
@@ -471,9 +495,11 @@ public function getAll($dataItemType, $dataItemID)
471495
WHERE
472496
data_item_id = %s
473497
AND
474-
data_item_type = %s",
498+
data_item_type = %s
499+
%s",
475500
$this->_db->makeQueryInteger($dataItemID),
476-
$this->_db->makeQueryInteger($dataItemType)
501+
$this->_db->makeQueryInteger($dataItemType),
502+
$criteria
477503
);
478504
$rs = $this->_db->getAllAssoc($sql);
479505

‎lib/Companies.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function delete($companyID)
232232
/* Find associated attachments. */
233233
$attachments = new Attachments();
234234
$attachmentsRS = $attachments->getAll(
235-
DATA_ITEM_COMPANY, $companyID
235+
DATA_ITEM_COMPANY, $companyID, false
236236
);
237237

238238
/* Delete associated contacts. */

‎modules/attachments/AttachmentsUI.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ private function getAttachment()
8484
);
8585
}
8686

87+
if ($rs['contentType'] === 'catsbackup' &&
88+
$this->getUserAccessLevel('settings.createBackup') < ACCESS_LEVEL_SA)
89+
{
90+
CommonErrors::fatal(COMMONERROR_PERMISSION, $this, 'Invalid user level for action.');
91+
}
92+
8793
$directoryName = $rs['directoryName'];
8894
$fileName = $rs['storedFilename'];
8995
$filePath = sprintf('attachments/%s/%s', $directoryName, $fileName);

‎modules/companies/CompaniesUI.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ private function show()
310310
/* Attachments */
311311
$attachments = new Attachments();
312312
$attachmentsRS = $attachments->getAll(
313-
DATA_ITEM_COMPANY, $companyID
313+
DATA_ITEM_COMPANY, $companyID, false
314314
);
315315

316316
foreach ($attachmentsRS as $rowNumber => $attachmentsData)
@@ -1145,7 +1145,8 @@ private function getCompanyAttachmentForAction($attachmentID, $companyID)
11451145
$attachment = $attachments->get($attachmentID);
11461146
if (!isset($attachment['attachmentID']) ||
11471147
$attachment['dataItemType'] != DATA_ITEM_COMPANY ||
1148-
$attachment['dataItemID'] != $companyID)
1148+
$attachment['dataItemID'] != $companyID ||
1149+
$attachment['contentType'] === 'catsbackup')
11491150
{
11501151
CommonErrors::fatalModal(COMMONERROR_BADINDEX, $this, 'Invalid attachment ID.');
11511152
}

‎modules/settings/SettingsUI.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ private function createBackup()
21592159
/* Attachments */
21602160
$attachments = new Attachments();
21612161
$attachmentsRS = $attachments->getAll(
2162-
DATA_ITEM_COMPANY, $_SESSION['CATS']->getSiteCompanyID()
2162+
DATA_ITEM_COMPANY, $_SESSION['CATS']->getSiteCompanyID(), true
21632163
);
21642164

21652165
foreach ($attachmentsRS as $index => $data)

‎modules/settings/ajax/backup.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
die('No action.');
3333
}
3434

35+
// Direct AJAX requests must use the same protected attachment directory as the dispatcher.
36+
if (!chdir(LEGACY_ROOT))
37+
{
38+
die('Backup directory is unavailable.');
39+
}
40+
3541
$action = $_POST['a'];
3642

3743
$completedTasks = '';

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getAssoc($sql = null)
5959
{
6060
return $sql === null ? (array) array_shift($this->backupRows) : $this->scenario['backup'];
6161
}
62+
if (preg_match('/FROM\s+attachment\b/', $sql)) return $this->scenario['attachment'] ?? array();
6263
if (preg_match('/FROM\s+activity\b/', $sql)) return $this->scenario['activity'] ?? array();
6364
if (preg_match('/FROM\s+candidate_joborder\b/', $sql))
6465
{
@@ -101,7 +102,10 @@ class ACL_SETUP
101102
ob_start();
102103
register_shutdown_function(function () use ($db, &$issues) {
103104
$output = ob_get_clean();
104-
session_destroy();
105+
if (session_status() === PHP_SESSION_ACTIVE)
106+
{
107+
session_destroy();
108+
}
105109
echo json_encode(array('output' => $output, 'writes' => $db->writes, 'reads' => $db->reads,
106110
'issues' => $issues, 'fatal' => error_get_last()), JSON_THROW_ON_ERROR);
107111
});
@@ -118,6 +122,24 @@ class ACL_SETUP
118122
require $script;
119123
return;
120124
}
125+
if (in_array($endpoint, array('getAttachment', 'deleteBackup'), true))
126+
{
127+
require_once './lib/UserInterface.php';
128+
require_once './lib/Template.php';
129+
require_once './modules/attachments/AttachmentsUI.php';
130+
require_once './modules/settings/SettingsUI.php';
131+
(new ReflectionProperty(CATSSession::class, '_siteCompanyID'))->setValue($session, 7);
132+
$_SERVER['HTTP_HOST'] = 'example.test';
133+
$_SERVER['PHP_SELF'] = '/index.php';
134+
$_GET = array('a' => $endpoint, 'id' => '42',
135+
'directoryNameHash' => md5($scenario['attachment']['directoryName']));
136+
$_POST['postback'] = 'postback';
137+
chdir($scenario['fixture']);
138+
139+
$ui = $endpoint === 'getAttachment' ? new AttachmentsUI() : new SettingsUI();
140+
$ui->handleRequest();
141+
return;
142+
}
121143
if ($endpoint === 'processMassImportItem')
122144
{
123145
$_SESSION['CATS_LEGACY_BULK_RESUMES'] = $scenario['queue'];

0 commit comments

Comments
 (0)