简体   繁体   English

清理PHP密码字符串

[英]Sanitize a PHP password string

I have a PHP page that allows people to run htpasswd to update their password. 我有一个PHP页面,允许人们运行htpasswd来更新他们的密码。 What is the best way to sanitize their input. 什么是消毒他们的输入的最佳方法。 I don't want to restrict the input to much because I want to allow for secure passwords. 我不想限制输入,因为我想允许安全密码。 This is what I have. 这就是我所拥有的。 How can it be improved? 怎么改进?

$newPasswd = preg_replace('/[^a-z0-9~!()_+=[]{}<>.\\\/?:@#$%^&*]/is', '', $inputPasswd);
$cmdline = $htpasswd . " " . $passwd_file . " " . escapeshellarg($username) . " " .escapeshellarg($newpasswd);
exec( $cmdline, $output, $return_var );

The ecscapeshellarg function is built-in to PHP, and differs across systems based on the shell to sanitize the text so that no "unsafe" characters can be passed into exec. ecscapeshellarg函数内置于PHP,并且基于shell的系统不同,以清理文本,因此不会将“不安全”字符传递给exec。

http://us2.php.net/manual/en/function.escapeshellarg.php http://us2.php.net/manual/en/function.escapeshellarg.php

I would not use preg_replace with empty string on "dangerous characters", since that will make the user believe he changed the password into something, but you exec something else.. So the user will set the password to something different than he put in... It would be better to give feedback to the user telling him that some characters (preferably capture and display the offending characters) did not pass through validation. 我不会在“危险字符”上使用带有空字符串的preg_replace,因为这会让用户相信他将密码更改为某些内容,但是您执行了其他操作。因此,用户会将密码设置为与他输入的内容不同的密码。 ..最好向用户提供反馈,告诉他某些字符(最好是捕获并显示有问题的字符)没有通过验证。

Other than that, I think it looks pretty good. 除此之外,我认为它看起来很不错。 I think it provides the user with a wide range of possible character, and he doesn't need more than those to create a secure password. 我认为它为用户提供了广泛的可能性,并且他不需要创建安全密码。 It might also be useful to tell the user which characters that are allowed in the password so he doesn't have to guess. 告诉用户密码中允许哪些字符也是有用的,这样他就不必猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM