From df2828c857a5c21cb32d18ec529b1dc07b0d113c Mon Sep 17 00:00:00 2001
From: Frederik Kemner
Date: Tue, 4 Oct 2022 12:07:57 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Don't=20accept=20user=20names=20?=
=?UTF-8?q?with=20extra=20spaces=20(#4631)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes #4621
Individual commits:
* User names with extra spaces are invalid
* fix return value formatting
* API: Add user name validation
* Core Wizard: Add error message
Add 'invalid user name' error message
* Fix colon typo
* Fix style
* Fix import
* fix error state in core wizard
separately handle invalid user name
* fix isort style error
* Fix API validation
python strings use strip()
* add user name validation to settings
* Correct spelling of username
* implemented foosel's requested changes
changed checks from invalidUsername to validUsername everywhere
standardized the requirements for valid user names
* introduced providedUsername function
ensures user name field doesn't start in an error state
Co-authored-by: Frederik
---
AUTHORS.md | 1 +
src/octoprint/access/users.py | 8 ++++++++
src/octoprint/cli/user.py | 3 +++
.../plugins/corewizard/static/js/corewizard.js | 11 +++++++++--
.../templates/corewizard_acl_wizard.jinja2 | 3 ++-
src/octoprint/server/api/access.py | 2 ++
src/octoprint/static/js/app/viewmodels/access.js | 13 +++++++++++--
.../snippets/settings/accesscontrol/users.jinja2 | 7 ++++---
8 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/AUTHORS.md b/AUTHORS.md
index 81dc11118..11a135347 100644
--- a/AUTHORS.md
+++ b/AUTHORS.md
@@ -171,6 +171,7 @@ date of first contribution):
* [Dawid Pieper](https://github.com/dawidpieper)
* ["arrdem"](https://github.com/arrdem)
* [Arkadiusz MiĆkiewicz](https://github.com/arekm)
+ * [Frederik Kemner](https://github.com/040medien)
OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by
[Daid Braam](https://github.com/daid). Parts of its communication layer and
diff --git a/src/octoprint/access/users.py b/src/octoprint/access/users.py
index 5b04d1414..25facd782 100644
--- a/src/octoprint/access/users.py
+++ b/src/octoprint/access/users.py
@@ -655,6 +655,9 @@ class FilebasedUserManager(UserManager):
if username in self._users and not overwrite:
raise UserAlreadyExists(username)
+ if not username.strip() or username != username.strip():
+ raise InvalidUsername(username)
+
self._users[username] = User(
username,
UserManager.create_password_hash(password, settings=self._settings),
@@ -1054,6 +1057,11 @@ class UserAlreadyExists(Exception):
Exception.__init__(self, "User %s already exists" % username)
+class InvalidUsername(Exception):
+ def __init__(self, username):
+ Exception.__init__(self, "Username '%s' is invalid" % username)
+
+
class UnknownUser(Exception):
def __init__(self, username):
Exception.__init__(self, "Unknown user: %s" % username)
diff --git a/src/octoprint/cli/user.py b/src/octoprint/cli/user.py
index d07d2cba3..d0813464d 100644
--- a/src/octoprint/cli/user.py
+++ b/src/octoprint/cli/user.py
@@ -11,6 +11,7 @@ from octoprint.access.groups import FilebasedGroupManager
from octoprint.access.users import (
CorruptUserStorage,
FilebasedUserManager,
+ InvalidUsername,
UnknownUser,
UserAlreadyExists,
)
@@ -126,6 +127,8 @@ def add_user_command(ctx, username, password, groups, permissions, is_admin):
click.echo(f"\t{_user_to_line(user.as_dict())}")
except UserAlreadyExists:
click.echo(f"A user with the name {username} does already exist!", err=True)
+ except InvalidUsername:
+ click.echo(f"The username '{username}' is invalid!", err=True)
@cli.command(name="remove")
diff --git a/src/octoprint/plugins/corewizard/static/js/corewizard.js b/src/octoprint/plugins/corewizard/static/js/corewizard.js
index ba87f2f04..370be77eb 100644
--- a/src/octoprint/plugins/corewizard/static/js/corewizard.js
+++ b/src/octoprint/plugins/corewizard/static/js/corewizard.js
@@ -16,8 +16,12 @@ $(function () {
return self.password() !== self.confirmedPassword();
});
+ self.providedUsername = ko.pureComputed(function () {
+ return self.username() && self.username().trim();
+ });
+
self.validUsername = ko.pureComputed(function () {
- return self.username() && self.username().trim() !== "";
+ return !self.username() || self.username() == self.username().trim();
});
self.validPassword = ko.pureComputed(function () {
@@ -26,7 +30,10 @@ $(function () {
self.validData = ko.pureComputed(function () {
return (
- !self.passwordMismatch() && self.validUsername() && self.validPassword()
+ self.providedUsername() &&
+ self.validUsername() &&
+ !self.passwordMismatch() &&
+ self.validPassword()
);
});
diff --git a/src/octoprint/plugins/corewizard/templates/corewizard_acl_wizard.jinja2 b/src/octoprint/plugins/corewizard/templates/corewizard_acl_wizard.jinja2
index 1537989d2..4a98ff790 100644
--- a/src/octoprint/plugins/corewizard/templates/corewizard_acl_wizard.jinja2
+++ b/src/octoprint/plugins/corewizard/templates/corewizard_acl_wizard.jinja2
@@ -7,10 +7,11 @@
OctoPrint's settings:
{% endtrans %}