mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #2155 from ethereum/guard-readcallback
Guard ReadFileCallback exceptions
This commit is contained in:
commit
a9f4215720
@ -79,7 +79,8 @@ class CompilerStack: boost::noncopyable
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Creates a new compiler stack.
|
/// Creates a new compiler stack.
|
||||||
/// @param _readFile callback to used to read files for import statements. Should return
|
/// @param _readFile callback to used to read files for import statements. Must return
|
||||||
|
/// and must not emit exceptions.
|
||||||
explicit CompilerStack(ReadFile::Callback const& _readFile = ReadFile::Callback());
|
explicit CompilerStack(ReadFile::Callback const& _readFile = ReadFile::Callback());
|
||||||
|
|
||||||
/// Sets path remappings in the format "context:prefix=target"
|
/// Sets path remappings in the format "context:prefix=target"
|
||||||
|
@ -38,7 +38,8 @@ class StandardCompiler: boost::noncopyable
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Creates a new StandardCompiler.
|
/// Creates a new StandardCompiler.
|
||||||
/// @param _readFile callback to used to read files for import statements. Should return
|
/// @param _readFile callback to used to read files for import statements. Must return
|
||||||
|
/// and must not emit exceptions.
|
||||||
StandardCompiler(ReadFile::Callback const& _readFile = ReadFile::Callback())
|
StandardCompiler(ReadFile::Callback const& _readFile = ReadFile::Callback())
|
||||||
: m_compilerStack(_readFile), m_readFile(_readFile)
|
: m_compilerStack(_readFile), m_readFile(_readFile)
|
||||||
{
|
{
|
||||||
|
@ -618,32 +618,43 @@ bool CommandLineInterface::processInput()
|
|||||||
{
|
{
|
||||||
ReadFile::Callback fileReader = [this](string const& _path)
|
ReadFile::Callback fileReader = [this](string const& _path)
|
||||||
{
|
{
|
||||||
auto path = boost::filesystem::path(_path);
|
try
|
||||||
auto canonicalPath = boost::filesystem::canonical(path);
|
|
||||||
bool isAllowed = false;
|
|
||||||
for (auto const& allowedDir: m_allowedDirectories)
|
|
||||||
{
|
{
|
||||||
// If dir is a prefix of boostPath, we are fine.
|
auto path = boost::filesystem::path(_path);
|
||||||
if (
|
auto canonicalPath = boost::filesystem::canonical(path);
|
||||||
std::distance(allowedDir.begin(), allowedDir.end()) <= std::distance(canonicalPath.begin(), canonicalPath.end()) &&
|
bool isAllowed = false;
|
||||||
std::equal(allowedDir.begin(), allowedDir.end(), canonicalPath.begin())
|
for (auto const& allowedDir: m_allowedDirectories)
|
||||||
)
|
|
||||||
{
|
{
|
||||||
isAllowed = true;
|
// If dir is a prefix of boostPath, we are fine.
|
||||||
break;
|
if (
|
||||||
|
std::distance(allowedDir.begin(), allowedDir.end()) <= std::distance(canonicalPath.begin(), canonicalPath.end()) &&
|
||||||
|
std::equal(allowedDir.begin(), allowedDir.end(), canonicalPath.begin())
|
||||||
|
)
|
||||||
|
{
|
||||||
|
isAllowed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isAllowed)
|
||||||
|
return ReadFile::Result{false, "File outside of allowed directories."};
|
||||||
|
else if (!boost::filesystem::exists(path))
|
||||||
|
return ReadFile::Result{false, "File not found."};
|
||||||
|
else if (!boost::filesystem::is_regular_file(canonicalPath))
|
||||||
|
return ReadFile::Result{false, "Not a valid file."};
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto contents = dev::contentsString(canonicalPath.string());
|
||||||
|
m_sourceCodes[path.string()] = contents;
|
||||||
|
return ReadFile::Result{true, contents};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isAllowed)
|
catch (Exception const& _exception)
|
||||||
return ReadFile::Result{false, "File outside of allowed directories."};
|
|
||||||
else if (!boost::filesystem::exists(path))
|
|
||||||
return ReadFile::Result{false, "File not found."};
|
|
||||||
else if (!boost::filesystem::is_regular_file(canonicalPath))
|
|
||||||
return ReadFile::Result{false, "Not a valid file."};
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
auto contents = dev::contentsString(canonicalPath.string());
|
return ReadFile::Result{false, "Exception in read callback: " + boost::diagnostic_information(_exception)};
|
||||||
m_sourceCodes[path.string()] = contents;
|
}
|
||||||
return ReadFile::Result{true, contents};
|
catch (...)
|
||||||
|
{
|
||||||
|
return ReadFile::Result{false, "Unknown exception in read callback."};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user