-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[VUL-78] Add IP-based lockout to login endpoint #18947
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d93d51f
Refactor: lockout by emailLockout
jvcalderon 9da1598
ipLockout middleware
jvcalderon f7fcff2
Add tests
jvcalderon 59a73e2
Merge branch 'master' into feature/vul-78-hardening-measure-sheets
jvcalderon 2fddce3
Run ipLockout before emailLockout on the login route
jvcalderon 68ad5e7
Lower LOGIN_IP_LOCKOUT_LIMIT default from 30 to 10
jvcalderon cdca660
Use atomic Redis INCR for IP lockout counter
jvcalderon ff1a1da
Merge branch 'master' into feature/vul-78-hardening-measure-sheets
jvcalderon debc100
Fix test
jvcalderon a4b9e42
Merge branch 'master' into feature/vul-78-hardening-measure-sheets
jvcalderon 68c43ab
Merge branch 'master' into feature/vul-78-hardening-measure-sheets
jvcalderon c92b573
Fix: always refresh TTL on increment to prevent permanent counters
jvcalderon 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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,3 +1,4 @@ | ||
| export { default as cloudRestricted } from "./cloudRestricted" | ||
| export { handleScimBody } from "./handleScimBody" | ||
| export { default as lockout } from "./lockout" | ||
| export { default as emailLockout } from "./emailLockout" | ||
| export { default as ipLockout } from "./ipLockout" |
This file contains hidden or 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,32 @@ | ||
| import { cache } from "@budibase/backend-core" | ||
| import { Ctx } from "@budibase/types" | ||
| import { Next } from "koa" | ||
| import env from "../environment" | ||
|
|
||
| const ipKey = (ip: string) => `auth:login:ip:${ip}` | ||
|
|
||
| /** | ||
| * Middleware to rate-limit login attempts by source IP. | ||
| * Blocks with 429 once the request count exceeds LOGIN_IP_LOCKOUT_LIMIT | ||
| * within the LOGIN_LOCKOUT_SECONDS window. | ||
| */ | ||
| export default async (ctx: Ctx, next: Next) => { | ||
| const ip = (ctx.ip || "").toString() | ||
|
|
||
| if (!ip) { | ||
| return await next() | ||
| } | ||
|
|
||
| const key = ipKey(ip) | ||
| const count = await cache.increment(key, env.LOGIN_LOCKOUT_SECONDS) | ||
|
|
||
| if (count > env.LOGIN_IP_LOCKOUT_LIMIT) { | ||
| ctx.set("Retry-After", String(env.LOGIN_LOCKOUT_SECONDS)) | ||
| console.log( | ||
| `[auth] login blocked due to IP lockout ip=${ip} count=${count}` | ||
| ) | ||
| return ctx.throw(429, "Too many login attempts. Try again later.") | ||
| } | ||
|
|
||
| return await next() | ||
| } |
This file contains hidden or 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 hidden or 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,81 @@ | ||
| import ipLockout from "../ipLockout" | ||
| import { cache } from "@budibase/backend-core" | ||
| import env from "../../environment" | ||
|
|
||
| jest.mock("@budibase/backend-core", () => ({ | ||
| cache: { | ||
| increment: jest.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| describe("ipLockout middleware", () => { | ||
| let ctx: any | ||
| let next: jest.Mock | ||
|
|
||
| beforeEach(() => { | ||
| ctx = { | ||
| ip: "1.2.3.4", | ||
| set: jest.fn(), | ||
| throw: jest.fn(), | ||
| } | ||
| next = jest.fn() | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it("should call next if no IP is present", async () => { | ||
| ctx.ip = "" | ||
|
|
||
| await ipLockout(ctx, next) | ||
|
|
||
| expect(next).toHaveBeenCalled() | ||
| expect(ctx.throw).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should call next when count is below the limit", async () => { | ||
| jest.mocked(cache.increment).mockResolvedValue(1) | ||
|
|
||
| await ipLockout(ctx, next) | ||
|
|
||
| expect(cache.increment).toHaveBeenCalledWith( | ||
| "auth:login:ip:1.2.3.4", | ||
| env.LOGIN_LOCKOUT_SECONDS | ||
| ) | ||
| expect(next).toHaveBeenCalled() | ||
| expect(ctx.throw).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should call next when count equals the limit", async () => { | ||
| jest.mocked(cache.increment).mockResolvedValue(env.LOGIN_IP_LOCKOUT_LIMIT) | ||
|
|
||
| await ipLockout(ctx, next) | ||
|
|
||
| expect(next).toHaveBeenCalled() | ||
| expect(ctx.throw).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should throw 429 with Retry-After when count exceeds the limit", async () => { | ||
| jest | ||
| .mocked(cache.increment) | ||
| .mockResolvedValue(env.LOGIN_IP_LOCKOUT_LIMIT + 1) | ||
|
|
||
| ctx.throw = jest.fn().mockImplementation((status, message) => { | ||
| const error = new Error(message) | ||
| ;(error as any).status = status | ||
| throw error | ||
| }) | ||
|
|
||
| await expect(ipLockout(ctx, next)).rejects.toThrow( | ||
| "Too many login attempts. Try again later." | ||
| ) | ||
|
|
||
| expect(next).not.toHaveBeenCalled() | ||
| expect(ctx.set).toHaveBeenCalledWith( | ||
| "Retry-After", | ||
| String(env.LOGIN_LOCKOUT_SECONDS) | ||
| ) | ||
| expect(ctx.throw).toHaveBeenCalledWith( | ||
| 429, | ||
| "Too many login attempts. Try again later." | ||
| ) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.