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

refactor(encryption): extract standalone encrypter/decrypter #1945

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Jan 7, 2025

No description provided.

Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a comprehensive encryption and decryption system in the runtime package. The changes include creating two new classes, Encrypter and Decrypter, along with utility functions for encryption operations. The implementation provides a robust mechanism for encrypting and decrypting data using Web Crypto API, with support for key validation, metadata handling, and error management. The changes simplify the encryption process in the EncryptedHandler class by leveraging these new encryption utility classes.

Changes

File Change Summary
packages/runtime/src/encryption/index.ts Added Encrypter and Decrypter classes with methods for encrypting and decrypting data
packages/runtime/src/encryption/utils.ts Introduced encryption utility constants, functions for key loading, digest computation, encryption, and decryption
packages/runtime/src/enhancements/node/encryption.ts Updated EncryptedHandler to use new Encrypter and Decrypter classes, simplified encryption logic

Sequence Diagram

sequenceDiagram
    participant Client
    participant Encrypter
    participant Decrypter
    participant CryptoAPI

    Client->>Encrypter: encrypt(data)
    Encrypter->>CryptoAPI: loadKey()
    Encrypter->>CryptoAPI: getKeyDigest()
    Encrypter->>CryptoAPI: encrypt data
    Encrypter-->>Client: encrypted data

    Client->>Decrypter: decrypt(data)
    Decrypter->>CryptoAPI: loadKey()
    Decrypter->>CryptoAPI: getKeyDigest()
    Decrypter->>CryptoAPI: decrypt data
    Decrypter-->>Client: decrypted data
Loading

Possibly related PRs


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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
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 (2)
packages/runtime/src/encryption/index.ts (2)

20-26: Optimize key initialization to avoid redundant operations

In the encrypt method, this.key and this.keyDigest are initialized when they are undefined. If encrypt is called concurrently before these properties are set, it could result in multiple initializations, potentially impacting performance. Consider initializing this.key and this.keyDigest in the constructor or implementing a mechanism (such as a promise-based initialization guard) to ensure they are loaded only once.


54-61: Prevent multiple initializations of decryption keys

In the decrypt method, this.keys is populated when it is empty. If decrypt is called concurrently before this.keys is initialized, it may lead to redundant initializations. To improve efficiency, consider initializing this.keys in the constructor or using a single initialization promise to ensure the keys are loaded only once.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 96d0ce5 and 1f700dd.

⛔ Files ignored due to path filters (1)
  • packages/runtime/package.json is excluded by !**/*.json
📒 Files selected for processing (3)
  • packages/runtime/src/encryption/index.ts (1 hunks)
  • packages/runtime/src/encryption/utils.ts (1 hunks)
  • packages/runtime/src/enhancements/node/encryption.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build-test (20.x)
  • GitHub Check: dependency-review
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: build-test (20.x)
🔇 Additional comments (1)
packages/runtime/src/encryption/utils.ts (1)

1-96: LGTM!

The encryption utilities are well-implemented, adhering to best practices with appropriate error handling and input validation.

Comment on lines +43 to +44
private encrypter: Encrypter | undefined;
private decrypter: Decrypter | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure 'this.encrypter' is defined before usage to prevent runtime errors

In cases where custom encryption is used, this.encrypter remains undefined, but in the encrypt method, this.encrypter!.encrypt(data) is called without checking if this.encrypter is defined. This could lead to a runtime error if this.encrypter is undefined. Consider adding a check to ensure this.encrypter is defined before using it or refactor the code to guarantee that this.encrypter is always initialized when not using custom encryption.

Apply this diff to check if this.encrypter is defined:

+        if (!this.encrypter) {
+            throw new Error('Encrypter is not initialized');
+        }
         return this.encrypter.encrypt(data);

Also applies to: 82-82

private readonly ENCRYPTER_VERSION = 1;
private readonly KEY_DIGEST_BYTES = 8;
private encrypter: Encrypter | undefined;
private decrypter: Decrypter | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure 'this.decrypter' is defined before usage to prevent runtime errors

Similarly, when custom encryption is employed, this.decrypter remains undefined. In the decrypt method, calling this.decrypter!.decrypt(data) without verifying that this.decrypter is defined may result in a runtime error. Please ensure this.decrypter is defined before usage or modify the code to handle scenarios where custom encryption is used.

Apply this diff to check if this.decrypter is defined:

+        if (!this.decrypter) {
+            throw new Error('Decrypter is not initialized');
+        }
         return this.decrypter.decrypt(data);

Also applies to: 90-90

@ymc9 ymc9 merged commit 4605278 into dev Jan 7, 2025
9 checks passed
@ymc9 ymc9 deleted the refactor/standalone-encrypter branch January 7, 2025 11:13
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