Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Restrict tag usage and ensure proper escaping of element name, caption, and description fields #15936

Open
wants to merge 9 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use fully-qualified classname in conditional
Change made by request for better maintainability.
  • Loading branch information
Jim Graham committed Jan 21, 2023
commit c6812e424c946107895377f17cfbcf4f0b52c0db
7 changes: 4 additions & 3 deletions core/src/Revolution/Processors/Element/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MODX\Revolution\modElement;
use MODX\Revolution\Processors\Model\CreateProcessor;
use MODX\Revolution\modTemplate;
use MODX\Revolution\modTemplateVar;
use MODX\Revolution\Validation\modValidator;

/**
Expand Down Expand Up @@ -61,9 +62,9 @@
$locked = (bool)$this->getProperty('locked', false);
$this->object->set('locked', $locked);

$elementClassName = array_pop(explode('\\', $this->classKey));
$isTV = $this->classKey === modTemplateVar::class;

if ($elementClassName === 'modTemplateVar') {
if ($isTV) {
if ($caption = trim($this->getProperty('caption', ''))) {
$caption = $this->modx->stripHtml(
$caption,
Expand All @@ -75,12 +76,12 @@
}

if ($description = trim($this->getProperty('description', ''))) {
$description = $elementClassName === 'modTemplateVar'
$description = $isTV
? $this->modx->stripHtml(
$description,
$this->modx->getOption('elements_description_allowedtags'),
$this->modx->getOption('elements_description_allowedattr')
)

Check warning on line 84 in core/src/Revolution/Processors/Element/Create.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Create.php#L80-L84

Added lines #L80 - L84 were not covered by tests
: strip_tags($description)
;
$this->object->set('description', $description);
Expand Down
7 changes: 4 additions & 3 deletions core/src/Revolution/Processors/Element/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MODX\Revolution\modElement;
use MODX\Revolution\Processors\Model\UpdateProcessor;
use MODX\Revolution\modTemplate;
use MODX\Revolution\modTemplateVar;

/**
* Abstract class for Update Element processors. To be extended for each derivative element type.
Expand All @@ -34,7 +35,7 @@
public function initialize()
{
if ($this->classKey === modTemplate::class) {
$this->elementNameField = 'templatename';

Check warning on line 38 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L38

Added line #L38 was not covered by tests
}
return parent::initialize();
}
Expand All @@ -56,26 +57,26 @@
$this->object->set('locked', (bool)$locked);
}

$elementClassName = array_pop(explode('\\', $this->classKey));
$isTV = $this->classKey === modTemplateVar::class;

if ($elementClassName === 'modTemplateVar') {
if ($isTV) {
if ($caption = trim($this->getProperty('caption', ''))) {
$caption = $this->modx->stripHtml(
$caption,
$this->modx->getOption('elements_caption_allowedtags'),
$this->modx->getOption('elements_caption_allowedattr')
);
$this->object->set('caption', $caption);

Check warning on line 69 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L63-L69

Added lines #L63 - L69 were not covered by tests
}
}

if ($description = trim($this->getProperty('description', ''))) {
$description = $elementClassName === 'modTemplateVar'
$description = $isTV
? $this->modx->stripHtml(
$description,
$this->modx->getOption('elements_description_allowedtags'),
$this->modx->getOption('elements_description_allowedattr')
)

Check warning on line 79 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L75-L79

Added lines #L75 - L79 were not covered by tests
: strip_tags($description)
;
$this->object->set('description', $description);
Expand All @@ -86,13 +87,13 @@
$name = $this->getProperty($this->elementNameField, '');

if (empty($name)) {
$this->addFieldError($this->elementNameField, $this->modx->lexicon($this->objectType . '_err_ns_name'));

Check warning on line 90 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L90

Added line #L90 was not covered by tests
} else {
if ($this->alreadyExists($name)) {
$this->addFieldError(
$this->elementNameField,
$this->modx->lexicon($this->objectType . '_err_ae', ['name' => $name])
);

Check warning on line 96 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L93-L96

Added lines #L93 - L96 were not covered by tests
}
}

Expand All @@ -110,7 +111,7 @@
if ($this->object->staticContentChanged()) {
if (!$this->object->isStaticSourceMutable()) {
$this->addFieldError('static_file', $this->modx->lexicon('element_static_source_immutable'));
} else if (!$this->object->isStaticSourceValidPath()) {

Check warning on line 114 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L114

Added line #L114 was not covered by tests
$this->addFieldError('static_file', $this->modx->lexicon('element_static_source_protected_invalid'));
}
}
Expand All @@ -135,13 +136,13 @@

public function cleanup()
{
$fields = ['id', $this->elementNameField, 'description', 'locked', 'category', 'content'];

Check warning on line 139 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L139

Added line #L139 was not covered by tests
return $this->success(
'',
array_merge(
$this->object->get($fields),
['previous_category' => $this->previousCategory]
)

Check warning on line 145 in core/src/Revolution/Processors/Element/Update.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Element/Update.php#L142-L145

Added lines #L142 - L145 were not covered by tests
);
}
}
Loading