-
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.
feat: ✨ unimplemented functions detector
- Loading branch information
Showing
9 changed files
with
262 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { AnalyserConfig, Detector, DetectorViolation, ParsedContract, Severity } from "@/types"; | ||
import { AnalyserConfig, Detector, DetectorViolation, ParsedContracts, Severity } from "@/types"; | ||
|
||
export abstract class AbstractDetector implements Detector { | ||
public abstract id: string; | ||
public abstract title: string; | ||
public abstract description: string; | ||
public abstract severity: Severity; | ||
|
||
abstract detect(code: ParsedContract, config?: AnalyserConfig): Promise<DetectorViolation[]>; | ||
abstract detect(code: ParsedContracts, config?: AnalyserConfig): Promise<DetectorViolation[]>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ContractDefinition, FunctionDefinition } from "@solidity-parser/parser/dist/src/ast-types"; | ||
|
||
import { AbstractDetector } from "./abstract-detector"; | ||
|
||
import { SolidityParser } from "@/modules"; | ||
import { DetectorViolation, ParsedContracts, Severity } from "@/types"; | ||
|
||
export const UNIMPLEMENTED_FUNCTION_DETECTOR = "unimplemented-function"; | ||
|
||
export class UnimplementedFunctionDetector implements AbstractDetector { | ||
public id = UNIMPLEMENTED_FUNCTION_DETECTOR; | ||
public title = "Unimplemented Function"; | ||
public description = "Detects unimplemented functions on derived contracts"; | ||
public severity = Severity.Informational; | ||
|
||
detect( | ||
code: ParsedContracts | ||
// config: AnalyserConfig = {} | ||
): Promise<DetectorViolation[]> { | ||
const violations: DetectorViolation[] = []; | ||
|
||
const addViolation = (target: string, name: string, violation: string) => { | ||
violations.push({ target, name, violation }); | ||
}; | ||
|
||
const contracts = SolidityParser.getContracts(code); | ||
|
||
contracts.forEach((contract) => { | ||
const functions = this.detectUnimplementedFunction(contract, contracts); | ||
functions.forEach((func) => { | ||
addViolation("function", func.name ?? "unknown", "unimplemented"); | ||
}); | ||
}); | ||
|
||
return Promise.resolve(violations); | ||
} | ||
|
||
private detectUnimplementedFunction( | ||
contract: ContractDefinition, | ||
contracts: ContractDefinition[] | ||
): FunctionDefinition[] { | ||
const unimplemented: FunctionDefinition[] = []; | ||
|
||
const implementedFunctions = SolidityParser.getFunctions([contract]); | ||
const inheritedContracts = SolidityParser.getInheritedContracts(contract, contracts); | ||
|
||
inheritedContracts.forEach((inheritedContract) => { | ||
const inheritedFunctions = SolidityParser.getFunctions([inheritedContract]); | ||
inheritedFunctions.forEach((inheritedFunction) => { | ||
if (inheritedFunction.isConstructor) return; | ||
if (inheritedFunction.isFallback) return; | ||
|
||
let isImplemented = implementedFunctions.some( | ||
(func) => func.name === inheritedFunction.name | ||
); | ||
|
||
if (!isImplemented && this.matchStateVariable(contract, inheritedFunction)) { | ||
isImplemented = true; | ||
} | ||
|
||
if (!isImplemented) { | ||
unimplemented.push(inheritedFunction); | ||
} | ||
}); | ||
}); | ||
|
||
return unimplemented; | ||
} | ||
|
||
/** | ||
* Since Solidity v0.5.1, a state variable can be used to return the value for a function. | ||
*/ | ||
private matchStateVariable(contract: ContractDefinition, func: FunctionDefinition): boolean { | ||
// TODO only run if Solidity version > 0.5.1 | ||
const stateVariables = SolidityParser.getStateVariables([contract]); | ||
|
||
return stateVariables.some((stateVariable) => | ||
stateVariable.variables.some((variable) => variable.name === func.name) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.