Prevent malformed upload path causing arbitrary write#1170
Closed
marty1885 wants to merge 4 commits intodrogonframework:masterfrom
Closed
Prevent malformed upload path causing arbitrary write#1170marty1885 wants to merge 4 commits intodrogonframework:masterfrom
marty1885 wants to merge 4 commits intodrogonframework:masterfrom
Conversation
Kirill89
reviewed
Jan 30, 2022
| fsFileName = fsUploadPath / fsFileName; | ||
| } | ||
| fsFileName = fsFileName.lexically_normal(); | ||
| if (fsFileName.is_relative()) |
Contributor
There was a problem hiding this comment.
Unfortunately it is not enough to check if path is not relative. In this case if filename_ will be /malicious-file user still will be able to upload outside of uploads folder.
Can I suggest to do something like:
int HttpFileImpl::save(const std::string &path) const
{
assert(!path.empty());
if (fileName_.empty())
return -1;
filesystem::path fsPath(utils::toNativePath(path));
if (!fsPath.is_absolute() &&
(!fsPath.has_parent_path() ||
(fsPath.begin()->string() != "." && fsPath.begin()->string() != "..")))
{
filesystem::path fsUploadPath(utils::toNativePath(
HttpAppFrameworkImpl::instance().getUploadPath()));
fsPath = fsUploadPath / fsPath;
}
fsPath += "/";
fsPath = fsPath.lexically_normal();
auto fsSaveToPath = filesystem::path(fsPath);
fsSaveToPath += filesystem::path(utils::toNativePath(fileName_));
fsSaveToPath = fsSaveToPath.lexically_normal();
if (fsSaveToPath.string().find(fsPath.string(), 0) != 0)
{
LOG_ERROR
<< "Attempt writing outside of upload directory detected. Path: "
<< fileName;
return -1;
}
// ...
return saveTo(fsSaveToPath);
- You need to clean all multiple slashes and dot segments –
lexically_normal. - Concatenate path with
std::filesystem::path::concat(+= operator) instead ofstd::filesystem::path::append(/ operator) because if the second argument ofappendis an absolute path it will resolve the whole path to the second argument. - Call
lexically_normalagain to avoid//after concat. - And then you need to check if final upload path starts with the uploads folder path.
Alternatively you can drop all .. and / symbols from the fileName_.
Member
There was a problem hiding this comment.
@Kirill89 I am very grateful for your advice. we'll fix this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR prevents a malformed upload path causing the upload function to write outside of the destination directory by checking the upload path.
TBH I'm not 100% sure my patch fixes everything and didn't break unintentionally. Need some review.