<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/security.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'security.filesystem.nullbytes.php',
    1 => 'Probl&egrave;mes li&eacute;s aux octets nuls',
    2 => 'Probl&egrave;mes li&eacute;s aux octets nuls',
  ),
  'up' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'S&eacute;curit&eacute; des fichiers',
  ),
  'prev' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'S&eacute;curit&eacute; des fichiers',
  ),
  'next' => 
  array (
    0 => 'security.database.php',
    1 => 'S&eacute;curit&eacute; des bases de donn&eacute;es',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'security/filesystem.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="security.filesystem.nullbytes" class="sect1">
  <h2 class="title">Problèmes liés aux octets nuls</h2>
  <p class="simpara">
   Comme <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> utilise des fonctions C pour les opérations sous-jacentes,
   notamment au niveau du système de fichiers, il peut gérer les octets nuls
   d&#039;une façon inattendue. Sachant que les octets nuls dénotent la
   fin d&#039;une chaîne de caractères en C, certaines fonctions vont donc
   considérer ces chaînes jusqu&#039;à la première occurrence d&#039;un octet nul.

   L&#039;exemple suivant présente un code vulnérable qui montre ce problème :
  </p>
  <div class="example" id="example-5">
   <p><strong>Exemple #1 Script vulnérable aux octets nuls</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// "../../etc/passwd\0"<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'/home/wwwrun/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">)) {<br />   </span><span style="color: #FF8000">// file_exists retournera true sachant que le fichier /home/wwwrun/../../etc/passwd existe<br />   </span><span style="color: #007700">include </span><span style="color: #DD0000">'/home/wwwrun/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />   </span><span style="color: #FF8000">// le fichier /etc/passwd sera inclus<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
 </div>

</div>
<p class="para">
 Ainsi, toute chaîne utilisée dans des opérations sur le système de fichiers
 doit toujours être validée proprement. Voici une meilleure solution de
 l&#039;exemple précédent :
</p>
<div class="example" id="example-6">
 <p><strong>Exemple #2 Validation correcte de l&#039;entrée</strong></p>
 <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Liste blanche des valeurs possibles<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br />   case </span><span style="color: #DD0000">'main'</span><span style="color: #007700">:<br />   case </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">:<br />   case </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">:<br />   include </span><span style="color: #DD0000">'/home/wwwrun/include/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />   break;<br />   default:<br />   include </span><span style="color: #DD0000">'/home/wwwrun/include/main.php'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div><?php manual_footer($setup); ?>