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

chore: use plain app id in key gen #960

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

juliusmarminge
Copy link
Collaborator

@juliusmarminge juliusmarminge commented Sep 18, 2024

We decided it was too hard to port the encoding to other languages
so we now do a prefix check on appId in plain text instead of the
encoded value.

ref: https://github.com/pingdotgg/uploadthing-infra/pull/550

Summary by CodeRabbit

  • New Features

    • Simplified file key generation process for uploads, enhancing clarity and efficiency.
    • Introduced requirements for user-defined file seeds to be unique, URL-safe, and a minimum of 36 characters.
  • Bug Fixes

    • Removed unnecessary complexity in key generation, improving overall functionality and reducing dependencies.
    • Eliminated the verifyKey function, streamlining key management and enhancing security.

Copy link

vercel bot commented Sep 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs-uploadthing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 8, 2024 10:31pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
legacy-docs-uploadthing ⬜️ Ignored (Inspect) Visit Preview Oct 8, 2024 10:31pm

Copy link

changeset-bot bot commented Sep 18, 2024

⚠️ No Changeset found

Latest commit: 37a73ff

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Sep 18, 2024

Walkthrough

The changes primarily involve the simplification of the file key generation process. The previous implementation utilized the Sqids library for encoding and randomization, while the new approach directly concatenates the appId with a user-defined file seed. The updated requirements for the file seed ensure it is unique, URL-safe, and at least 36 characters long, streamlining the overall key generation without the need for additional complexity.

Changes

File Path Change Summary
docs/src/app/(docs)/uploading-files/page.mdx, packages/shared/src/crypto.ts The generateKey function was modified to simplify the key generation process by removing the encoding of appId and directly concatenating it with the encodedFileSeed. The new implementation eliminates the use of the Sqids library and focuses on ensuring the file seed is URL-safe. The verifyKey function was also removed, affecting key verification.

Poem

In the meadow where the keys align,
A rabbit hops, feeling just fine.
With seeds so safe and bright,
The keys now shine, a pure delight!
No more shuffle, just a simple way,
To share our files and play all day! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 75c1da4 and b281a5c.

Files selected for processing (1)
  • packages/shared/src/crypto.ts (1 hunks)
Additional comments not posted (1)
packages/shared/src/crypto.ts (1)

112-112: Carefully assess the impact of the change in key generation format.

The change in the generateKey function, which now directly concatenates appId and encodedFileSeed without encoding appId, could have significant implications:

  1. Compatibility issues: The change in key format could lead to compatibility issues with existing systems or databases that rely on the previous format. Ensure that this change is thoroughly tested and validated against all dependent systems.

  2. Security and integrity: The removal of the verifyKey function indicates that there is no longer a mechanism to validate the relationship between the key and the appId. This could have implications for security and integrity checks in the system. Consider implementing an alternative verification mechanism if necessary.

  3. Key uniqueness and security: Using the raw appId instead of the encoded version may affect the uniqueness and security properties of the generated keys. Assess whether the new key format maintains the required level of uniqueness and security. Consider adding tests to validate these properties.

To ensure system stability and compatibility, please:

  • Review and update any key format validation logic in the codebase.
  • Modify existing tests to account for the new key format.
  • Add new tests to verify the compatibility of the new key format with all dependent systems.
  • Perform integration testing to ensure that URL generation and API calls work correctly with the new key format.

Run the following script to verify the usage of the generateKey function and its return value across the codebase:

Verification successful

Verification confirms removal of verifyKey function and potential compatibility.

Based on the verification results:

  1. The verifyKey function has indeed been removed from the codebase, confirming the change mentioned in the original review comment.

  2. No direct key format validations or comparisons were found, suggesting that the system may not rely on a specific key format for validation. This could reduce the impact of the format change.

  3. Key-related types and interfaces (found in packages/uploadthing/src/sdk/types.ts) appear to be focused on file operations rather than the internal structure of the key itself. This suggests that the change in key format may not significantly impact these types.

  4. The generateKey function is used in test files and the SDK, but its usage doesn't seem to depend on the internal structure of the key.

Given these findings:

  • The removal of the verifyKey function and the change in key generation format appear to be intentional changes that don't severely impact the existing codebase.
  • The lack of key format validations and the nature of key-related types suggest that the system may be more resilient to the key format change than initially thought.

However, to ensure system stability:

  1. Update any remaining tests that still use the verifyKey function.
  2. Review the generateSignedURL function (which wasn't found in our search) to ensure it's compatible with the new key format.
  3. Perform thorough integration testing, especially for API calls using the utapi object, to verify that the new key format works as expected in all scenarios.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `generateKey` function and its return value.

# Test 1: Search for the function usage and its return value usage
rg --type ts -A 10 'const key = .*generateKey'

# Test 2: Search for any key format validations or comparisons
rg --type ts 'key\.match|key\.startsWith|key\.endsWith|key\.includes'

# Test 3: Look for any database operations or API calls using the key
rg --type ts 'db\..*\(.*key|api\..*\(.*key'

Length of output: 6681


Script:

#!/bin/bash
# Description: Further investigation of key-related functions and types

# Check for the current state of the verifyKey function
rg --type ts 'function verifyKey'

# Look for key-related type definitions or interfaces
rg --type ts 'type.*Key|interface.*Key'

# Investigate the generateSignedURL function
rg --type ts -A 10 'function generateSignedURL'

# Check for configuration files or environment variables related to key generation
rg --type ts 'process\.env\..*KEY|config\..*key'

Length of output: 1717


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 generate interesting stats about this repository and render them as a table.
    -- @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 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

pkg-pr-new bot commented Sep 18, 2024

Copy link
Contributor

github-actions bot commented Sep 18, 2024

📦 Bundle size comparison

Bundle Size (gzip) Visualization
Main 26.04KB No treemap on forks
PR (090f17b) 26.04KB No treemap on forks
Diff No change

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

Commits

Files that changed from the base of the PR and between 95319f2 and 75c1da4.

Files selected for processing (2)
  • docs/src/app/(docs)/uploading-files/page.mdx (1 hunks)
  • packages/shared/src/crypto.ts (1 hunks)
Additional comments not posted (2)
docs/src/app/(docs)/uploading-files/page.mdx (2)

99-103: LGTM!

The requirements for generating a file key are clear and reasonable. Using base64 encoding to ensure the file seed is URL-safe is a good approach.


106-111: LGTM!

The generateKey function correctly implements the requirements for generating a file key. The use of base64 encoding ensures the fileSeed is URL-safe, and the concatenation with appId generates a unique file key.


// Concatenate them
return encodedAppId + encodedFileSeed;
return appId + encodedFileSeed;
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Update key format validation and related tests

The change in the generateKey function, which now directly concatenates appId and encodedFileSeed without encoding, could have significant implications:

  1. The key format validation in packages/shared/src/crypto.ts (key.startsWith(expectedPrefix)) may need to be adjusted to accommodate the new key format.
  2. Tests in packages/shared/test/crypto.test.ts should be reviewed and updated to ensure they correctly validate the new key format.
  3. The following operations that use the generated key should be thoroughly tested with the new format:
    • URL generation (e.g., in packages/uploadthing/src/internal/handler.ts)
    • API calls such as updateACL, deleteFiles, and getSignedURL

To ensure system stability and compatibility:

  • Review and update the key format validation logic.
  • Modify existing tests to account for the new key format.
  • Add new tests to verify the compatibility of the new key format with all dependent systems.
  • Perform integration testing to ensure that URL generation and API calls work correctly with the new key format.
Analysis chain

Verify the impact of the change in key generation format.

The change in the key generation format, by removing the encoding of appId, could lead to compatibility issues with existing systems or databases that rely on the previous format. Please ensure that this change is thoroughly tested and validated against all dependent systems.

Run the following script to verify the usage of the generateKey function and its return value across the codebase:

Additionally, please ensure that the new key generation format maintains the required level of uniqueness and security. Consider adding tests to validate the uniqueness and security properties of the generated keys.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `generateKey` function and its return value.

# Test 1: Search for the function usage. Expect: Occurrences of the function call with the expected arguments.
ast-grep --lang typescript --pattern $'generateKey($_, $_, $_)'

# Test 2: Search for the usage of the return value. Expect: Occurrences of the key being used in the expected format.
rg --type typescript -A 5 $'generateKey'

Length of output: 1760


Script:

#!/bin/bash
# Description: Verify the usage of the `generateKey` function and its return value.

# Test 1: Search for the function usage and its return value usage
rg --type ts -A 10 'const key = .*generateKey'

# Test 2: Search for any key format validations or comparisons
rg --type ts 'key\.match|key\.startsWith|key\.endsWith|key\.includes'

# Test 3: Look for any database operations or API calls using the key
rg --type ts 'db\..*\(.*key|api\..*\(.*key'

Length of output: 6754

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

Successfully merging this pull request may close these issues.

1 participant