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

Limit the used password string length to prevent redos attacks #167

Merged
merged 4 commits into from
Jan 28, 2023
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: 6 additions & 2 deletions packages/libraries/main/src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export class Options {

graphs: OptionsGraph = {}

availableGraphs: string[] = []

useLevenshteinDistance: boolean = false

levenshteinThreshold: number = 2

l33tMaxSubstitutions: number = 100

maxLength: number = 256

constructor() {
this.setRankedDictionaries()
}
Expand Down Expand Up @@ -72,6 +72,10 @@ export class Options {
if (options.l33tMaxSubstitutions !== undefined) {
this.l33tMaxSubstitutions = options.l33tMaxSubstitutions
}

if (options.maxLength !== undefined) {
this.maxLength = options.maxLength
}
}

setTranslations(translations: TranslationKeys) {
Expand Down
5 changes: 3 additions & 2 deletions packages/libraries/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export const zxcvbnAsync = async (
password: string,
userInputs?: (string | number)[],
): Promise<ZxcvbnResult> => {
const usedPassword = password.substring(0, zxcvbnOptions.maxLength)
const start = time()
const matches = await main(password, userInputs)
const matches = await main(usedPassword, userInputs)

return createReturnValue(matches, password, start)
return createReturnValue(matches, usedPassword, start)
}

export {
Expand Down
5 changes: 5 additions & 0 deletions packages/libraries/main/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export interface OptionsType {
useLevenshteinDistance?: boolean
levenshteinThreshold?: number
l33tMaxSubstitutions?: number
/**
* @description Defines how many character of the password are checked. A password longer than the default are considered strong anyway, but it can be increased as pleased. Be aware that this could open some attack vectors.
* @default 256
*/
maxLength?: number
}

export interface RankedDictionary {
Expand Down
15 changes: 12 additions & 3 deletions packages/libraries/main/test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,18 @@ describe('main', () => {
})
})

it('should not die while processing and have a appropriate calcTime', () => {
const result = zxcvbn('4@8({[</369&#!1/|0$5+7%2/4@8({[</369&#!1/|0$5+7%2/"')
expect(result.calcTime).toBeLessThan(2000)
describe('attack vectors', () => {
it('should not die while processing and have a appropriate calcTime for l33t attack', () => {
const result = zxcvbn(
'4@8({[</369&#!1/|0$5+7%2/4@8({[</369&#!1/|0$5+7%2/"',
)
expect(result.calcTime).toBeLessThan(2000)
})

it('should not die while processing and have a appropriate calcTime for regex attacks', () => {
const result = zxcvbn(`\x00\x00${'\x00'.repeat(100)}\n`)
expect(result.calcTime).toBeLessThan(2000)
})
})

describe('password tests', () => {
Expand Down