Skip to content
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

fix: use @eggjs/security and @eggjs/session #5384

Merged
merged 4 commits into from
Jan 19, 2025
Merged

fix: use @eggjs/security and @eggjs/session #5384

merged 4 commits into from
Jan 19, 2025

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jan 17, 2025

export Singleton

Summary by CodeRabbit

  • Dependencies

    • Replaced egg-security with @eggjs/security package
    • Replaced egg-session with @eggjs/session package
    • Updated security and session plugin references across documentation
  • Documentation

    • Updated plugin, security, and core documentation to reflect new package naming
    • Corrected GitHub repository links for security and session plugins
  • Configuration

    • Modified plugin configuration to use new @eggjs/security and @eggjs/session packages
    • Removed legacy security configuration from type definitions
  • Core Functionality

    • Added singleton instance management method
    • Enhanced type definitions for core framework components

Copy link

coderabbitai bot commented Jan 17, 2025

Warning

Rate limit exceeded

@fengmk2 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 0 minutes and 21 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between fb0b2d7 and d567afe.

📒 Files selected for processing (2)
  • test/index.test.ts (1 hunks)
  • test/lib/core/loader/config_loader.test.ts (1 hunks)

Walkthrough

This pull request represents a significant update to the Egg.js framework's security and plugin management. The primary change involves migrating from the egg-security package to the scoped @eggjs/security package. This transition impacts multiple aspects of the framework, including type definitions, documentation, configuration, and import statements. The changes are comprehensive, touching documentation files, configuration settings, and type declarations across the project.

Changes

