mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 09:13:33 +00:00
[PM-3565] Enforce higher minimum KDF (#6440)
Changes minimum iterations for PBKDF2 to 600 000. Also converts the constants into ranges to ensure there is only a single place for all checks.
This commit is contained in:
26
libs/common/src/platform/misc/range-with-default.spec.ts
Normal file
26
libs/common/src/platform/misc/range-with-default.spec.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { RangeWithDefault } from "./range-with-default";
|
||||
|
||||
describe("RangeWithDefault", () => {
|
||||
describe("constructor", () => {
|
||||
it("should throw an error when min is greater than max", () => {
|
||||
expect(() => new RangeWithDefault(10, 5, 0)).toThrowError("10 is greater than 5.");
|
||||
});
|
||||
|
||||
it("should throw an error when default value is not in range", () => {
|
||||
expect(() => new RangeWithDefault(0, 10, 20)).toThrowError("Default value is not in range.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("inRange", () => {
|
||||
it("should return true when in range", () => {
|
||||
const range = new RangeWithDefault(0, 10, 5);
|
||||
expect(range.inRange(5)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false when not in range", () => {
|
||||
const range = new RangeWithDefault(5, 10, 7);
|
||||
expect(range.inRange(1)).toBe(false);
|
||||
expect(range.inRange(20)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
24
libs/common/src/platform/misc/range-with-default.ts
Normal file
24
libs/common/src/platform/misc/range-with-default.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* A range with a default value.
|
||||
*
|
||||
* Enforces constraints to ensure min > default > max.
|
||||
*/
|
||||
export class RangeWithDefault {
|
||||
constructor(
|
||||
readonly min: number,
|
||||
readonly max: number,
|
||||
readonly defaultValue: number,
|
||||
) {
|
||||
if (min > max) {
|
||||
throw new Error(`${min} is greater than ${max}.`);
|
||||
}
|
||||
|
||||
if (this.inRange(defaultValue) === false) {
|
||||
throw new Error("Default value is not in range.");
|
||||
}
|
||||
}
|
||||
|
||||
inRange(value: number): boolean {
|
||||
return value >= this.min && value <= this.max;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user