Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook Auth and CSRF mechanism
    primarykey
    data
    text
    <p>I am currently building a website using facebook authentication. I am running locally and keep getting CSRF errors. I am trying to understand their CSRF protection mechanism:</p> <pre><code> $code = $_REQUEST["code"]; $_SESSION['state']= $_REQUEST['state'];//GETS SITE WORKING BUT UNSAFE!!!/// if(empty($code)) { $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;state=" . $_SESSION['state']; echo("&lt;script&gt; top.location.href='" . $dialog_url . "'&lt;/script&gt;"); } if($_REQUEST['state'] == $_SESSION['state']) { </code></pre> <p>What happens is that it seems my $_REQUEST['state'] is set but my $_SESSION['state'] is not. Hence to get it working i have the line which sets both equal. My $code variable never seems to be empty hence a new $_SESSION['state' variable is never set.</p> <p>Firstly what is the role of $_REQUEST['code'] and where does this/is this variable come from/set?</p> <p>Any ideas how to fix this with my unsafe line of code?</p> <p>p.s I know I should use the FB sdk, but am learning web design/security so this is a good exercise to learn how it works!!</p> <p>Many Thanks Sam</p> <hr> <p>To anybody who is interested, I have worked out how it works, but still haven't fixed my problem...Anyway</p> <p>The CSFR protection system works by saving a State 'ID' on the server-side in the $_SESSION['state'], the same ID is saved also inside the $_REQUEST['state'] , so on the client side. This therefore means if a CSRF attack occurs, the state value of the CSRF attack (client side) won't match that of $_SESSION['state'] hence the code does not proceed and the attack is prevented.</p> <p>Please correct me if I am wrong!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload