-
Notifications
You must be signed in to change notification settings - Fork 0
Test: exercise PRowl reviewer #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||
| // Demo file for exercising PRowl end-to-end on a real PR. | ||||||||||||||||||||||||
| // It contains three deliberate issues (one per detectable type) plus one | ||||||||||||||||||||||||
| // intentionally clean function that must NOT be flagged. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const db = require("./db"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (1) SECURITY — SQL injection: user input concatenated straight into a query. | ||||||||||||||||||||||||
| function getOrder(req, res) { | ||||||||||||||||||||||||
| const orderId = req.query.id; | ||||||||||||||||||||||||
| const sql = "SELECT * FROM orders WHERE id = " + orderId; | ||||||||||||||||||||||||
| return db.query(sql); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (2) BUG — unguarded access: cart.items may be undefined/empty, [0].price throws. | ||||||||||||||||||||||||
| function firstItemPrice(cart) { | ||||||||||||||||||||||||
| return cart.items[0].price; | ||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [BUG] The code assumes that 'cart.items' is not empty, which can lead to an error if 'cart.items' is an empty array. Accessing the first element without checking can cause an 'undefined' error.
Suggested change
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (3) PERFORMANCE — O(n^2) duplicate scan with a doubly-nested loop. | ||||||||||||||||||||||||
| function hasDuplicate(arr) { | ||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||
| for (let j = 0; j < arr.length; j++) { | ||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [PERFORMANCE] The nested loop results in an O(n^2) time complexity, which is inefficient for checking duplicates. Using a Set to track seen elements reduces the complexity to O(n).
Suggested change
|
||||||||||||||||||||||||
| if (i !== j && arr[i] === arr[j]) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (4) CLEAN — straightforward, no issues. Should produce NO comment. | ||||||||||||||||||||||||
| function add(a, b) { | ||||||||||||||||||||||||
| return a + b; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| module.exports = { getOrder, firstItemPrice, hasDuplicate, add }; | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[SECURITY] The code is vulnerable to SQL injection because it directly concatenates user input (orderId) into the SQL query string.