Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix password strength feedback for empty password #6008

Merged
merged 5 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/javascript/packs/pw-strength.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function getStrength(z) {
return z && z.password.length ? scale[z.score] : fallback;
}

function getFeedback(z) {
if (!z || z.score > 2) {
export function getFeedback(z) {
if (!z || !z.password || z.score > 2) {
return ' ';
}

Expand All @@ -56,6 +56,7 @@ function getFeedback(z) {
// i18n-tasks-use t('zxcvbn.feedback.dates_are_often_easy_to_guess')
// i18n-tasks-use t('zxcvbn.feedback.for_a_stronger_password_use_a_few_words_separated_by_spaces_but_avoid_common_phrases')
// i18n-tasks-use t('zxcvbn.feedback.names_and_surnames_by_themselves_are_easy_to_guess')
// i18n-tasks-use t('zxcvbn.feedback.no_need_for_symbols_digits_or_uppercase_letters')
// i18n-tasks-use t('zxcvbn.feedback.predictable_substitutions_like__instead_of_a_dont_help_very_much')
// i18n-tasks-use t('zxcvbn.feedback.recent_years_are_easy_to_guess')
// i18n-tasks-use t('zxcvbn.feedback.repeats_like_aaa_are_easy_to_guess')
Expand All @@ -69,6 +70,7 @@ function getFeedback(z) {
// i18n-tasks-use t('zxcvbn.feedback.this_is_a_top_10_common_password')
// i18n-tasks-use t('zxcvbn.feedback.this_is_a_very_common_password')
// i18n-tasks-use t('zxcvbn.feedback.this_is_similar_to_a_commonly_used_password')
// i18n-tasks-use t('zxcvbn.feedback.use_a_few_words_avoid_common_phrases')
// i18n-tasks-use t('zxcvbn.feedback.use_a_longer_keyboard_pattern_with_more_turns')
return t(`zxcvbn.feedback.${snakeCase(str)}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the first time we've had some missing translations with zxcvbn (#5776 comes to mind) is there a better way we can list these, or maybe a safer fallback we can use than to print the slug?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the first time we've had some missing translations with zxcvbn (#5776 comes to mind) is there a better way we can list these, or maybe a safer fallback we can use than to print the slug?

Yeah, these changes should have been included in #5776. As far as preventative measures, I mentioned at the time that it's a bit hard to test for. There was also a reference to formigarafa/zxcvbn-rb#4 where similar issues had been reported upstream. Since that time, there's been a comment of all strings we should cross-reference to our translations.

Aside from that, some ideas we could consider:

  • Auto-include all zxcvbn.feedback keys into the locale string extraction
  • Don't print original key if there is no translation (at least avoid the non-human-readable text from being shown)

Copy link
Contributor

@zachmargolis zachmargolis Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like both those, including all zxcvbn and also not passing through the key (maybe we add a default: option like ruby i18n has, and we call back to something general like "please use a different password")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea is to use TypeScript to enforce coverage for all strings, but the DefinitelyTyped types appear to only list out the warnings, and not the suggestions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double-checked the list in formigarafa/zxcvbn-rb#4 (comment) and made sure we have all strings listed there. I also added a spec in 6649e74 for the specific behavior being introduced here for an empty password. I was going to add a spec for the joining, though from looking at the source code, it doesn't appear there's a combination that could result in a multi-suggestion result being used, since we defer to a warning if it's present.

The ideas in #6008 (comment) are reasonable improvements, but moving forward I might suggest to propose an upstream improvement to the DefinitelyTyped type definitions to include all possible suggestions, then update our implementation to strongly type the feedback mappings, e.g.:

const FEEDBACK: Record<ZXCVBNFeedbackWarning & ZXCVBNFeedbackSuggestion, string> = {
  "Straight rows of keys are easy to guess": '...',
  // ...
};

}
Expand All @@ -80,7 +82,7 @@ function getFeedback(z) {
return lookup(warning);
}

return `${suggestions.map((s) => lookup(s)).join('')}`;
return `${suggestions.map((s) => lookup(s)).join('. ')}`;
}

function disableSubmit(submitEl, length = 0, score = 0) {
Expand Down
25 changes: 24 additions & 1 deletion spec/javascripts/packs/pw-strength-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getForbiddenPasswords } from '../../../app/javascript/packs/pw-strength';
import zxcvbn from 'zxcvbn';
import { getForbiddenPasswords, getFeedback } from '../../../app/javascript/packs/pw-strength';

describe('pw-strength', () => {
describe('getForbiddenPasswords', () => {
Expand Down Expand Up @@ -32,4 +33,26 @@ describe('pw-strength', () => {
expect(result).to.be.deep.equal(['foo', 'bar', 'baz']);
});
});

describe('getFeedback', () => {
const EMPTY_RESULT = '&nbsp;';

it('returns an empty result for empty password', () => {
const z = zxcvbn('');

expect(getFeedback(z)).to.equal(EMPTY_RESULT);
});

it('returns an empty result for a strong password', () => {
const z = zxcvbn('!Juq2Uk2**RBEsA8');

expect(getFeedback(z)).to.equal(EMPTY_RESULT);
});

it('returns feedback for a weak password', () => {
const z = zxcvbn('password');

expect(getFeedback(z)).to.equal('zxcvbn.feedback.this_is_a_top_10_common_password');
});
});
});