Why does PDO print my password when the connection fails?

后端 未结 4 583
甜味超标
甜味超标 2020-12-13 06:08

I have a simple website where I establish a connection to a MySQL server using PDO.

$dbh = new PDO(\'mysql:host=localhost;dbname=DB;port=3306\',
                     


        
4条回答
  •  余生分开走
    2020-12-13 06:19

    We use encoded username and passwords, and decode those in the PDO constructor. Then we catch the PDOException and throw a new PDOException with the old exception its message, so that the trace will show only the encoded username and password.

    A good encryption library for PHP is defuse/php-encryption.

    Example code:

    decodeFunction($encodedUser), $this->decodeFunction($encodedPassword),
                        [
                            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                        ]
                    );
                }
                catch (PDOException $exception) {
                    throw new PDOException($exception->getMessage());
                }
            }
    
            private function decodeFunction(string $encoded): string
            {
                return \Defuse\Crypto\Crypto::decrypt($encoded, $this->decodeKey());
            }
    
            private function decodeKey(): \Defuse\Crypto\Key
            {
                static $key = null;
    
                if(null === $key) {
                    $key = \Defuse\Crypto\Key::loadFromAsciiSafeString(getenv('MY_PDO_DECODE_KEY'));
                }
    
                return $key;
            }
        }
    

提交回复
热议问题