Handle allowing empty paths correctly

This commit is contained in:
Kamil Śliwak 2021-07-24 01:30:17 +02:00
parent 3ac3612767
commit 479ba7c523
5 changed files with 27 additions and 16 deletions

View File

@ -33,11 +33,29 @@ using std::string;
namespace solidity::frontend
{
FileReader::FileReader(
boost::filesystem::path _basePath,
FileSystemPathSet _allowedDirectories
):
m_allowedDirectories(std::move(_allowedDirectories)),
m_sourceCodes()
{
setBasePath(_basePath);
for (boost::filesystem::path const& allowedDir: m_allowedDirectories)
solAssert(!allowedDir.empty(), "");
}
void FileReader::setBasePath(boost::filesystem::path const& _path)
{
m_basePath = (_path.empty() ? "" : normalizeCLIPathForVFS(_path));
}
void FileReader::allowDirectory(boost::filesystem::path _path)
{
solAssert(!_path.empty(), "");
m_allowedDirectories.insert(std::move(_path));
}
void FileReader::setSource(boost::filesystem::path const& _path, SourceCode _source)
{
boost::filesystem::path normalizedPath = normalizeCLIPathForVFS(_path);

View File

@ -46,20 +46,12 @@ public:
/// Constructs a FileReader with a base path and a set of allowed directories that
/// will be used when requesting files from this file reader instance.
explicit FileReader(
boost::filesystem::path _basePath = {},
FileSystemPathSet _allowedDirectories = {}
):
m_allowedDirectories(std::move(_allowedDirectories)),
m_sourceCodes()
{
setBasePath(_basePath);
}
explicit FileReader(boost::filesystem::path _basePath = {}, FileSystemPathSet _allowedDirectories = {});
void setBasePath(boost::filesystem::path const& _path);
boost::filesystem::path const& basePath() const noexcept { return m_basePath; }
void allowDirectory(boost::filesystem::path _path) { m_allowedDirectories.insert(std::move(_path)); }
void allowDirectory(boost::filesystem::path _path);
FileSystemPathSet const& allowedDirectories() const noexcept { return m_allowedDirectories; }
StringMap const& sourceCodes() const noexcept { return m_sourceCodes; }

View File

@ -358,7 +358,7 @@ bool CommandLineParser::parseInputPathsAndRemappings()
boost::filesystem::path remappingDir = remapping->target;
remappingDir.remove_filename();
m_options.input.allowedDirectories.insert(remappingDir);
m_options.input.allowedDirectories.insert(remappingDir.empty() ? "." : remappingDir);
m_options.input.remappings.emplace_back(move(remapping.value()));
}
@ -979,7 +979,8 @@ bool CommandLineParser::processArgs()
{
vector<string> paths;
for (string const& allowedPath: boost::split(paths, m_args[g_strAllowPaths].as<string>(), boost::is_any_of(",")))
m_options.input.allowedDirectories.insert(allowedPath);
if (!allowedPath.empty())
m_options.input.allowedDirectories.insert(allowedPath);
}
if (m_args.count(g_strStopAfter))

View File

@ -288,8 +288,8 @@ BOOST_FIXTURE_TEST_CASE(allow_path_should_handle_empty_paths, AllowPathsFixture)
BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "x,,y"}) == ImportCheck::PathDisallowed());
// Work dir is not base path
BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"}));
BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"}));
BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"}) == ImportCheck::PathDisallowed());
BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"}) == ImportCheck::PathDisallowed());
BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"}));
BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"}));
}

View File

@ -168,7 +168,7 @@ BOOST_AUTO_TEST_CASE(cli_mode_options)
expectedOptions.input.addStdin = true;
expectedOptions.input.basePath = "/home/user/";
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"};
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", ".", "c", "/usr/lib"};
expectedOptions.input.ignoreMissingFiles = true;
expectedOptions.input.errorRecovery = (inputMode == InputMode::Compiler);
expectedOptions.output.dir = "/tmp/out";
@ -307,7 +307,7 @@ BOOST_AUTO_TEST_CASE(assembly_mode_options)
};
expectedOptions.input.addStdin = true;
expectedOptions.input.basePath = "/home/user/";
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"};
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", ".", "c", "/usr/lib"};
expectedOptions.input.ignoreMissingFiles = true;
expectedOptions.output.overwriteFiles = true;
expectedOptions.output.evmVersion = EVMVersion::spuriousDragon();