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

Add input validation for API numerical inputs #204

Conversation

OhmSpectator
Copy link
Member

@OhmSpectator OhmSpectator commented Jan 13, 2024

Description

This PR introduces a utility function getDataTypeRange to check the range of data types for numerical inputs in the API, ensuring inputs are within specific ranges. The routes in regionRoutes.js have been updated to utilize these validations for improved robustness and error handling.

Related Issues

Closes: #202

How Was This Tested?

  • The utility function getDataTypeRange was tested with different model fields to ensure correct range retrieval.
  • Updated routes were tested with various inputs to verify proper validation and error handling.

Checklist

  • Commit messages follow the standard template.
  • All commits are signed.
  • Related issues are mentioned in the description above.
  • I have followed the project's directory

Summary by CodeRabbit

  • New Features
    • Enhanced data validation for region management by incorporating dynamic data type ranges.
  • Refactor
    • Updated backend configurations to support newer JavaScript features.

This commit includes the addition of a utility function that checks the
range of the data types and validates them. This is particularly useful
for numerical inputs in the API that need to be within a specific range.
The commit also updates the backend routes to use these validations.

Issue: #202

Signed-off-by: Nikolay Martyanov <[email protected]>
@OhmSpectator OhmSpectator linked an issue Jan 13, 2024 that may be closed by this pull request
Copy link

coderabbitai bot commented Jan 13, 2024

Walkthrough

The code updates introduce a function to validate integer inputs by enforcing range constraints, preventing server crashes due to excessively large integer values. This is achieved by adding a utility function to get data type ranges and modifying API route validation to utilize these ranges, ensuring that integer inputs are within the permissible bounds for the database.

Changes

File Path Change Summary
backend/.eslintrc Added parser options for ECMAScript 2020 to support BigInt.
backend/src/routes/... Imported new utilities and models; updated validation logic.
backend/src/utils/... Introduced getDataTypeRange function for data type validation.

Assessment against linked issues

Objective Addressed Explanation
Address server crash due to large integer values (#202)
API handling of large integer inputs without server crash (#202)
Investigate/resolve SequelizeDatabaseError for out-of-range integers (#202)
Implement input validation for integer values within permissible range (#202)
Modify Sequelize query handling for large integers (#202) The provided summary does not specify how Sequelize queries have been modified to handle large integers.
Exception handling for SequelizeDatabaseError to prevent crashes due to integer limits (#202) The summary mentions improved validation but does not detail the implementation of exception handling for SequelizeDatabaseError.

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 527ad8e and 39e8890.
Files selected for processing (3)
  • backend/.eslintrc (1 hunks)
  • backend/src/routes/regionRoutes.js (8 hunks)
  • backend/src/utils/dataTypes.js (1 hunks)
Additional comments: 10
backend/.eslintrc (1)
  • 14-16: The update to the ESLint configuration to support ECMAScript 2020 features, including BigInt, is correctly implemented and aligns with the PR objectives.
backend/src/utils/dataTypes.js (1)
  • 1-18: The getDataTypeRange function is well-implemented with appropriate error handling for undefined fields and unsupported data types. It correctly returns the range for INTEGER, BIGINT, and SMALLINT data types, which should help in validating numerical inputs as per the PR objectives.
backend/src/routes/regionRoutes.js (8)
  • 4-5: The addition of imports for getDataTypeRange and the Sequelize models Hierarchy and Region is correct and necessary for the subsequent validation logic.

  • 18-20: The validation logic for hierarchyId using getDataTypeRange to set the maximum value is correctly implemented. This ensures that the hierarchyId is within the acceptable range for the data type.

  • 36-38: The validation for hierarchyId in the search route is consistent with the validation in the root route, correctly using getDataTypeRange to determine the maximum value.

  • 53-58: The validation for regionId and hierarchyId in the region ID route is correctly implemented, using getDataTypeRange to ensure the IDs are within the valid range for their respective data types.

  • 72-78: The validation for regionId and hierarchyId in the subregions route is consistent with the other routes, correctly using getDataTypeRange for determining the maximum values.

  • 93-95: The validation for regionId in the ancestors route is correctly implemented, using getDataTypeRange to ensure the ID is within the valid range for its data type.

  • 110-112: The validation for regionId in the geometry route is correctly implemented, using getDataTypeRange to ensure the ID is within the valid range for its data type.

  • 128-133: The validation for regionId and hierarchyId in the siblings route is correctly implemented, using getDataTypeRange to ensure the IDs are within the valid range for their respective data types.

@OhmSpectator OhmSpectator merged commit 9f334bf into main Jan 13, 2024
14 checks passed
@OhmSpectator OhmSpectator deleted the bugfix/202-server-crash-on-handling-api-requests-with-large-integer-values branch January 13, 2024 17:27
Copy link
Contributor

sweep-ai bot commented Jan 14, 2024

Apply Sweep Rules to your PR?

  • Apply: All new business logic should have corresponding unit tests.
  • Apply: Add jsdoc to all functions and file headers.

This is an automated message generated by Sweep AI.

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.

Server crash on handling API requests with large integer values
1 participant