Skip to content

Commit

Permalink
FIX: fixed some issue and vulnerabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonaion02 committed Jul 8, 2023
1 parent e0a7c69 commit 8dd3500
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 73 deletions.
10 changes: 7 additions & 3 deletions mysql/CreateDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ CREATE TABLE Game (
description text DEFAULT(""),
shortDescription text DEFAULT(""),
releaseDate date DEFAULT("1999-09-09"),
state ENUM("Released", "Beta", "Alpha", "Coming_soon", "Unlisted") NOT NULL DEFAULT("Coming_soon"),
state ENUM("Released", "Beta", "Alpha", "Unlisted") NOT NULL DEFAULT("Alpha"),
pegi int NOT NULL DEFAULT(18),
publisher varchar(30) NOT NULL DEFAULT("Microsoft"),
primary key(id)
primary key(id),
constraint number_price_game
check(price >= 0 and price <= 1000)
);

CREATE TABLE Category (
Expand Down Expand Up @@ -95,7 +97,9 @@ CREATE TABLE Purchase (
CONSTRAINT usernameConsPurchase
foreign key (username) references User (username),
UNIQUE (gameId, username), -- TEST: IF WORK
primary key (id)
primary key (id),
constraint number_price_purchase
check(price >= 0 and price <= 1000)
);

CREATE TABLE Review (
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/control/BaseServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ protected boolean validParameters(HttpServletRequest request, HttpServletRespons
return true;
}

protected void showError(HttpServletRequest request, HttpServletResponse response, String message, String path) {
protected void showError(HttpServletRequest request, HttpServletResponse response, String message, String path) throws ServletException, IOException {
request.setAttribute("logError", message);
RequestDispatcher rs = request.getRequestDispatcher(path);
try {
rs.forward(request, response);
} catch (ServletException | IOException e) {
//AAAAAAAA
}

rs.forward(request, response);
}
}
4 changes: 2 additions & 2 deletions src/main/java/control/DeleteFromCartServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import model.Cart;
import model.Interested;
import model.User;

import utility.BackendException;
import utility.InvalidParameters;

@WebServlet("/DeleteFromCartServlet")
Expand Down Expand Up @@ -70,7 +70,7 @@ protected synchronized void doGet(HttpServletRequest request, HttpServletRespons
try {
interestedDAO.removeInterest(interested);
} catch (SQLException e) {
e.printStackTrace();
throw new BackendException();
}
//Add interest to the database
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/dao/GameDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void updateGame (Game game) throws SQLException {
}
}

private static final String COUNTGAMEQUERY = "SELECT Count(DISTINCT id) as count"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in ";

public int countGames(List<Category> categories, int maxPrice, int pegi, String searchText, boolean unListed) throws SQLException {
int size = 0;

Expand All @@ -110,17 +114,13 @@ public int countGames(List<Category> categories, int maxPrice, int pegi, String
//Construct query string

//Construct query
String query = "SELECT Count(DISTINCT id) as count"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in "
String query = COUNTGAMEQUERY
+ categoriesToSearch
+ "AND G.price <= ? AND "
+ "G.pegi <= ? AND INSTR(G.name, ?) > 0 ";

if(! unListed)
query = "SELECT Count(DISTINCT id) as count"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in "
query = COUNTGAMEQUERY
+ categoriesToSearch
+ "AND G.price <= ? AND "
+ "G.state != 'unlisted' AND "
Expand Down Expand Up @@ -158,6 +158,10 @@ public int countGames(List<Category> categories, int maxPrice, int pegi, String
return size;
}

private static final String RETRIEVEGAMEQUERY = "SELECT DISTINCT id, price, G.name, description, shortDescription, releaseDate, state, pegi, publisher"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in ";

public List<Game> retrieveGames(List<Category> categories, int maxPrice, int pegi, String searchText, String order, int limit, int offset, boolean unListed) throws SQLException {
List<Game> games = new ArrayList<>();

Expand All @@ -182,19 +186,15 @@ public List<Game> retrieveGames(List<Category> categories, int maxPrice, int peg
//Construct query string

//Construct query
String query = "SELECT DISTINCT id, price, G.name, description, shortDescription, releaseDate, state, pegi, publisher"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in "
String query = RETRIEVEGAMEQUERY
+ categoriesToSearch
+ "AND G.price <= ? AND "
+ "G.pegi <= ? AND INSTR(G.name, ?) > 0 "
+ "ORDER BY " + order
+ " LIMIT ? OFFSET ?";

if(! unListed) {
query = "SELECT DISTINCT id, price, G.name, description, shortDescription, releaseDate, state, pegi, publisher"
+ " FROM Game as G, Belongs as B, Category as C "
+ "WHERE G.id = B.gameId AND C.name = B.categoryName AND C.name in "
query = RETRIEVEGAMEQUERY
+ categoriesToSearch
+ "AND G.price <= ? AND "
+ "G.pegi <= ? AND INSTR(G.name, ?) > 0 "
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dao/ImageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ImageDAO(DataSource ds) {
super(ds);
}

public synchronized Image getImageFromID(String Id) throws SQLException {
public synchronized Image getImageFromID(String id) throws SQLException {

Image image = null;

Expand All @@ -28,7 +28,7 @@ public synchronized Image getImageFromID(String Id) throws SQLException {
//Retrieve connection and make prepared statement

//Create query
ps.setString(1, Id);
ps.setString(1, id);
//Create query

//Execute query
Expand All @@ -40,7 +40,7 @@ public synchronized Image getImageFromID(String Id) throws SQLException {

//Create the image object
image = new Image();
image.setId(Id);
image.setId(id);
image.setAltText(rs.getString("alt"));
byte[] bytes = rs.getBytes("raw");
image.setBytes(bytes);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public enum State{
RELEASED("Released"),
BETA("Beta"),
ALPHA("Alpha"),
COMING_SOON("Coming_soon"),
UNLISTED("Unlisted");

private String value;
Expand Down
1 change: 0 additions & 1 deletion src/main/webapp/CSS/CartStyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ p.gamePrice span.removeButton{

#emptyCartButton{
font-size: 12px;
float:right;
}
3 changes: 2 additions & 1 deletion src/main/webapp/Cart.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<link rel="stylesheet" href="./CSS/CartStyle.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src="./Scripts/CartScript.js" defer></script>

<title>Cart</title>
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/Catalog.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<html lang = en>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src=./Scripts/CatalogScript.js></script>

<meta charset="ISO-8859-1">
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/PersonalGamePage.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<link rel="stylesheet" href="./CSS/PersonalGamePage.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src="./Scripts/PersonalGamePageScript.js" defer></script>

<title>Personal Game Page</title>
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/Register.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import="org.owasp.encoder.Encode" %>
<link rel="stylesheet" href="./CSS/Register.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src="./Scripts/RegisterScript.js"defer></script>

<title>Sign up to Gaming World</title>
Expand Down
8 changes: 4 additions & 4 deletions src/main/webapp/Scripts/CartScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let USDollar = new Intl.NumberFormat('en-US', {
//when document is ready
window.addEventListener('load', function () {
//format prices
classes = document.getElementsByClassName("price")
let classes = document.getElementsByClassName("price")
for( i = 0; i<classes.length; i++){
classes[i].innerHTML = USDollar.format(classes[i].innerHTML/100);
}
Expand Down Expand Up @@ -48,10 +48,10 @@ function calculateTotalPrice(){
//total price starts at 0

//get all price divs
classes = document.getElementsByClassName("price")
let classes = document.getElementsByClassName("price");
//get all price divs

for(var i = 0; i<classes.length; i++){
for(let i = 0; i<classes.length; i++){
//format div to only read number (and skip dollar sign)
let string = classes[i].innerHTML.replace(/,/g, ''); //replace thousand commas with white space otherwise they can't be parsed
total += parseInt(string.substring(1, string.length)*100);
Expand All @@ -60,7 +60,7 @@ function calculateTotalPrice(){

//insert total price in DOM
$('#totalPrice').html("");
$('#totalPrice').append("Total price: "+USDollar.format(total/100));
$('#totalPrice').append("Total price: " + USDollar.format(total/100));
//insert total price in DOM
}
//sets total price of items in cart
6 changes: 3 additions & 3 deletions src/main/webapp/Scripts/CatalogScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ $(document).ready(function() {

function updateCatalog(page) {
//convert form to array
var data = $('form').serializeArray();
let data = $('form').serializeArray();
//convert form to array

//add the page value to the data to send to server
data.push({name: "page", value: page})
//add the page value to the data to send to server

//Send GET response to servlet to retrieve games
var url="/GamingWorldShop/GetCatalogGameObjects"
let url="/GamingWorldShop/GetCatalogGameObjects"

$.get(url, data, function(responseData){
let giochi = responseData;
Expand All @@ -56,7 +56,7 @@ function updateCatalog(page) {
}

//add pagination based on number of games found
gameCount = responseData.gamesCount;
let gameCount = responseData.gamesCount;
if(gameCount > 0 ) {
$('#gameListSection').append("<div class=pageDiv>")
let pageSize = 10;
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/Scripts/GameLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ let USDollar = new Intl.NumberFormat('en-US', {

$(document).ready(function() {
//format prices
var classes = document.getElementsByClassName("price")
for(var i = 0; i < classes.length; i++){
let classes = document.getElementsByClassName("price")
for(let i = 0; i < classes.length; i++){
classes[i].innerHTML = USDollar.format(classes[i].innerHTML/100);
}
//format prices
Expand Down
8 changes: 4 additions & 4 deletions src/main/webapp/Scripts/PersonalGamePageScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ function formatPrice(price) {

//Convert price of game in dollars
$(document).ready(function() {
var stringPrice = document.getElementById("buyButton").innerHTML;
let stringPrice = document.getElementById("buyButton").innerHTML;
document.getElementById("buyButton").innerHTML = formatPrice(stringPrice);
});
//Convert price of game in dollars

//Function to change schede requirement
function changeSchedeReq(id) {
schedes = document.getElementsByClassName("reqSchede");
for(schede in schedes) {
let schedes = document.getElementsByClassName("reqSchede");
for(let schede in schedes) {
schedes[schede].style = "display: none";
}

Expand All @@ -32,7 +32,7 @@ function addToCart(id) {

$.get("/GamingWorldShop/AddToCartServlet?gameId="+id+"&category=cart");

str = $('#cartItemCount').html();
let str = $('#cartItemCount').html();
str = str.replace(/\s/g, '');

if(str == "") {
Expand Down
11 changes: 6 additions & 5 deletions src/main/webapp/Scripts/Purchase.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var errorMessages = {
fname: "The name can contain only characters",
fname: "The name can contain only characters separeted by a space",
state: "The state is a pair of char",
zip: "ZipCode is a number of 5 digit",
cname: "The name on card can contain only characters",
cname: "The name on card can contain only characters separeted by a spaces",
cvv: "CVV is a number of 3 digit",
ccnum: "Card number follow this pattern: dddd-dddd-dddd-dddd"
ccnum: "Card number follow this pattern: dddd-dddd-dddd-dddd",
email: "Email must follow this pattern: [email protected]"
}

//Function to check validity of a field
Expand All @@ -31,7 +32,7 @@ function checkField(field) {
function checkForm(e) {

//Check if all the values of the fields are valid
for(f in errorMessages) {
for(let f in errorMessages) {
let i = f;
//In case the field hasn't a valid value
if(! checkField(i)) {
Expand All @@ -53,7 +54,7 @@ function checkForm(e) {
var oldLength = 0;
function assistCardNumber() {

textContent = document.getElementById("ccnum").value;
let textContent = document.getElementById("ccnum").value;

if(oldLength < textContent.length) {
if(textContent.length == 4 || textContent.length == 9 || textContent.length == 14)
Expand Down
5 changes: 2 additions & 3 deletions src/main/webapp/Scripts/PurchaseListScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ $(document).ready(function() {
//Function to update the page
function update() {
//Retrieve parameter from the form
var data = $("form").serializeArray();
console.log(data);
let data = $("form").serializeArray();
//Retrieve parameter from the form

//Retrieve purchases from the db
var url="/GamingWorldShop/admin/SearchPurchasesServlet";
let url="/GamingWorldShop/admin/SearchPurchasesServlet";

$.get(url, data, function buildTable(responseData) {
$('table tbody').empty();
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/Scripts/RegisterScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function validateFormElem(formElem, span, errorMessage) {
if(formElem.checkValidity()){

//Remove error for pattern mismatching
var doc = new DOMParser().parseFromString(span.innerHTML, "text/html")
let doc = new DOMParser().parseFromString(span.innerHTML, "text/html")
formElem.classList.remove("error");
if(errorMessage != null && doc.documentElement.textContent == errorMessage) {
formElem.classList.remove("error");
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/admin/PurchasesList.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<link rel="stylesheet" href="../CSS/PurchasesStyle.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src=../Scripts/PurchaseListScript.js></script>

<title>Purchase</title>
Expand Down
7 changes: 4 additions & 3 deletions src/main/webapp/admin/UpdateGame.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<meta name="viewport" content="initial-scale=1, width=device-width">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="></script>
integrity = "sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ=="
crossorigin="anonymous"></script>
<script src="../Scripts/UpdateGameScript.js"></script>

<link rel="stylesheet" href="../CSS/BaseStyle.css">
Expand Down Expand Up @@ -50,11 +51,11 @@
Description:<br> <textarea name="description" rows=30 cols=80 required><%=game.getDescription() %> </textarea>
</div>
<div id=shortDescriptionDiv>
Short description:<br> <textarea name="shortDescription" rows=10 cols=80 required> <%=game.getShortDescription() %> </textarea>
Short description:<br> <textarea name="shortDescription" maxlength=300 rows=10 cols=80 required> <%=game.getShortDescription() %> </textarea>
</div>
<div id=priceDiv>
Price: &#36;
<input type="number" value=<%=(float)game.getPrice()/100 %> min="0" max="100000000" step="0.01" id=price name="price" required onchange="convertToDecimal(this)" />
<input type="number" value=<%=(float)game.getPrice()/100 %> min="0" max="1000" step="0.01" id=price name="price" required onchange="convertToDecimal(this)" />
</div>
<div id=stateDiv>
State: <select name="state">
Expand Down
Loading

0 comments on commit 8dd3500

Please sign in to comment.