Skip to content

Commit

Permalink
[PM-14198] zero minimums when the character category is disabled (#11906
Browse files Browse the repository at this point in the history
)
  • Loading branch information
audreyality authored Nov 7, 2024
1 parent e95af82 commit b2811e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
2 changes: 2 additions & 0 deletions libs/tools/generator/core/src/policies/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Constraint } from "@bitwarden/common/tools/types";

import { sum } from "../util";

const Zero: Constraint<number> = { min: 0, max: 0 };
const AtLeastOne: Constraint<number> = { min: 1 };
const RequiresTrue: Constraint<boolean> = { requiredValue: true };

Expand Down Expand Up @@ -159,6 +160,7 @@ export {
enforceConstant,
readonlyTrueWhen,
fitLength,
Zero,
AtLeastOne,
RequiresTrue,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DefaultPasswordBoundaries, DefaultPasswordGenerationOptions, Policies } from "../data";

import { AtLeastOne } from "./constraints";
import { AtLeastOne, Zero } from "./constraints";
import { DynamicPasswordPolicyConstraints } from "./dynamic-password-policy-constraints";

describe("DynamicPasswordPolicyConstraints", () => {
Expand Down Expand Up @@ -207,7 +207,7 @@ describe("DynamicPasswordPolicyConstraints", () => {
expect(calibrated.constraints.minNumber).toEqual(dynamic.constraints.minNumber);
});

it("disables the minNumber constraint when the state's number flag is false", () => {
it("outputs the zero constraint when the state's number flag is false", () => {
const dynamic = new DynamicPasswordPolicyConstraints(Policies.Password.disabledValue);
const state = {
...DefaultPasswordGenerationOptions,
Expand All @@ -216,7 +216,7 @@ describe("DynamicPasswordPolicyConstraints", () => {

const calibrated = dynamic.calibrate(state);

expect(calibrated.constraints.minNumber).toBeUndefined();
expect(calibrated.constraints.minNumber).toEqual(Zero);
});

it("outputs the minSpecial constraint when the state's special flag is true", () => {
Expand All @@ -231,7 +231,7 @@ describe("DynamicPasswordPolicyConstraints", () => {
expect(calibrated.constraints.minSpecial).toEqual(dynamic.constraints.minSpecial);
});

it("disables the minSpecial constraint when the state's special flag is false", () => {
it("outputs the zero constraint when the state's special flag is false", () => {
const dynamic = new DynamicPasswordPolicyConstraints(Policies.Password.disabledValue);
const state = {
...DefaultPasswordGenerationOptions,
Expand All @@ -240,23 +240,7 @@ describe("DynamicPasswordPolicyConstraints", () => {

const calibrated = dynamic.calibrate(state);

expect(calibrated.constraints.minSpecial).toBeUndefined();
});

it("copies the minimum length constraint", () => {
const dynamic = new DynamicPasswordPolicyConstraints(Policies.Password.disabledValue);

const calibrated = dynamic.calibrate(DefaultPasswordGenerationOptions);

expect(calibrated.constraints.minSpecial).toBeUndefined();
});

it("overrides the minimum length constraint when it is less than the sum of the state's minimums", () => {
const dynamic = new DynamicPasswordPolicyConstraints(Policies.Password.disabledValue);

const calibrated = dynamic.calibrate(DefaultPasswordGenerationOptions);

expect(calibrated.constraints.minSpecial).toBeUndefined();
expect(calibrated.constraints.minSpecial).toEqual(Zero);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { DefaultPasswordBoundaries } from "../data";
import { PasswordGeneratorPolicy, PasswordGeneratorSettings } from "../types";

import { atLeast, atLeastSum, maybe, readonlyTrueWhen, AtLeastOne } from "./constraints";
import { atLeast, atLeastSum, maybe, readonlyTrueWhen, AtLeastOne, Zero } from "./constraints";
import { PasswordPolicyConstraints } from "./password-policy-constraints";

/** Creates state constraints by blending policy and password settings. */
Expand Down Expand Up @@ -68,8 +68,8 @@ export class DynamicPasswordPolicyConstraints
...this.constraints,
minLowercase: maybe<number>(lowercase, this.constraints.minLowercase ?? AtLeastOne),
minUppercase: maybe<number>(uppercase, this.constraints.minUppercase ?? AtLeastOne),
minNumber: maybe<number>(number, this.constraints.minNumber),
minSpecial: maybe<number>(special, this.constraints.minSpecial),
minNumber: maybe<number>(number, this.constraints.minNumber) ?? Zero,
minSpecial: maybe<number>(special, this.constraints.minSpecial) ?? Zero,
};

// lower bound of length must always at least fit its sub-lengths
Expand Down

0 comments on commit b2811e0

Please sign in to comment.