File Change Summary
index-old.d.ts Removed imports for egg-session, egg-security, and @eggjs/schedule
package.json Replaced egg-security with @eggjs/security and egg-session with @eggjs/session dependencies
site/docs/* Updated references from egg-security to @eggjs/security and adjusted plugin URLs across multiple documentation files
src/config/plugin.ts Updated security and session plugin package references
src/index.ts Added export for ./lib/core/singleton.js
src/lib/core/singleton.ts Refactored type references and added type safety improvements
src/lib/egg.ts Added addSingleton method to EggCore interface
src/lib/types.ts Added new imports for @eggjs/security, @eggjs/schedule, and @eggjs/session; removed security property from EggAppConfig
test/index.test-d.ts Introduced new tests for singleton behavior and security configurations
site/docs/basics/controller.md Updated session plugin reference from "Session" to "session"
site/docs/core/cookie-and-session.md Updated session plugin reference from egg-session to @eggjs/session

Sequence Diagram

sequenceDiagram
    participant App as Egg Application
    participant Core as EggCore
    participant Security as @eggjs/security
    
    App->>Core: addSingleton('security', createMethod)
    Core->>Security: Initialize security plugin
    Security-->>Core: Return security configuration
    Core-->>App: Provide secure application context
Loading

Possibly related PRs

Poem

🐰 A Rabbit's Security Sonnet 🛡️
From egg-security to scoped delight,
@eggjs/security takes its flight!
Types refined, configs anew,
A framework dance, both brave and true.
Hop, hop, hooray for safer code! 🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

@fengmk2 fengmk2 changed the title refactor: use @eggjs/security plugin fix: use @eggjs/security plugin Jan 19, 2025
@fengmk2 fengmk2 changed the title fix: use @eggjs/security plugin fix: use @eggjs/security and @eggjs/session Jan 19, 2025
Copy link

@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: 0

🔭 Outside diff range comments (1)
src/lib/types.ts (1)

Documentation needed for security configuration migration

The removal of the security property from EggAppConfig is a breaking change that lacks proper migration documentation. While the @eggjs/security plugin is mentioned in the documentation as being "built into the framework", there are no clear instructions for users on:

  • How to migrate from the old configuration format
  • The new configuration structure with @eggjs/security
  • Examples of the new configuration format

This could lead to confusion and potential security misconfigurations during upgrades.

🔗 Analysis chain

Line range hint 1-1: Verify migration path for security configurations.

The removal of the security property from EggAppConfig is a breaking change. Users will need guidance on how to migrate their existing security configurations to the new @eggjs/security plugin format.

Please ensure that:

  1. Migration documentation exists for users to transition from the old configuration format to the new one
  2. The README or migration guide includes examples of the new configuration format
  3. Any automated migration tools or codemods are provided if possible
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for migration documentation and examples

# Look for migration guides or documentation updates
rg -l --type md "migration|upgrade|breaking.+change" docs/

# Check for examples of the new security configuration
rg -l --type md "@eggjs/security" docs/

Length of output: 192


Script:

#!/bin/bash
# Find all markdown files and search for relevant content
echo "=== Markdown files ==="
fd -e md

echo -e "\n=== Security migration content ==="
rg -l "security|@eggjs/security" -g "*.md"

echo -e "\n=== Breaking changes content ==="
rg -l "breaking.+changes?|migration|upgrade" -g "*.md"

echo -e "\n=== Package.json content ==="
cat package.json | jq -r '.homepage,.repository,.documentation'

Length of output: 5239


Script:

#!/bin/bash
echo "=== Security documentation content ==="
rg -A 5 -B 5 "@eggjs/security" "site/docs/core/security.md" || true

echo -e "\n=== Migration guide content ==="
rg -A 5 -B 5 "security|@eggjs/security" "site/docs/intro/migration.md" || true

echo -e "\n=== Recent changelog entries ==="
head -n 50 CHANGELOG.md

Length of output: 3257

🧹 Nitpick comments (3)
test/index.test-d.ts (3)

53-54: Enhance security plugin type tests.

The current test only covers csrf.headerName. Consider adding type tests for other critical CSRF configurations:

  • security.csrf.enable
  • security.csrf.ignoreJSON
  • security.csrf.useSession
  • security.csrf.cookieName
  • security.csrf.sessionName
 // security plugin types
 expectType<string>(app.config.security.csrf.headerName);
+expectType<boolean>(app.config.security.csrf.enable);
+expectType<boolean>(app.config.security.csrf.ignoreJSON);
+expectType<boolean>(app.config.security.csrf.useSession);
+expectType<string>(app.config.security.csrf.cookieName);
+expectType<string>(app.config.security.csrf.sessionName);

56-57: Enhance session plugin type tests.

The current test only covers httpOnly. Consider adding type tests for other important session configurations:

  • session.maxAge
  • session.key
  • session.encrypt
  • session.signed
  • session.sameSite
 // session plugin types
 expectType<boolean>(app.config.session.httpOnly);
+expectType<number>(app.config.session.maxAge);
+expectType<string>(app.config.session.key);
+expectType<boolean>(app.config.session.encrypt);
+expectType<boolean>(app.config.session.signed);
+expectType<boolean | 'lax' | 'strict' | 'none'>(app.config.session.sameSite);

161-171: Consider documenting the commented type test and enhancing type safety.

  1. The commented out expectType<Redis>(redis.get('client1')) appears intentional. Consider adding a comment explaining why this type check is expected to fail, or remove it if no longer needed.

  2. Consider enhancing type safety:

 class Redis {
+  private constructor() {}  // Ensure singleton pattern
+
   get(key: string) {
+    if (!key) throw new TypeError('key is required');
     return key;
   }
+
+  static create(): Redis & Singleton<Redis> {
+    return {} as Redis & Singleton<Redis>;
+  }
 }
-const redis = {} as Redis & Singleton<Redis>;
+const redis = Redis.create();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f1f8745 and fb0b2d7.

📒 Files selected for processing (11)
  • index-old.d.ts (0 hunks)
  • package.json (1 hunks)
  • site/docs/basics/controller.md (1 hunks)
  • site/docs/basics/controller.zh-CN.md (2 hunks)
  • site/docs/basics/plugin.md (1 hunks)
  • site/docs/basics/plugin.zh-CN.md (1 hunks)
  • site/docs/core/cookie-and-session.md (1 hunks)
  • site/docs/core/cookie-and-session.zh-CN.md (1 hunks)
  • src/config/plugin.ts (2 hunks)
  • src/lib/types.ts (1 hunks)
  • test/index.test-d.ts (3 hunks)
💤 Files with no reviewable changes (1)
  • index-old.d.ts
✅ Files skipped from review due to trivial changes (4)
  • site/docs/core/cookie-and-session.md
  • site/docs/basics/controller.md
  • site/docs/basics/controller.zh-CN.md
  • site/docs/core/cookie-and-session.zh-CN.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • site/docs/basics/plugin.zh-CN.md
  • site/docs/basics/plugin.md
  • src/config/plugin.ts
  • package.json
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Node.js / Test (windows-latest, 22)
  • GitHub Check: Node.js / Test (windows-latest, 20)
  • GitHub Check: Node.js / Test (windows-latest, 18.19.0)
  • GitHub Check: Node.js / Test (windows-latest, 18)
  • GitHub Check: Node.js / Test (ubuntu-latest, 22)
  • GitHub Check: Node.js / Test (ubuntu-latest, 20)
  • GitHub Check: Node.js / Test (ubuntu-latest, 18.19.0)
  • GitHub Check: Node.js / Test (ubuntu-latest, 18)
  • GitHub Check: Node.js / Test (macos-latest, 22)
  • GitHub Check: Node.js / Test (macos-latest, 20)
  • GitHub Check: Node.js / Test (macos-latest, 18)
  • GitHub Check: Node.js / Test (macos-latest, 18.19.0)
  • GitHub Check: Deploy-Preview
🔇 Additional comments (2)
test/index.test-d.ts (1)

9-9: LGTM!

The Singleton import is correctly added and properly used later in the file.

src/lib/types.ts (1)

25-27: Verify package dependencies for the new imports.

The imports look good and align with the migration to scoped packages. Let's verify that these dependencies are properly declared in package.json.

✅ Verification successful

Dependencies are properly configured for the new imports

All three packages (@eggjs/security, @eggjs/schedule, and @eggjs/session) are correctly declared in package.json, and no references to the legacy packages were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the imported packages are declared as dependencies

# Check if the packages are declared in package.json
jq -r '.dependencies | select(has("@eggjs/security") and has("@eggjs/schedule") and has("@eggjs/session"))' package.json

# Check if there are any remaining references to the old packages
rg -l "egg-security|egg-schedule|egg-session" --type json

Length of output: 1076

@fengmk2 fengmk2 merged commit d11ecd3 into next Jan 19, 2025
18 of 19 checks passed
@fengmk2 fengmk2 deleted the security-plugin branch January 19, 2025 15:43
fengmk2 pushed a commit that referenced this pull request Jan 19, 2025
[skip ci]

## [4.0.2](v4.0.1...v4.0.2) (2025-01-19)

### Bug Fixes

* use @eggjs/security and @eggjs/session ([#5384](#5384)) ([d11ecd3](d11ecd3))
Copy link

codecov bot commented Jan 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.15%. Comparing base (e5a697e) to head (d567afe).
Report is 3 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #5384      +/-   ##
==========================================
- Coverage   95.17%   95.15%   -0.02%     
==========================================
  Files          41       41              
  Lines        3915     3903      -12     
  Branches      434      433       -1     
==========================================
- Hits         3726     3714      -12     
  Misses        189      189              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant