-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Enhance filename validation #23386
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
base: master
Are you sure you want to change the base?
Enhance filename validation #23386
Conversation
src/base/bittorrent/sessionimpl.cpp
Outdated
| else | ||
| { | ||
| path = Utils::Fs::toValidPath(categoryName); | ||
| path = Path(Utils::Fs::toValidFileName(categoryName)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, the category name can represent a path, so you seem to have broken it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldnt fault itbwhen I was testing. I also tested subcategories. Can u give me an example scenario that would break it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldnt fault itbwhen I was testing. I also tested subcategories. Can u give me an example scenario that would break it?
Did you test it with "flat" categories (i.e. when subcategories is disabled)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it with both flat categories and subcategories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it with both flat categories and subcategories.
Name like some/category/name is supposed to represent corresponding path some/category/name. Doesn't toValidFileName() replace slashes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok give me a few days and ill see what I can do.
Are you ok with Path.isvalid() being moved to fs utils and reworked too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you ok with Path.isvalid() being moved to fs utils and reworked too ?
Ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions implemented as discussed.
|
1st commit title has typo: Should be written |
Co-authored-by: Vladimir Golovnev <[email protected]>
|
@Chocobo1 @glassez , ready for review. Additional Notes:
|
| const QString newFileName = QFileInfo(params()[u"newPath"_s]).fileName(); | ||
| if (!Utils::Fs::isValidName(newFileName)) | ||
| throw APIError(APIErrorType::Conflict, tr("File name has invalid characters")); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove it? It should be checked anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Becuase it's a redundant check now. It is already checked downstream via BitTorrent::AbstractFileStorage::renameFile. Now that Path.isValid() is using the new logic we not longer need to check it via Utils::Fs::isValidName in torrentscontroller.cpp.
The error checking is also better via BitTorrent::AbstractFileStorage::renameFile as it is more descriptive (it specifies if the oldpath or newpath is invalid instead of just the whole thing)
Process Flow:
TorrentsController::renameFileAction() -> torrent->renameFile -> BitTorrent::AbstractFileStorage::renameFile -> oldPath.isValid()
void BitTorrent::AbstractFileStorage::renameFile(const Path &oldPath, const Path &newPath)
{
if (!oldPath.isValid())
throw RuntimeError(tr("The old path is invalid: '%1'.").arg(oldPath.toString()));
if (!newPath.isValid())
throw RuntimeError(tr("The new path is invalid: '%1'.").arg(newPath.toString()));
if (newPath.isAbsolute())
throw RuntimeError(tr("Absolute path isn't allowed: '%1'.").arg(newPath.toString()));
My previous comment (for reference):
For the commit "Remove Utils::Fs::isValidFileName from TorrentsController::renameFileAction",
the functions below invoke "torrent->renameFile", which now performs validation via the
new "Path.isValid()". The prior "isValidName" checks in torrentcontentmodel.cpp are therefore
redundant and have been removed.
src/gui/torrentcontentmodel.cpp
..
TorrentsController::renameFileAction
TorrentsController::renameFolderAction
| const QString newFolderName = QFileInfo(params()[u"newPath"_s]).fileName(); | ||
| if (!Utils::Fs::isValidName(newFolderName)) | ||
| throw APIError(APIErrorType::Conflict, tr("Folder name has invalid characters")); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too.
| const QRegularExpression regex {u"\\0"_s}; | ||
| #endif | ||
| return !m_pathStr.contains(regex); | ||
| return Utils::Fs::isValidPath(*this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to me that this change completely messed up Path class design.
I cannot understand the changes anymore. I'll stop here until things clears up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was agreed in the first review on this PR with glassez:
The thinking was to consolodate all file validation logic to FS Utils. This also has the benefit of allowing all related functions to share validation logic via the unamed namespace in fs.cpp.
I could just remove path.isValid() entirely from path.h and path.cpp and then change whatever was calling that to call Utils::Fs::isValidPath instead if preffered? *
* The reason I didn't do this initially is that:
- It is more changes
- I figured it would be good to keep the path.isValid method in path.h\path.cpp so that people can see it is available just by looking at path.h\path.cpp.
Can you expand on what you mean by "this change completely messed up Path class design" ?

Enhance filename validation
This PR extends [#23060] and implements the following key improvements:
isValidFileNameisValidPathto use new logictoValidFileNameto use new logictoValidPathto use new logicTorrent Creatorto use new logicTesting
Tested: Windows 11 + Linux Mint 22.1
Fixes: #22426 #21899 #16906 #16834 #16271 #11276 #9085 #11340 #14728 #20412 #20337 #17049 #17469 #15378 #15227 #14908 #20742