40
Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011

Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Cornac for PHPPHP static code analysis

OSCON, Portland, OR, USA, July 28th 2011

Page 2: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Agenda

•What is cornac?

•Applications and results

•How to make your code better

Page 3: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Who’s speaking?

•Damien Seguy

•In transition from Alter Way, France to Bysoft, China

•Industrialisation coach, LAMP expert

[email protected]

Page 4: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Yes, we take

Page 5: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

./bin/cornac -I spotweb.ini> Tokenizeur

> Auditeur> Done

Page 6: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Quick Inventory

Page 7: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Cornac

•Static auditor

•Analyze PHP code without executing it

•Study the application as a whole

Page 8: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Do not mistake with

•Xdebug

•xdebug executes code

•grep

•grep doesn’t understand PHP semantics

•CodeSniffer

•CodeSniffer check for coding and naming conventions

Page 9: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Close cousins

•PMD

•PHP Mess Detector

•PHP_Depends

Page 10: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

PHP extensions list

•The real list of extensions

•Useful for deployment

•Loved by hosting companies

Page 11: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Static audit•Process large quantities of code

•Process the same code over and over

•Depends on auditor expert level

•Automates searchs

•Make search systematics

Page 12: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Classes

Page 13: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Application inventory

•Taking a global look at the application

•List of structures names

•List of used PHP functionnalities

Page 14: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Technical aspects

•Listing all technical aspects

•PHP functionalities

•Advanced functionalities

•Deprecated functionalities

•Dependences

Page 15: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

5.3 migration•Incompatible evolutions

•Obsolet functions

•Reference handling

•References with the ‘new’ operator

•mktime doesn’t take 7 parameters anymore

Page 16: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Structures names

•Extract all structures names

•Study the convention

•Study the whole

•Study semantics

Page 17: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda
Page 18: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda
Page 19: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Inclusion network

Page 20: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Inclusion network

• include*, require*

• Ignore variables

• Circles represents files

• Arrows represent inclusions

Page 21: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Constant network

• Link between constant definition and its usage

• Constants are used within their definition file

• Except one

Page 22: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Global view

•Provide a graph for the whole application

•Get a feedback without dwelling on the precise names

Page 23: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Hierarchies

• Dot version

• Not too many levels

Page 24: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda
Page 25: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Hiérarchies

• dotclear hierarchy

Page 26: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Hierarchies

Page 27: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Classes network

•Gephi version

•Link classes based on composition

Page 28: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda
Page 29: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda
Page 30: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Cornac under the hood

•Cornac depends on PHP tokenizer

•It add a layer of structure : spoting larger structures

•It removes all useless separators

•{} [] () ; , ‘’ «»

Page 31: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Extractions [6] => Array ( [0] => 309 [1] => $world [2] => 1 )

[7] => Array ( [0] => 314 [1] => ! [2] => 1 )

[8] => " [9] => ) [10] => ; [1] => Array

( [0] => token PHP [1] => code PHP [2] => ligne ) [2] => "

<?php print ("hello $world! "); ?>

[1] => Array ( [0] => 266 [1] => print [2] => 1 )

[2] => Array ( [0] => 370 [1] => [2] => 1 )

[3] => ( [4] => " [5] => Array ( [0] => 314 [1] => hello [2] => 1 )

Page 32: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Extractions<?php print ("hello $world! "); ?>

Page 33: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Extractions

Page 34: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Iffectations

Page 35: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Unused classes

• Classes, properties, variables, functions,

Page 36: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Inner gears

Tokenizeur

Auditeur Display

Analyzer

Page 37: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Evolution

Auditeur

Analyzer

•Web

•XML

•ODS

•PHPCodeBrowser

•Sonar

•...

Tokenizeur

Page 38: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

Rules•Security

•Best practices (PHP, CMS..)

•In house conventions

•PHP 5.3 5.4 migration

•Performances

•Design patterns

Page 39: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda

http://www.cornac.info/[email protected]

Special thanks to Christophe Zadowski and Alexis Tellier

Page 40: Cornac for PHPassets.en.oreilly.com/1/event/61/Cornac_ Static Audit for... · 2011-08-18 · Cornac for PHP PHP static code analysis OSCON, Portland, OR, USA, July 28th 2011. Agenda