building modern and secure php applications – codementor office hours with ben edmunds

85
PHP modern not your grandma’s php & secure

Upload: codementor

Post on 10-May-2015

2.972 views

Category:

Technology


5 download

DESCRIPTION

Codementor PHP expert mentor Ben Edmunds is the co-host of PHP Town Hall and author of Building Secure PHP Apps. Ben is also the creator of Ion Auth, a simple, lightweight authentication library for CodeIgniter. In an interactive format, Ben talked about: Modern PHP Latest PHP tools SQL Injection Password Hashing and Authentication Other Common Hacks https://www.codementor.io/benedmunds https://www.codementor.io/php-tutorial/building-modern-secure-php-applications-codementor-office-hours-ben-edmunds

TRANSCRIPT

Page 1: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PHPmodern

not your grandma’s php

& secure

Page 2: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Who is this guy?

Ben Edmunds !

@benedmunds http://benedmunds.com

Page 3: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Who is this guy?

Ben Edmunds !

Open Source Author PHP Town Hall Podcast CTO at Mindfulware

Page 4: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Welcome to the Future

Page 5: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Welcome to the Future

Exceptions

NamespacesClosures

Page 6: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Welcome to the FutureStatics

PDOShort Arrays

Security

Page 7: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Legit Tools

Page 8: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Legit Tools

Built-in Server

Unit TestingComposer

Page 9: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Welcome to!the Future

Page 10: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Great Scott!

Page 11: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Exceptions

Page 12: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 13: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Exceptionstry { //your code goes here}catch (Exception $e) { die($e->getMessage());}

Page 14: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Exceptionstry { //your code goes here}catch (Exception $e) { die($e->getMessage());}

Page 15: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Closures

Page 16: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 17: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Closures

Route::get(‘/', function(){ return View::make(‘index');!

});

Page 18: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Closures

Route::get(‘/', function(){ return View::make(‘index');!

});

Page 19: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Namespaces

