Web Security Basics

CSE 190 M (Web Programming) Spring 2008

University of Washington

Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp, Jessica Miller, and Kevin Wallace, and are licensed under the Creative Commons Attribution 2.5 License.

Valid XHTML 1.1 Valid CSS!

Lecture outline

PHP/SQL review

let's write an unsecure page using PHP and SQL

Recall: PHP MySQL functions

Complete PHP MySQL example

# connect to world database on local computer
$db = mysql_connect("localhost", "traveler", "packmybags");

mysql_select_db("world");

# execute a SQL query on the database
$results = mysql_query("SELECT * FROM Countries WHERE population > 100000000;");


# loop through each country
while ($row = mysql_fetch_array($results)) {
?>
	<li><?= $row["name"] ?>, ruled by <?= $row["head_of_state"] ?></li>
<?php
}
?>

Complete example w/ error checking

# connect to world database on local computer
$db = mysql_connect("localhost", "traveler", "packmybags");
check_result($db);
check_result(mysql_select_db("world"));

# execute a SQL query on the database
$results = mysql_query("SELECT * FROM Countries WHERE population > 100000000;");
check_result($results);

# loop through each country
while ($row = mysql_fetch_array($results)) {
?>
	<li><?= $row["name"] ?>, ruled by <?= $row["head_of_state"] ?></li>
<?php
}

# stops the page if any MySQL error occurred
function check_result($value) {
	if (!$value) {
		die("SQL error occurred: " . mysql_error());
	}
}
?>

Simpsons database w/ passwords

students
idnameemailpassword
123Bartbart@fox.combartman
404Ralphralph@fox.comcatfood
456Milhousemilhouse@fox.comfallout
888Lisalisa@fox.comvegan
courses
idnameteacher_id
10001Computer Science 1421234
10002Computer Science 1435678
10003Computer Science 190M9012
10004Informatics 1001234
grades
student_idcourse_idgrade
12310001B-
12310002C
45610001B+
88810002A+
88810003A+
40410004D+
teachers
idname
1234Krabappel
5678Hoover
9012Stepp

Practice problem: Grades lookup

Write the code to allow a student to look up their grades in the 'simpsons' database, by first entering a name and password.

If the user fails to specify a correct name/password combo, display an error message.

Web Security

breaking and securing web pages

CSE <= 190M

The real world

HTML injection

a flaw where a user is able to inject arbitrary HTML content into your page

Securing against HTML injection

SQL injection

a flaw where the user is able to inject arbitrary SQL commands into your query

Securing against SQL injection

Practice problem: Hack Recipeland