简体   繁体   English

屏蔽没有 type=password 的输入字符

[英]Masking input characters without type=password

So I have a problem with newer browsers saving passwords.所以我对较新的浏览器保存密码有疑问。 Say I have a password box like so:假设我有一个这样的密码框:

<input type="password" autocomplete="off" />

New browsers like IE11 and Safari in iOS 7.1 have started ignoring the autocomplete="off" in password boxes specifically and offer the user to save the password. IE11 和 iOS 7.1 中的 Safari 等新浏览器已经开始专门忽略密码框中的 autocomplete="off" 并提供用户保存密码。 In my company (a bank), we view this as a security concern.在我的公司(一家银行),我们将此视为安全问题。

I was wondering if anybody has solved this problem yet.我想知道是否有人已经解决了这个问题。 Maybe somebody has written a javascript plugin that masks a normal input[type=text] so that the autocomplete="off" attribute will be respected.也许有人写了一个 javascript 插件来屏蔽正常的输入[type=text],这样 autocomplete="off" 属性就会被尊重。

Update:更新:

For a little more information, here is the documentation for autocomplete on msdn: http://msdn.microsoft.com/en-us/library/ie/ms533486%28v=vs.85%29.aspx有关更多信息,请参阅 msdn 上的自动完成文档: http://msdn.microsoft.com/en-us/library/ie/ms533486%28v=vs.85%29.aspx