Page 20: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 21: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Namespacesnamespace Illuminate\Console;class Command{ //…

Page 22: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Namespaces

use Illuminate\Console\Command;

namespace Illuminate\Console;class Command{ //…

Page 23: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Namespaces

use Illuminate\Console\Command;

namespace Illuminate\Console;class Command{ //…

Page 24: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Statics

Page 25: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 26: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

StaticsClass Route { public static function get() { //… }

Page 27: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Statics

Route::get();

Class Route { public static function get() { //… }

Page 28: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Statics

Route::get();

Class Route { public static function get() { //… }

Page 29: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Statics

NO $this $var = self::varAtDefinition;!

$var = static::varAtExec;

Page 30: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Short Array!Syntax

Page 31: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 32: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Short Array Syntax

$array = array( 0 => ‘value1’, 1 => ‘value2’,);

Page 33: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Short Array Syntax

$array = [ 0 => ‘value1’, 1 => ‘value2’,];

Page 34: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Short Array Syntax

$array = [ 0 => ‘value1’, 1 => ‘value2’,];

Page 35: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PDO

Page 36: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 37: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PDO

Cross System

Page 38: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PDO

Cross SystemMS SQLMySQLOraclePostgreSQLSQLite

CUBRIDFirebirdInformixODBC & DB24D

Page 39: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PDO

Cross SystemSafe Binding

Page 40: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

PDO$stmt = $db->prepare(‘ SELECT * FROM users WHERE id=:id’);!

$stmt->bindParam(‘:id’, $id);$stmt->execute();

Page 41: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

Page 42: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

SQL InjectionHTTPS

Password Hashing

Page 43: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

AuthenticationSafe DefaultsXSS & CSRF

Page 44: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 45: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

//escaping input$stmt->bindParam(‘:id’, $id);

Page 46: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

//escaping input$stmt->bindParam(‘:id’, $id);

//escaping outputhtmlentities($_POST[‘name’]);

Page 47: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security HTTPS / SSL!

Encrypts traffic across the wire!

Trusted sender and receiver!

Required by OAUTH 2

Page 48: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//authentication - access controlif (!$user->inGroup(‘admin’)) { return ‘ERROR YO’;}

Page 49: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//authentication - brute forceif ($user->loginAttempts > 5) { return ‘CAUGHT YA’;}

Page 50: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

//safe password hashingpassword_hash($_POST['pass']);

Page 51: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security

//safe password hashingpassword_hash($_POST['pass']);

//password verificationpassword_verify($_POST['pass'], $u->pass);

Page 52: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//safe defaultsclass Your Controller { protected $var1 = ‘default value’;!

function __construct() { … }}

Page 53: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//safe defaults$something = false;!

foreach ($array as $k => $v) { $something = $v->foo; if ($something == ‘bar’) { … }}

Page 54: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//Non-Persistent XSS!

http://www.yourSite.com/ ?page_num=2&per_page=50!

Send the link to someone, boom

Page 55: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//Persistent XSS!

Same idea, except with data that is saved to the server and re-displayed

Page 56: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//XSS Protection!

<h1>Title</h1>Hello <?=htmlentities($name)?>!!

Page 57: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//Cross Site Request Forgery//(CSRF)!

http://yourSite.com/ users/12/delete!!

Page 58: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//CSRF Protection!

POST / PUT / UPDATE / DELETEbehind forms with one-time use tokens!!

Page 59: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//CSRF Protection!

function generateCsrf() { $token = mcrypt_create_iv( 16, MCRYPT_DEV_URANDOM); Session::flash('csrfToken', $token); return $token; }!

Page 60: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Security//CSRF Protection!

if ( $_POST['token'] == Session::get(‘csrfToken')) { … }!

Page 61: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Legit Tools

Page 62: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 63: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Built-in !Web Server

Page 64: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Built-in Server

$ php -S localhost:8000!

PHP 5.4.0 Development Server started… Listening on localhost:8000 Document root is /home/ben/htdocs Press Ctrl-C to quit

Page 65: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer

Page 66: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Another

Package Manager!?

Page 67: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer

Sane PackageManagement

Page 68: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer

Autoloading

Page 69: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer

PEAR, ha!

packagist.org

Page 70: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer/ composer.json!

{ "require": { "stripe/stripe-php": "dev-master", "twilio/sdk": "dev-master" }}

Page 71: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer

$ php composer.phar update

$ php composer.phar install

Page 72: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Composer$client = new Services_Twilio($sid, $tkn);!

$client->account ->messages ->sendMessage(…)

Page 73: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Unit Testing

Page 74: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 75: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Unit Testing

PHPUnitBehatMink

SeleniumCodeCeptionPHPSpec

Page 76: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Unit Testingclass ApiAuthTest extends PHPUnit_Framework_TestCase {!

public function testVerify() {!

$auth = new apiAuth(); $this->assertTrue($auth->verify());

Page 77: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Unit Testingclass ApiAuthTest extends PHPUnit_Framework_TestCase {!

public function testVerify() {!

$auth = new apiAuth(); $this->assertTrue($auth->verify());

Page 78: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Unit Testing

$ phpunit tests!

PHPUnit 3.3.17 by Sebastian Bergmann.Time: 0.01 secondsOK (1 tests, 1 assertions)

Page 79: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Resources

Page 80: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds
Page 81: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Resources

PHP.net

Page 82: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Resources

Modern FrameworksLaravel

Symfony2

Fuel PHP

SlimPHP 2

Aura for PHP

Silex

Page 83: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Resources

leanpub.com/ phptherightway

PHPtheRightWay.com

Page 84: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

ResourcesBuildSecurePHPapps.com

Coupon Code: codementor $3 off

http://buildsecurephpapps.com/?coupon=codementor

Page 85: Building Modern and Secure PHP Applications – Codementor Office Hours with Ben Edmunds

Q/A TIME!Ben Edmunds @benedmunds http://benedmunds.com

http://buildsecurephpapps.com/?coupon=codementor