php - 当密码为空时,Windows 上的 PDO 会忽略主机名和用户名

标签 php mysql windows pdo sellvana

我不确定我的设置是否有问题,或者这是 Windows 上 PDO 中的错误:

<?php 

$dsn = 'mysql:host=127.0.0.1;dbname=mydb;charset=UTF8';
$username = 'myuser';
$password = '';
$pdo = new PDO($dsn, $username, $password);

我的错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user ''@'localhost' to database 'mydb'' in C:\Ampps\www\test.php:6 Stack trace: #0 C:\Ampps\www\test.php(6): PDO->__construct('mysql:host=127....', 'myuser', '') #1 {main} thrown in C:\Ampps\www\test.php on line 6

我已经能够在我的所有 3 台 Windows 开发机器上重现此内容,但没有在任何 Linux 服务器上重现。

任何人都可以确认或指出可能是什么问题吗?

谢谢!

编辑:密码为空的用户权限设置正确。

EDIT2:有报告称该问题可在运行 MAMP 3.0.3 的 Mac 上重现

EDIT3:之前有几个报告,但看起来还没有修复:

https://bugs.php.net/bug.php?id=43493

https://bugs.php.net/bug.php?id=46457

我的报告:https://bugs.php.net/bug.php?id=67026

EDIT4:当权限被授予 'myuser'@'localhost' 时它工作,但是当权限被授予 'myuser'@'%' 而没有密码时问题出现 - 在 linux 上也是如此

最佳答案

我想确认一下。

请确保在测试之前满足条件:

  1. 确保 nonexistent_user 不是数据库中的用户。
  2. 确保 root 设置了密码。
  3. 确保单独运行这些测试。
  4. 确保在 Windows(或任何受影响的机器)上运行这些测试。

我已将相关用户名加粗。

测试#1

不存在的用户 + 空密码 = 错误消息的空用户名

$mysqli = new Mysqli('localhost', 'nonexistent_user', '', 'database');
$pdo    = new PDO('mysql:host=localhost;dbname=database;charset=UTF8', 'nonexistent_user', '');

Warning: mysqli::mysqli(): (HY000/1044): Access denied for user ''@'localhost' to database 'database' in test.php

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'database'' in test.php Stack trace: #0 test.php(): PDO->__construct('mysql:host=loca...', 'nonexistent_use...', '') #1 {main} thrown in test.php

测试 #2

存在的用户 + 空密码 = 为错误消息设置的用户名

$mysqli = new Mysqli('localhost', 'root', '', 'database');
$pdo    = new PDO('mysql:host=localhost;dbname=database;charset=UTF8', 'root', '');

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in test.php

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)' in test.php Stack trace: #0 test.php(): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in test.php

测试 #3

不存在的用户+设置密码=为错误信息设置的用户名

$mysqli = new Mysqli('localhost', 'nonexistent_user', 'password', 'database');
$pdo    = new PDO('mysql:host=localhost;dbname=database;charset=UTF8', 'nonexistent_user', 'password');

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'nonexistent_user'@'localhost' (using password: YES) in test.php

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'nonexistent_user'@'localhost' (using password: YES)' in test.php Stack trace: #0 test.php(): PDO->__construct('mysql:host=loca...', 'nonexistent_use...', 'password') #1 {main} thrown in test.php

当用户不存在且提供空密码时,错误消息将显示空用户名。

我觉得这会存在安全风险,因为攻击者可能会通过发送带有空密码的测试用户名来测试哪些用户有效,并查看用户名的错误响应是否为空。

关于php - 当密码为空时,Windows 上的 PDO 会忽略主机名和用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876169/

相关文章:

php - 打开图形布局

c++ - 在 std::string 中存储 unicode UTF-8 字符串

.net - 安装后 MSI 文件复制到哪里?

php - Drupal - CCK 字段 - 需要

php echo html - margin-left 不工作

php - 合并 mysql 中的列条目

php - MySQL 与 PHP 书

php - 如果 mysql_query 不成功则显示错误

PHP 5.6 - 此 MySQL 版本不允许使用命令

windows - Silverlight 5 受信任模式。访问文件系统和本地驱动器