Skip to content

Change sanitize behavior for files with two dots in filename #14330

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

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion core/model/modx/processors/browser/file/create.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function process() {
$directory = ltrim(strip_tags(preg_replace('/[\.]{2,}/', '', htmlspecialchars($directory))),'/');

$name = $this->getProperty('name');
$name = ltrim(strip_tags(preg_replace('/[\.]{2,}/', '', htmlspecialchars($name))),'/');
$name = ltrim(strip_tags(htmlspecialchars($name)),'/');

$loaded = $this->getSource();
if (!($this->source instanceof modMediaSource)) {
Expand Down
10 changes: 8 additions & 2 deletions core/model/modx/processors/browser/file/remove.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public function process() {
if (empty($file)) {
return $this->modx->error->failure($this->modx->lexicon('file_err_ns'));
}
$file = preg_replace('/[\.]{2,}/', '', $file);
$oldlocale = setlocale(LC_ALL, 0);
setlocale(LC_ALL,'C.UTF-8');
$pathinfo = pathinfo($file);
setlocale(LC_ALL,$oldlocale);
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars($pathinfo['dirname']));
$name = htmlspecialchars($pathinfo['basename']);
$path = $directory.DIRECTORY_SEPARATOR.$name;

$loaded = $this->getSource();
if (!($this->source instanceof modMediaSource)) {
Expand All @@ -42,7 +48,7 @@ public function process() {
if (!$this->source->checkPolicy('remove')) {
return $this->failure($this->modx->lexicon('permission_denied'));
}
$success = $this->source->removeObject($file);
$success = $this->source->removeObject($path);

if (empty($success)) {
$errors = $this->source->getErrors();
Expand Down
18 changes: 14 additions & 4 deletions core/model/modx/processors/browser/file/rename.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ public function process() {
}

$oldFile = $this->getProperty('path');
$oldFile = preg_replace('/[\.]{2,}/', '', htmlspecialchars($oldFile));
$name = $this->getProperty('name');
$name = preg_replace('/[\.]{2,}/', '', htmlspecialchars($name));
$success = $this->source->renameObject($oldFile, $name);
$oldlocale = setlocale(LC_ALL, 0);
setlocale(LC_ALL,'C.UTF-8');
Copy link
Collaborator

@Jako Jako Mar 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check here, wether setlocale is successful. Otherwise an error should be logged, describing possible issues. Maybe the 'C.UTF-8' string should be configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trouble solving if MODX Setting "locale" is correct encoding for current server environment.
Now I don't run setlocale manually, just log about encoding if function pathinfo return not what was expected

$pathinfo = pathinfo($oldFile);
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars($pathinfo['dirname']));
$name = htmlspecialchars($pathinfo['basename']);
$oldFile = $directory.DIRECTORY_SEPARATOR.$name;

$newFile = $this->getProperty('name');
$pathinfo = pathinfo($newFile);
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars($pathinfo['dirname']));
$name = htmlspecialchars($pathinfo['basename']);
$newFile = $directory.DIRECTORY_SEPARATOR.$name;
setlocale(LC_ALL,$oldlocale);
$success = $this->source->renameObject($oldFile, $newFile);

if (empty($success)) {
$msg = '';
Expand Down