-
Notifications
You must be signed in to change notification settings - Fork 251
Fix allowed_local_media_path psirt #4199
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
Open
michalkulakowski
wants to merge
10
commits into
main
Choose a base branch
from
mkulakow/allowed_local_media_path_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24d622b
Fix allowed_local_media_path psirt
michalkulakowski 2b22550
style
michalkulakowski e842c78
fix
michalkulakowski 45b155f
fix
f60d673
fix
5ce0bfa
fix
5798544
Fix UTs on Windows
46b9394
fix
25addf1
fix
cb42017
fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <filesystem> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <sstream> | ||
|
|
@@ -46,6 +47,48 @@ namespace ovms { | |
|
|
||
| constexpr size_t DEFAULT_MAX_STOP_WORDS = 16; // same as deep-seek | ||
|
|
||
| namespace { | ||
|
|
||
| std::string normalizePathSeparators(const std::string& path) { | ||
| std::string normalizedPath = path; | ||
| std::replace(normalizedPath.begin(), normalizedPath.end(), '\\', '/'); | ||
| return normalizedPath; | ||
| } | ||
|
|
||
| std::string normalizeAllowedLocalMediaPath(const std::string& allowedLocalMediaPath) { | ||
| std::string normalizedAllowedLocalMediaPath = normalizePathSeparators(allowedLocalMediaPath); | ||
| if (normalizedAllowedLocalMediaPath.empty()) { | ||
| return normalizedAllowedLocalMediaPath; | ||
| } | ||
| if (normalizedAllowedLocalMediaPath.back() == '/') { | ||
| return normalizedAllowedLocalMediaPath; | ||
| } | ||
| return normalizedAllowedLocalMediaPath + '/'; | ||
| } | ||
|
|
||
| std::filesystem::path normalizeAndResolvePath(const std::string& pathString) { | ||
| std::filesystem::path path = normalizePathSeparators(pathString); | ||
| if (path.is_relative()) { | ||
| path = std::filesystem::current_path() / path; | ||
| } | ||
| path = path.lexically_normal(); | ||
| std::error_code ec; | ||
| auto weakCanonicalPath = std::filesystem::weakly_canonical(path, ec); | ||
| if (!ec) { | ||
| return weakCanonicalPath.lexically_normal(); | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| bool isPathInsideDirectory(const std::filesystem::path& testedPath, const std::filesystem::path& allowedDirectory) { | ||
| const auto mismatch = std::mismatch( | ||
| allowedDirectory.begin(), allowedDirectory.end(), | ||
| testedPath.begin(), testedPath.end()); | ||
| return mismatch.first == allowedDirectory.end(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ov::genai::JsonContainer rapidJsonValueToJsonContainer(const rapidjson::Value& value) { | ||
| if (value.IsNull()) { | ||
| return ov::genai::JsonContainer(nullptr); | ||
|
|
@@ -234,8 +277,14 @@ absl::StatusOr<ov::Tensor> loadImage(const std::string& imageSource, | |
| return absl::InvalidArgumentError(ss.str()); | ||
| } | ||
| SPDLOG_LOGGER_TRACE(llm_calculator_logger, "Loading image from local filesystem"); | ||
| const auto firstMissmatch = std::mismatch(imageSource.begin(), imageSource.end(), allowedLocalMediaPath.value().begin(), allowedLocalMediaPath.value().end()); | ||
| if (firstMissmatch.second != allowedLocalMediaPath.value().end()) { | ||
| const auto normalizedAllowedLocalMediaPath = normalizeAllowedLocalMediaPath(allowedLocalMediaPath.value()); | ||
| const auto imagePath = std::filesystem::path(normalizePathSeparators(imageSource)); | ||
| if (imagePath.is_relative()) { | ||
| return absl::InvalidArgumentError("Relative paths are not allowed for local filesystem access"); | ||
| } | ||
| const auto resolvedAllowedPath = normalizeAndResolvePath(normalizedAllowedLocalMediaPath); | ||
| const auto resolvedImagePath = normalizeAndResolvePath(imageSource); | ||
| if (!isPathInsideDirectory(resolvedImagePath, resolvedAllowedPath)) { | ||
| return absl::InvalidArgumentError("Given filepath is not subpath of allowed_local_media_path"); | ||
|
Comment on lines
+280
to
288
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. |
||
| } | ||
|
Comment on lines
+280
to
289
|
||
| try { | ||
|
|
||
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
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
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
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.
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.
Good point.