36
[Confidential] © 2013 Actcat, Inc. 1 Ruby用の静的コード解析ツール ざくっと紹介

Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

Embed Size (px)

Citation preview

Page 1: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc. 1

Ruby用の静的コード解析ツール ざくっと紹介

Page 2: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

はじめに 自己紹介

n プログラマ向けサービス「SideCI」を  開発している角(スミ) です  

n エンジニアとしては  Ruby,  Obj-­‐C,  Android  Javaなどが長いです  

n 毎月第四水曜日に「新宿.rb」を開催してます  

2

Page 3: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

コードレビュー  

してますか?

3

Page 4: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc. 4

PullRequestが届いたよ!  

Page 5: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

 なんでインデントが  

タブなんだろう(  ・ω・)

5

Page 6: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

tmp1,  tmp2,  v1,  v2…  

何の変数か分からない(  ・ω・)  

6

Page 7: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

$global1,  $global2,  @@global3…  

(  ・ω・)  

7

Page 8: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

こんな時こそ  

8

Page 9: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

コードレビューの自動化  

9

Page 10: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc. 10

引用  https://houndci.com/

Page 11: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

HoundCIの裏側は?

OSSのLintツール・Styleチェックツール  

n RuboCop  

n CoffeeLint  

n JSHint  

n SCSS-­‐Lint  

n haml-­‐lint  

11

Page 12: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc. 12

引用  https://www.sideci.com/

Page 13: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

SideCIの裏側は?

n RuboCop  

n CoffeeLint  

n JSHint  

n SCSS-­‐Lint  

n haml-­‐lint  

n Brakeman  

n RailsBestPractices  

n Reek  

n PHP_CodeSniffer  

n PHPMD  

13

Page 14: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

SideCIを  

HoundCIを知っている方に  

ざっくり説明すると  

14

Page 15: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

HoundCI  

-­‐  haml-­‐lint,  SCSS-­‐Lint  

+  Brakeman,  RailsBestPractices,  Reek  

−  $12  /month  

=  SideCI  

15

Page 16: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

今日紹介したい3つの静的解析ツール

1.  Brakeman  2.  Reek  3.  RailsBestPractices  

16

Page 17: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Brakeman

n 個人的に一押しのGem  

n Rails用セキュリティスキャナ  

n 初心者から、普段Rails使いの方まで

17

Page 18: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

コード例  

18

Page 19: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Rails  Beginnerさんのコードに  

たまにあるある  

19

Page 20: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Possible SQL injection

Problem  Code    User.first(:conditions  =>  "username  =  

'#{params[:username]}'")    Brakeman  Scan  Result    Possible  SQL  injection

20

Page 21: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

SideCIのコードでも問題  

見つかった(´・ω・`)  

21

Page 22: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

XSS Vulnerability in ActiveSupport::JSON.encode

There  is  an  XSS  vulnerability  in  the  ActiveSupport::JSON.encode  method  in  Ruby  on  Rails.    This  vulnerability  has  been  assigned  the  CVE  identifier  CVE-­‐2015-­‐3226.      Versions  Affected:    3.0.x,  3.1.x,  3.2.x,  4.1.x,  4.2.x.    Not  affected:              4.0.x.    Fixed  Versions:          4.2.2,  4.1.11    

22

Found  in  Gemfile.lock

Page 23: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

JSON.encode  使ってる人は  

4.2.2以降、4.1.11以降に  

アプデしよう!  

23

Page 24: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Reek

n コードのSmellを見つけるGem  

n Smellは「コードが読みにくい、または保守しづらい場所を示唆するもの(indicators  

of  where  your  code  might  be  hard  to  read,  

maintain  or  evolve)」  

24

Page 25: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

コード例  

25

Page 26: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

ちょっと冗長な感じしない?

26

Page 27: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

ちょっと(?)読みづらくない?

27

Page 28: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

ネストが深い 1つのメソッドに命令文が多い

28

Page 29: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

ちなみに修正後コード

29

Page 30: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Reekの詳細記事と  

設定のカスタマイズ  

ブログ書いたよ  

 

Rubyのコードスメルチェックツールreekで「こんなコードは嫌!」っていうのを防ごう  http://sideci.hatenablog.com/entry/2015/05/19/161330  

30

Page 31: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

RailsBestPractices

n コミュニティサイトの投票結果を元に作られたGem  

n Railsのコードのベスト・プラクティスを使うべきところを見つけてくれる  

n わりと面白い。色々出る  

31

Page 32: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

コード例  

32

Page 33: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

Always add DB index *_id系は常にdb index貼ろうよ警告

ActiveRecord::Schema.define(version:  20131103153637)  do      create_table  "book_authors",  force:  true  do  |t|          t.integer    "book_id"          t.integer    "author_id"          t.datetime  "created_at"          t.datetime  "updated_at"      end  

33

Page 34: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

remove empty helpers 空っぽだから消そうね、はい

module  ItemsHelper  end  

34

Page 35: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc.

3つの静的解析ツール まとめ

1.  Brakeman  l  セキュリティのアップデート情報と

初歩的ミスに役立つ  2.  Reek  l  コードの悪い傾向を検知、予防でき

る  3.  RailsBestPractices  l  Rails特有のbetterな方法について

チェックできる  

35

Page 36: Ruby用の静的コード解析ツールざくっと紹介 by SideCI #omotesandorb

[Confidential] © 2013 Actcat, Inc. 36

紹介した全てのツールで自動的にプルリクを解析、  プルリクに結果をコメントします。  

設定の所要時間は30秒,  利用料無料。今すぐ!  

SideCIをいっしょに作らなイカ?  エンジニア様募集中!