Skip to content

Update controller.js - #19

Open
haslam93 wants to merge 1 commit into
mainfrom
haslam93-patch-9
Open

Update controller.js#19
haslam93 wants to merge 1 commit into
mainfrom
haslam93-patch-9

Conversation

@haslam93

Copy link
Copy Markdown
Owner

I made changes not sure if secure.

@haslam93
haslam93 requested a review from Copilot August 13, 2025 17:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds input validation for the operand2 parameter in the controller's calculate function. The change appears to address security concerns by validating that operand2 is a properly formatted number.

  • Adds validation logic to check if operand2 exists and matches a numeric pattern
  • Throws an error with the operand2 value if validation fails

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread api/controller.js
!req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand2: " + req.query.operand2);
}

Copilot AI Aug 13, 2025

Copy link

Choose a reason for hiding this comment

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

The validation code is placed outside the function scope. This code should be moved inside the calculate function before the operation call to properly validate the input.

Suggested change
}

Copilot uses AI. Check for mistakes.
Comment thread api/controller.js
if (!req.query.operand2 ||
!req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand2: " + req.query.operand2);

Copilot AI Aug 13, 2025

Copy link

Choose a reason for hiding this comment

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

The error message includes the raw user input (req.query.operand2), which could lead to information disclosure or log injection attacks. Consider using a generic error message without exposing the actual input value.

Suggested change
throw new Error("Invalid operand2: " + req.query.operand2);
throw new Error("Invalid operand2");

Copilot uses AI. Check for mistakes.
Comment thread api/controller.js
//Make a change
if (!req.query.operand2 ||
!req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {

Copilot AI Aug 13, 2025

Copy link

Choose a reason for hiding this comment

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

The regex pattern allows multiple decimal points (e.g., '1.2.3' would pass). The pattern should use a more restrictive regex like /^-?\d+(.\d+)?(e-?\d+)?$/i to properly validate decimal numbers.

Suggested change
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
!req.query.operand2.match(/^-?\d+(\.\d+)?(e-?\d+)?$/i)) {

Copilot uses AI. Check for mistakes.
Comment thread api/controller.js
//Make a change
if (!req.query.operand2 ||
!req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {

Copilot AI Aug 13, 2025

Copy link

Choose a reason for hiding this comment

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

This validation logic is complex and unclear. The replace operation checks for multiple decimal points but is difficult to understand. Consider using a clearer validation approach or adding a comment explaining the purpose.

Suggested change
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
// Validate operand2: must be a valid floating-point number (optionally negative, with optional scientific notation)
if (!req.query.operand2 ||
!req.query.operand2.match(/^[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?$/i)) {

Copilot uses AI. Check for mistakes.
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