This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from astrodevs-labs/feature/50-solidity-linte…
…r-rules/121-avoid-tx-origin-rule-staging 121 - Avoid Tx Origin Rule
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
toolchains/solidity/linter/core/solidhunter-lib/src/rules/security/avoid_tx_origin.rs
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,95 @@ | ||
use crate::linter::SolidFile; | ||
use crate::rules::types::*; | ||
use crate::types::*; | ||
use ast_extractor::*; | ||
|
||
pub const RULE_ID: &str = "avoid-tx-origin"; | ||
const MESSAGE: &str = "Avoid to use tx.origin"; | ||
|
||
struct ExprVisitor { | ||
exprs: Vec<ExprMember>, | ||
} | ||
|
||
impl ExprVisitor { | ||
pub fn new() -> Self { | ||
Self { | ||
exprs: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'ast> Visit<'ast> for ExprVisitor { | ||
fn visit_expr_member(&mut self,i: &'ast ExprMember) { | ||
self.exprs.push(i.clone()); | ||
visit::visit_expr_member(self, i); | ||
} | ||
} | ||
|
||
pub struct AvoidTxOrigin { | ||
data: RuleEntry, | ||
} | ||
|
||
impl AvoidTxOrigin { | ||
fn create_diag( | ||
&self, | ||
location: (LineColumn, LineColumn), | ||
file: &SolidFile, | ||
) -> LintDiag { | ||
LintDiag { | ||
id: RULE_ID.to_string(), | ||
range: Range { | ||
start: Position { | ||
line: location.0.line, | ||
character: location.0.column, | ||
}, | ||
end: Position { | ||
line: location.1.line, | ||
character: location.1.column, | ||
}, | ||
}, | ||
message: MESSAGE.to_string(), | ||
severity: Some(self.data.severity), | ||
code: None, | ||
source: None, | ||
uri: file.path.clone(), | ||
source_file_content: file.content.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl RuleType for AvoidTxOrigin { | ||
fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> { | ||
let mut res = Vec::new(); | ||
let mut visitor = ExprVisitor::new(); | ||
for contract in ast_extractor::retriever::retrieve_contract_nodes(&file.data) { | ||
visitor.visit_item_contract(&contract); | ||
} | ||
|
||
for expr in visitor.exprs { | ||
if let Expr::Ident(ident) = &*expr.expr { | ||
if let Expr::Ident(ident2) = &*expr.member { | ||
if ident == "tx" && ident2 == "origin" { | ||
let location = (expr.span().start(), expr.span().end()); | ||
res.push(self.create_diag(location, file)); | ||
} | ||
} | ||
} | ||
} | ||
res | ||
} | ||
} | ||
|
||
impl AvoidTxOrigin { | ||
pub(crate) fn create(data: RuleEntry) -> Box<dyn RuleType> { | ||
let rule = AvoidTxOrigin { data }; | ||
Box::new(rule) | ||
} | ||
|
||
pub(crate) fn create_default() -> RuleEntry { | ||
RuleEntry { | ||
id: RULE_ID.to_string(), | ||
severity: Severity::WARNING, | ||
data: vec![], | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
toolchains/solidity/linter/core/solidhunter-lib/testdata/AvoidTxOrigin/.solidhunter.json
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,12 @@ | ||
{ | ||
"name": "solidhunter", | ||
"includes": [], | ||
"plugins": [], | ||
"rules": [ | ||
{ | ||
"id": "avoid-tx-origin", | ||
"severity": "WARNING", | ||
"data": [] | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
toolchains/solidity/linter/core/solidhunter-lib/testdata/AvoidTxOrigin/file.sol
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,10 @@ | ||
pragma solidity 0.8.0; | ||
|
||
contract Test { | ||
function awesome() public returns (address) { | ||
return tx.origin; | ||
} | ||
function notAwesome() public returns (address) { | ||
return msg.sender; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
toolchains/solidity/linter/core/solidhunter-lib/testdata/AvoidTxOrigin/findings.csv
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 @@ | ||
avoid-tx-origin:5:15:5:24 |