crytic-compile icon indicating copy to clipboard operation
crytic-compile copied to clipboard

ValueError from `filename_lookup` resolving an import path (truffle platform)

Open duckki opened this issue 2 years ago • 3 comments

The CryticCompile.filename_looup crashes when resolving import directives.

Reproducer: test-filename-lookup-error.zip

Contents:

.
└── subdir
    ├── contracts
    │   ├── Context.sol
    │   └── Test.sol
    └── truffle-config.js

Since filename_looup is used only by slither, slither is used to reproduce. Run slither subdir and it crashes with following error:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 743, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 84, in process_all
    ) = process_single(compilation, args, detector_classes, printer_classes)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/__main__.py", line 67, in process_single
    slither = Slither(target, ast_format=ast, **vars(args))
  File "/opt/homebrew/lib/python3.9/site-packages/slither/slither.py", line 97, in __init__
    parser.parse_top_level_from_loaded_json(ast, path)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/solc_parsing/slither_compilation_unit_solc.py", line 224, in parse_top_level_from_loaded_json
    get_imported_scope = self.compilation_unit.get_scope(import_directive.filename)
  File "/opt/homebrew/lib/python3.9/site-packages/slither/core/compilation_unit.py", line 231, in get_scope
    filename = self._crytic_compile_compilation_unit.crytic_compile.filename_lookup(
  File "/opt/homebrew/lib/python3.9/site-packages/crytic_compile/crytic_compile.py", line 199, in filename_lookup
    raise ValueError(
ValueError: contracts/Context.sol does not exist in ['subdir/contracts/Context.sol', 'subdir/contracts/Context.sol', 'subdir/contracts/Test.sol', 'subdir/contracts/Test.sol']

On the other hand, if you run slither under the subdir directory as slither ., slither runs without any crashes.

duckki avatar Mar 04 '22 02:03 duckki

It is intended that you run slither with subdir as the present working directory. Since it works when you do that, what is the bug?

cd subdir
slither .

0xalpharush avatar Mar 04 '22 02:03 0xalpharush

This is a regression in slither 0.8.2. 0.8.1 does not have this issue. Unfortunately, it breaks my use case.

duckki avatar Mar 04 '22 02:03 duckki

Here's my suggested patch:

diff --git a/crytic_compile/crytic_compile.py b/crytic_compile/crytic_compile.py
index cb3506e..67d8527 100644
--- a/crytic_compile/crytic_compile.py
+++ b/crytic_compile/crytic_compile.py
@@ -187,7 +187,7 @@ class CryticCompile:
         """

         if isinstance(self.platform, Truffle) and filename.startswith("project:/"):
-            filename = filename[len("project:/") :]
+            filename = "./" + filename[len("project:/") :]

         if self._filenames_lookup is None:
             self._filenames_lookup = {}

When looking up AST paths like "project:/path/to/file.sol", it will try to match the "used" file path (in the form of ./path/to/file.sol), which should match consistently.

Before the fix, path/to/file.sol matched the "relative" file path, only if the working_dir were ., which is the case of slither ..

duckki avatar Mar 04 '22 02:03 duckki