Adds XSRF protection to the Carnet module.
authorVincent Zanotti <vincent.zanotti@polytechnique.org>
Sun, 18 May 2008 17:11:06 +0000 (19:11 +0200)
committerVincent Zanotti <vincent.zanotti@polytechnique.org>
Sun, 18 May 2008 17:11:06 +0000 (19:11 +0200)
Signed-off-by: Vincent Zanotti <vincent.zanotti@polytechnique.org>
include/rss.inc.php
modules/carnet.php
templates/carnet/mescontacts.tpl
templates/carnet/notifs.tpl
templates/carnet/panel.tpl
templates/carnet/rss.tpl
templates/gadgets/ig-minifiche.tpl
templates/include/minifiche.tpl
templates/profile/profile.tpl

index 89d63ca..0117fc0 100644 (file)
@@ -51,6 +51,7 @@ function init_rss($template, $alias, $hash, $require_uid = true)
     }
 
     if ($template) {
+        $page->assign('rss_hash', $hash);
         header('Content-Type: application/rss+xml; charset=utf8');
     }
     return $uid;
index 4dbb162..194d866 100644 (file)
@@ -127,6 +127,10 @@ class CarnetModule extends PLModule
         $promo_sortie = $res->fetchOneCell();
         $page->assign('promo_sortie', $promo_sortie);
 
+        if ($action && !S::has_xsrf_token()) {
+            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
+            $action = false;
+        }
         switch ($action) {
           case 'add_promo':
           case 'del_promo':
@@ -142,14 +146,24 @@ class CarnetModule extends PLModule
             break;
         }
 
-        if (Env::has('subs'))       $watch->_subs->update('sub');
-        if (Env::has('flags_contacts')) {
+        if (Env::has('subs') && S::has_xsrf_token()) {
+            $watch->_subs->update('sub');
+        } elseif (Env::has('subs')) {
+            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
+        }
+
+        if (Env::has('flags_contacts') && S::has_xsrf_token()) {
             $watch->watch_contacts = Env::b('contacts');
             $watch->saveFlags();
+        } elseif (Env::has('flags_contacts')) {
+            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
         }
-        if (Env::has('flags_mail')) {
-            $watch->watch_mail     = Env::b('mail');
+
+        if (Env::has('flags_mail') && S::has_xsrf_token()) {
+            $watch->watch_mail = Env::b('mail');
             $watch->saveFlags();
+        } elseif (Env::has('flags_mail')) {
+            $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
         }
 
         $page->assign_by_ref('watch', $watch);
@@ -198,8 +212,11 @@ class CarnetModule extends PLModule
         $uid  = S::v('uid');
         $user = Env::v('user');
 
-        switch (Env::v('action')) {
-            case 'retirer':
+        // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
+        // It allows direct linking to contact adding in the RSS feed.
+        if (Env::v('action') && (S::has_xsrf_token() || Env::v('token') === S::v('core_rss_hash'))) {
+            switch (Env::v('action')) {
+              case 'retirer':
                 if (is_numeric($user)) {
                     if (XDB::execute('DELETE FROM contacts
                                        WHERE uid = {?} AND contact = {?}',
@@ -219,7 +236,7 @@ class CarnetModule extends PLModule
                 }
                 break;
 
-            case 'ajouter':
+              case 'ajouter':
                 require_once('user.func.inc.php');
                 if (($login = get_user_login($user)) !== false) {
                     if (XDB::execute(
@@ -233,6 +250,9 @@ class CarnetModule extends PLModule
                         $page->trig('Contact déjà dans la liste !');
                     }
                 }
+            }
+        } elseif (Env::v('action')) {
+            $page->trig("La modification du contact a échouée, merci de réessayer.");
         }
 
         $search = false;
index 65c9ef9..7264691 100644 (file)
@@ -30,6 +30,7 @@
 Ajouter la personne suivante à ma liste de contacts&nbsp;:
 <div style="float: right">
 <form id="add_user" action="carnet/contacts" method="post">
+  {xsrf_token_field}
   <div>
   <input type="hidden" name="action" value="ajouter" />
   <input type="text" size="30" name="user" class="quick_search"
index 1df6000..17c9068 100644 (file)
@@ -27,6 +27,7 @@
 S'il n'y a rien à te signaler le mail ne t'est pas envoyé.</p>
 
 <form action="carnet/notifs" method="post">
+  {xsrf_token_field}
   <fieldset>
     <legend>Mail</legend>
     <input type='checkbox' name='mail' onclick="this.form.submit();" {if $watch->watch_mail}checked="checked"{/if} />
@@ -36,6 +37,7 @@ S'il n'y a rien à te signaler le mail ne t'est pas envoyé.</p>
 </form>
 
 <form action="carnet/notifs" method="post">
+  {xsrf_token_field}
   <fieldset>
     <legend>Événements à surveiller</legend>
     {foreach from=$watch->cats() item=s key=i}
@@ -55,6 +57,7 @@ S'il n'y a rien à te signaler le mail ne t'est pas envoyé.</p>
 <h2>Surveiller ses contacts</h2>
 
 <form action="carnet/notifs#middle" method="post">
+  {xsrf_token_field}
   <fieldset>
     <legend>Contacts</legend>
     <input type='checkbox' name='contacts' onclick="this.form.submit();" {if $watch->watch_contacts}checked="checked"{/if} /> Surveiller mes contacts<br />
@@ -71,6 +74,7 @@ Attention&nbsp;: pour les promos, tu n'es pas notifié des événements trop fr
 </p>
 
 <form action="carnet/notifs/" method="post">
+  {xsrf_token_field}
   <fieldset>
     <legend>Ajouter une promo</legend>
     Tu peux surveiller des promos (mettre la promo sur 4 chiffres),
@@ -114,7 +118,7 @@ et cliquer sur les icones {icon name=add} pour les ajouter à cette liste.
     <ul>
     {foreach from=$watch->nonins() item=p}
     <li>
-      {$p.prenom} {$p.nom} ({$p.promo}) <a href="carnet/notifs/del_nonins/{$p.user_id}">{icon name='cross' title='retirer'}/<>
+      {$p.prenom} {$p.nom} ({$p.promo}) <a href="carnet/notifs/del_nonins/{$p.user_id}?token={xsrf_token}">{icon name='cross' title='retirer'}</a>
     </li>
     {/foreach}
   </ul>
index 541a5da..446d644 100644 (file)
@@ -73,7 +73,7 @@ Il faut pour cela se rendre sur la page de <a href='carnet/notifs'>configuration
         {$promo[row].prenom} {$promo[row].nom}
       </a>
       {if !$promo[row].contact}
-      <a href="carnet/contacts?action=ajouter&amp;user={$promo[row].bestalias}">{*
+      <a href="carnet/contacts?action=ajouter&amp;user={$promo[row].bestalias}&amp;token={xsrf_token}">{*
         *}{icon name=add title="ajouter à mes contacts"}</a>
       {/if}
       {else}
index d279c87..0ec701e 100644 (file)
@@ -44,7 +44,7 @@
       <description><![CDATA[
         {if $x.data}{$x.prenom} {$x.nom} a mis à jours les données suivantes&nbsp;:<br />{$x.data}<br />{/if}
         {if !$x.contact and !$x.dcd}
-        <a href="{#globals.baseurl#}/carnet/contacts?action=ajouter&amp;user={$x.bestalias}">
+        <a href="{#globals.baseurl#}/carnet/contacts?action=ajouter&amp;user={$x.bestalias}&amp;token={$rss_hash}">
           {icon name=add title="Ajouter" full=true} Ajouter &agrave; mes contacts
         </a><br />
         {/if}
index b04a21d..244fda4 100644 (file)
     {if $smarty.session.auth ge AUTH_COOKIE}
     {if !$c.wasinscrit && !$c.dcd}
       {if $show_action eq ajouter}
-        <a href="carnet/notifs/add_nonins/{$c.user_id}" target="_top">{*
+        <a href="carnet/notifs/add_nonins/{$c.user_id}?token={xsrf_token}" target="_top">{*
         *}{icon name=add title="Ajouter à la liste de mes surveillances"}</a>
       {else}
-        <a href="carnet/notifs/del_nonins/{$c.user_id}" target="_top">{*
+        <a href="carnet/notifs/del_nonins/{$c.user_id}?token={xsrf_token}" target="_top">{*
         *}{icon name=cross title="Retirer de la liste de mes surveillances"}</a>
       {/if}
     {elseif $c.wasinscrit && !$c.dcd}
         <a href="vcard/{$c.forlife}.vcf">{*
         *}{icon name=vcard title="Afficher la carte de visite"}</a>
       {if $show_action eq ajouter}
-        <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}" target="_top">{*
+        <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}&amp;token={xsrf_token}" target="_top">{*
         *}{icon name=add title="Ajouter à mes contacts"}</a>
       {else}
-        <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}" target="_top">{*
+        <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}&amp;token={xsrf_token}" target="_top">{*
         *}{icon name=cross title="Retirer de mes contacts"}</a>
       {/if}
     {/if}
index f739586..a355e14 100644 (file)
     <div>
       {if !$c.wasinscrit && !$c.dcd}
         {if $show_action eq ajouter}
-    <a href="carnet/notifs/add_nonins/{$c.user_id}">{*
+    <a href="carnet/notifs/add_nonins/{$c.user_id}?token={xsrf_token}">{*
     *}{icon name=add title="Ajouter à la liste de mes surveillances"}</a>
         {else}
-    <a href="carnet/notifs/del_nonins/{$c.user_id}">{*
+    <a href="carnet/notifs/del_nonins/{$c.user_id}?token={xsrf_token}">{*
     *}{icon name=cross title="Retirer de la liste de mes surveillances"}</a>
         {/if}
       {elseif $c.wasinscrit}
     <a href="vcard/{$c.forlife}.vcf">{*
     *}{icon name=vcard title="Afficher la carte de visite"}</a>
           {if $show_action eq ajouter}
-    <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}">{*
+    <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}&amp;token={xsrf_token}">{*
     *}{icon name=add title="Ajouter à mes contacts"}</a>
           {else}
-    <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}">{*
+    <a href="carnet/contacts?action={$show_action}&amp;user={$c.forlife}&amp;token={xsrf_token}">{*
     *}{icon name=cross title="Retirer de mes contacts"}</a>
           {/if}
         {/if}
index d4572fb..68ce962 100644 (file)
@@ -65,10 +65,10 @@ function chgMainWinLoc(strPage)
       &nbsp;{if !$x.dcd}<a href="vcard/{$x.forlife}.vcf">{*
         *}{icon name=vcard title="Afficher la carte de visite"}</a>{/if}
       {if !$x.is_contact}
-      <a href="javascript:chgMainWinLoc('carnet/contacts?action=ajouter&amp;user={$x.forlife}')">
+      <a href="javascript:chgMainWinLoc('carnet/contacts?action=ajouter&amp;user={$x.forlife}&amp;token={xsrf_token}')">
         {icon name=add title="Ajouter à mes contacts"}</a>
       {else}
-      <a href="javascript:chgMainWinLoc('carnet/contacts?action=retirer&amp;user={$x.forlife}')">
+      <a href="javascript:chgMainWinLoc('carnet/contacts?action=retirer&amp;user={$x.forlife}&amp;token={xsrf_token}')">
         {icon name=cross title="Retirer de mes contacts"}</a>
       {/if}
       {if hasPerm('admin')}