Skip to content

Commit 1e55a64

Browse files
committed
feat: check for empty str in isHashMatch
1 parent 3b50bfc commit 1e55a64

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

src/node/cli.ts

+8
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export const parse = (
263263
if (opts?.configFile) {
264264
msg = `error reading ${opts.configFile}: ${msg}`
265265
}
266+
266267
return new Error(msg)
267268
}
268269

@@ -286,6 +287,13 @@ export const parse = (
286287
const split = splitOnFirstEquals(arg.replace(/^--/, ""))
287288
key = split[0] as keyof Args
288289
value = split[1]
290+
} else {
291+
const short = arg.replace(/^-/, "")
292+
const pair = Object.entries(options).find(([, v]) => v.short === short)
293+
if (pair) {
294+
key = pair[0] as keyof Args
295+
}
296+
}
289297

290298
if (!key || !options[key]) {
291299
throw error(`Unknown option ${arg}`)

src/node/util.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export const hash = async (password: string): Promise<string> => {
134134
* Used to verify if the password matches the hash
135135
*/
136136
export const isHashMatch = async (password: string, hash: string) => {
137+
if (password === "" || hash === "") {
138+
return false
139+
}
137140
try {
138141
return await argon2.verify(hash, password)
139142
} catch (error) {
@@ -209,11 +212,12 @@ type HandlePasswordValidationArgs = {
209212
* Checks if a password is valid and also returns the hash
210213
* using the PasswordMethod
211214
*/
212-
export async function handlePasswordValidation(
213-
passwordValidationArgs: HandlePasswordValidationArgs,
214-
): Promise<PasswordValidation> {
215-
const { passwordMethod, passwordFromArgs, passwordFromRequestBody, hashedPasswordFromArgs } = passwordValidationArgs
216-
// TODO implement
215+
export async function handlePasswordValidation({
216+
passwordMethod,
217+
passwordFromArgs,
218+
passwordFromRequestBody,
219+
hashedPasswordFromArgs,
220+
}: HandlePasswordValidationArgs): Promise<PasswordValidation> {
217221
const passwordValidation = <PasswordValidation>{
218222
isPasswordValid: false,
219223
hashedPassword: "",
@@ -257,10 +261,14 @@ export type IsCookieValidArgs = {
257261
}
258262

259263
/** Checks if a req.cookies.key is valid using the PasswordMethod */
260-
export async function isCookieValid(isCookieValidArgs: IsCookieValidArgs): Promise<boolean> {
264+
export async function isCookieValid({
265+
passwordFromArgs = "",
266+
cookieKey,
267+
hashedPasswordFromArgs = "",
268+
passwordMethod,
269+
}: IsCookieValidArgs): Promise<boolean> {
261270
let isValid = false
262-
const { passwordFromArgs = "", cookieKey, hashedPasswordFromArgs = "" } = isCookieValidArgs
263-
switch (isCookieValidArgs.passwordMethod) {
271+
switch (passwordMethod) {
264272
case "PLAIN_TEXT":
265273
isValid = await isHashMatch(passwordFromArgs, cookieKey)
266274
break

test/unit/node/util.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ describe("isHashMatch", () => {
185185
const actual = await isHashMatch(password, _hash)
186186
expect(actual).toBe(true)
187187
})
188+
it("should return false if the password is empty", async () => {
189+
const password = ""
190+
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
191+
const actual = await isHashMatch(password, _hash)
192+
expect(actual).toBe(false)
193+
})
194+
it("should return false if the hash is empty", async () => {
195+
const password = "hellowpasssword"
196+
const _hash = ""
197+
const actual = await isHashMatch(password, _hash)
198+
expect(actual).toBe(false)
199+
})
188200
})
189201

190202
describe("hashLegacy", () => {
@@ -325,7 +337,7 @@ describe("handlePasswordValidation", () => {
325337
})
326338
})
327339

328-
describe.only("isCookieValid", () => {
340+
describe("isCookieValid", () => {
329341
it("should be valid if hashed-password for SHA256 matches cookie.key", async () => {
330342
const isValid = await isCookieValid({
331343
passwordMethod: "SHA256",
@@ -384,7 +396,7 @@ describe.only("isCookieValid", () => {
384396
})
385397
})
386398

387-
describe.only("sanitizeString", () => {
399+
describe("sanitizeString", () => {
388400
it("should return an empty string if passed a type other than a string", () => {
389401
expect(sanitizeString({} as string)).toBe("")
390402
})

yarn.lock

+1-21
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@
896896
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
897897
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==
898898

899-
"@mapbox/node-pre-gyp@^1.0.0", "@mapbox/node-pre-gyp@^1.0.1":
899+
"@mapbox/node-pre-gyp@^1.0.1":
900900
version "1.0.5"
901901
resolved "https://registry.yarnpkg.com/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.5.tgz#2a0b32fcb416fb3f2250fd24cb2a81421a4f5950"
902902
integrity sha512-4srsKPXWlIxp5Vbqz5uLfBN+du2fJChBoYn/f2h991WLdk7jUvcSk/McVLSv/X+xQIPI8eGD5GjrnygdyHnhPA==
@@ -1059,13 +1059,6 @@
10591059
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.1.tgz#a6ca6a9a0ff366af433f42f5f0e124794ff6b8f1"
10601060
integrity sha512-FTgBI767POY/lKNDNbIzgAX6miIDBs6NTCbdlDb8TrWovHsSvaVIZDlTqym29C6UqhzwcJx4CYr+AlrMywA0cA==
10611061

1062-
"@types/bcrypt@^5.0.0":
1063-
version "5.0.0"
1064-
resolved "https://registry.yarnpkg.com/@types/bcrypt/-/bcrypt-5.0.0.tgz#a835afa2882d165aff5690893db314eaa98b9f20"
1065-
integrity sha512-agtcFKaruL8TmcvqbndlqHPSJgsolhf/qPWchFlgnW1gECTN/nKbFcoFnvKAQRFfKbh+BO6A3SWdJu9t+xF3Lw==
1066-
dependencies:
1067-
"@types/node" "*"
1068-
10691062
"@types/body-parser@*", "@types/body-parser@^1.19.0":
10701063
version "1.19.0"
10711064
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.0.tgz#0685b3c47eb3006ffed117cdd55164b61f80538f"
@@ -1773,14 +1766,6 @@ bcrypt-pbkdf@^1.0.0:
17731766
dependencies:
17741767
tweetnacl "^0.14.3"
17751768

1776-
bcrypt@^5.0.1:
1777-
version "5.0.1"
1778-
resolved "https://registry.yarnpkg.com/bcrypt/-/bcrypt-5.0.1.tgz#f1a2c20f208e2ccdceea4433df0c8b2c54ecdf71"
1779-
integrity sha512-9BTgmrhZM2t1bNuDtrtIMVSmmxZBrJ71n8Wg+YgdjHuIWYF7SjjmCPZFB+/5i/o/PIeRpwVJR3P+NrpIItUjqw==
1780-
dependencies:
1781-
"@mapbox/node-pre-gyp" "^1.0.0"
1782-
node-addon-api "^3.1.0"
1783-
17841769
binary-extensions@^1.0.0:
17851770
version "1.13.1"
17861771
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65"
@@ -5432,11 +5417,6 @@ node-addon-api@^3.0.2:
54325417
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
54335418
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==
54345419

5435-
node-addon-api@^3.1.0:
5436-
version "3.2.0"
5437-
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.0.tgz#7028b56a7eb572b73873aed731a7f9c9365f5ee4"
5438-
integrity sha512-kcwSAWhPi4+QzAtsL2+2s/awvDo2GKLsvMCwNRxb5BUshteXU8U97NCyvQDsGKs/m0He9WcG4YWew/BnuLx++w==
5439-
54405420
node-fetch@^2.6.1:
54415421
version "2.6.1"
54425422
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"

0 commit comments

Comments
 (0)