Skip to content

Updated 2FA to clarify different modes #22817

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 2 commits into from
Apr 15, 2025
Merged

Updated 2FA to clarify different modes #22817

merged 2 commits into from
Apr 15, 2025

Conversation

ErisDS
Copy link
Member

@ErisDS ErisDS commented Apr 4, 2025

ref https://linear.app/ghost/issue/ENG-2074/adjust-verification-copy-if-2fa-is-enabled-for-all-logins

  • 2FA is actually two features: New device detection, and forced/required 2FA
  • These two features do the same thing, they both add security to logins by requiring a code is provided from email
  • However, depending on what mode you are in, the feature can be expected, or a bit of a surprise
  • We want to handle them slightly differently, so we can make it clearer to users what is happening and why

Copy link
Contributor

coderabbitai bot commented Apr 4, 2025

Walkthrough

The changes update the error handling for two-factor authentication in both the admin AJAX service and the core session middleware. In the AJAX service, the function formerly checking against a single error code now uses an array of two-factor authentication codes and calls a helper to correctly parse JSON payloads. In the session middleware, the error code is chosen dynamically based on whether verification is required, and additional context is provided with the error message. Corresponding unit, end-to-end, and integration tests have been updated to use async/await syntax, verify the new error codes, and simulate the necessary service behavior. These adjustments ensure the system distinguishes between a generic two-factor token requirement and a new device detection scenario.

Possibly related PRs

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ghost/admin/app/services/ajax.js

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser '@babel/eslint-parser' declared in 'ghost/admin/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /ghost/admin/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
ghost/admin/tests/integration/services/ajax-test.js

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser '@babel/eslint-parser' declared in 'ghost/admin/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /ghost/admin/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e34e369 and fda1594.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/e2e-api/admin/__snapshots__/session.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • ghost/admin/app/services/ajax.js (1 hunks)
  • ghost/admin/tests/integration/services/ajax-test.js (1 hunks)
  • ghost/core/core/server/services/auth/session/middleware.js (1 hunks)
  • ghost/core/test/e2e-api/admin/session.test.js (2 hunks)
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • ghost/core/core/server/services/auth/session/middleware.js
  • ghost/core/test/e2e-api/admin/session.test.js
  • ghost/admin/app/services/ajax.js
  • ghost/admin/tests/integration/services/ajax-test.js
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: Ghost-CLI tests
  • GitHub Check: Unit tests (Node 22.13.1)
  • GitHub Check: Database tests (Node 20.11.1, sqlite3)
  • GitHub Check: Regression tests (Node 20.11.1, sqlite3)
  • GitHub Check: Unit tests (Node 20.11.1)
  • GitHub Check: Database tests (Node 22.13.1, mysql8)
  • GitHub Check: Regression tests (Node 20.11.1, mysql8)
  • GitHub Check: Unit tests (Node 18.12.1)
  • GitHub Check: Database tests (Node 20.11.1, mysql8)
  • GitHub Check: Database tests (Node 18.12.1, mysql8)
  • GitHub Check: Admin tests - Chrome
  • GitHub Check: Lint

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ErisDS ErisDS force-pushed the 2fa-admin-errors branch 2 times, most recently from 6804d87 to a22fadd Compare April 4, 2025 14:36
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
ghost/admin/app/controllers/signin.js (1)

60-66: Enhanced 2FA error handling approach

This change improves the system's ability to handle different types of 2FA errors by capturing the specific error code from the payload and storing it in the session before transitioning. This will allow more specific handling of different 2FA scenarios downstream.

Consider adding a fallback in case the error structure doesn't match expectations:

-let errorCode = error.payload?.errors[0]?.code;
+let errorCode = error.payload?.errors?.[0]?.code || '2FA_TOKEN_REQUIRED';
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a22fadd and b6e3210.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/e2e-api/admin/__snapshots__/session.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • ghost/admin/app/controllers/signin-verify.js (1 hunks)
  • ghost/admin/app/controllers/signin.js (1 hunks)
  • ghost/admin/app/templates/signin-verify.hbs (2 hunks)
  • ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (6 hunks)
  • ghost/core/test/e2e-api/admin/session.test.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup
🔇 Additional comments (3)
ghost/core/test/e2e-api/admin/session.test.js (1)

107-107: Error code change from 2FA_TOKEN_REQUIRED to 2FA_NEW_DEVICE_DETECTED looks appropriate.

