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

Add support for a list of allowed mime-types instead of only one #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion server/src/config.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ return [
'secretBcrypt' => '',
'saveDirName' => '/data/',
'maxScreenshotSize' => 2 * 1048576,
'screenshotMimeType' => 'image/png'
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ]
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ]
'screenshotMimeTypes' => ['image/png', 'image/jpeg']

];
7 changes: 4 additions & 3 deletions server/src/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'secretBcrypt' => '',
'saveDirName' => '/data/',
'maxScreenshotSize' => 2 * 1048576,
'screenshotMimeType' => 'image/png'
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ]
],
file_exists('./config.php') ? include_once('./config.php') : []
);
Expand All @@ -20,21 +20,22 @@
exit();
}

$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : "error";
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : "error";
$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : 'error';

if (
// Make sure it is a successful single file upload
!isset($screenshot['error']) ||
is_array($screenshot['error']) ||
$screenshot['error'] !== UPLOAD_ERR_OK ||
// Verify size and mime type
$screenshot['size'] > $config['maxScreenshotSize'] ||
mime_content_type($screenshot['tmp_name']) !== $config['screenshotMimeType']
!in_array( $mimetype, $config['screenshotMimeTypes'], true)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
!in_array( $mimetype, $config['screenshotMimeTypes'], true)
!in_array($mimetype, $config['screenshotMimeTypes'], true)

) {
http_response_code(422);
exit();
}

// Generate screenshot path
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.png'));
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == "image/png" ? "png" : "jpg" )));
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == "image/png" ? "png" : "jpg" )));
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == 'image/png' ? 'png' : 'jpg' )));

Copy link
Owner

Choose a reason for hiding this comment

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

If someone adds other mimetypes besides these 2, I guess we can't really have this assumption of using .jpg here.


// Create save directory and move screenshot to it
if (
Expand Down