From 1585ac78f99357cbbc5041026af1c6c585b728e4 Mon Sep 17 00:00:00 2001 From: Mavridis Philippe Date: Mon, 10 Jun 2024 11:58:46 +0300 Subject: [PATCH] KEditFileType: prevent inadvertent editing of application/octet-stream The default fallback mime type is not something that should be edited by the user except when explicitly requested. A confirmation is shown now before allowing the user to edit this mime type. Also, since KMimeType::mimeType(...) does NOT return a null value but the default mime type, the check for incorrect mime type failed, since it expected a null return value, and the default fallback mime type was used instead, making the user inadvertedly edit the default mime type. Incorrect mime types are now promptly recognized, and an error message is shown to the user in such cases. Signed-off-by: Mavridis Philippe --- kcontrol/filetypes/keditfiletype.cpp | 45 ++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/kcontrol/filetypes/keditfiletype.cpp b/kcontrol/filetypes/keditfiletype.cpp index 8c6c06931..924782cf5 100644 --- a/kcontrol/filetypes/keditfiletype.cpp +++ b/kcontrol/filetypes/keditfiletype.cpp @@ -28,6 +28,9 @@ #include #include #include +#include + +#include #ifdef TQ_WS_X11 #include @@ -131,7 +134,7 @@ int main(int argc, char ** argv) bool createType = arg.startsWith( "*" ); KMimeType::Ptr mime; - + if ( createType ) { TQString mimeString = "application/x-kdeuser%1"; TQString loc; @@ -152,10 +155,40 @@ int main(int argc, char ** argv) } mime = new KMimeType( loc, mimeString.arg( inc ), TQString::null, comment, patterns ); } - else { - mime = KMimeType::mimeType( arg ); - if (!mime) - kdFatal() << "Mimetype " << arg << " not found" << endl; + else { + // The user has explicitly requested application/octet-stream + if (arg == KMimeType::defaultMimeType()) + { + int res = KMessageBox::warningYesNo(nullptr, i18n( + "You are about to edit the fallback filetype for unknown/binary files." + "

This is not something you should normally do unless you know exactly" + " what you are doing." + "
Incautious editing of this file type may cause unexpected behaviour of" + " your system." + "

Are you sure you want to continue?
" + )); + + if (res != KMessageBox::Yes) + { + kdWarning() << "Editing of default fallback mime type prevented." << endl; + ::exit(0); + } + + mime = KMimeType::defaultMimeTypePtr(); + } + + // The user has NOT explicitly requested it, so if KMimeType::mimeType(...) + // returns it, then it's safe to assume that the mimetype is incorrect. + else + { + mime = KMimeType::mimeType( arg ); + if (mime == KMimeType::defaultMimeTypePtr()) + { + KMessageBox::error(nullptr, i18n("Mimetype %1 not found!").arg(arg)); + kdError() << "Mimetype " << arg << " not found" << endl; + ::exit(1); + } + } } FileTypeDialog dlg( mime, createType ); @@ -169,7 +202,7 @@ int main(int argc, char ** argv) #endif args->clear(); if ( !createType ) - dlg.setCaption( i18n("Edit File Type %1").arg(mime->name()) ); + dlg.setCaption( i18n("Edit File Type %1").arg(mime->name()) ); else { dlg.setCaption( i18n("Create New File Type %1").arg(mime->name()) ); dlg.enableButton( KDialogBase::Apply, true );