You can make a fake password input with type text using a custom font:您可以使用自定义字体输入带有类型文本的假密码输入:

 @font-face { font-family: 'password'; font-style: normal; font-weight: 400; src: url(https://jsbin-user-assets.s3.amazonaws.com/rafaelcastrocouto/password.ttf); } input.key { font-family: 'password'; width: 100px; height: 16px; }
 <p>Password: <input class="key" type="text" autocomplete="off" /></p>

JSBin Demo JSBin 演示

Notice that this only raises more security concerns.请注意,这只会引起更多的安全问题。

Here's an idea, but I'm suggesting it for cases where browsers get the autofill wrong for passwords that aren't used for logins.这是一个想法,但我建议在浏览器自动填充不用于登录的密码的情况下使用它。 There probably needs to be a better standard for identifying login screens so browsers don't have to use heuristics looking for fields with type="password" .可能需要一个更好的标准来识别登录屏幕,这样浏览器就不必使用启发式方法寻找type="password"字段。

Load the form with the password field with type="text" , so browsers' autocompletion algorithms will ignore it.使用type="text"的密码字段加载表单,因此浏览器的自动完成算法将忽略它。 When the user inputs something in that field, switch its type="password" .当用户在该字段中输入内容时,切换其type="password"

I'm not much of a JavaScript programmer, but I hacked a JSFiddle to show how it theoretically works.我不是一个 JavaScript 程序员,但我破解了一个JSFiddle来展示它在理论上是如何工作的。

Perhaps onfocus would be a better way to go, too, but I didn't try it.也许onfocus也是一个更好的方法,但我没有尝试。

The solution that worked for me was like this :对我有用的解决方案是这样的:

First input type should be text第一个输入类型应该是text

At the focus event turn input type to password在焦点事件中将输入类型转为password

Listen for user inputs and if input field value is empty set type to text again侦听用户输入,如果输入字段值为空,则再次将类型设置为text

This way at the start and when input field is empty browser will not show suggestions.这样在开始时和输入字段为空时浏览器将不会显示建议。

Firstly, autocomplete = "off" should be on the <form> element, not the individual fields.首先, autocomplete = "off"应该在<form>元素上,而不是单个字段上。 This is because browsers typically store the values for all fields in a given form together (eg to allow for saving multiple username/password combinations for the same site).这是因为浏览器通常将给定表单中所有字段的值存储在一起(例如,允许为同一站点保存多个用户名/密码组合)。

Set it in the form, and it should work just fine for you.在表单中设置它,它应该适合你。 (although passwords already saved will typically still be auto-completed, so clear your password store before testing) (虽然已经保存的密码通常仍会自动完成,因此请在测试前清除您的密码存储)

However, I would suggest that you're probably chasing the wrong target if this is considered a security concern.但是,如果这被认为是安全问题,我建议您可能正在追逐错误的目标。

The reason browsers offer this feature is because users want to be able to store their login credentials.浏览器提供此功能的原因是用户希望能够存储他们的登录凭据。 Preventing them from doing so won't stop them wanting to, and if users really want to, there are still a number of ways they can get around it -- there are browser plug-ins explicitly designed to kill thew autocomplete = "off" feature and allow all passwords to be saved.阻止他们这样做并不能阻止他们这样做,如果用户真的想这样做,他们仍然有很多方法可以解决这个问题——有一些浏览器插件明确设计用来杀死autocomplete = "off"功能并允许保存所有密码。

How your user stores the password at their end is ultimately not your security concern, and not something you really have any control over anyway.您的用户最终如何存储密码最终不是您的安全问题,也不是您真正可以控制的事情。

In fact, if we prevent people from storing their passwords, it is more likely that they will use the same password in multiple places (simply because people don't have the capacity to remember different passwords for every site they use), so by preventing them from saving it, you might actually be making your users' passwords less secure.事实上,如果我们阻止人们存储他们的密码,他们更有可能在多个地方使用相同的密码(仅仅是因为人们没有能力记住他们使用的每个站点的不同密码),因此通过防止他们从拯救它,你实际上可能使您的用户密码安全性较低

If your site has a genuine need for security that cannot allow a password to be saved, then you will need to consider an alternative mechanism entirely.如果您的站点真正需要安全性而不允许保存密码,那么您将需要完全考虑替代机制。 For example, bank logins these days often require users to enter specific numbered characters from their password -- eg "Please enter the fifth, eighth and twelfth characters from your password" .例如,如今的银行登录经常要求用户输入密码中的特定数字字符——例如“请输入密码中的第五、第八和第十二个字符”

However, these schemes are more aimed at securing the transmission of the password rather than the storing of it: by only entering certain given characters, we don't have to input or transmit the entire password at all, so there is no chance of it being hacked en-route.然而,这些方案更多地旨在保护密码的传输而不是存储它:通过只输入某些给定的字符,我们根本不必输入或传输整个密码,因此没有机会在途中被黑客入侵。 It is still assumed that the user will probably have the password noted down somewhere (especially if they have to work out which is the twelfth character in the string)仍然假设用户可能会在某处记下密码(特别是如果他们必须计算出字符串中的第十二个字符)

This kind of scheme can be a real pain for users, but does offer a genuine level of login security without having to actually input or transmit the password.这种方案对用户来说可能是一种真正的痛苦,但确实提供了真正级别的登录安全性,而无需实际输入或传输密码。 The additional level of difficulty it adds to the login process, however, means that only really high-security sites like banks are likely to use this kind of scheme over a regular password.然而,它给登录过程增加了额外的难度,这意味着只有像银行这样的真正高度安全的网站才有可能通过常规密码使用这种方案。

Have you tried changing the name attribute to something ridiculous?您是否尝试将 name 属性更改为可笑的东西? I believe the autocomplete functionality is based off the name attribute.我相信自动完成功能基于 name 属性。 I'm not 100% sure but in the limited testing I did changing the name attribute was changing what type of autocomplete data was being presented.我不是 100% 肯定,但在有限的测试中,我确实改变了 name 属性,改变了所呈现的自动完成数据的类型。

Obvious example just to be clear: name="username" was showing my username while name="email" was showing my previously entered email addresses.显而易见的例子是: name="username" 显示我的用户名,而 name="email" 显示我以前输入的电子邮件地址。 When I switched to name="browsersAreStupidForImplementingThisFeature" I didn't get any autocomplete data.当我切换到 name="browsersAreStupidForImplementingThisFeature" 时,我没有得到任何自动完成数据。

Needs further testing but might be a good place to start?需要进一步测试,但可能是一个很好的起点? Good luck祝你好运

We had a use case where admins could view other user records and make changes.我们有一个用例,管理员可以查看其他用户记录并进行更改。 One of the fields was that other user's password.其中一个字段是其他用户的密码。 In this instance, letting the password manager pre-fill the field was a "bad thing".在这种情况下,让密码管理器预先填写该字段是一件“坏事”。 So what we ended up doing was waiting a short period of time after the page had loaded and then cleared out the password field...所以我们最终做的是在页面加载后等待一小段时间,然后清除密码字段......

// After the page has loaded...
window.addEventListener('load', setTimeout(function() {
  // Wait a bit and then clear out the contents of the field
  document.getElementById('other-password').value='';
}), 100);

I struggled with this for quite some time, but here is a solution that solved the issue for me.我为此苦苦挣扎了很长一段时间,但这里有一个解决方案为我解决了这个问题。

Initially, create the password input as a 'text' input:最初,将密码输入创建为“文本”输入:

<input type="text" name="mfa_psw" id="mfa_psw" autocomplete="off">

Then use Javascript to listen for keystroke events.然后使用Javascript监听击键事件。 If the password input is not null, convert the type to 'password'.如果输入的密码不是null,将类型转换为'password'。

<script type="text/javascript">
var pswInput = document.getElementById("mfa_psw");

pswInput.onkeyup = function(e){
    if(e.keyCode == 13){
        mfa(); // enter key pressed, call next function (i.e. log in)
        return false;
    } else {
        if (pswInput.value==null) {
            pswInput.type = 'text'; 
            // input is empty, covert to 'text' to prevent password autofil
        } else {
            pswInput.type = 'password'; 
            // input is not empty, convert to 'password' to hide text entry
        }
    }
}

Note: this solution does mean that the first character of the user's password is exposed for a moment before the input type is changed to 'password'.注意:此解决方案确实意味着在输入类型更改为“密码”之前,用户密码的第一个字符会暴露片刻。 However, assuming the user's password is longer than 1 character, this really shouldn't be an issue!然而,假设用户的密码超过 1 个字符,这应该不是问题!

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

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