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

Commit

Permalink
Merge pull request #158 from astrodevs-labs/feature/50-solidity-linte…
Browse files Browse the repository at this point in the history
…r-rules/121-avoid-tx-origin-rule-staging

121 - Avoid Tx Origin Rule
  • Loading branch information
0xtekgrinder authored Oct 9, 2023
2 parents 440221c + 1f1db49 commit 4f5394d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
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![],
}
}
}
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": []
}
]
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
avoid-tx-origin:5:15:5:24

0 comments on commit 4f5394d

Please sign in to comment.