Skip to content

Fix for code scanning alert no. 30: Prototype-polluting assignment#7027

Merged
ardatan merged 2 commits intomasterfrom
alert-autofix-30
Mar 13, 2025
Merged

Fix for code scanning alert no. 30: Prototype-polluting assignment#7027
ardatan merged 2 commits intomasterfrom
alert-autofix-30

Conversation

@ardatan
Copy link
Copy Markdown
Owner

@ardatan ardatan commented Mar 13, 2025

Potential fix for https://github.com/ardatan/graphql-tools/security/code-scanning/30

To fix the prototype pollution issue, we need to ensure that typeName and key cannot be set to __proto__, constructor, or prototype. This can be achieved by adding a validation step before using these variables as keys in the this.store object. If either typeName or key matches any of these restricted values, we should throw an error or handle it appropriately.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 13, 2025

⚠️ No Changeset found

Latest commit: a3d0612

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 13, 2025

Warning

Rate limit exceeded

@ardatan has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 24 minutes and 37 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 3f9cee8 and a3d0612.

📒 Files selected for processing (1)
  • packages/mock/src/MockStore.ts (1 hunks)
📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced validations to reject the use of reserved identifiers, ensuring potential conflicts and errors are caught early to improve overall system stability and security.

Walkthrough

The pull request adds additional error handling to the setImpl method in the MockStore class. It checks if the typeName or key is one of the reserved JavaScript identifiers ('__proto__', 'constructor', or 'prototype') and throws an error if they are. The process follows the new validations before any further logic, ensuring that invalid identifiers are caught early without altering the existing functionality.

Changes

File Change Summary
packages/mock/src/MockStore.ts Added error checks in the setImpl method to validate that neither typeName nor key equals __proto__, constructor, or prototype, throwing an error if invalid.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant MockStore
    Caller->>MockStore: setImpl(typeName, key, value)
    alt Invalid Identifier
        MockStore-->>Caller: Throw error "Invalid typeName or key"
    else Valid Identifier
        MockStore->>MockStore: Check if store for typeName exists
        alt Store Undefined
            MockStore->>MockStore: Initialize store
        end
        MockStore-->>Caller: Process value as usual
    end
Loading

Poem

Hop, hop, I code away,
With error checks that light the way,
No risk of __proto__ astray,
constructor and prototype kept at bay,
Secure in store—hooray, hooray! 🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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.

@ardatan ardatan changed the title Potential fix for code scanning alert no. 30: Prototype-polluting assignment Fix for code scanning alert no. 30: Prototype-polluting assignment Mar 13, 2025
@ardatan ardatan marked this pull request as ready for review March 13, 2025 12:38
@ardatan ardatan merged commit e890972 into master Mar 13, 2025
17 checks passed
@ardatan ardatan deleted the alert-autofix-30 branch March 13, 2025 12:41
Copy link
Copy Markdown
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

🧹 Nitpick comments (1)
packages/mock/src/MockStore.ts (1)

266-271: Enhance error messages with security context

The current error messages are clear but don't explain why these keys are invalid.

Consider improving the error messages to indicate the security implications:

-      throw new Error(`Invalid typeName: ${typeName}`);
+      throw new Error(`Invalid typeName: ${typeName} - using JS prototype property names poses a security risk`);

-      throw new Error(`Invalid key: ${key}`);
+      throw new Error(`Invalid key: ${key} - using JS prototype property names poses a security risk`);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 784c2be and 3f9cee8.

📒 Files selected for processing (1)
  • packages/mock/src/MockStore.ts (1 hunks)
🔇 Additional comments (1)
packages/mock/src/MockStore.ts (1)

266-271: Good security enhancement!

The added validation checks effectively prevent prototype pollution by rejecting reserved JavaScript property names (__proto__, constructor, and prototype) for both typeName and key parameters. This addresses the code scanning alert mentioned in the PR objectives.

Comment on lines 266 to +271
if (typeName === '__proto__' || typeName === 'constructor' || typeName === 'prototype') {
throw new Error(`Invalid typeName: ${typeName}`);
}
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
throw new Error(`Invalid key: ${key}`);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Consider similar validation in other methods that access this.store

