Skip to content

Commit a8a667c

Browse files
authored
Add sanity checks on the asset file (#31710)
1 parent 8b15f5f commit a8a667c

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

administrator/components/com_templates/src/Controller/TemplateController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ public function delete()
456456
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
457457
$this->setRedirect(Route::_($url, false));
458458
}
459+
if (base64_decode(urldecode($file)) == '/joomla.asset.json')
460+
{
461+
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_ASSET_FILE_DELETE'), 'warning');
462+
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
463+
$this->setRedirect(Route::_($url, false));
464+
}
459465
elseif ($model->deleteFile($file))
460466
{
461467
$this->setMessage(Text::_('COM_TEMPLATES_FILE_DELETE_SUCCESS'));
@@ -699,6 +705,12 @@ public function renameFile()
699705
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
700706
$this->setRedirect(Route::_($url, false));
701707
}
708+
elseif (base64_decode(urldecode($file)) == '/joomla.asset.json')
709+
{
710+
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_RENAME_ASSET_FILE'), 'warning');
711+
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
712+
$this->setRedirect(Route::_($url, false));
713+
}
702714
elseif (!preg_match('/^[a-zA-Z0-9-_]+$/', $newName))
703715
{
704716
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FILE_NAME'), 'error');

administrator/components/com_templates/src/Model/TemplateModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,14 @@ public function save($data)
10121012
// Make sure EOL is Unix
10131013
$data['source'] = str_replace(array("\r\n", "\r"), "\n", $data['source']);
10141014

1015+
// If the asset file for the template ensure we have valid template so we don't instantly destroy it
1016+
if ($fileName === '/joomla.asset.json' && json_decode($data['source']) === null)
1017+
{
1018+
$this->setError(Text::_('COM_TEMPLATES_ERROR_ASSET_FILE_INVALID_JSON'));
1019+
1020+
return false;
1021+
}
1022+
10151023
$return = File::write($filePath, $data['source']);
10161024

10171025
if (!$return)

administrator/language/en-GB/com_templates.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ COM_TEMPLATES_COPY_SUCCESS="New template called %s was installed."
3838
COM_TEMPLATES_CROP_AREA_ERROR="Crop area not selected."
3939
COM_TEMPLATES_DIRECTORY_NOT_WRITABLE="The template folder is not writable. Some features may not work."
4040
COM_TEMPLATES_ERR_XML="Template XML data not available"
41+
COM_TEMPLATES_ERROR_ASSET_FILE_DELETE="The file index.php can't be deleted. Make changes in the editor if you want to change the file."
42+
COM_TEMPLATES_ERROR_ASSET_FILE_INVALID_JSON="The joomla.asset.json file doesn't not contain valid JSON, aborting save."
4143
COM_TEMPLATES_ERROR_CANNOT_DELETE_LAST_STYLE="Can't delete the last style of a template."
4244
COM_TEMPLATES_ERROR_CANNOT_UNSET_DEFAULT_STYLE="Can't unset default style."
4345
COM_TEMPLATES_ERROR_COULD_NOT_COPY="Unable to copy template files to temporary folder."
@@ -62,6 +64,7 @@ COM_TEMPLATES_ERROR_INVALID_FROM_NAME="Template to copy from can't be found."
6264
COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME="Invalid template name. Please use only letters, numbers, dashes and underscores."
6365
COM_TEMPLATES_ERROR_NO_FILE_SELECTED="No file selected."
6466
COM_TEMPLATES_ERROR_RENAME_INDEX="The file index.php can't be renamed."
67+
COM_TEMPLATES_ERROR_RENAME_ASSET_FILE="The file joomla.asset.json can't be renamed."
6568
COM_TEMPLATES_ERROR_ROOT_DELETE="The root folder can't be deleted."
6669
COM_TEMPLATES_ERROR_SAVE_DISABLED_TEMPLATE="Unable to save a style associated to a disabled template."
6770
COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND="Source file not found."

0 commit comments

Comments
 (0)