Skip to content

Conversation

@barnasm1
Copy link
Contributor

Details:

  • add path safety checking
  • unify error messages
  • remove double quote at error messages

Tickets:

@barnasm1 barnasm1 self-assigned this Aug 20, 2025
@barnasm1 barnasm1 requested a review from a team as a code owner August 20, 2025 14:54
@barnasm1 barnasm1 requested review from mryzhov and removed request for a team August 20, 2025 14:54
@github-actions github-actions bot added the category: Core OpenVINO Core (aka ngraph) label Aug 20, 2025
@barnasm1 barnasm1 requested a review from a team as a code owner August 20, 2025 16:23
@mlukasze mlukasze added this to the 2025.3 milestone Aug 21, 2025
}
}

const std::filesystem::path check_path_safety(const std::filesystem::path& path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-use update current utils functions:
Use ov::util::sanitize_path which should remove traversal part from path.

There are also utils like in std like canonical, weakly_canonical

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1244 to 1245

return check_path_safety(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return check_path_safety(path);
OPENVINO_ASSERT(!std::filesystem::is_symlink(path), "Path must not be symbolic link: " , path);
return ov::util::sanitize(path);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto path = xml_path;
path.replace_extension(".bin");
return path;
return check_path_safety(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The xml path should be validated and bin path can be created without check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return check_path_safety(path);
} else {
return bin_path;
return check_path_safety(bin_path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if not symlink and use sanitize path to remove traversal part.

Update doxy for class to mention that traversal part from path will be removed and symlinks are not allowed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@barnasm1 barnasm1 requested a review from a team as a code owner August 21, 2025 11:16
@github-actions github-actions bot added category: transformations OpenVINO Runtime library - Transformations category: CPP API OpenVINO CPP API bindings labels Aug 21, 2025
Comment on lines 126 to 127
string path = "a/b/../../../../tensor.data";
EXPECT_STREQ("a/b/tensor.data", ov::util::prevent_path_traversal(path).string().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .. is skipped over (ignored)? Shouldn't it end up as ../../tensor.data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent traversal into parent directories

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposed solution changes the path in fact. Is it what user expects (is aware of)?
Example path from another test a/b/../tensor.data doesn't leave working directory and should end up as a/tensor.data.
Generally I don't think OV should bother where a user wants its files to be stored in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jszczepa Could you please share the motivation for preventing path traversal

path);

return safe_path;
OPENVINO_ASSERT(std::filesystem::is_symlink(path) == false, "Path must not refer to a symbolic link: ", path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OPENVINO_ASSERT(std::filesystem::is_symlink(path) == false, "Path must not refer to a symbolic link: ", path);
OPENVINO_ASSERT(!std::filesystem::is_symlink(path), "Path must not refer to a symbolic link: ", path);

return (start == std::string::npos) ? "" : sanitized_path.substr(start);
}

const std::filesystem::path ov::util::check_path_safety(const std::filesystem::path& path) {
Copy link
Contributor

@praasz praasz Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this function simple like:

bool is_path_traversal(const std::filesystem::path& path){
    return std::find(path.begin(), path.end(), "..") != path.end();
}

#include <fstream>
#include <sstream>

#include "openvino/core/except.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not part of util library

/// \return A sanitized path
std::string sanitize_path(const std::string& path);

/// \brief Check the safety of a file path by expecting no parent directory references ("..") and no symbolic links
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should current dir . be checked, also?

* @brief Serialize transformation converts ov::Model into IR files
* @attention
* - dynamic shapes are not supported
* - any parent directory traversal (e.g. ".." in path) will be removed from output file paths
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be updated

/// references.
/// \param path A path to file
/// \return A validated path to file
const std::filesystem::path check_path_safety(const std::filesystem::path& path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for this function ?
Add some Unicode examples?
Why return path form function which check only?

@praasz praasz removed the Code Freeze label Sep 1, 2025
path,
"\"");
return path;
"Path for xml file doesn't contains file name with 'xml' extension: ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider change this helper to:

void validate_ir_path(const std::filesystem::path& path, std::filesystem::path&& ext){
   OPENVINO_ASSERT(path.extension() == ext, "Path ",  path, " doesn't contains file name with ", ext,  " extension");
   OPENVINO_ASSERT(!is_path_traversal(path), "Path has traversal component: ", path);
   OPENVINO_ASSERT(!std::filesystem::is_symlink(path), "Path is symlink ", path);
}

And use for xml, bin file validation, and additional check can be added here if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Core OpenVINO Core (aka ngraph) category: CPP API OpenVINO CPP API bindings category: transformations OpenVINO Runtime library - Transformations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants