Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: leaked password detection does not work

Blue Mouse1186f5e6706a
diff --git a/backend/apps/cloud/src/auth/auth.controller.ts b/backend/apps/cloud/src/auth/auth.controller.ts
index bc151fb0e..1f0441359 100644
--- a/backend/apps/cloud/src/auth/auth.controller.ts
+++ b/backend/apps/cloud/src/auth/auth.controller.ts
@@ -105,7 +105,9 @@ export class AuthController {
const isLeaked = await this.authService.checkIfLeaked(body.password)
if (isLeaked) {
- throw new ConflictException(i18n.t('auth.leakedPassword'))
+ throw new ConflictException(
+ 'The provided password is leaked, please use another one',
+ )
}
}
diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.ts
index 69b1bfe0a..1be8ee77a 100644
--- a/backend/apps/cloud/src/auth/auth.service.ts
+++ b/backend/apps/cloud/src/auth/auth.service.ts
@@ -95,27 +95,23 @@ export class AuthService {
})
}
- private getFirstFiveChars(passwordHash: string): string {
- return passwordHash.slice(0, 5)
- }
+ public async checkIfLeaked(password: string): Promise<boolean> {
+ const sha1Hash = await this.createSha1Hash(password)
+ const firstFiveChars = sha1Hash.slice(0, 5)
+ const lastChars = sha1Hash.slice(5)
- private async sendRequestToApi(passwordHash: string) {
- const url = `https://api.pwnedpasswords.com/range/${passwordHash}`
- const response = await axios.get(url)
+ const response = await axios.get(
+ `https://api.pwnedpasswords.com/range/${firstFiveChars}`,
+ )
if (response.status !== 200) {
- throw new InternalServerErrorException()
+ console.error(
+ `[ERROR][AuthService -> checkIfLeaked]: Failed to get pwned passwords for ${firstFiveChars}: ${response.status}`,
+ )
+ return false
}
- return response.data
- }
-
- public async checkIfLeaked(password: string): Promise<boolean> {
- const sha1Hash = await this.createSha1Hash(password)
- const firstFiveChars = this.getFirstFiveChars(sha1Hash)
- const response = await this.sendRequestToApi(firstFiveChars)
-
- return response.includes(firstFiveChars)
+ return response.data.includes(lastChars)
}
public async hashPassword(password: string): Promise<string> {
diff --git a/backend/apps/cloud/src/i18n/en/auth.json b/backend/apps/cloud/src/i18n/en/auth.json
index 5f090f3b4..aefbb5bc4 100644
--- a/backend/apps/cloud/src/i18n/en/auth.json
+++ b/backend/apps/cloud/src/i18n/en/auth.json
@@ -1,5 +1,4 @@
{
- "leakedPassword": "The password is in the leak database.",
"invalidCredentials": "Incorrect email or password.",
"invalidVerificationToken": "Invalid verification token.",
"accountNotExists": "An account with this email does not exist.",
diff --git a/backend/apps/community/src/auth/auth.controller.ts b/backend/apps/community/src/auth/auth.controller.ts
index b0d5c227a..225418e84 100644
--- a/backend/apps/community/src/auth/auth.controller.ts
+++ b/backend/apps/community/src/auth/auth.controller.ts
@@ -110,7 +110,9 @@ export class AuthController {
const isLeaked = await this.authService.checkIfLeaked(body.password)
if (isLeaked) {
- throw new ConflictException(i18n.t('auth.leakedPassword'))
+ throw new ConflictException(
+ 'The provided password is leaked, please use another one',
+ )
}
}
diff --git a/backend/apps/community/src/auth/auth.service.ts b/backend/apps/community/src/auth/auth.service.ts
index d3d97f4f5..5fab26454 100644
--- a/backend/apps/community/src/auth/auth.service.ts
+++ b/backend/apps/community/src/auth/auth.service.ts
@@ -57,27 +57,23 @@ export class AuthService {
})
}
- private getFirstFiveChars(passwordHash: string): string {
- return passwordHash.slice(0, 5)
- }
+ public async checkIfLeaked(password: string): Promise<boolean> {
+ const sha1Hash = await this.createSha1Hash(password)
+ const firstFiveChars = sha1Hash.slice(0, 5)
+ const lastChars = sha1Hash.slice(5)
- private async sendRequestToApi(passwordHash: string) {
- const url = `https://api.pwnedpasswords.com/range/${passwordHash}`
- const response = await axios.get(url)
+ const response = await axios.get(
+ `https://api.pwnedpasswords.com/range/${firstFiveChars}`,
+ )
if (response.status !== 200) {
- throw new InternalServerErrorException()
+ console.error(
+ `[ERROR][AuthService -> checkIfLeaked]: Failed to get pwned passwords for ${firstFiveChars}: ${response.status}`,
+ )
+ return false
}
- return response.data
- }
-
- public async checkIfLeaked(password: string): Promise<boolean> {
- const sha1Hash = await this.createSha1Hash(password)
- const firstFiveChars = this.getFirstFiveChars(sha1Hash)
- const response = await this.sendRequestToApi(firstFiveChars)
-
- return response.includes(firstFiveChars)
+ return response.data.includes(lastChars)
}
public async hashPassword(password: string): Promise<string> {
diff --git a/backend/apps/community/src/i18n/en/auth.json b/backend/apps/community/src/i18n/en/auth.json
index a90d2c2a9..a40e8bdb0 100644
--- a/backend/apps/community/src/i18n/en/auth.json
+++ b/backend/apps/community/src/i18n/en/auth.json
@@ -1,5 +1,4 @@
{
- "leakedPassword": "The password is in the leak database.",
"invalidCredentials": "Incorrect email or password.",
"invalidVerificationToken": "Invalid verification token.",
"accountNotExists": "An account with this email does not exist.",