Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/pr-reviewer/fixtures/demo-pr/cart-total.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export function cartTotal(items: { price: number; qty: number }[]): number {
export type CartItem = { price: number; qty: number; discountCents?: number };

export function cartTotal(items: CartItem[]): number {
let total = 0;
for (let i = 0; i < items.length; i++) {
if (items[i].qty < 0) {
throw new RangeError(`Negative quantity for item ${i}`);
}
total += items[i].price * items[i].qty;
const line = items[i].price * items[i].qty;
total += line - (items[i].discountCents ?? 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Discount can make cart total negative

High Severity

discountCents is subtracted from the line in full, so cartTotal can return a negative amount when the discount exceeds price * qty or when quantity is zero. Zero-quantity items previously contributed nothing to the total.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e5c70d3. Configure here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The discountCents value is not validated, which can lead to incorrect cart totals if it's negative or larger than the line item price.
Severity: HIGH

Suggested Fix

Add validation to ensure discountCents is not negative and does not exceed the line item's total price. Throw a RangeError if the value is invalid, similar to the existing validation for qty.

const line = items[i].price * items[i].qty;
if ((items[i].discountCents ?? 0) < 0) {
  throw new RangeError(`Negative discount for item ${i}`);
}
if ((items[i].discountCents ?? 0) > line) {
  throw new RangeError(`Discount exceeds line total for item ${i}`);
}
total += line - (items[i].discountCents ?? 0);
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: apps/pr-reviewer/fixtures/demo-pr/cart-total.ts#L10

Potential issue: The `cartTotal` function applies a discount from `discountCents`
without proper validation. This allows for two potential issues: 1) If `discountCents`
is a negative number, it will be added to the line item's total, incorrectly inflating
the final price. 2) If `discountCents` is greater than the line item's total price, it
will result in a negative value for that line item, which could lead to a negative
overall cart total, effectively paying a customer to take items.

Did we get this right? 👍 / 👎 to inform future reviews.

}
return total;
}
Loading