Skip to content

Commit

Permalink
feat: check for empty str in isHashMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjoeio committed Jun 8, 2021
1 parent 3b50bfc commit 4e074f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export const parse = (
if (opts?.configFile) {
msg = `error reading ${opts.configFile}: ${msg}`
}

return new Error(msg)
}

Expand All @@ -286,6 +287,13 @@ export const parse = (
const split = splitOnFirstEquals(arg.replace(/^--/, ""))
key = split[0] as keyof Args
value = split[1]
} else {
const short = arg.replace(/^-/, "")
const pair = Object.entries(options).find(([, v]) => v.short === short)
if (pair) {
key = pair[0] as keyof Args
}
}

if (!key || !options[key]) {
throw error(`Unknown option ${arg}`)
Expand Down
3 changes: 3 additions & 0 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export const hash = async (password: string): Promise<string> => {
* Used to verify if the password matches the hash
*/
export const isHashMatch = async (password: string, hash: string) => {
if (password === "" || hash === "") {
return false
}
try {
return await argon2.verify(hash, password)
} catch (error) {
Expand Down
16 changes: 14 additions & 2 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ describe("isHashMatch", () => {
const actual = await isHashMatch(password, _hash)
expect(actual).toBe(true)
})
it("should return false if the password is empty", async () => {
const password = ""
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
const actual = await isHashMatch(password, _hash)
expect(actual).toBe(false)
})
it("should return false if the hash is empty", async () => {
const password = "hellowpasssword"
const _hash = ""
const actual = await isHashMatch(password, _hash)
expect(actual).toBe(false)
})
})

describe("hashLegacy", () => {
Expand Down Expand Up @@ -325,7 +337,7 @@ describe("handlePasswordValidation", () => {
})
})

describe.only("isCookieValid", () => {
describe("isCookieValid", () => {
it("should be valid if hashed-password for SHA256 matches cookie.key", async () => {
const isValid = await isCookieValid({
passwordMethod: "SHA256",
Expand Down Expand Up @@ -384,7 +396,7 @@ describe.only("isCookieValid", () => {
})
})

describe.only("sanitizeString", () => {
describe("sanitizeString", () => {
it("should return an empty string if passed a type other than a string", () => {
expect(sanitizeString({} as string)).toBe("")
})
Expand Down

0 comments on commit 4e074f8

Please sign in to comment.