While this fix addresses the vulnerability in setImpl, other methods like has, get, getImpl, and insert also access this.store using typeName and key as property names without similar validation.

Consider extracting the validation logic into a reusable helper method:

+  private validateStoreKey(key: string, keyType: string): void {
+    if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
+      throw new Error(`Invalid ${keyType}: ${key}`);
+    }
+  }

   private setImpl<KeyT extends KeyTypeConstraints>(args: SetArgs<KeyT>) {
     const { typeName, key, fieldName, fieldArgs, noOverride } = args;
     let { value } = args;
     if (isMockList(value)) {
       value = deepResolveMockList(value);
     }

-    if (typeName === '__proto__' || typeName === 'constructor' || typeName === 'prototype') {
-      throw new Error(`Invalid typeName: ${typeName}`);
-    }
-    if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
-      throw new Error(`Invalid key: ${key}`);
-    }
+    this.validateStoreKey(typeName, 'typeName');
+    this.validateStoreKey(key as string, 'key');

Then apply this validation to all methods that access this.store.


🏁 Script executed:

#!/bin/bash
# Check other methods that use typeName and key to access this.store
rg -A 3 -B 3 "this\.store\[(?:typeName|key)" --type ts packages/mock/src/MockStore.ts | grep -v "setImpl"

Length of output: 2311


Action Required: Implement Uniform Key Validation Across All Store Access Methods

The issue in setImpl—ensuring that neither typeName nor key correspond to special properties like __proto__, constructor, or prototype—remains unaddressed in other methods (e.g. has, get, getImpl, insert, etc.). The grep output confirms that many of these methods directly access this.store without applying similar validation.

To resolve this:

  • Extract a Validation Helper: Create a reusable helper such as:
    private validateStoreKey(key: string, keyType: string): void {
      if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
        throw new Error(`Invalid ${keyType}: ${key}`);
      }
    }
  • Apply in All Relevant Methods: In every method that accesses this.store using typeName or key, call this.validateStoreKey(...) before using them. For example, update setImpl like this:
       // before:
    -    if (typeName === '__proto__' || typeName === 'constructor' || typeName === 'prototype') {
    -      throw new Error(`Invalid typeName: ${typeName}`);
    -    }
    -    if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
    -      throw new Error(`Invalid key: ${key}`);
    -    }
    
       // after:
    +    this.validateStoreKey(typeName, 'typeName');
    +    this.validateStoreKey(key as string, 'key');
  • Review All Store Accesses: Similarly, review methods like has, get, getImpl, insert, filter, and find to ensure they perform equivalent validation on both typeName and key before using them.

Please address these adjustments to ensure the vulnerability is mitigated uniformly across the class.

@theguild-bot
Copy link
Copy Markdown
Collaborator

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-tools/executor 1.4.5-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-apollo-link 1.0.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-envelop 3.0.26-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-legacy-ws 1.1.16-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-urql-exchange 1.0.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-yoga 3.0.26-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/graphql-tag-pluck 8.3.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
graphql-tools 9.0.17-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/import 7.0.17-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/links 9.0.26-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/load 8.0.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/apollo-engine-loader 8.0.19-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/code-file-loader 8.1.19-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/git-loader 8.0.23-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/github-loader 8.0.19-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/graphql-file-loader 8.0.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/json-file-loader 8.0.17-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/module-loader 8.0.17-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/url-loader 8.0.30-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/merge 9.0.23-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/mock 9.0.21-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/node-require 7.0.19-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/relay-operation-optimizer 7.0.18-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/resolvers-composition 7.0.17-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/schema 10.0.22-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎
@graphql-tools/utils 10.8.5-alpha-20250313124234-a3d061232db9cefabe856272b6dab452fbb71cf4 npm ↗︎ unpkg ↗︎

@github-actions
Copy link
Copy Markdown
Contributor

💻 Website Preview

The latest changes are available as preview in: https://ed126f8a.graphql-tools.pages.dev

@coderabbitai coderabbitai bot mentioned this pull request Mar 13, 2025
ardatan added a commit that referenced this pull request Sep 22, 2025
…7027)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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.

2 participants