|
| 1 | +import { |
| 2 | + hash, |
| 3 | + isHashMatch, |
| 4 | + handlePasswordValidation, |
| 5 | + PasswordMethod, |
| 6 | + getPasswordMethod, |
| 7 | + hashLegacy, |
| 8 | + isHashLegacyMatch, |
| 9 | + isCookieValid, |
| 10 | + sanitizeString, |
| 11 | +} from "../../../src/node/util" |
| 12 | + |
1 | 13 | describe("getEnvPaths", () => {
|
2 | 14 | describe("on darwin", () => {
|
3 | 15 | let ORIGINAL_PLATFORM = ""
|
@@ -145,3 +157,253 @@ describe("getEnvPaths", () => {
|
145 | 157 | })
|
146 | 158 | })
|
147 | 159 | })
|
| 160 | + |
| 161 | +describe("hash", () => { |
| 162 | + it("should return a hash of the string passed in", async () => { |
| 163 | + const plainTextPassword = "mySecretPassword123" |
| 164 | + const hashed = await hash(plainTextPassword) |
| 165 | + expect(hashed).not.toBe(plainTextPassword) |
| 166 | + }) |
| 167 | +}) |
| 168 | + |
| 169 | +describe("isHashMatch", () => { |
| 170 | + it("should return true if the password matches the hash", async () => { |
| 171 | + const password = "codeserver1234" |
| 172 | + const _hash = await hash(password) |
| 173 | + const actual = await isHashMatch(password, _hash) |
| 174 | + expect(actual).toBe(true) |
| 175 | + }) |
| 176 | + it("should return false if the password does not match the hash", async () => { |
| 177 | + const password = "password123" |
| 178 | + const _hash = await hash(password) |
| 179 | + const actual = await isHashMatch("otherPassword123", _hash) |
| 180 | + expect(actual).toBe(false) |
| 181 | + }) |
| 182 | + it("should return true with actual hash", async () => { |
| 183 | + const password = "password123" |
| 184 | + const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY" |
| 185 | + const actual = await isHashMatch(password, _hash) |
| 186 | + expect(actual).toBe(true) |
| 187 | + }) |
| 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 | + }) |
| 200 | +}) |
| 201 | + |
| 202 | +describe("hashLegacy", () => { |
| 203 | + it("should return a hash of the string passed in", () => { |
| 204 | + const plainTextPassword = "mySecretPassword123" |
| 205 | + const hashed = hashLegacy(plainTextPassword) |
| 206 | + expect(hashed).not.toBe(plainTextPassword) |
| 207 | + }) |
| 208 | +}) |
| 209 | + |
| 210 | +describe("isHashLegacyMatch", () => { |
| 211 | + it("should return true if is match", () => { |
| 212 | + const password = "password123" |
| 213 | + const _hash = hashLegacy(password) |
| 214 | + expect(isHashLegacyMatch(password, _hash)).toBe(true) |
| 215 | + }) |
| 216 | + it("should return false if is match", () => { |
| 217 | + const password = "password123" |
| 218 | + const _hash = hashLegacy(password) |
| 219 | + expect(isHashLegacyMatch("otherPassword123", _hash)).toBe(false) |
| 220 | + }) |
| 221 | + it("should return true if hashed from command line", () => { |
| 222 | + const password = "password123" |
| 223 | + // Hashed using printf "password123" | sha256sum | cut -d' ' -f1 |
| 224 | + const _hash = "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f" |
| 225 | + expect(isHashLegacyMatch(password, _hash)).toBe(true) |
| 226 | + }) |
| 227 | +}) |
| 228 | + |
| 229 | +describe("getPasswordMethod", () => { |
| 230 | + it("should return PLAIN_TEXT for no hashed password", () => { |
| 231 | + const hashedPassword = undefined |
| 232 | + const passwordMethod = getPasswordMethod(hashedPassword) |
| 233 | + const expected: PasswordMethod = "PLAIN_TEXT" |
| 234 | + expect(passwordMethod).toEqual(expected) |
| 235 | + }) |
| 236 | + it("should return ARGON2 for password with 'argon2'", () => { |
| 237 | + const hashedPassword = |
| 238 | + "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY" |
| 239 | + const passwordMethod = getPasswordMethod(hashedPassword) |
| 240 | + const expected: PasswordMethod = "ARGON2" |
| 241 | + expect(passwordMethod).toEqual(expected) |
| 242 | + }) |
| 243 | + it("should return SHA256 for password with legacy hash", () => { |
| 244 | + const hashedPassword = "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af" |
| 245 | + const passwordMethod = getPasswordMethod(hashedPassword) |
| 246 | + const expected: PasswordMethod = "SHA256" |
| 247 | + expect(passwordMethod).toEqual(expected) |
| 248 | + }) |
| 249 | +}) |
| 250 | + |
| 251 | +describe("handlePasswordValidation", () => { |
| 252 | + it("should return true with a hashedPassword for a PLAIN_TEXT password", async () => { |
| 253 | + const p = "password" |
| 254 | + const passwordValidation = await handlePasswordValidation({ |
| 255 | + passwordMethod: "PLAIN_TEXT", |
| 256 | + passwordFromRequestBody: p, |
| 257 | + passwordFromArgs: p, |
| 258 | + hashedPasswordFromArgs: undefined, |
| 259 | + }) |
| 260 | + |
| 261 | + const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword) |
| 262 | + |
| 263 | + expect(passwordValidation.isPasswordValid).toBe(true) |
| 264 | + expect(matchesHash).toBe(true) |
| 265 | + }) |
| 266 | + it("should return false when PLAIN_TEXT password doesn't match args", async () => { |
| 267 | + const p = "password" |
| 268 | + const passwordValidation = await handlePasswordValidation({ |
| 269 | + passwordMethod: "PLAIN_TEXT", |
| 270 | + passwordFromRequestBody: "password1", |
| 271 | + passwordFromArgs: p, |
| 272 | + hashedPasswordFromArgs: undefined, |
| 273 | + }) |
| 274 | + |
| 275 | + const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword) |
| 276 | + |
| 277 | + expect(passwordValidation.isPasswordValid).toBe(false) |
| 278 | + expect(matchesHash).toBe(false) |
| 279 | + }) |
| 280 | + it("should return true with a hashedPassword for a SHA256 password", async () => { |
| 281 | + const p = "helloworld" |
| 282 | + const passwordValidation = await handlePasswordValidation({ |
| 283 | + passwordMethod: "SHA256", |
| 284 | + passwordFromRequestBody: p, |
| 285 | + passwordFromArgs: undefined, |
| 286 | + hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", |
| 287 | + }) |
| 288 | + |
| 289 | + const matchesHash = isHashLegacyMatch(p, passwordValidation.hashedPassword) |
| 290 | + |
| 291 | + expect(passwordValidation.isPasswordValid).toBe(true) |
| 292 | + expect(matchesHash).toBe(true) |
| 293 | + }) |
| 294 | + it("should return false when SHA256 password doesn't match hash", async () => { |
| 295 | + const p = "helloworld1" |
| 296 | + const passwordValidation = await handlePasswordValidation({ |
| 297 | + passwordMethod: "SHA256", |
| 298 | + passwordFromRequestBody: p, |
| 299 | + passwordFromArgs: undefined, |
| 300 | + hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", |
| 301 | + }) |
| 302 | + |
| 303 | + const matchesHash = isHashLegacyMatch(p, passwordValidation.hashedPassword) |
| 304 | + |
| 305 | + expect(passwordValidation.isPasswordValid).toBe(false) |
| 306 | + expect(matchesHash).toBe(false) |
| 307 | + }) |
| 308 | + it("should return true with a hashedPassword for a ARGON2 password", async () => { |
| 309 | + const p = "password" |
| 310 | + const passwordValidation = await handlePasswordValidation({ |
| 311 | + passwordMethod: "ARGON2", |
| 312 | + passwordFromRequestBody: p, |
| 313 | + passwordFromArgs: undefined, |
| 314 | + hashedPasswordFromArgs: |
| 315 | + "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 316 | + }) |
| 317 | + |
| 318 | + const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword) |
| 319 | + |
| 320 | + expect(passwordValidation.isPasswordValid).toBe(true) |
| 321 | + expect(matchesHash).toBe(true) |
| 322 | + }) |
| 323 | + it("should return false when ARGON2 password doesn't match hash", async () => { |
| 324 | + const p = "password1" |
| 325 | + const passwordValidation = await handlePasswordValidation({ |
| 326 | + passwordMethod: "ARGON2", |
| 327 | + passwordFromRequestBody: p, |
| 328 | + passwordFromArgs: undefined, |
| 329 | + hashedPasswordFromArgs: |
| 330 | + "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 331 | + }) |
| 332 | + |
| 333 | + const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword) |
| 334 | + |
| 335 | + expect(passwordValidation.isPasswordValid).toBe(false) |
| 336 | + expect(matchesHash).toBe(false) |
| 337 | + }) |
| 338 | +}) |
| 339 | + |
| 340 | +describe("isCookieValid", () => { |
| 341 | + it("should be valid if hashed-password for SHA256 matches cookie.key", async () => { |
| 342 | + const isValid = await isCookieValid({ |
| 343 | + passwordMethod: "SHA256", |
| 344 | + cookieKey: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", |
| 345 | + hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", |
| 346 | + passwordFromArgs: undefined, |
| 347 | + }) |
| 348 | + expect(isValid).toBe(true) |
| 349 | + }) |
| 350 | + it("should be invalid if hashed-password for SHA256 does not match cookie.key", async () => { |
| 351 | + const isValid = await isCookieValid({ |
| 352 | + passwordMethod: "SHA256", |
| 353 | + cookieKey: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb9442bb6f8f8f07af", |
| 354 | + hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", |
| 355 | + passwordFromArgs: undefined, |
| 356 | + }) |
| 357 | + expect(isValid).toBe(false) |
| 358 | + }) |
| 359 | + it("should be valid if hashed-password for ARGON2 matches cookie.key", async () => { |
| 360 | + const isValid = await isCookieValid({ |
| 361 | + passwordMethod: "ARGON2", |
| 362 | + cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 363 | + hashedPasswordFromArgs: |
| 364 | + "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 365 | + passwordFromArgs: undefined, |
| 366 | + }) |
| 367 | + expect(isValid).toBe(true) |
| 368 | + }) |
| 369 | + it("should be invalid if hashed-password for ARGON2 does not match cookie.key", async () => { |
| 370 | + const isValid = await isCookieValid({ |
| 371 | + passwordMethod: "ARGON2", |
| 372 | + cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H", |
| 373 | + hashedPasswordFromArgs: |
| 374 | + "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 375 | + passwordFromArgs: undefined, |
| 376 | + }) |
| 377 | + expect(isValid).toBe(false) |
| 378 | + }) |
| 379 | + it("should be valid if password for PLAIN_TEXT matches cookie.key", async () => { |
| 380 | + const isValid = await isCookieValid({ |
| 381 | + passwordMethod: "PLAIN_TEXT", |
| 382 | + cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY", |
| 383 | + passwordFromArgs: "password", |
| 384 | + hashedPasswordFromArgs: undefined, |
| 385 | + }) |
| 386 | + expect(isValid).toBe(true) |
| 387 | + }) |
| 388 | + it("should be invalid if hashed-password for PLAIN_TEXT does not match cookie.key", async () => { |
| 389 | + const isValid = await isCookieValid({ |
| 390 | + passwordMethod: "PLAIN_TEXT", |
| 391 | + cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H", |
| 392 | + passwordFromArgs: "password1234", |
| 393 | + hashedPasswordFromArgs: undefined, |
| 394 | + }) |
| 395 | + expect(isValid).toBe(false) |
| 396 | + }) |
| 397 | +}) |
| 398 | + |
| 399 | +describe("sanitizeString", () => { |
| 400 | + it("should return an empty string if passed a type other than a string", () => { |
| 401 | + expect(sanitizeString({} as string)).toBe("") |
| 402 | + }) |
| 403 | + it("should trim whitespace", () => { |
| 404 | + expect(sanitizeString(" hello ")).toBe("hello") |
| 405 | + }) |
| 406 | + it("should always return an empty string", () => { |
| 407 | + expect(sanitizeString(" ")).toBe("") |
| 408 | + }) |
| 409 | +}) |
0 commit comments