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

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

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 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,12 @@ public function process() {
if (empty($file)) {
return $this->modx->error->failure($this->modx->lexicon('file_err_ns'));
}
$file = preg_replace('/[\.]{2,}/', '', $file);
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars(pathinfo($file, PATHINFO_DIRNAME)));
if (!empty($directory)) {
$directory .= DIRECTORY_SEPARATOR;
}
$name = htmlspecialchars(end(explode( DIRECTORY_SEPARATOR, $file )));
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could use pathinfo for both parts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did so the first time.
Pathinfo with PATHINFO_BASENAME and PATHINFO_FILENAME have trouble with filename with unciode symbols and more dots.
So after testing and catch more bugs I replace this on more simple - end->explode for filename.

e.g. http://codepad.org/73SaPqcF, first 4 characters in filename are loses.

Copy link
Collaborator

Choose a reason for hiding this comment

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

pathinfo is locale aware, so you have to set an UTF8 capable locale before:

$oldlocale = setlocale(LC_ALL, 0);
setlocale(LC_ALL,'C.UTF-8');
$pathinfo = pathinfo($file);
setlocale(LC_ALL,$oldlocale);

Copy link
Collaborator

Choose a reason for hiding this comment

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

You have to check, if 'C.UTF-8' is available.

$path = $directory.$name;

$loaded = $this->getSource();
if (!($this->source instanceof modMediaSource)) {
Expand All @@ -42,7 +47,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
19 changes: 15 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,21 @@ 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);
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars(pathinfo($oldFile, PATHINFO_DIRNAME)));
if (!empty($directory)) {
$directory .= DIRECTORY_SEPARATOR;
}
$name = htmlspecialchars(end(explode( DIRECTORY_SEPARATOR, $oldFile )));
$oldFile = $directory.$name;

$newFile = $this->getProperty('name');
$directory = preg_replace('/[\.]{2,}/', '', htmlspecialchars(pathinfo($newFile, PATHINFO_DIRNAME)));
if (!empty($directory)) {
$directory .= DIRECTORY_SEPARATOR;
}
$name = htmlspecialchars(end(explode( DIRECTORY_SEPARATOR, $newFile )));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jako,

my last fix with setlocale is not right yet?

$newFile = $directory.$name;
$success = $this->source->renameObject($oldFile, $newFile);

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