-
Notifications
You must be signed in to change notification settings - Fork 4
DR-112 - New Feature #29
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
ff24541
4744482
258833f
5d6b789
d201499
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 | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,38 +30,9 @@ | ||||||||||||||||||||||||||||||||||
| return listSale; | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| public void save(Sale sale) throws DuplicateKeyException { | |||||||||||||||||||||||||||||||||||
| try { | |||||||||||||||||||||||||||||||||||
| System.out.println(sale); // log the Sale object | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| if (sale == null) { | |||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Sale object cannot be null"); | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| if (jdbcTemplate == null) { | |||||||||||||||||||||||||||||||||||
| throw new IllegalStateException("JdbcTemplate cannot be null"); | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
| // Check if a record with the same primary key already exists | |||||||||||||||||||||||||||||||||||
| int count = jdbcTemplate.queryForObject( | |||||||||||||||||||||||||||||||||||
| "SELECT COUNT(*) FROM sales WHERE serial_number = ?", Integer.class, sale.getSerialNumber()); | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| if (count > 0) { | |||||||||||||||||||||||||||||||||||
| // If such a record exists, throw an exception | |||||||||||||||||||||||||||||||||||
| throw new DuplicateKeyException("A record with the same serial number already exists."); | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| // If no such record exists, insert the new record | |||||||||||||||||||||||||||||||||||
| SimpleJdbcInsert insertActor = | |||||||||||||||||||||||||||||||||||
| new SimpleJdbcInsert(jdbcTemplate != null ? jdbcTemplate : new JdbcTemplate()); | |||||||||||||||||||||||||||||||||||
| insertActor.withTableName("sales").usingColumns("serial_number", "item", "quantity", "amount", "date"); | |||||||||||||||||||||||||||||||||||
| BeanPropertySqlParameterSource param = new BeanPropertySqlParameterSource(sale); | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| insertActor.execute(param); | |||||||||||||||||||||||||||||||||||
| } catch (DuplicateKeyException e) { | |||||||||||||||||||||||||||||||||||
| throw e; // rethrow the exception to be handled by the caller | |||||||||||||||||||||||||||||||||||
| } catch (Exception e) { | |||||||||||||||||||||||||||||||||||
| e.printStackTrace(); // log any other exceptions | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
| public void save(Sale sale) { | |||||||||||||||||||||||||||||||||||
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES ('" + sale.getItem() + "', " + sale.getQuantity() + ", " + sale.getAmount() + ")"; | |||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES ('" + sale.getItem() + "', " + sale.getQuantity() + ", " + sale.getAmount() + ")"; | |
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES (?, ?, ?)"; | |
| jdbcTemplate.update(sql, sale.getItem(), sale.getQuantity(), sale.getAmount()); |
Check failure
Code scanning / CodeQL
Query built from user-controlled sources High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 12 months ago
To fix the problem, we need to replace the string concatenation in the save method with a parameterized query using PreparedStatement. This will ensure that user input is properly escaped and prevent SQL injection attacks.
- Change the SQL query construction in the
savemethod to use placeholders (?) for the values. - Use
jdbcTemplate.updatewith the SQL query and the values from theSaleobject as parameters.
-
Copy modified lines R33-R36
| @@ -32,6 +32,6 @@ | ||
|
|
||
| public void save(Sale sale) { | ||
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES ('" + sale.getItem() + "', " + sale.getQuantity() + ", " + sale.getAmount() + ")"; | ||
| jdbcTemplate.update(sql); | ||
| } | ||
| public void save(Sale sale) { | ||
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES (?, ?, ?)"; | ||
| jdbcTemplate.update(sql, sale.getItem(), sale.getQuantity(), sale.getAmount()); | ||
| } | ||
|
|
Copilot
AI
Jun 10, 2025
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.
This concatenated SQL string is vulnerable to SQL injection and omits the serial_number and date columns; switch to a parameterized query using jdbcTemplate.update(String, Object...) or NamedParameterJdbcTemplate.
| public void save(Sale sale) { | |
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES ('" + sale.getItem() + "', " + sale.getQuantity() + ", " + sale.getAmount() + ")"; | |
| jdbcTemplate.update(sql); | |
| } | |
| public void save(Sale sale) { | |
| String sql = "INSERT INTO SALES (item, quantity, amount) VALUES (?, ?, ?)"; | |
| jdbcTemplate.update(sql, sale.getItem(), sale.getQuantity(), sale.getAmount()); | |
| } |
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.
The removal of the null check for the sale object might lead to a NullPointerException. Re-add the null check for the sale object.