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.
feat(solidity/linter/core): StateVisibility rule
- Loading branch information
1 parent
5f66ff7
commit f3cf5fe
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
toolchains/solidity/linter/core/solidhunter-lib/src/rules/security/state_visibility.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,73 @@ | ||
use crate::linter::SolidFile; | ||
use crate::rules::types::*; | ||
use crate::types::*; | ||
use ast_extractor::*; | ||
|
||
pub const RULE_ID: &str = "state-visibility"; | ||
const MESSAGE: &str = "Explicitly mark visibility of state"; | ||
|
||
pub struct StateVisibility { | ||
data: RuleEntry, | ||
} | ||
|
||
impl StateVisibility { | ||
fn create_diag( | ||
&self, | ||
location: (ast_extractor::LineColumn, ast_extractor::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 StateVisibility { | ||
fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> { | ||
let mut res = Vec::new(); | ||
let contracts = retriever::retrieve_contract_nodes(&file.data); | ||
|
||
for contract in contracts.iter() { | ||
for node_var in contract.body.iter() { | ||
if let Item::Variable(var) = node_var { | ||
if var.attributes.visibility().is_none() { | ||
let span = var.name.span(); | ||
res.push(self.create_diag((span.start(), span.end()), file)); | ||
} | ||
} | ||
} | ||
} | ||
res | ||
} | ||
} | ||
|
||
impl StateVisibility { | ||
pub(crate) fn create(data: RuleEntry) -> Box<dyn RuleType> { | ||
let rule = StateVisibility { data }; | ||
Box::new(rule) | ||
} | ||
|
||
pub(crate) fn create_default() -> RuleEntry { | ||
RuleEntry { | ||
id: RULE_ID.to_string(), | ||
severity: Severity::WARNING, | ||
data: vec![], | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
toolchains/solidity/linter/core/solidhunter-lib/testdata/StateVisibility/.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,72 @@ | ||
{ | ||
"name": "solidhunter", | ||
"includes": [], | ||
"plugins": [], | ||
"rules": [ | ||
{ | ||
"id": "line-max-len", | ||
"severity": "WARNING", | ||
"data": [ | ||
"80" | ||
] | ||
}, | ||
{ | ||
"id": "max-states-count", | ||
"severity": "WARNING", | ||
"data": [ | ||
"15" | ||
] | ||
}, | ||
{ | ||
"id": "function-max-lines", | ||
"severity": "WARNING", | ||
"data": [ | ||
"20" | ||
] | ||
}, | ||
{ | ||
"id": "reason-string", | ||
"severity": "WARNING", | ||
"data": [ | ||
"32" | ||
] | ||
}, | ||
{ | ||
"id": "contract-name-pascalcase", | ||
"severity": "WARNING", | ||
"data": [] | ||
}, | ||
{ | ||
"id": "func-name-camelcase", | ||
"severity": "WARNING", | ||
"data": [] | ||
}, | ||
{ | ||
"id": "func-param-name-camelcase", | ||
"severity": "WARNING", | ||
"data": [] | ||
}, | ||
{ | ||
"id": "use-forbidden-name", | ||
"severity": "WARNING", | ||
"data": [] | ||
}, | ||
{ | ||
"id": "import-on-top", | ||
"severity": "WARNING", | ||
"data": [] | ||
}, | ||
{ | ||
"id": "func-visibility", | ||
"severity": "WARNING", | ||
"data": [ | ||
"false" | ||
] | ||
}, | ||
{ | ||
"id": "state-visibility", | ||
"severity": "WARNING", | ||
"data": [] | ||
} | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
toolchains/solidity/linter/core/solidhunter-lib/testdata/StateVisibility/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,11 @@ | ||
pragma solidity 0.8.0; | ||
|
||
contract Test { | ||
uint data; | ||
uint public data; | ||
uint private data; | ||
|
||
string data = "test"; | ||
string public data = "test"; | ||
string private data = "test"; | ||
} |
2 changes: 2 additions & 0 deletions
2
toolchains/solidity/linter/core/solidhunter-lib/testdata/StateVisibility/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,2 @@ | ||
state-visibility:4:9:4:13 | ||
state-visibility:8:11:8:15 |
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 |
---|---|---|
|
@@ -108,4 +108,5 @@ test_directories! { | |
CustomErrors, | ||
EventNameCamelCase | ||
ConstNameSnakeCase, | ||
StateVisibility, | ||
} |