forked from GaloyMoney/blink
-
Notifications
You must be signed in to change notification settings - Fork 8
feat(core): add test_accounts_captcha for staging CAPTCHA bypass #454
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
Open
blink-claw-bot
wants to merge
2
commits into
blinkbitcoin:main
Choose a base branch
from
blink-claw-bot:kn/test-accounts-captcha-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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
225 changes: 225 additions & 0 deletions
225
core/api/test/unit/app/auth/request-code-captcha-bypass.spec.ts
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,225 @@ | ||
| import { CaptchaUserFailToPassError } from "@/domain/captcha/errors" | ||
|
|
||
| // Mock functions declared at module scope — jest.fn() calls are hoisted. | ||
| const mockGetTestAccountsCaptcha = jest.fn() | ||
| const mockGetGeetestConfig = jest.fn() | ||
| const mockGetTestAccounts = jest.fn() | ||
| const mockGetAccountsOnboardConfig = jest.fn() | ||
| const mockGeetestValidate = jest.fn() | ||
| const mockConsumeLimiter = jest.fn() | ||
| const mockAddAttributesToCurrentSpan = jest.fn() | ||
| const mockRecordExceptionInCurrentSpan = jest.fn() | ||
|
|
||
| jest.mock("@/config", () => { | ||
| // Inline rate limit defaults — cannot reference outer const due to jest.mock hoisting | ||
| const rl = { points: 10, duration: 3600, blockDuration: 3600 } | ||
| return { | ||
| getTestAccountsCaptcha: (...args: unknown[]) => mockGetTestAccountsCaptcha(...args), | ||
| getGeetestConfig: (...args: unknown[]) => mockGetGeetestConfig(...args), | ||
| getTestAccounts: (...args: unknown[]) => mockGetTestAccounts(...args), | ||
| getAccountsOnboardConfig: (...args: unknown[]) => | ||
| mockGetAccountsOnboardConfig(...args), | ||
| getRequestCodePerEmailLimits: () => rl, | ||
| getRequestCodePerPhoneNumberLimits: () => rl, | ||
| getRequestCodePerIpLimits: () => rl, | ||
| getRequestTelegramPassportNoncePerPhoneNumberLimits: () => rl, | ||
| getRequestTelegramPassportNoncePerIpLimits: () => rl, | ||
| getLoginAttemptPerLoginIdentifierLimits: () => rl, | ||
| getFailedLoginAttemptPerIpLimits: () => rl, | ||
| getInvoiceCreateAttemptLimits: () => rl, | ||
| getInvoiceCreateForRecipientAttemptLimits: () => rl, | ||
| getOnChainAddressCreateAttemptLimits: () => rl, | ||
| getDeviceAccountCreateAttemptLimits: () => rl, | ||
| getAppcheckJtiAttemptLimits: () => rl, | ||
| getAddQuizPerIpLimits: () => rl, | ||
| getAddQuizPerPhoneLimits: () => rl, | ||
| getSmsAuthUnsupportedCountries: () => [], | ||
| getWhatsAppAuthUnsupportedCountries: () => [], | ||
| getTelegramAuthUnsupportedCountries: () => [], | ||
| TWILIO_ACCOUNT_SID: "test-sid", | ||
| UNSECURE_DEFAULT_LOGIN_CODE: undefined, | ||
| } | ||
| }) | ||
|
|
||
| jest.mock("@/services/geetest", () => ({ | ||
| __esModule: true, | ||
| default: jest.fn(() => ({ | ||
| validate: (...args: unknown[]) => mockGeetestValidate(...args), | ||
| })), | ||
| })) | ||
|
|
||
| jest.mock("@/services/rate-limit", () => ({ | ||
| consumeLimiter: (...args: unknown[]) => mockConsumeLimiter(...args), | ||
| })) | ||
|
|
||
| jest.mock("@/services/mongoose", () => ({ | ||
| UsersRepository: jest.fn(() => ({ | ||
| findByPhone: jest.fn(() => new Error("not found")), | ||
| })), | ||
| })) | ||
|
|
||
| jest.mock("@/services/phone-provider", () => ({ | ||
| getPhoneProviderVerifyService: jest.fn(() => ({ | ||
| initiateVerify: jest.fn(() => true), | ||
| })), | ||
| })) | ||
|
|
||
| jest.mock("@/services/phone-provider/twilio-service", () => ({ | ||
| TWILIO_ACCOUNT_TEST: "twilio-test-sid", | ||
| })) | ||
|
|
||
| jest.mock("@/services/ipfetcher", () => ({ | ||
| IpFetcher: jest.fn(() => ({ | ||
| fetchIPInfo: jest.fn(() => ({})), | ||
| })), | ||
| })) | ||
|
|
||
| jest.mock("@/services/logger", () => ({ | ||
| __esModule: true, | ||
| baseLogger: { | ||
| info: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn(), | ||
| debug: jest.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| jest.mock("@/services/tracing", () => ({ | ||
| addAttributesToCurrentSpan: (...args: unknown[]) => | ||
| mockAddAttributesToCurrentSpan(...args), | ||
| recordExceptionInCurrentSpan: (...args: unknown[]) => | ||
| mockRecordExceptionInCurrentSpan(...args), | ||
| })) | ||
|
|
||
| jest.mock("@/services/kratos", () => ({ | ||
| AuthWithEmailPasswordlessService: jest.fn(), | ||
| })) | ||
|
|
||
| import { requestPhoneCodeWithCaptcha } from "@/app/authentication/request-code" | ||
| import { baseLogger } from "@/services/logger" | ||
|
|
||
| const testPhone = "+16505551234" as PhoneNumber | ||
| const otherPhone = "+16505559999" as PhoneNumber | ||
| const testIp = "127.0.0.1" as IpAddress | ||
|
|
||
| const baseCaptchaArgs = { | ||
| geetestChallenge: "test-challenge", | ||
| geetestValidate: "test-validate", | ||
| geetestSeccode: "test-seccode", | ||
| ip: testIp, | ||
| channel: "sms", | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
|
|
||
| mockGetGeetestConfig.mockReturnValue({ id: "test-id", key: "test-key" }) | ||
| mockGetTestAccounts.mockReturnValue([]) | ||
| mockGetTestAccountsCaptcha.mockReturnValue([]) | ||
| mockGetAccountsOnboardConfig.mockReturnValue({ | ||
| phoneMetadataValidationSettings: { enabled: false }, | ||
| ipMetadataValidationSettings: { enabled: false }, | ||
| }) | ||
| mockConsumeLimiter.mockResolvedValue(true) | ||
| mockGeetestValidate.mockResolvedValue(true) | ||
| }) | ||
|
|
||
| describe("requestPhoneCodeWithCaptcha - CAPTCHA bypass for test accounts", () => { | ||
| describe("when phone IS in test_accounts_captcha", () => { | ||
| beforeEach(() => { | ||
| mockGetTestAccountsCaptcha.mockReturnValue([{ phone: testPhone }]) | ||
| }) | ||
|
|
||
| it("skips CAPTCHA validation and does not call Geetest.validate", async () => { | ||
| const result = await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(result).not.toBeInstanceOf(Error) | ||
| expect(mockGeetestValidate).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("proceeds to rate limiting after skipping CAPTCHA", async () => { | ||
| await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(mockConsumeLimiter).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("logs that CAPTCHA is being skipped", async () => { | ||
| await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(baseLogger.info).toHaveBeenCalledWith( | ||
| { phone: testPhone }, | ||
| "Skipping CAPTCHA validation for test account", | ||
| ) | ||
| }) | ||
|
|
||
| it("sets tracing attributes when CAPTCHA is skipped", async () => { | ||
| await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(mockAddAttributesToCurrentSpan).toHaveBeenCalledWith({ | ||
| "requestCode.captchaSkipped": true, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("when phone is NOT in test_accounts_captcha", () => { | ||
| beforeEach(() => { | ||
| mockGetTestAccountsCaptcha.mockReturnValue([{ phone: otherPhone }]) | ||
| }) | ||
|
|
||
| it("validates CAPTCHA normally via Geetest.validate", async () => { | ||
| await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(mockGeetestValidate).toHaveBeenCalledWith( | ||
| "test-challenge", | ||
| "test-validate", | ||
| "test-seccode", | ||
| ) | ||
| }) | ||
|
|
||
| it("returns error when CAPTCHA validation fails", async () => { | ||
| const captchaError = new CaptchaUserFailToPassError() | ||
| mockGeetestValidate.mockResolvedValue(captchaError) | ||
|
|
||
| const result = await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(result).toBeInstanceOf(CaptchaUserFailToPassError) | ||
| }) | ||
| }) | ||
|
|
||
| describe("when test_accounts_captcha is empty", () => { | ||
| beforeEach(() => { | ||
| mockGetTestAccountsCaptcha.mockReturnValue([]) | ||
| }) | ||
|
|
||
| it("validates CAPTCHA normally via Geetest.validate", async () => { | ||
| await requestPhoneCodeWithCaptcha({ | ||
| phone: testPhone, | ||
| ...baseCaptchaArgs, | ||
| }) | ||
|
|
||
| expect(mockGeetestValidate).toHaveBeenCalledWith( | ||
| "test-challenge", | ||
| "test-validate", | ||
| "test-seccode", | ||
| ) | ||
| }) | ||
| }) | ||
| }) | ||
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,36 @@ | ||
| import { getTestAccountsCaptcha } from "@/config/yaml" | ||
|
|
||
| describe("getTestAccountsCaptcha", () => { | ||
| it("returns phone numbers from config", () => { | ||
| const config = { | ||
| test_accounts_captcha: [{ phone: "+16505551234" }, { phone: "+16505555678" }], | ||
| } as unknown as YamlSchema | ||
|
|
||
| const result = getTestAccountsCaptcha(config) | ||
|
|
||
| expect(result).toEqual([{ phone: "+16505551234" }, { phone: "+16505555678" }]) | ||
| expect(result).toHaveLength(2) | ||
| }) | ||
|
|
||
| it("returns empty array when config has no test accounts", () => { | ||
| const config = { | ||
| test_accounts_captcha: [], | ||
| } as unknown as YamlSchema | ||
|
|
||
| const result = getTestAccountsCaptcha(config) | ||
|
|
||
| expect(result).toEqual([]) | ||
| expect(result).toHaveLength(0) | ||
| }) | ||
|
|
||
| it("returns single phone number from config", () => { | ||
| const config = { | ||
| test_accounts_captcha: [{ phone: "+16505551111" }], | ||
| } as unknown as YamlSchema | ||
|
|
||
| const result = getTestAccountsCaptcha(config) | ||
|
|
||
| expect(result).toEqual([{ phone: "+16505551111" }]) | ||
| expect(result).toHaveLength(1) | ||
| }) | ||
| }) |
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.
The comment about hoisting is misleading: Jest hoists
jest.mock()calls, butjest.fn()declarations are not hoisted. Consider rewording to avoid confusion (e.g., explain that the mock factory is hoisted and therefore must not reference later-initialized variables).