r/PHPhelp • • Oct 24 '16

Solved Am I vulnerable?

Hi there,

I just finished preparing my registration page with PDO, and wanted to make sure that I am not vulnerable to SQL Injection. If someone could look over what I have here: http://pastie.org/10949079 it would be much appreciated. I think (hopefully) that I am good, but would like a second opinion.

Thanks!

7 Upvotes

9 comments sorted by

3

u/halfercode Oct 25 '16

Thanks for the PM, and thanks for posting this. Looks good to me.

I would modify the hashing algorithm to PASSWORD_DEFAULT, so that if a new PHP release comes with a better default algorithm it will switch automatically.

I'd improve the indentation a bit too, just for readability - did you know PHP works well with multiline statements? You can wrap however you feel it is readable:

$sth = $dbh->prepare(
    "INSERT INTO users
        (uid, email, pwd, first_name, last_name, house_number, phone, date_entered) 
        VALUES (NULL, :email, :pwd, :first_name, :last_name, :house_number, :phone, NULL)"
);

2

u/adinbied Oct 25 '16

Thank you so much!

1

u/liquid_at Oct 25 '16

I would modify the hashing algorithm to PASSWORD_DEFAULT, so that if a new PHP release comes with a better default algorithm it will switch automatically.

Wouldn't that break passwords that have been encoded with the old standard?

1

u/halfercode Oct 25 '16

Good question. It would end up with a table of hashes of different types (BCRYPT and then the new one) but I think the hash type is detected in the verify phase. The verify function does not accept an algo code, so I guess it must.

Edit, aha, from the manual:

Note that password_hash() returns the algorithm, cost and salt as part of the returned hash.

1

u/liquid_at Oct 26 '16

thanks a lot. something for my little TIL-Book XD.

2

u/[deleted] Oct 25 '16

A combination of prepared statements (which you've used) and validating input can go a long way with helping.

Eg. Does the email match an email pattern, limiting first and last name string length etc.

Other things to consider:

  • Don't allow a user to submit lengthy strings.

  • Limit char limits in column tables to assumed lengths.

  • Ask yourself if there should be strict types. Eg. Age should be set as an integer in tables and validated as being numeric.

6

u/dyloot Oct 24 '16

You do 3 queries and each of the 3 queries are done using prepared statements. I believe you are not vulnerable to SQL injection.

5

u/rifts Oct 25 '16

you did good