Skip to content
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

Fix security vulnerabilities in the project #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var log4js = require("log4js");
var csrf = require('csurf');

Check failure on line 11 in app.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app.js#L11

Require statement not part of import statement.

var init_db = require('./model/init_db');
var login = require('./routes/login');
Expand Down Expand Up @@ -41,12 +42,13 @@
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'ñasddfilhpaf78h78032h780g780fg780asg780dsbovncubuyvqy',
secret: process.env.SESSION_SECRET,
cookie: {
secure: false,
secure: true,
maxAge: 99999999999
}
}));
app.use(csrf());

/*
* Routes config
Expand Down
15 changes: 0 additions & 15 deletions attacks/evil_regex/attack_1.sh

This file was deleted.

15 changes: 0 additions & 15 deletions attacks/log_injection.sh

This file was deleted.

3 changes: 0 additions & 3 deletions attacks/sqli/login.sh

This file was deleted.

6 changes: 3 additions & 3 deletions model/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var config = require("../config"),
function do_auth(username, password) {
var db = pgp(config.db.connectionString);

var q = "SELECT * FROM users WHERE name = '" + username + "' AND password ='" + password + "';";
var q = "SELECT * FROM users WHERE name = $1 AND password = $2;";

return db.one(q);
return db.one(q, [username, password]);
}

module.exports = do_auth;
module.exports = do_auth;
25 changes: 8 additions & 17 deletions model/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,32 @@ function list_products() {

function getProduct(product_id) {

var q = "SELECT * FROM products WHERE id = '" + product_id + "';";
var q = "SELECT * FROM products WHERE id = $1;";

return db.one(q);
return db.one(q, [product_id]);
}

function search(query) {

var q = "SELECT * FROM products WHERE name ILIKE '%" + query + "%' OR description ILIKE '%" + query + "%';";
var q = "SELECT * FROM products WHERE name ILIKE $1 OR description ILIKE $2;";

return db.many(q);
return db.many(q, ['%' + query + '%', '%' + query + '%']);

}

function purchase(cart) {

var q = "INSERT INTO purchases(mail, product_name, user_name, product_id, address, phone, ship_date, price) VALUES('" +
cart.mail + "', '" +
cart.product_name + "', '" +
cart.username + "', '" +
cart.product_id + "', '" +
cart.address + "', '" +
cart.ship_date + "', '" +
cart.phone + "', '" +
cart.price +
"');";
var q = "INSERT INTO purchases(mail, product_name, user_name, product_id, address, phone, ship_date, price) VALUES($1, $2, $3, $4, $5, $6, $7, $8);";

return db.one(q);
return db.one(q, [cart.mail, cart.product_name, cart.username, cart.product_id, cart.address, cart.ship_date, cart.phone, cart.price]);

}

function get_purcharsed(username) {

var q = "SELECT * FROM purchases WHERE user_name = '" + username + "';";
var q = "SELECT * FROM purchases WHERE user_name = $1;";

return db.many(q);
return db.many(q, [username]);

}

Expand Down
7 changes: 5 additions & 2 deletions routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ router.post('/login/auth', function(req, res) {
var password = req.body.password;
var returnurl = req.body.returnurl;

logger.error("Tried to login attempt from user = " + user);
// Sanitize user input before logging
var sanitizedUser = user.replace(/[^a-zA-Z0-9]/g, '');
logger.error("Tried to login attempt from user = " + sanitizedUser);

Comment on lines +27 to 28
Copy link
Preview

Copilot AI Feb 3, 2025

Choose a reason for hiding this comment

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

[nitpick] Use 'Login attempt from user = ...' for correct grammar to avoid redundancy.

Suggested change
logger.error("Tried to login attempt from user = " + sanitizedUser);
logger.error("Login attempt from user = " + sanitizedUser);

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
auth(user, password)
.then(function (data) {
req.session.logged = true;
req.session.user_name = user;

if (returnurl == undefined || returnurl == ""){
// Validate the return URL
if (!returnurl || !returnurl.startsWith('/')) {
returnurl = "/";
}

Expand Down