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

Fixed #7920 - "File contains dangerous PHP" #8666

Merged

Conversation

arek1-618
Copy link
Contributor

Fixed #7920 - "File contains dangerous PHP"

@arek1-618 arek1-618 added the ▶ in progress We’re currently working on this matter. label Nov 22, 2018
@arek1-618 arek1-618 added this to the YetiForce 5.0 milestone Nov 22, 2018
@arek1-618 arek1-618 added ✔ finished This pull request has been finished. and removed ▶ in progress We’re currently working on this matter. labels Nov 23, 2018
if (($type && $type === 'image') || $this->getShortMimeType(0) === 'image') {
$this->validateImage();
$this->validateCodeInjectionInMetadata();
} else {
$this->validateCodeInjection();

Choose a reason for hiding this comment

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

?

$returnVal = false;
}
} else {
if (@imagecreatefromstring($this->getContents()) === false) {

Choose a reason for hiding this comment

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

?

$img->clear();
$img->destroy();
} else {
$img = @\imagecreatefromstring(\file_get_contents($fileImgIn));

Choose a reason for hiding this comment

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

?

} else {
$img = @\imagecreatefromstring(\file_get_contents($fileImgIn));
if ($img === false) {
throw new \App\Exceptions\AppException('Wrong file type');

Choose a reason for hiding this comment

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

?

@rskrzypczak rskrzypczak added 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. and removed ✔ finished This pull request has been finished. labels Nov 23, 2018
@arek1-618 arek1-618 added ✔ finished This pull request has been finished. and removed 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. labels Nov 29, 2018
@mariuszkrzaczkowski mariuszkrzaczkowski added 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. and removed ✔ finished This pull request has been finished. labels Nov 30, 2018
@arek1-618 arek1-618 added the ✔ finished This pull request has been finished. label Dec 5, 2018
@arek1-618 arek1-618 removed the 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. label Dec 5, 2018
if (extension_loaded('imagick')) {
try {
$img = new \imagick($this->path);
$img->valid();
Copy link
Member

Choose a reason for hiding this comment

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

?

}

/**
* Validate code injection.
*
* @throws \Exception
*/
private function validateCodeInjection()
public function validateCodeInjection()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function validateCodeInjection()
private function validateCodeInjection()

stripos($contents, '<? ') !== false ||
stripos($contents, '<% ') !== false ||
stripos($contents, '<?xpacket') !== false
) {
throw new \App\Exceptions\AppException('ERR_FILE_PHP_CODE_INJECTION');
Copy link
Member

Choose a reason for hiding this comment

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

\App\Exceptions\DangerousFile

(empty($imageInfo['APP1']) || strpos($imageInfo['APP1'], 'Exif') === 0) &&
($exifdata = exif_read_data($this->path)) && !$this->validateImageMetadata($exifdata)
) {
throw new \App\Exceptions\AppException('ERR_FILE_PHP_CODE_INJECTION');
Copy link
Member

Choose a reason for hiding this comment

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

\App\Exceptions\DangerousFile

@@ -420,29 +444,56 @@ private function validateImage()
if (preg_match('[\x01-\x08\x0c-\x1f]', $this->getContents())) {
throw new \App\Exceptions\AppException('ERR_FILE_WRONG_IMAGE');
}
$this->validateCodeInjectionInMetadata();
if (!$this->validateImageContent()) {
throw new \App\Exceptions\AppException('ERR_FILE_WRONG_IMAGE ||' . $this->validateError);
Copy link
Member

Choose a reason for hiding this comment

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

\App\Exceptions\DangerousFile

}
if ($type && $this->getShortMimeType(0) !== $type) {
throw new \App\Exceptions\AppException('Wrong file type');
throw new \App\Exceptions\AppException('ERR_FILE_ILLEGAL_FORMAT');
Copy link
Member

Choose a reason for hiding this comment

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

\App\Exceptions\DangerousFile

$img = new \imagick($fileImgIn);
$img->stripImage();
$img->setImageCompression(\Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(80);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$img->setImageCompressionQuality(80);
$img->setImageCompressionQuality(99);

$file = static::loadFromRequest($fileDetails);
if (!$file->validate($type)) {
$attach[] = ['name' => $file->getName(), 'error' => $file->validateError, 'hash' => $request->getByType('hash', 'Text')];
continue;
if (!static::removeForbiddenTags($file->getPath(), $file->getPath())) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!static::removeForbiddenTags($file->getPath(), $file->getPath())) {
if (!static::removeForbiddenTags($file->getPath())) {

@@ -985,10 +1036,19 @@ public static function uploadAndSave(\App\Request $request, array $files, string
$attach = [];
foreach (static::transform($files, true) as $key => $transformFiles) {
foreach ($transformFiles as $fileDetails) {
$infoFile = '';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$infoFile = '';
$additionalNotes = '';

@@ -1006,7 +1066,8 @@ public static function uploadAndSave(\App\Request $request, array $files, string
'name' => $file->getName(),
'size' => \vtlib\Functions::showBytes($file->getSize()),
'key' => $key,
'hash' => $request->getByType('hash', 'string')
'hash' => $request->getByType('hash', 'string'),
'info' => $infoFile
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'info' => $infoFile
'additionalNotes' => $additionalNotes

@mariuszkrzaczkowski mariuszkrzaczkowski added 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. and removed ✔ finished This pull request has been finished. labels Dec 7, 2018
$this->validateError = $e->getMessage();
$returnVal = false;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

?

@arek1-618 arek1-618 added ✔ finished This pull request has been finished. and removed 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. labels Dec 7, 2018
$file = static::loadFromRequest($fileDetails);
if (!$file->validate($type)) {
$attach[] = ['name' => $file->getName(), 'error' => $file->validateError, 'hash' => $request->getByType('hash', 'Text')];
continue;
if (!static::removeForbiddenTags($file->getPath())) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!static::removeForbiddenTags($file->getPath())) {
if (!static::removeForbiddenTags($file)) {

*
* @return bool
*/
public static function removeForbiddenTags(string $fileImgIn): bool
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public static function removeForbiddenTags(string $fileImgIn): bool
public static function removeForbiddenTags(string $file): bool

$result = false;
if (extension_loaded('imagick')) {
try {
$img = new \imagick($fileImgIn);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$img = new \imagick($fileImgIn);
$img = new \imagick($file->getPath());

try {
$img = new \imagick($fileImgIn);
$img->stripImage();
switch (strtolower(pathinfo($fileImgIn, PATHINFO_EXTENSION))) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
switch (strtolower(pathinfo($fileImgIn, PATHINFO_EXTENSION))) {
switch ($file->getExtension()) {

} else {
$img = \imagecreatefromstring(\file_get_contents($fileImgIn));
if (false !== $img) {
switch (strtolower(pathinfo($fileImgIn, PATHINFO_EXTENSION))) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
switch (strtolower(pathinfo($fileImgIn, PATHINFO_EXTENSION))) {
switch ($file->getExtension()) {

@@ -169,6 +169,7 @@
"LBL_HELP_LDAP": "Protok\u00f3\u0142 u\u017cywany do uzyskiwania dost\u0119pu do baz danych przechowuj\u0105cych informacje w strukturze drzewa.",
"LBL_HELP_OPCACHE": "Poprawia wydajno\u015b\u0107 poprzez przechowywanie skompilowanego kodu bajtowego w pami\u0119ci wsp\u00f3\u0142dzielonej.",
"LBL_HELP_APCU": "",
"LBL_HELP_IMAGICK": "Zaawansowana obr\u00f3bka zdje\u0107",
Copy link
Member

Choose a reason for hiding this comment

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

Biblioteka zalecana do zabezpieczania potencjalnie niebezpiecznych plików graficznych

@@ -227,6 +227,7 @@
"LBL_EXPORT_RECORDS": "Eksportuj rekordy",
"LBL_EXPORT_SELECTED_RECORDS": "Eksportuj zaznaczone rekordy",
"LBL_Feb": "Lut",
"LBL_FILE_HAS_BEEN_MODIFIED": "Tw\u00f3j plik zosta\u0142 zmodyfikowany.",
Copy link
Member

Choose a reason for hiding this comment

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

Plik został zmodyfikowany ponieważ zawierał niebezpieczny kod.

@mariuszkrzaczkowski mariuszkrzaczkowski added 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. and removed ✔ finished This pull request has been finished. labels Dec 7, 2018
@arek1-618 arek1-618 added ✔ finished This pull request has been finished. and removed 🔧 AmendmentsRequired This pull request requires some amendments in order to be merged. labels Dec 7, 2018
@mariuszkrzaczkowski mariuszkrzaczkowski merged commit dfc103e into YetiForceCompany:developer Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✔ finished This pull request has been finished.
Projects
YetiForceCRM
Awaiting triage
Development

Successfully merging this pull request may close these issues.

None yet

3 participants