Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Level Up Your Development Workflow with Static ...

Level Up Your Development Workflow with Static Analysis

Avatar for Devon Blandin

Devon Blandin

March 29, 2016
Tweet

More Decks by Devon Blandin

Other Decks in Programming

Transcript

  1. Dynamic analysis tests and evaluates the behavior of software during

    execution. Static analysis evaluates software as it is written or defined. STATIC VS DYNAMIC ANALYSIS Reek brakeman
  2. EXAMPLE OF DYNAMIC ANALYSIS class User < ActiveRecord::Base # ...

    def approve(approved_by,send_mail=true) self.is_approved = true if approved_by.is_a?(Fixnum) self.approved_by_id = approved_by else self.approved_by = approved_by end self.approved_at = Time.now if save and send_mail Jobs.enqueue(:user_email, type: :signup_after_approval, user_id: id, email_token: email_tokens.first.token ) # ... end end end describe ".approve" do before do user.approve(admin) end it "enqueues a 'signup after approval' email" do Jobs.expects(:enqueue).with( :user_email, has_entries(type: :signup_after_approval) ) end it "marks the user as approved" do expect(user).to be_approved end it "has the admin as the approved by" do expect(user.approved_by).to eq(admin) end it "has a value for approved_at" do expect(user.approved_at).to be_present end end discourse/discourse - app/models/user.rb
  3. class User < ActiveRecord::Base # ... def approve(apprved_by,send_mail=true) self.is_approved =

    true if approved_by.is_a?(Fixnum) self.approved_by_id = approved_by else self.approved_by = approved_by end self.approved_at = Time.now if save and send_mail Jobs.enqueue(:user_email, type: :signup_after_approval, user_id: id, email_token: email_tokens.first.token ) # ... end end end EXAMPLE OF STATIC ANALYSIS Use `&&` instead of `and`. [rubocop] Method has too many lines. [14/10] [rubocop] Inconsistent indentation detected. [rubocop] Surrounding space missing in default value assignment. [rubocop] Space missing after comma. [rubocop] Align `)` with `(`. [rubocop] Align the parameters of a method call if they span more than one line. [rubocop] discourse/discourse - app/models/user.rb Unused method argument - `apprved_by` [rubocop]
  4. Why use static analysis? ▸ Enforce team and community code

    style guidelines ▸ Detect security vulnerabilities in your code and dependencies ▸ Detect complexity, duplication, and other code smells
  5. ENFORCE TEAM AND COMMUNITY CODE STYLE GUIDELINES ▸ Maintain a

    styleguide within your organization ▸ Setup a process for updating your styleguide ▸ Use static analysis tools to auto enforce styleguide violations ▸ Share default configuration for static analysis tools pep8 SCSS Lint Static analysis tools that check for style violations:
  6. DETECT SECURITY VULNERABILITIES brakeman bundler-audit node security project Scans your

    Ruby on Rails code for vulnerabilities Scans your project for vulnerable dependencies
  7. DETECT SECURITY VULNERABILITIES NODE SECURITY PROJECT { "name": "demo", "version":

    "1.0.0", "dependencies": { "marked": { "version": "0.3.3" } } }
  8. DETECT SECURITY VULNERABILITIES BUNDLER AUDIT GEM remote: https://rubygems.org/ specs: actionmailer

    (4.2.4) actionpack (= 4.2.4) actionview (= 4.2.4) activejob (= 4.2.4) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 1.0, >= 1.0.5) actionpack (4.2.4) actionview (= 4.2.4) activesupport (= 4.2.4) rack (~> 1.6) rack-test (~> 0.6.2) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (4.2.4) activesupport (= 4.2.4) builder (~> 3.1)
  9. DETECT SECURITY VULNERABILITIES ▸ Use Brakeman to detect vulnerabilities in

    Rails code ▸ Use bundler-audit and NSP to detect vulnerable dependencies Static analysis tools that check for security vulnerabilities: brakeman bundler-audit node security project
  10. Smells are indicators of where your code might be hard

    to read, maintain or evolve, rather than things that are specifically wrong. - reek project DETECT COMPLEXITY, DUPLICATION, AND OTHER CODE SMELLS
  11. Static analysis tools that check for complexity and/or code smells:

    PHP Mess Detector Radon Reek ShellCheck HLint ▸ Detect complexity and duplication ▸ Detect language or framework-specific smells DETECT COMPLEXITY, DUPLICATION, AND OTHER CODE SMELLS
  12. HONORABLE MENTIONS ▸ Use static analysis when you're learning a

    new language/framework ▸ Use static analysis for Markdown or prose
  13. When do I run static analysis? ▸ On the command

    line ▸ Within your editor ▸ During or alongside CI
  14. ESLINT RULE EXAMPLE /** * @fileoverview Rule to flag use

    of ternary operators. * @author Ian Christian Myers */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = function(context) { return { "ConditionalExpression": function(node) { context.report(node, "Ternary operator used."); } }; }; module.exports.schema = [];
  15. RUBOCOP RULE EXAMPLE module RuboCop module Cop module Style #

    This cop checks for the use of the send method. class Send < Cop MSG = 'Prefer `Object#__send__` or `Object#public_send` to ' \ '`send`.'.freeze def on_send(node) _receiver, method_name, *args = *node return unless method_name == :send && !args.empty? add_offense(node, :selector) end end end end end
  16. CODE CLIMATE SPEC { "categories": [ "Security" ], "check_name": "Insecure

    Dependency", "content": { "body": "**Advisory**: CVE-2015-7581\n\n**URL**: https://groups.google.com/forum/#! topic/rubyonrails-security/dthJ5wL69JE\n\n**Solution**: upgrade to >= 4.2.5.1, ~> 4.2.5, >= 4.1.14.1, ~> 4.1.14" }, "description": "Object leak vulnerability for wildcard controller routes in Action Pack", "fingerprint": "06ae795b91e09069846af543d755b9e1", "location": { "lines": { "begin": 18, "end": 18 }, "path": "Gemfile.lock" }, "remediation_points": 50000, "severity": "normal", "type": "Issue" }
  17. LEVEL UP YOUR DEVELOPMENT WORKFLOW WITH STATIC ANALYSIS ▸ Enforce

    team and community code style guidelines ▸ Detect security vulnerabilities in your code and dependencies ▸ Detect complexity, duplication, and other code smells ▸ Run static analysis locally or during CI ▸ Write your own tool extensions or Code Climate engines