The updated error code provides more specific context about why 2FA verification is required (new device detection) rather than just indicating that a token is needed. This change aligns with the PR objective of enhancing error handling for different 2FA scenarios.

Also applies to: 136-136

ghost/admin/app/templates/signin-verify.hbs (2)

7-7: Effective conditional message based on authentication context

The conditional header text properly differentiates between standard verification and 2FA confirmation contexts.


27-27: Security improvement for input placeholder

Changing the placeholder from a numeric example to asterisks is a security improvement that avoids revealing the expected input format.

@ErisDS ErisDS force-pushed the 2fa-admin-errors branch 2 times, most recently from fba46eb to 4c4a90b Compare April 8, 2025 10:04
@TryGhost TryGhost deleted a comment from coderabbitai bot Apr 8, 2025
@ErisDS ErisDS force-pushed the 2fa-admin-errors branch 4 times, most recently from 1e0a4fe to 91ad6d8 Compare April 10, 2025 13:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (1)

1-91: Consider adding tests for the new device detection error case.

While the first test verifies that 2FA_NEW_DEVICE_DETECTED is correctly identified as a 2FA error, the second test only tests the modal behavior with 2FA_TOKEN_REQUIRED. Consider adding another test or extending the current one to verify that the re-verify modal is also opened when encountering a 2FA_NEW_DEVICE_DETECTED error.

it('calls re-verify modal for new device detection 2FA error', async function () {
    const modals = this.owner.lookup('service:modals');
    const openSpy = sinon.spy(modals, 'open');

    const handleError = async (error) => {
        if (isTwoFactorTokenRequiredError(error)) {
            await modals.open('editor/modals/re-verify');
            return true;
        }
        return false;
    };

    const newDeviceError = {
        payload: {
            errors: [{
                code: '2FA_NEW_DEVICE_DETECTED'
            }]
        }
    };

    await handleError(newDeviceError);

    expect(openSpy.calledOnce, 're-verify modal called once for new device error').to.be.true;
    expect(openSpy.firstCall.args[0]).to.equal('editor/modals/re-verify');
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1e0a4fe and 91ad6d8.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/e2e-api/admin/__snapshots__/session.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (7)
  • ghost/admin/app/services/ajax.js (1 hunks)
  • ghost/admin/tests/integration/services/ajax-test.js (1 hunks)
  • ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (1 hunks)
  • ghost/core/core/server/services/auth/session/middleware.js (1 hunks)
  • ghost/core/core/server/services/auth/session/session-service.js (2 hunks)
  • ghost/core/test/e2e-api/admin/session.test.js (2 hunks)
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • ghost/core/core/server/services/auth/session/middleware.js
  • ghost/core/test/e2e-api/admin/session.test.js
  • ghost/core/core/server/services/auth/session/session-service.js
  • ghost/admin/app/services/ajax.js
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js
  • ghost/admin/tests/integration/services/ajax-test.js
🧰 Additional context used
🧬 Code Graph Analysis (1)
ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (1)
ghost/admin/app/services/ajax.js (1)
  • isTwoFactorTokenRequiredError (202-211)
🔇 Additional comments (3)
ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (3)

1-14: Good setup with proper testing dependencies and service stub.

The file correctly imports necessary testing utilities and sets up a clean ModalsStub implementation that returns a Promise, mimicking the actual service behavior.


15-21: Well-structured test suite with proper service registration.

The test suite is properly configured with setupTest() and correctly registers the stub service before each test, following best practices for Ember testing.


66-90: Good test for modal invocation with proper spy usage.

This test effectively verifies that the error handler opens the re-verify modal exactly once for 2FA errors. The spy is properly set up to monitor the modal's open method, and the assertions correctly verify both that it was called once and with the expected modal name.

@ErisDS ErisDS force-pushed the 2fa-admin-errors branch from 91ad6d8 to e34e369 Compare April 10, 2025 13:53
ErisDS added 2 commits April 14, 2025 11:47
- We're going to be diversifying the error codes returned from
  the auth middleware, depending on different cases
- This makes sure that ghost admin will still handle the cases correctly
@ErisDS ErisDS force-pushed the 2fa-admin-errors branch from e34e369 to fda1594 Compare April 14, 2025 10:50
@ErisDS ErisDS changed the title Updated admin to handle different 2FA errors Updated 2FA to clarify different modes Apr 14, 2025
@ErisDS ErisDS merged commit e8b1305 into main Apr 15, 2025
28 checks passed
@ErisDS ErisDS deleted the 2fa-admin-errors branch April 15, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant