Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Expand scope of detect-possible-timing-attack #6

Merged
merged 5 commits into from
Oct 9, 2018
Merged

Expand scope of detect-possible-timing-attack #6

merged 5 commits into from
Oct 9, 2018

Conversation

g-marconet
Copy link
Contributor

@g-marconet g-marconet commented Oct 8, 2018

In my usage, I found detect-possible-timing-attack to be essentially toothless. Rarely did I find issues as simple as password === 'mypass'. More often, it would be something like, password.toString() === expectedPassword, which the old version does not recognize.

Changes

  • Runs on BinaryExpressions instead of IfStatements. This way it catches something like if (checkPassword(a, b)) { ... } where checkPassword = (password, expected) => password === expected;
  • Checks identifiers, call expressions, element access experssions, and property access expressions for keywords, rather than just identifiers. Now can detect user.password === 'password', user['password'] === 'password', and password.toString() === 'password', for example
  • Will only check for keywords if it makes sense. For example, password === true does not cause an issue anymore
  • Keyword regex matches if a keyword is anywhere within the target, instead of if it exactly equals the target. For example, expectedPassword === 'password' will now be flagged
  • Removed auth as keyword. In practice, it caused many false-positives and any actual positive was already captured by another keyword.

This is to tslint-config-security what eslint-community/eslint-plugin-security#37 is to eslint-plugin-security

Copy link
Owner

@webschik webschik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @g-marconet !
Thanks for your help! I have some comments about the code style and removing of the auth keyword

'password',
'secret',
'api',
'apiKey',
'token',
'auth',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you think that auth keyword shouldn't be a part of this validation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly it's pretty anecdotal, but in our codebase, any time the auth keyword was found was either a false positive or already covered by another keyword (mostly token). That said, not super married to that and it can definitely be left in if you think that's best!

function isVulnerableType (node: ts.Expression): boolean {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
return isVulnCallExpression(node as ts.CallExpression);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have more readable function names: isVuln* => isVulnerable*

this.addFailureAtNode(expression, 'Potential timing attack on the right side of expression');

visitBinaryExpression (node: ts.BinaryExpression) {
if (node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {kind: operatorTokenKind} = node.operatorToken;

if (
    operatorTokenKind === ts.SyntaxKind.EqualsEqualsToken ||
    operatorTokenKind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
...

@@ -84,6 +84,7 @@ export class Rule extends Lint.Rules.AbstractRule {

class RuleWalker extends Lint.RuleWalker {
visitBinaryExpression (node: ts.BinaryExpression) {
const operatorTokenKind = node.operatorToken.kind;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! But below all node.operatorToken.kind expressions should be replaced with operatorTokenKind ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is embarrassing... forgot to hit ctrl+s :D

'password',
'secret',
'api',
'apiKey',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By accident, I've merged your change on this line, but I think we should add auth keyword back into this list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that. Added back

@webschik webschik merged commit 85eb9ee into webschik:master Oct 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants