Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
feat(solidity/linter): load solidhunter ignore by default and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ByFishh committed Nov 2, 2023
1 parent 76ffe63 commit 2605481
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
1 change: 0 additions & 1 deletion remove-me-bfa9fe2ae6eb4793aaed.txt

This file was deleted.

24 changes: 15 additions & 9 deletions toolchains/solidity/core/crates/linter-lib/src/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use std::path::Path;
use glob::glob;
use crate::errors::SolidHunterError;

fn parse_line (line: &str) -> Vec<String> {
fn parse_line(line: &str, path: &Path) -> Vec<String> {
let mut files = Vec::new();

if let Ok(entries) = glob(line) {
for entry in entries.flatten() {
files.push(entry.into_os_string().into_string().unwrap())
let line = line.replace("./", "");
if let Some(parent) = path.parent() {
if let Some(filepath) = parent.join(line).to_str() {
if let Ok(entries) = glob(filepath) {
for entry in entries.flatten() {
files.push(entry.into_os_string().into_string().unwrap())
}
}
}
}

files
}

pub fn get_ignored_files (filepath: &str) -> Result<Vec<String>, SolidHunterError> {
pub fn get_ignored_files(filepath: &str) -> Result<Vec<String>, SolidHunterError> {
let mut ignored_files = Vec::new();
let path = Path::new(filepath);

if !std::path::Path::new(filepath).is_file() {
if !path.is_file() {
return Ok(ignored_files);
}

let file = std::fs::read_to_string(filepath)?;
let file = std::fs::read_to_string(path)?;

for line in file.lines() {
ignored_files.append(&mut parse_line(line))
ignored_files.append(&mut parse_line(line, path))
}
Ok(ignored_files)
}
10 changes: 7 additions & 3 deletions toolchains/solidity/core/crates/linter-lib/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl SolidLinter {
Ok(())
}

pub fn initialize_ignore_file(&mut self, filepath: &str) ->Result<(), SolidHunterError> {
pub fn initialize_ignore_file(&mut self, filepath: &str) -> Result<(), SolidHunterError> {
let ignored_files = get_ignored_files(filepath)?;
for file in ignored_files {
self.ignored_files.push(file.to_string())
Expand Down Expand Up @@ -104,7 +104,9 @@ impl SolidLinter {
}

pub fn parse_file(&mut self, filepath: String) -> LintResult {
println!("Parsing content: {}", filepath);
if self.ignored_files.contains(&filepath) {
return Ok(Vec::new());
}
let content = fs::read_to_string(filepath.clone())?;
let res = osmium_libs_solidity_ast_extractor::extract::extract_ast_from_content(&content)?;

Expand Down Expand Up @@ -135,7 +137,9 @@ impl SolidLinter {
let mut result: Vec<LintResult> = Vec::new();
if let Ok(entries) = glob(&(folder + "/**/*.sol")) {
for entry in entries.flatten() {
result.push(self.parse_file(entry.into_os_string().into_string().unwrap()));
if !self.ignored_files.contains(&entry.clone().into_os_string().into_string().unwrap()) {
result.push(self.parse_file(entry.into_os_string().into_string().unwrap()));
}
}
}
result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "solidhunter", "rules": [
{
"id": "avoid-tx-origin",
"severity": "WARNING"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma solidity 0.8.0;

contract Test {
function awesome() public returns (address) {
return tx.origin;
}
function notAwesome() public returns (address) {
return msg.sender;
}
}
9 changes: 7 additions & 2 deletions toolchains/solidity/core/crates/linter-lib/tests/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Finding {
fn test_directory(base_name: &str) {
let mut source = String::new();
let mut config = String::new();
let mut ignore = String::new();
let mut expected_findings: Vec<Finding> = Vec::new();

for path in fs::read_dir(
Expand All @@ -27,6 +28,8 @@ fn test_directory(base_name: &str) {
source = path.to_str().unwrap().to_string();
} else if filename == ".solidhunter.json" {
config = path.to_str().unwrap().to_string();
} else if filename == ".solidhunterignore" {
ignore = path.to_str().unwrap().to_string();
} else if filename == "findings.csv" {
for line in fs::read_to_string(path).unwrap().lines() {
let splitted_line: Vec<&str> = line.split(':').collect();
Expand All @@ -46,12 +49,13 @@ fn test_directory(base_name: &str) {
}
}

test_linter(&config, &source, &expected_findings);
test_linter(&config, &source, &expected_findings, &ignore );
}

fn test_linter(config: &str, source: &str, expected_findings: &Vec<Finding>) {
fn test_linter(config: &str, source: &str, expected_findings: &Vec<Finding>, ignore: &str) {
let mut linter: SolidLinter = SolidLinter::new();
let _ = linter.initialize_rules(&String::from(config));
let _ = linter.initialize_ignore_file(&String::from(ignore));

let result = linter.parse_file(String::from(source));
let mut found_findings: Vec<&Finding> = Vec::new();
Expand Down Expand Up @@ -172,4 +176,5 @@ test_directories! {
PrivateVarsLeadingUnderscore,
FoundryTestFunctions,
AvoidTxOrigin,
SolidhunterIgnore
}

0 comments on commit 2605481

Please sign in to comment.