-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: migrate FailedAuthRequest to pg #9500
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 32 additions & 20 deletions
52
packages/server/graphql/mutations/helpers/attemptLogin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,56 @@ | ||
import bcrypt from 'bcryptjs' | ||
import {sql} from 'kysely' | ||
import ms from 'ms' | ||
import {AuthenticationError, Threshold} from 'parabol-client/types/constEnums' | ||
import sleep from 'parabol-client/utils/sleep' | ||
import {AuthIdentityTypeEnum} from '../../../../client/types/constEnums' | ||
import getRethink from '../../../database/rethinkDriver' | ||
import {RDatum} from '../../../database/stricterR' | ||
import getKysely from '../../../postgres/getKysely' | ||
import AuthIdentityLocal from '../../../database/types/AuthIdentityLocal' | ||
import AuthToken from '../../../database/types/AuthToken' | ||
import FailedAuthRequest from '../../../database/types/FailedAuthRequest' | ||
import {getUserByEmail} from '../../../postgres/queries/getUsersByEmails' | ||
|
||
const logFailedLogin = async (ip: string, email: string) => { | ||
const r = await getRethink() | ||
const pg = getKysely() | ||
if (ip) { | ||
const failedAuthRequest = new FailedAuthRequest({ip, email}) | ||
await r.table('FailedAuthRequest').insert(failedAuthRequest).run() | ||
await pg.insertInto('FailedAuthRequest').values(failedAuthRequest).execute() | ||
} | ||
} | ||
|
||
const attemptLogin = async (denormEmail: string, password: string, ip = '') => { | ||
const r = await getRethink() | ||
const pg = getKysely() | ||
const yesterday = new Date(Date.now() - ms('1d')) | ||
const email = denormEmail.toLowerCase().trim() | ||
|
||
const existingUser = await getUserByEmail(email) | ||
const {failOnAccount, failOnTime} = await r({ | ||
failOnAccount: r | ||
.table('FailedAuthRequest') | ||
.getAll(ip, {index: 'ip'}) | ||
.filter({email}) | ||
.filter((row: RDatum) => row('time').ge(yesterday)) | ||
.count() | ||
.ge(Threshold.MAX_ACCOUNT_PASSWORD_ATTEMPTS) as unknown as boolean, | ||
failOnTime: r | ||
.table('FailedAuthRequest') | ||
.getAll(ip, {index: 'ip'}) | ||
.filter((row: RDatum) => row('time').ge(yesterday)) | ||
.count() | ||
.ge(Threshold.MAX_DAILY_PASSWORD_ATTEMPTS) as unknown as boolean | ||
}).run() | ||
const {failOnAccount, failOnTime} = (await pg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an equivalent query to what we were doing with Rethink. Just 1 trip and returns an object with these two fields. While the Kysely is pretty readable, here's what the query compiles to: WITH "byAccount" AS (
SELECT
COUNT("id") AS "attempts"
FROM
"FailedAuthRequest"
WHERE
"ip" = $1
AND "email" = $2
AND "time" >= $3
),
"byTime" AS (
SELECT
COUNT("id") AS "attempts"
FROM
"FailedAuthRequest"
WHERE
"ip" = $4
AND "time" >= $5
)
SELECT
"byAccount"."attempts" >= $6 AS "failOnAccount",
"byTime"."attempts" >= $7 AS "failOnTime"
FROM
"byAccount",
"byTime"; |
||
.with('byAccount', (qb) => | ||
qb | ||
.selectFrom('FailedAuthRequest') | ||
.select((eb) => eb.fn.count<number>('id').as('attempts')) | ||
.where('ip', '=', ip) | ||
.where('email', '=', email) | ||
.where('time', '>=', yesterday) | ||
) | ||
.with('byTime', (qb) => | ||
qb | ||
.selectFrom('FailedAuthRequest') | ||
.select((eb) => eb.fn.count<number>('id').as('attempts')) | ||
.where('ip', '=', ip) | ||
.where('time', '>=', yesterday) | ||
) | ||
.selectFrom(['byAccount', 'byTime']) | ||
.select(({ref}) => [ | ||
sql<boolean>`${ref('byAccount.attempts')} >= ${Threshold.MAX_ACCOUNT_PASSWORD_ATTEMPTS}`.as( | ||
'failOnAccount' | ||
), | ||
sql<boolean>`${ref('byTime.attempts')} >= ${Threshold.MAX_DAILY_PASSWORD_ATTEMPTS}`.as( | ||
'failOnTime' | ||
) | ||
]) | ||
.executeTakeFirst()) as {failOnAccount: boolean; failOnTime: boolean} | ||
|
||
if (failOnAccount || failOnTime) { | ||
await sleep(1000) | ||
// silently fail to trick security researchers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/server/postgres/migrations/1709927369000_addFailedAuthRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Client} from 'pg' | ||
import getPgConfig from '../getPgConfig' | ||
|
||
export async function up() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
CREATE TABLE "FailedAuthRequest" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"email" "citext" NOT NULL, | ||
"ip" "inet" NOT NULL, | ||
"time" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS "idx_FailedAuthRequest_email" ON "FailedAuthRequest"("email"); | ||
CREATE INDEX IF NOT EXISTS "idx_FailedAuthRequest_ip" ON "FailedAuthRequest"("ip"); | ||
`) | ||
await client.end() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DROP TABLE IF EXISTS "FailedAuthRequest"; | ||
`) | ||
await client.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id now managed by Postgres