Skip to content

Commit

Permalink
Merge pull request from GHSA-g9v2-wqcj-j99g
Browse files Browse the repository at this point in the history
* Fix attempt

* Update message
  • Loading branch information
louislam authored Oct 8, 2023
1 parent bd9c44c commit 88afab6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
15 changes: 15 additions & 0 deletions server/model/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { BeanModel } = require("redbean-node/dist/bean-model");
const passwordHash = require("../password-hash");
const { R } = require("redbean-node");
const jwt = require("jsonwebtoken");
const { shake256, SHAKE256_LENGTH } = require("../util-server");

class User extends BeanModel {
/**
Expand All @@ -27,6 +29,19 @@ class User extends BeanModel {
this.password = newPassword;
}

/**
* Create a new JWT for a user
* @param {User} user
* @param {string} jwtSecret
* @return {string}
*/
static createJWT(user, jwtSecret) {
return jwt.sign({
username: user.username,
h: shake256(user.password, SHAKE256_LENGTH),
}, jwtSecret);
}

}

module.exports = User;
23 changes: 14 additions & 9 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ const app = server.app;
log.info("server", "Importing this project modules");
log.debug("server", "Importing Monitor");
const Monitor = require("./model/monitor");
const User = require("./model/user");

log.debug("server", "Importing Settings");
const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, doubleCheckPassword, startE2eTests } = require("./util-server");
const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, doubleCheckPassword, startE2eTests, shake256, SHAKE256_LENGTH
} = require("./util-server");

log.debug("server", "Importing Notification");
const { Notification } = require("./notification");
Expand Down Expand Up @@ -296,6 +299,11 @@ let needSetup = false;
decoded.username,
]);

// Check if the password changed
if (decoded.h !== shake256(user.password, SHAKE256_LENGTH)) {
throw new Error("The token is invalid due to password change or old token");
}

if (user) {
log.debug("auth", "afterLogin");
afterLogin(socket, user);
Expand All @@ -316,9 +324,10 @@ let needSetup = false;
});
}
} catch (error) {

log.error("auth", `Invalid token. IP=${clientIP}`);

if (error.message) {
log.error("auth", error.message, `IP=${clientIP}`);
}
callback({
ok: false,
msg: "Invalid token.",
Expand Down Expand Up @@ -357,9 +366,7 @@ let needSetup = false;

callback({
ok: true,
token: jwt.sign({
username: data.username,
}, server.jwtSecret),
token: User.createJWT(user, server.jwtSecret),
});
}

Expand Down Expand Up @@ -387,9 +394,7 @@ let needSetup = false;

callback({
ok: true,
token: jwt.sign({
username: data.username,
}, server.jwtSecret),
token: User.createJWT(user, server.jwtSecret),
});
} else {

Expand Down
18 changes: 18 additions & 0 deletions server/util-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const dayjs = require("dayjs");
// SASLOptions used in JSDoc
// eslint-disable-next-line no-unused-vars
const { Kafka, SASLOptions } = require("kafkajs");
const crypto = require("crypto");

const isWindows = process.platform === /^win/.test(process.platform);
/**
Expand Down Expand Up @@ -1055,6 +1056,23 @@ module.exports.grpcQuery = async (options) => {
});
};

module.exports.SHAKE256_LENGTH = 16;

/**
*
* @param {string} data
* @param {number} len
* @return {string}
*/
module.exports.shake256 = (data, len) => {
if (!data) {
return "";
}
return crypto.createHash("shake256", { outputLength: len })
.update(data)
.digest("hex");
};

// For unit test, export functions
if (process.env.TEST_BACKEND) {
module.exports.__test = {
Expand Down

0 comments on commit 88afab6

Please sign in to comment.