mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-11 01:10:11 +01:00
enfore min and max username lengths to prevent DOS via spam-length names
This commit is contained in:
parent
323e37e684
commit
3ef009de73
@ -1024,7 +1024,7 @@ function systemEndpoints(app) {
|
|||||||
|
|
||||||
const updates = {};
|
const updates = {};
|
||||||
if (username) {
|
if (username) {
|
||||||
updates.username = String(username);
|
updates.username = User.validations.username(String(username));
|
||||||
}
|
}
|
||||||
if (password) {
|
if (password) {
|
||||||
updates.password = String(password);
|
updates.password = String(password);
|
||||||
|
@ -10,6 +10,20 @@ const User = {
|
|||||||
"role",
|
"role",
|
||||||
"suspended",
|
"suspended",
|
||||||
],
|
],
|
||||||
|
validations: {
|
||||||
|
username: (newValue = "") => {
|
||||||
|
try {
|
||||||
|
if (String(newValue).length > 100)
|
||||||
|
throw new Error("Username cannot be longer than 100 characters");
|
||||||
|
if (String(newValue).length < 2)
|
||||||
|
throw new Error("Username must be at least 2 characters");
|
||||||
|
return String(newValue);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e.message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// validations for the above writable fields.
|
// validations for the above writable fields.
|
||||||
castColumnValue: function (key, value) {
|
castColumnValue: function (key, value) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
@ -36,9 +50,9 @@ const User = {
|
|||||||
const hashedPassword = bcrypt.hashSync(password, 10);
|
const hashedPassword = bcrypt.hashSync(password, 10);
|
||||||
const user = await prisma.users.create({
|
const user = await prisma.users.create({
|
||||||
data: {
|
data: {
|
||||||
username,
|
username: this.validations.username(username),
|
||||||
password: hashedPassword,
|
password: hashedPassword,
|
||||||
role,
|
role: String(role),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return { user: this.filterFields(user), error: null };
|
return { user: this.filterFields(user), error: null };
|
||||||
@ -75,7 +89,13 @@ const User = {
|
|||||||
// and force-casts to the proper type;
|
// and force-casts to the proper type;
|
||||||
Object.entries(updates).forEach(([key, value]) => {
|
Object.entries(updates).forEach(([key, value]) => {
|
||||||
if (this.writable.includes(key)) {
|
if (this.writable.includes(key)) {
|
||||||
|
if (this.validations.hasOwnProperty(key)) {
|
||||||
|
updates[key] = this.validations[key](
|
||||||
|
this.castColumnValue(key, value)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
updates[key] = this.castColumnValue(key, value);
|
updates[key] = this.castColumnValue(key, value);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
delete updates[key];
|
delete updates[key];
|
||||||
|
Loading…
Reference in New Issue
Block a user