|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-10-10 16:11 UTC] steve at mountainmedia dot com
Description:
------------
Private variables are accessible to the print_r function outside of the object. Even if one cannot access the variable directly, one could easily parse the output of print_r to grab private data from an object.
Reproduce code:
---------------
<?
class Example {
private $secret = "My secret";
public $notsecret = "Not my secret";
}
$ex = new Example;
$x = print_r ($ex, true);
print $x;
?>
Expected result:
----------------
I expected the private members to be invisible or replaced with something to indicate the lack of access such as <Private>.
Actual result:
--------------
Example Object
(
[secret:private] => My secret
[notsecret] => Not my secret
)
Private variables are displayed and data is stored in the string variable $x which one could easily parse to get the value of secret:private.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 00:00:01 2025 UTC |
It is possible to prevent the output of sensitive private data by creating an anonymous function, but the object is not serializable anymore. This should work with PHP 4 >= 4.0.1 and PHP 5 (based on the documentation of the used functions). code: ----- <?php class Credentials { private $_user; private $_password; function __construct($user, $password) { $this->_user = $user; //uses base64 to get sure the string is escaped $base64 = base64_encode($password); $function = "return base64_decode('" . $base64 . "');"; $this->_password = create_function("", $function); } public function getUser() { return $this->_user; } public function getPassword() { return call_user_func($this->_password); } } $credentials = new Credentials("theUserName", "thePassKey"); echo "\n\nprint_r:\n"; print_r($credentials); echo "\n\nvar_dump:\n"; var_dump($credentials); echo "\n\nvar_export:\n"; var_export($credentials); output: ----- print_r: Credentials Object ( [_user:Credentials:private] => theUserName [_password:Credentials:private] => lambda_1 ) var_dump: object(Credentials)#1 (2) { ["_user":"Credentials":private]=> string(11) "theUserName" ["_password":"Credentials":private]=> string(9) "lambda_1" } var_export: Credentials::__set_state(array( '_user' => 'theUserName', '_password' => '' . "\0" . 'lambda_1', ))