Reject invalid uploads and expand flow tests.

This commit is contained in:
tim
2026-09-26 15:27:51 +02:00
parent 716a609f4e
commit 8f4851e576
9 changed files with 427 additions and 11 deletions
+23 -9
View File
@@ -157,8 +157,11 @@ function get_mimetype($image_file) {
function save_uploaded_file() {
// A new upload in this request: PHP removes the file after the request
if (is_uploaded_file($_FILES['upload']['tmp_name'] ?? '')) {
return $_FILES['upload']['tmp_name'];
$upload_tmp = $_FILES['upload']['tmp_name'] ?? '';
if (!is_string($upload_tmp))
return FALSE;
if (is_uploaded_file($upload_tmp)) {
return $upload_tmp;
}
// An image uploaded at the preview step; the session knows which one
if (($_POST['uploaded'] ?? '') === 'pending') {
@@ -188,6 +191,7 @@ function validate_data() {
extract($result[0], EXTR_OVERWRITE);
$upload_error = $_FILES['upload']['error'] ?? UPLOAD_ERR_NO_FILE;
$upload_tmp = $_FILES['upload']['tmp_name'] ?? '';
$upload_name = $_FILES['upload']['name'] ?? '';
// Visitors must solve the captcha before a preview or a save
if (!$is_admin && !captcha_check()) {
@@ -206,22 +210,32 @@ function validate_data() {
$validate['story'] = "You've tried to remove an image without replacing it with text.";
}
if (!is_array($_FILES['upload'] ?? array()) || !is_int($upload_error) ||
!is_string($upload_tmp) || !is_string($upload_name)) {
$validate['upload'] = 'The upload is invalid. Please choose the image again.';
return TRUE;
}
if ($upload_error === UPLOAD_ERR_NO_FILE)
return TRUE;
$upload_max = upload_max_bytes();
$too_large = htmlspecialchars((string)($_FILES['upload']['name'] ?? '')) .
$too_large = htmlspecialchars($upload_name) .
($upload_max === NULL ? ' is larger than the server accepts.' : ' is larger than '.upload_max_mb().' MB.') .
' Please choose a smaller image.';
// 0 == no error; 4 == no file; all others are errors.
if ($upload_error === UPLOAD_ERR_INI_SIZE || $upload_error === UPLOAD_ERR_FORM_SIZE) {
$validate['upload'] = $too_large;
} else if ($upload_error > 0 &&
$upload_error !== 4) {
} else if ($upload_error !== UPLOAD_ERR_OK) {
$validate['upload'] = 'There was an error uploading the file ('.$upload_error.')';
} else if ($upload_tmp && $upload_max !== NULL && (int)@filesize($upload_tmp) > $upload_max) {
} else if ($upload_tmp === '' || !is_file($upload_tmp) || !is_readable($upload_tmp)) {
$validate['upload'] = 'The uploaded file is unavailable. Please choose the image again.';
} else if ($upload_max !== NULL && (int)@filesize($upload_tmp) > $upload_max) {
$validate['upload'] = $too_large;
} else if ($upload_tmp) {
$upload = getimagesize($upload_tmp);
} else {
// The file can disappear after the check above, or contain incomplete image data.
$upload = @getimagesize($upload_tmp);
if (!$upload)
$validate['upload'] = htmlspecialchars($_FILES['upload']['name']) .
$validate['upload'] = htmlspecialchars($upload_name) .
" wasn't an image file. Please go back.";
}
return TRUE;