Skip to content

Instantly share code, notes, and snippets.

@rocky
Created November 23, 2018 23:07
Show Gist options
  • Save rocky/c36a4387061230d429d159a6d3ea91c2 to your computer and use it in GitHub Desktop.
Save rocky/c36a4387061230d429d159a6d3ea91c2 to your computer and use it in GitHub Desktop.
tx.origin antlr vs solc
AuthorizationTXOrigin = function (sol_file: SolFile, plugin_config: PluginConfig): IssuePointer[] {
const issuePointers: IssuePointer[] = [];
parser.visit(sol_file.block, {
BinaryOperation(b_op: any) {
if (AstUtility.matchRegex(b_op.operator, new RegExp("==")) ||
AstUtility.matchRegex(b_op.operator, new RegExp("!="))) {
parser.visit(b_op, {
MemberAccess(member: any) {
parser.visit(b_op, {
Identifier(identifier: any) {
if (
AstUtility.matchRegex(member.memberName, new RegExp("^origin$")) &&
AstUtility.matchRegex(identifier.name, new RegExp("^tx$"))
) {
const location: Location = SolidityAntlr.parseLocation(member.loc, member.range);
issuePointers.push(new IssuePointer(plugin_config.swcID, plugin_config.descriptionShort[0], location));
}
},
});
}
});
}
},
});
return issuePointers;
};
// Return true if node is the tx.origin which refers to an
// address.
function txOriginViolation(node: any, issues: Issue[]): boolean {
const attrib = node.attributes;
if (attrib.type !== "address") {
return false;
}
if (attrib.member_name !== "origin") {
return false
}
if (node.childen[0].type === "tx") {
const mess = "Don't use `tx.origin`; Use `msg.sender` instead.";
const issue = new Issue(node.src, "SWC-115", Severity.Warning, mess);
issues.push(issue);
return true;
}
return false;
}
@rocky
Copy link
Author

rocky commented Nov 25, 2018

Testing reveals that line 11 should be:

    if (node.children[0].attributes.type === "tx") {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment