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

feat: add unpkg-white-list to detect sync unpkg files or not #686

Merged
merged 2 commits into from
May 18, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented May 18, 2024

see https://github.com/cnpm/unpkg-white-list

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration option enableSyncUnpkgFilesWhiteList to enhance package version file synchronization.
  • Improvements

    • Enhanced logging in package version file operations for better traceability.
    • Simplified file redirection logic for improved performance and readability.
  • Tests

    • Added test cases for the new enableSyncUnpkgFilesWhiteList configuration to ensure reliability.

@fengmk2 fengmk2 added the enhancement New feature or request label May 18, 2024
@fengmk2 fengmk2 requested a review from elrrrrrrr May 18, 2024 04:42
Copy link
Contributor

coderabbitai bot commented May 18, 2024

Walkthrough

The changes enhance the package version file synchronization process by introducing a whitelist mechanism for unpkg file synchronization, improving logging, and adding new configuration options. These modifications provide better control over the packages allowed for unpkg synchronization and enhance code readability and maintainability.

Changes

Files Change Summary
app.ts Added binaryHTML: string; to the Application interface.
app/core/event/SyncPackageVersionFile.ts Imported ForbiddenError from 'egg-errors'.
app/core/service/PackageVersionFileService.ts Introduced getFullname function, PackageVersionRepository, private fields for unpkg whitelist, and new methods for whitelist management. Updated syncPackageVersionFiles method.
app/port/config.ts Added enableSyncUnpkgFilesWhiteList boolean property to CnpmcoreConfig type.
app/port/controller/PackageVersionFileController.ts Simplified conditional logic for file redirection.
app/port/controller/package/SavePackageVersionController.ts Introduced lockName constant and updated logging to include it.
config/config.default.ts Added enableSyncUnpkgFilesWhiteList: false to cnpmcoreConfig object.
test/port/controller/PackageVersionFileController/listFiles.test.ts Added test cases related to enableSyncUnpkgFilesWhiteList.

🐇
In code we trust, with changes bright,
A whitelist guards the sync at night.
Logs now clear, with names in sight,
Configs set to steer the flight.
Test the files, ensure they're right,
Our app's now ready for the light.
🌟


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@@ -54,6 +54,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = {
redirectNotFound: true,
enableUnpkg: true,
enableSyncUnpkgFiles: true,
enableSyncUnpkgFilesWhiteList: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

默认关闭

Copy link

codecov bot commented May 18, 2024

Codecov Report

Attention: Patch coverage is 91.01124% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 96.81%. Comparing base (ff00e42) to head (344bf2d).
Report is 2 commits behind head on master.

Files Patch % Lines
app/core/event/SyncPackageVersionFile.ts 46.66% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #686      +/-   ##
==========================================
- Coverage   96.89%   96.81%   -0.08%     
==========================================
  Files         181      181              
  Lines       17902    17982      +80     
  Branches     2325     2348      +23     
==========================================
+ Hits        17346    17410      +64     
- Misses        556      572      +16     

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

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

Out of diff range and nitpick comments (2)
app.ts (1)

5-5: Add documentation for the new binaryHTML property.

It's good practice to document new properties, especially when they are public or part of an interface. This helps other developers understand the purpose and usage of the property.

app/port/config.ts (1)

152-155: Document the new configuration property enableSyncUnpkgFilesWhiteList.

Adding documentation for new configuration properties helps maintainers and users understand their purpose and impact on the system.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between c5c6145 and e23080b.
Files selected for processing (8)
  • app.ts (1 hunks)
  • app/core/event/SyncPackageVersionFile.ts (2 hunks)
  • app/core/service/PackageVersionFileService.ts (4 hunks)
  • app/port/config.ts (1 hunks)
  • app/port/controller/PackageVersionFileController.ts (1 hunks)
  • app/port/controller/package/SavePackageVersionController.ts (2 hunks)
  • config/config.default.ts (1 hunks)
  • test/port/controller/PackageVersionFileController/listFiles.test.ts (3 hunks)
Additional comments not posted (10)
app.ts (1)

5-5: Ensure the initialization of binaryHTML in the constructor aligns with its intended use.

Verification successful

Ensure Proper Initialization and Usage of binaryHTML

The initialization and usage of binaryHTML in the codebase have been verified:

  • Declaration and Initialization:

    • binaryHTML is declared in the Application interface and initialized in the constructor of CnpmcoreAppHook in app.ts.
    • It is initially set to an empty string in the constructor.
  • Usage:

    • binaryHTML is assigned a value in the didReady method of CnpmcoreAppHook, where it reads and processes the content of binary.html.
    • It is used in BinarySyncController.ts to serve the processed HTML content.

The initialization aligns with its intended use, ensuring that binaryHTML is properly set up before it is used.

  • Locations:
    • app.ts: Initialization and assignment.
    • app/port/controller/BinarySyncController.ts: Usage in the showBinaryHTML method.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `binaryHTML` is used correctly throughout the application.

# Test: Search for usages of `binaryHTML`. Expect: Proper usage in context.
rg --type typescript $'binaryHTML'

Length of output: 68



Script:

#!/bin/bash
# Description: Verify that `binaryHTML` is used correctly throughout the application.

# Test: Search for usages of `binaryHTML`. Expect: Proper usage in context.
rg --type ts 'binaryHTML'

Length of output: 266



Script:

#!/bin/bash
# Extract relevant lines from app.ts and BinarySyncController.ts for detailed inspection

# Extract lines from app.ts
rg --type ts --context 5 'binaryHTML' app.ts

# Extract lines from BinarySyncController.ts
rg --type ts --context 5 'binaryHTML' app/port/controller/BinarySyncController.ts

Length of output: 1273

app/core/event/SyncPackageVersionFile.ts (1)

5-5: Confirm the necessity of importing ForbiddenError.

Verification successful

ForbiddenError is used in the file

The ForbiddenError import is indeed used within the SyncPackageVersionFile.ts file.

  • ForbiddenError is imported from egg-errors.
  • It is used in a conditional check: if (err instanceof ForbiddenError).

This confirms the necessity of importing ForbiddenError.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `ForbiddenError` is used in the file.

# Test: Search for usages of `ForbiddenError`. Expect: At least one usage.
rg --type typescript $'ForbiddenError' app/core/event/SyncPackageVersionFile.ts

Length of output: 113



Script:

#!/bin/bash
# Description: Verify that `ForbiddenError` is used in the file.

# Test: Search for usages of `ForbiddenError`. Expect: At least one usage.
rg --type ts $'ForbiddenError' app/core/event/SyncPackageVersionFile.ts

Length of output: 158

config/config.default.ts (1)

57-57: Ensure the default value of enableSyncUnpkgFilesWhiteList aligns with expected system behavior.

app/port/controller/PackageVersionFileController.ts (1)

Line range hint 5-5: Review the simplified conditional logic for file redirection to ensure it handles all necessary cases.

Verification successful

The search results indicate two instances of ctx.redirect in the PackageVersionFileController.ts file:

  1. ctx.redirect(join(ctx.path, indexFile));
  2. ctx.redirect(route);

Additionally, there is a comment explaining the redirection logic for directories with index.js files.

Analysis:

  • The first redirection handles cases where the path is joined with an indexFile.
  • The second redirection handles a generic route redirection.
  • The comment suggests that directories with index.js files are redirected appropriately.

Conclusion:

The redirection logic appears to cover the necessary cases, including handling directories with index.js files and generic route redirections.

Recommendation:

No further action is required as the redirection logic seems comprehensive.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new file redirection logic is comprehensive and does not omit important cases.

# Test: Search for the redirection logic in the controller. Expect: Comprehensive handling of cases.
rg --type typescript $'redirect' app/port/controller/PackageVersionFileController.ts

Length of output: 118



Script:

#!/bin/bash
# Description: Verify that the new file redirection logic is comprehensive and does not omit important cases.

# Test: Search for the redirection logic in the controller. Expect: Comprehensive handling of cases.
rg --type ts $'redirect' app/port/controller/PackageVersionFileController.ts

Length of output: 293

app/port/controller/package/SavePackageVersionController.ts (1)

Line range hint 224-244: Validate the usage of the lockName constant in logging statements.

Verification successful

lockName is used correctly in logging statements

The lockName constant is correctly used in the logging statement within the SavePackageVersionController.ts file.

  • Line 224: const lockName = ${pkg.name}:publish;
  • Line 227: this.logger.warn('[package:version:add] check lock:%s fail', lockName);
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `lockName` is used correctly in logging statements.

# Test: Search for usages of `lockName` in logging. Expect: Correct and consistent usage.
rg --type typescript $'lockName' app/port/controller/package/SavePackageVersionController.ts

Length of output: 126



Script:

#!/bin/bash
# Description: Verify that `lockName` is used correctly in logging statements.

# Test: Search for usages of `lockName` in logging. Expect: Correct and consistent usage.
rg --type ts 'lockName' app/port/controller/package/SavePackageVersionController.ts

Length of output: 205

app/core/service/PackageVersionFileService.ts (1)

Line range hint 49-102: Ensure the new private fields and methods for handling the unpkg-white-list are secure and performant.

Verification successful

The search results indicate that the unpkgWhiteList related fields and methods are used for access control and synchronization within PackageVersionFileService.ts. The methods include checks for the current version, updating the version, and validating packages and scopes against the whitelist. These operations appear to be secure and performant, with proper error handling and efficient data access patterns.

  • The #unpkgWhiteListCurrentVersion is checked and updated to ensure synchronization.
  • The #unpkgWhiteListAllowPackages and #unpkgWhiteListAllowScopes are used to validate access, with appropriate error handling using ForbiddenError.

Based on the analysis, the implementation of the unpkgWhiteList related methods and fields appears to be secure and performant.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the security and performance of the new `unpkg-white-list` related methods.

# Test: Search for usages of the new methods and fields. Expect: Secure and efficient handling.
rg --type typescript $'unpkgWhiteList' app/core/service/PackageVersionFileService.ts

Length of output: 118



Script:

#!/bin/bash
# Description: Verify the security and performance of the new `unpkg-white-list` related methods.

# Test: Search for usages of the new methods and fields. Expect: Secure and efficient handling.
rg --type ts $'unpkgWhiteList' app/core/service/PackageVersionFileService.ts

Length of output: 1060

test/port/controller/PackageVersionFileController/listFiles.test.ts (4)

403-427: LGTM! This test case correctly handles the scenario where a package name is not in the white list, returning the expected 403 status and error message.


429-470: LGTM! This test case correctly handles the scenario where a package version does not match the white list, returning the expected 403 status and error message.


472-509: LGTM! This test case correctly handles the scenario where a scope is in the white list, returning the expected 200 status.


511-613: LGTM! This test case correctly handles the scenario where a package version is in the white list, returning the expected 200 status.

app/core/event/SyncPackageVersionFile.ts Outdated Show resolved Hide resolved
@fengmk2 fengmk2 marked this pull request as draft May 18, 2024 04:51
@fengmk2 fengmk2 marked this pull request as ready for review May 18, 2024 04:55
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: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between e23080b and 344bf2d.
Files selected for processing (2)
  • app/core/event/SyncPackageVersionFile.ts (3 hunks)
  • app/core/service/PackageVersionFileService.ts (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/core/event/SyncPackageVersionFile.ts
  • app/core/service/PackageVersionFileService.ts

fengmk2 added a commit to cnpm/unpkg-white-list that referenced this pull request May 18, 2024
@fengmk2 fengmk2 merged commit 0530116 into master May 18, 2024
11 of 13 checks passed
@fengmk2 fengmk2 deleted the unpkg-white-list branch May 18, 2024 05:11
fengmk2 pushed a commit that referenced this pull request May 18, 2024
[skip ci]

## [3.59.0](v3.58.1...v3.59.0) (2024-05-18)

### Features

* add unpkg-white-list to detect sync unpkg files or not ([#686](#686)) ([0530116](0530116))
fengmk2 added a commit to cnpm/unpkg-white-list that referenced this pull request May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant