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

Bugfix/CopyDir recursive destination check #126

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/Task/FileSystem/CopyDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ protected function copyDir($src, $dst)
if (false === $dir) {
throw new TaskException($this, "Cannot open source directory '" . $src . "'");
}
@mkdir($dst);
if (!is_dir($dst)) {
mkdir($dst, 0777, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't 777 a little too much? I think 775 or 755 would be better.

Or check the parent folders permission and inherit them?

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 not want to change existing behavior, and 0777 is the default (http://php.net/manual/de/function.mkdir.php).

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not like that standard php is secure at all. I think especially in the case of dealing with assets that are usually going into a public webroot it is critical to not set them to 777. This will make it very easy to add malicious files and code into files.

I would propose to set the default to 755 and give the user the option to change it:

->permissions(777)
->run();

Copy link
Member

Choose a reason for hiding this comment

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

permissions option is not very descriptive, could you propose something better?
I agree to provide better option for that, and maybe 755 is a good idea for making it standard.

Copy link
Contributor

Choose a reason for hiding this comment

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

dirPermissions(), folderPermissions()? chmod()?

}
while (false !== ($file = readdir($dir))) {
if (($file !== '.') && ($file !== '..')) {
$srcFile = $src . '/' . $file;
Expand Down
1 change: 1 addition & 0 deletions tests/_data/claypit/some/deeply/existing_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some existing file
1 change: 1 addition & 0 deletions tests/_data/claypit/some/deeply/nested/structu.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just a file
1 change: 1 addition & 0 deletions tests/_data/claypit/some_destination/deeply/existing_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some_destination existing file
6 changes: 3 additions & 3 deletions tests/cli/CliGuy.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public function taskConcat($files) {
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $file
* @return ReplaceInFile
* @return Replace
* @see \Codeception\Module\CliHelper::taskReplaceInFile()
*/
public function taskReplaceInFile($file) {
Expand All @@ -760,7 +760,7 @@ public function taskReplaceInFile($file) {
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $file
* @return WriteToFile
* @return Write
* @see \Codeception\Module\CliHelper::taskWriteToFile()
*/
public function taskWriteToFile($file) {
Expand Down Expand Up @@ -807,7 +807,7 @@ public function taskCopyDir($dirs) {
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @return Filesystem
* @return FilesystemStack
* @see \Codeception\Module\CliHelper::taskFilesystemStack()
*/
public function taskFilesystemStack() {
Expand Down
11 changes: 11 additions & 0 deletions tests/cli/CopyDirOverwritesFilesCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
$I = new CliGuy($scenario);
$I->wantTo('overwrite a file with CopyDir task');
$I->amInPath(codecept_data_dir() . 'sandbox');
$I->seeDirFound('some');
$I->seeFileFound('existing_file', 'some');
$I->taskCopyDir(['some' => 'some_destination'])
->run();
$I->seeFileFound('existing_file', 'some_destination/deeply');
$I->openFile('some_destination/deeply/existing_file');
$I->seeInThisFile('some existing file');
10 changes: 10 additions & 0 deletions tests/cli/CopyDirRecursiveCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$I = new CliGuy($scenario);
$I->wantTo('copy dir recursively with CopyDir task');
$I->amInPath(codecept_data_dir() . 'sandbox');
$I->seeDirFound('some/deeply/nested');
$I->seeFileFound('structu.re', 'some/deeply/nested');
$I->taskCopyDir(['some/deeply' => 'some_destination/deeply'])
->run();
$I->seeDirFound('some_destination/deeply/nested');
$I->seeFileFound('structu.re', 'some_destination/deeply/nested');