Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #114 not firing false positive on mark-callable-contracts for structs #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/rules/security/mark-callable-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,25 @@ class MarkCallableContractsChecker {
this.reporter = reporter
this.ruleId = ruleId
this.meta = meta
this.structNames = []
}

enterStructDefinition(ctx) {
this.gatherStructNames(ctx)
}

exitIdentifier(ctx) {
const identifier = ctx.getText()
const isFirstCharUpper = /[A-Z]/.test(identifier[0])
const isContainsTrustInfo = identifier.toLowerCase().includes('trust')
const containsTrustInfo = identifier.toLowerCase().includes('trust')
const isStatement = traversing.findParentType(ctx, 'StatementContext')

if (isFirstCharUpper && !isContainsTrustInfo && isStatement) {
if (
isFirstCharUpper &&
!containsTrustInfo &&
isStatement &&
!this.structNames.includes(identifier)
) {
this.reporter.addMessage(
ctx.getSourceInterval(),
SEVERITY.WARN,
Expand All @@ -42,6 +52,15 @@ class MarkCallableContractsChecker {
)
}
}

gatherStructNames(ctx) {
const identifier = ctx.children[1]
const structName = identifier.getText()

if (structName) {
this.structNames.push(structName)
}
}
}

module.exports = MarkCallableContractsChecker
44 changes: 43 additions & 1 deletion test/rules/security/mark-callable-contracts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require('assert')
const linter = require('./../../../lib/index')
const funcWith = require('./../../common/contract-builder').funcWith
const { funcWith, contractWith } = require('./../../common/contract-builder')

describe('Linter - mark-callable-contracts', () => {
it('should return error that external contract is not marked as trusted / untrusted', () => {
Expand All @@ -13,4 +13,46 @@ describe('Linter - mark-callable-contracts', () => {
assert.equal(report.warningCount, 1)
assert.ok(report.reports[0].message.includes('trusted'))
})

it('should not return error for external contract that is marked as trusted', () => {
const code = funcWith('TrustedBank.withdraw(100);')

const report = linter.processStr(code, {
rules: { 'mark-callable-contracts': 'warn' }
})

assert.equal(report.warningCount, 0)
})

it('should not return error for external contract that is marked as untrusted', () => {
const code = funcWith('UntrustedBank.withdraw(100);')

const report = linter.processStr(code, {
rules: { 'mark-callable-contracts': 'warn' }
})

assert.equal(report.warningCount, 0)
})

it('should not return error for a struct', () => {
const code = contractWith(`
struct Token {
address tokenAddress;
string tokenSymbol;
}

mapping(address => Token) public acceptedTokens;

function b(address tokenAddress, string memory tokenSymbol) public {
Token memory token = Token(tokenAddress, tokenSymbol);
acceptedTokens[tokenAddress] = token;
}
`)

const report = linter.processStr(code, {
rules: { 'mark-callable-contracts': 'warn' }
})

assert.equal(report.warningCount, 0)
})
})