Skip to content

Commit

Permalink
Merge pull request #115 from habdelra/114-mark-callable-contracts-fal…
Browse files Browse the repository at this point in the history
…se-positive

Fixes #114 not firing false positive on mark-callable-contracts for structs
  • Loading branch information
fvictorio committed May 14, 2019
2 parents ca9d6e5 + ccdda9b commit 0d322c7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
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)
})
})

0 comments on commit 0d322c7

Please sign in to comment.