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
36 changes: 25 additions & 11 deletions WebAppServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const port = 3000;

const fs = require('fs');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');

const connection = mysql.createConnection({
'host': 'localhost',
Expand All @@ -16,6 +18,14 @@ const connection = mysql.createConnection({

app.use(express.json());
app.use(cors());
app.use(helmet());

const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});

app.use(limiter);

app.post('/register', async function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*')
Expand All @@ -25,14 +35,14 @@ app.post('/register', async function (req, res) {
return
}

const username = mysql.escape(req.body.username);
const username = req.body.username;
const password = req.body.password;

const hashPassword = mysql.escape(await bcrypt.hash(password, 10));
const hashPassword = await bcrypt.hash(password, 10);

const sql = "INSERT INTO user (username, password) VALUES (" + username + "," + hashPassword + ")";
const sql = "INSERT INTO user (username, password) VALUES (?, ?)";

connection.query(sql, function (err, sqlRes) {
connection.query(sql, [username, hashPassword], function (err, sqlRes) {
if (err == null) {
res.sendStatus(201);
} else {
Expand Down Expand Up @@ -63,10 +73,12 @@ app.post('/screenshot', async (req, res) => {
return;
}

const username = mysql.escape(getUsernameFromAuth(req));
const username = getUsernameFromAuth(req);

fs.mkdir(username, function () {
const stream = fs.createWriteStream(username + '/' + Date.now().toString() + '.jpeg')
const sanitizedUsername = username.replace(/[^a-zA-Z0-9]/g, '');

fs.mkdir(sanitizedUsername, { recursive: true }, function () {
const stream = fs.createWriteStream(sanitizedUsername + '/' + Date.now().toString() + '.jpeg')
stream.write(new Uint8Array(req.body))
});

Expand All @@ -81,9 +93,11 @@ app.get('/gallery', async (req, res) => {
return;
}

const username = mysql.escape(getUsernameFromAuth(req));
const username = getUsernameFromAuth(req);

const sanitizedUsername = username.replace(/[^a-zA-Z0-9]/g, '');

fs.readdir(username, (err, files) => {
fs.readdir(sanitizedUsername, (err, files) => {
if (err) {
res.sendStatus(404);
return
Expand All @@ -92,7 +106,7 @@ app.get('/gallery', async (req, res) => {
const arr = [];

files.forEach(file => {
const data = fs.readFileSync(username + '/' + file)
const data = fs.readFileSync(sanitizedUsername + '/' + file)
arr.push([...data]);
});

Expand All @@ -115,7 +129,7 @@ async function isAuthorized(req) {
}

login = mysql.escape(login);
connection.query('select password from user where username = ' + login, async function (err, res) {
connection.query('select password from user where username = ?', [login], async function (err, res) {
if (err == null) {
const dbPassword = res[0].password;

Expand Down