forked from Cyfrin/aderyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,552 additions
and
815 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod constants_instead_of_literals; | ||
pub mod require_with_string; | ||
pub mod unindexed_events; | ||
pub mod useless_public_function; | ||
pub mod zero_address_check; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::error::Error; | ||
|
||
use crate::{ | ||
ast::Identifier, | ||
context::loader::{ASTNode, ContextLoader}, | ||
detector::detector::{Detector, IssueSeverity}, | ||
}; | ||
use eyre::Result; | ||
|
||
#[derive(Default)] | ||
pub struct RequireWithStringDetector { | ||
found_require_without_string: Vec<Option<ASTNode>>, | ||
} | ||
|
||
impl Detector for RequireWithStringDetector { | ||
fn detect(&mut self, loader: &ContextLoader) -> Result<bool, Box<dyn Error>> { | ||
// Collect all require statements without a string literal. | ||
let requires_and_reverts: Vec<&Identifier> = loader | ||
.get_identifiers() | ||
.iter() | ||
.filter(|id| id.name == "revert" || id.name == "require") | ||
.cloned() | ||
.collect(); | ||
|
||
for id in requires_and_reverts { | ||
if id.name == "revert" && id.argument_types.as_ref().unwrap().len() == 0 { | ||
self.found_require_without_string | ||
.push(Some(ASTNode::Identifier(id.clone()))); | ||
} else if id.name == "require" && id.argument_types.as_ref().unwrap().len() == 1 { | ||
self.found_require_without_string | ||
.push(Some(ASTNode::Identifier(id.clone()))); | ||
} | ||
} | ||
|
||
Ok(!self.found_require_without_string.is_empty()) | ||
} | ||
|
||
fn title(&self) -> String { | ||
String::from("`require()` / `revert()` statements should have descriptive reason strings or custom errors") | ||
} | ||
|
||
fn description(&self) -> String { | ||
String::from("") | ||
} | ||
|
||
fn severity(&self) -> IssueSeverity { | ||
IssueSeverity::NC | ||
} | ||
|
||
fn instances(&self) -> Vec<Option<ASTNode>> { | ||
self.found_require_without_string.clone() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod require_with_string_tests { | ||
use crate::detector::detector::{detector_test_helpers::load_contract, Detector}; | ||
|
||
use super::RequireWithStringDetector; | ||
|
||
#[test] | ||
fn test_require_with_string() { | ||
let context_loader = load_contract( | ||
"./tests/contract-playground/out/DeprecatedOZFunctions.sol/DeprecatedOZFunctions.json", | ||
); | ||
let mut detector = RequireWithStringDetector::default(); | ||
// assert that the detector finds something | ||
let found = detector.detect(&context_loader).unwrap(); | ||
assert!(found); | ||
// assert that the detector returns the correct number of instances | ||
assert_eq!(detector.instances().len(), 2); | ||
// assert that the detector returns the correct severity | ||
assert_eq!( | ||
detector.severity(), | ||
crate::detector::detector::IssueSeverity::NC | ||
); | ||
// assert that the detector returns the correct title | ||
assert_eq!( | ||
detector.title(), | ||
String::from("`require()` / `revert()` statements should have descriptive reason strings or custom errors") | ||
); | ||
// assert that the detector returns the correct description | ||
assert_eq!(detector.description(), String::from("")); | ||
} | ||
} |
Oops, something went wrong.