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: add createHttpClient back to app instance #5383

Merged
merged 2 commits into from
Jan 14, 2025
Merged

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jan 13, 2025

security plugin need it to create a new httpClient for SSRF

egg/lib/egg.js

Line 293 in a612e80

createHttpClient(options) {
/~https://github.com/eggjs/security/blob/e3408408adec5f8d009d37f75126ed082481d0ac/lib/extend/safe_curl.js#L21

Summary by CodeRabbit

  • New Features

    • Enhanced TypeScript type definitions for Context, Request, Response, and HttpClient interfaces.
    • Added createHttpClient method to create HTTP client instances with custom options.
  • Improvements

    • Improved type safety and configuration flexibility for core framework interfaces.
    • Streamlined HTTP client configuration management.
  • Testing

    • Added test coverage for HTTP client instance creation and usage.
    • Updated test cases to enhance type safety for the app parameter in loader tests.

Copy link

coderabbitai bot commented Jan 13, 2025

Walkthrough

This pull request introduces enhancements to TypeScript type definitions and configuration options within the Egg.js framework. Key changes involve extending the Context, Request, and Response interfaces, as well as introducing the createHttpClient method in the EggApplicationCore class, allowing for the creation of HTTP client instances with custom options. The modifications aim to improve type safety and flexibility in configuration, while also streamlining the management of HTTP client settings.

Changes

File Change Summary
src/app/extend/context.ts Added type declarations for Context interface, including methods like curl(), getLogger(), and various getters/setters for router, helper, httpclient, logger, etc.
src/app/extend/request.ts Added module augmentation for Request interface with properties like body, acceptJSON, query, and queries.
src/app/extend/response.ts Added realStatus getter and setter to Response interface.
src/lib/core/httpclient.ts Updated HttpClient constructor to accept optional options parameter and export ClientOptions.
src/lib/egg.ts Added createHttpClient method, new HttpClientOptions type, and expanded EggCore interface declarations.
src/lib/types.ts Introduced new HttpClientConfig interface and updated EggAppConfig configuration structure.
test/lib/core/httpclient.test.ts Added test suite for app.createHttpClient(options) method.
src/app/extend/context.types.ts, src/app/extend/request.types.ts, src/app/extend/response.types.ts, src/lib/egg.types.ts Deleted files containing previous type declarations that are now integrated into their respective interfaces.

Possibly related issues

Possibly related PRs

  • refactor: use @eggjs/jsonp @eggjs/i18n @eggjs/static #5382: The changes in this PR involve modifications to the Context interface in the @eggjs/core module, specifically the removal of properties related to internationalization (i18n) and JSONP, which are also referenced in the main PR's updates to the Context interface.

Poem

🐰 Hop, hop, type definitions dance,
Egg.js framework gets a new stance!
Interfaces bloom with grace and might,
HttpClient shines so very bright 🌟
Code types leap with joyful prance!


📜 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 c0c2ac9 and 21b1679.

📒 Files selected for processing (2)
  • src/lib/egg.ts (3 hunks)
  • test/lib/core/loader/load_plugin.test.ts (16 hunks)
⏰ 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 (5)
test/lib/core/loader/load_plugin.test.ts (1)

6-6: LGTM! Type safety improvements

The changes consistently add proper type casting of the app parameter to EggApplicationCore across all test cases, improving type safety without affecting functionality.

Also applies to: 25-25, 69-69, 91-91, 113-113, 139-139, 164-164, 197-197, 210-210, 223-223, 257-257, 270-270, 283-283, 298-298, 330-330, 346-346

src/lib/egg.ts (4)

34-36: LGTM! Necessary type imports

Added required HttpClient type imports to support the new functionality.


403-405: LGTM! Improved httpClient initialization

The httpClient getter now uses the new createHttpClient method, maintaining consistency in client creation.


692-705: LGTM! Comprehensive type declarations

Added necessary type declarations to the EggCore interface to support the new HttpClient functionality and other core features.


390-396: LGTM! Implementation aligns with PR objective

The createHttpClient method allows creating new HttpClient instances with custom options, which is essential for the security plugin's SSRF protection requirements.

Let's verify the security plugin's usage:

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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.

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c098596 and c0c2ac9.

📒 Files selected for processing (11)
  • src/app/extend/context.ts (1 hunks)
  • src/app/extend/context.types.ts (0 hunks)
  • src/app/extend/request.ts (1 hunks)
  • src/app/extend/request.types.ts (0 hunks)
  • src/app/extend/response.ts (1 hunks)
  • src/app/extend/response.types.ts (0 hunks)
  • src/lib/core/httpclient.ts (2 hunks)
  • src/lib/egg.ts (3 hunks)
  • src/lib/egg.types.ts (0 hunks)
  • src/lib/types.ts (2 hunks)
  • test/lib/core/httpclient.test.ts (1 hunks)
💤 Files with no reviewable changes (4)
  • src/app/extend/request.types.ts
  • src/app/extend/response.types.ts
  • src/lib/egg.types.ts
  • src/app/extend/context.types.ts
🔇 Additional comments (10)
src/app/extend/response.ts (1)

37-44: LGTM! Type declarations look good.

The TypeScript declarations for realStatus getter/setter methods are correctly defined in the module augmentation.

src/lib/core/httpclient.ts (2)

24-34: LGTM! Constructor changes enhance configurability.

The addition of the options parameter with proper merging of configuration allows for customized HTTP client instances, which is essential for the security plugin's SSRF protection.


5-5: LGTM! Type exports are properly defined.

The HttpClientOptions type is correctly exported both in the imports and exports sections.

Also applies to: 13-13

src/app/extend/request.ts (1)

267-276: LGTM! Request interface types are well-defined.

The TypeScript declarations for the Request interface methods and properties are correctly defined in the module augmentation.

src/app/extend/context.ts (1)

308-322: LGTM! Context interface types are comprehensive.

The TypeScript declarations properly include all necessary methods and properties, particularly the HTTP client-related ones needed for SSRF protection:

  • curl method
  • httpclient and httpClient getters
src/lib/types.ts (2)

69-78: Well-structured interface definition with proper documentation.

The HttpClientConfig interface is well-defined with clear property types and documentation. The deprecated flag for useHttpClientNext is properly marked.


155-155: Clean refactoring of httpclient configuration.

Good refactoring to use the new HttpClientConfig interface, improving type safety and maintainability.

src/lib/egg.ts (3)

34-36: Clean type imports with proper organization.

The HttpClient-related type imports are well-organized and properly scoped.


692-706: Comprehensive type declarations for EggCore interface.

The type declarations in the @eggjs/core module are well-structured and maintain type safety.


390-397: Implementation aligns with PR objectives.

The createHttpClient method is properly implemented to support SSRF protection by allowing custom HTTP client instances.

Let's verify the SSRF protection integration:

✅ Verification successful

SSRF protection is properly integrated.

The codebase shows proper SSRF protection implementation through:

  • Built-in security configuration interface with IP blacklisting and custom validation
  • Integration with egg-security plugin
  • Support for custom HttpClient options
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify SSRF protection usage in security plugin
# Test: Search for createHttpClient usage in security plugin
rg -A 5 'createHttpClient' | grep -C 5 'ssrf'

Length of output: 43


Script:

#!/bin/bash
# Find HttpClient class definition and implementation
ast-grep --pattern 'class HttpClient {
  $$$
}'

# Find HttpClientOptions interface
ast-grep --pattern 'interface HttpClientOptions {
  $$$
}'

# Search for security-related configurations
rg -l "HttpClientOptions|HttpClient" | xargs rg -A 5 -B 5 "(security|protect|validate|whitelist|blacklist|allowlist|blocklist|host|domain|url)"

Length of output: 83710


Script:

#!/bin/bash
# Search for SSRF implementation and integration
rg -A 5 -B 5 "ssrf" --type ts

# Look for security plugin integration with HttpClient
ast-grep --pattern 'class HttpClient {
  $$$
  request($_, $_) {
    $$$
  }
  $$$
}'

Length of output: 1743

Comment on lines +638 to +655
describe('app.createHttpClient(options)', () => {
let app: MockApplication;
before(() => {
app = createApp('apps/httpclient-retry');
return app.ready();
});
after(() => app.close());

it('should work', async () => {
const client1 = app.createHttpClient();
const client2 = app.createHttpClient();
assert.notEqual(client1, client2);
const res = await client1.request(url, {
method: 'GET',
});
assert.equal(res.status, 200);
});
});
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for createHttpClient.

While the current tests verify basic functionality, consider adding tests for:

  1. Custom options handling
  2. Error scenarios
  3. SSRF protection functionality (main PR objective)

Example test cases:

it('should apply custom options', () => {
  const client = app.createHttpClient({ timeout: 1000 });
  assert.equal(client.options.timeout, 1000);
});

it('should handle SSRF protection', async () => {
  const client = app.createHttpClient();
  await assert.rejects(
    () => client.request('http://internal-network'),
    /SSRF protection/
  );
});

Copy link

codecov bot commented Jan 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.17%. Comparing base (562d76a) to head (21b1679).
Report is 3 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #5383      +/-   ##
==========================================
- Coverage   95.25%   95.17%   -0.08%     
==========================================
  Files          45       41       -4     
  Lines        3980     3915      -65     
  Branches      433      433              
==========================================
- Hits         3791     3726      -65     
  Misses        189      189              

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

@fengmk2 fengmk2 merged commit e5a697e into next Jan 14, 2025
21 checks passed
@fengmk2 fengmk2 deleted the add-createHttpClient branch January 14, 2025 00:23
fengmk2 pushed a commit that referenced this pull request Jan 14, 2025
[skip ci]

## [4.0.1](v4.0.0...v4.0.1) (2025-01-14)

### Bug Fixes

* add createHttpClient back to app instance ([#5383](#5383)) ([e5a697e](e5a697e)), closes [/github.com/eggjs/egg/blob/a612e806019402aa217a1562b5ad847a308e843b/lib/egg.js#L293](/~https://github.com/eggjs//github.com/eggjs/egg/blob/a612e806019402aa217a1562b5ad847a308e843b/lib/egg.js/issues/L293) [/github.com/eggjs/security/blob/e3408408adec5f8d009d37f75126ed082481d0ac/lib/extend/safe_curl.js#L21](/~https://github.com/eggjs//github.com/eggjs/security/blob/e3408408adec5f8d009d37f75126ed082481d0ac/lib/extend/safe_curl.js/issues/L21)
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