Bug #68380 [NEW]: Problem with HTTP authentication with PHP

From: Date: Sat, 08 Nov 2014 18:41:51 +0000
Subject: Bug #68380 [NEW]: Problem with HTTP authentication with PHP
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188519@lists.php.net to get a copy of this message
From: thomaxz at gmail dot com Operating system: Unkowen PHP version: Irrelevant Package: HTTP related Bug Type: Bug Bug description:Problem with HTTP authentication with PHP Description: ------------ First of the, I use an online web hosting server so can't upgrade anything. PHP 5.3 When using Example 2 in the HTTP authentication with PHP article it works first, but when wrong creditnals is neter, it will be wrong forever. Which makes the user not able to login. Test script: --------------- $realm = "Hurra 2"; //user => password $users = array('tcx' => 'yesmrsir'); if (empty($_SERVER['PHP_AUTH_DIGEST'])) { header('WWW-Authenticate: Digest realm="'.$realm. '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); header($_SERVER["SERVER_PROTOCOL"].' 401 Unauthorized'); $txts='<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>403 Forbidden</title></head><body>'; $txts.='<h1>Forbidden</h1><p>You don\'t have permission to access ny.php on this server.</p><hr>'; $txts.='<address>Apache Server at example.com Port 80</address></body></html>'; die($txts); } // analyze the PHP_AUTH_DIGEST variable if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || !isset($users[$data['username']])) header('WWW-Authenticate: Digest realm="'.$realm. '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); header($_SERVER["SERVER_PROTOCOL"].' 401 Unauthorized'); die('Wrong Credentials!'); var_dump($_SERVER['PHP_AUTH_DIGEST']); // generate the valid response $A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]); $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']); $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2); if ($data['response'] != $valid_response) die('Wrong Credentials!'); // ok, valid username & password echo 'You are logged in as: ' . $data['username']; //comment reae change // function to parse the http auth header function http_digest_parse($txt) { // protect against missing data $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1); $data = array(); preg_match_all('@(\w+)=(?:([\'"])([^$2]+)$2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER); foreach ($matches as $m) { $data[$m[1]] = $m[3] ? trim($m[3],"\",'") : trim($m[4],"\",'"); unset($needed_parts[$m[1]]); } return $needed_parts ? false : $data; } Expected result: ---------------- the user is able to renter creditals and then login. Actual result: -------------- the script show wrong crenditals and not show new login box -- Edit bug report at https://bugs.php.net/bug.php?id=68380&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68380&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68380&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68380&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68380&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68380&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68380&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68380&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68380&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68380&r=support Expected behavior: https://bugs.php.net/fix.php?id=68380&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68380&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68380&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68380&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68380&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68380&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68380&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68380&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68380&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68380&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68380&r=mysqlcfg

« previous php.bugs (#188519) next »