Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit ab66108

Browse files
author
mattpass
committed
Usernames can have alphanums, underscore and hyphen only
1 parent e720ba5 commit ab66108

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/login.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
if (true === $ICEcoder["multiUser"]) {
6666
// Also set value to "admin" if only 1 user (has to be admin)
6767
$showAdminValue = 1 === count($configUsernames) ? ' value="admin"' : '';
68-
echo '<input type="text" name="username"' . $showAdminValue . ' class="password"><br><br>';
68+
echo '<input type="text" name="username"' . $showAdminValue . ' class="password" id="username" onkeydown="return checkUernameKey(event.key)" onkeyup="checkUsername(this.value, true)" onchange="checkUsername(this.value, true)" onpaste="checkUsername(this.value, true)"><br><br>';
6969
};
7070
?>
7171
<input type="password" name="password" class="password" id="password"<?php
@@ -128,6 +128,21 @@
128128
return document.getElementById(elem);
129129
};
130130

131+
// Check keydown in username field meets simple rules (alphanums, underscore and hyphen only)
132+
const checkUernameKey = function(key) {
133+
return /[\w_\-]/g.test(key);
134+
}
135+
136+
// Check username value meets simple rules (alphanums, underscore and hyphen only)
137+
const checkUsername = function(username, amend) {
138+
// Amend username if OK to do this
139+
if (true === amend) {
140+
get("username").value = username.replace(/[^\w_\-]/g, "");
141+
}
142+
// Return a bool based on meeting the requirements
143+
return username.replace(/[^\w_\-]/g, "").length === username.length;
144+
};
145+
131146
// Check password strength and color requirements not met
132147
const pwStrength = function(pw) {
133148
// Set variables
@@ -165,7 +180,16 @@
165180

166181
// Check if we can submit, else shake requirements
167182
const checkCanSubmit = function() {
168-
// Password isn't strong enough, shake requirements
183+
<?php
184+
// Check username field if multiUser enabled
185+
if (true === $ICEcoder["multiUser"]) {
186+
?>// Username isn't simple, can't submit
187+
if(false === checkUsername(get("username").value, false)) {
188+
return false;
189+
}
190+
<?php
191+
}
192+
?>// Password isn't strong enough, shake requirements
169193
if(false === pwStrength(get("password").value)) {
170194
var posArray = [24, -24, 12, -12, 6, -6, 3, -3, 0];
171195
var pos = -1;

lib/settings.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@
5757
// Load common functions
5858
include_once dirname(__FILE__) . "/settings-common.php";
5959

60+
$postUsername = true === isset($_POST['username']) && is_string($_POST['username'])
61+
? preg_replace("/[^\w_\-]/", "", $_POST['username'])
62+
: "";
63+
6064
// Establish user settings file
6165
$username = "admin-";
62-
if (true === isset($_POST['username']) && "" !== $_POST['username']) {$username = $_POST['username'] . "-";};
66+
if ("" !== $postUsername) {$username = $postUsername . "-";};
6367
if (true === isset($_SESSION['username']) && "" !== $_SESSION['username']) {$username = $_SESSION['username'] . "-";};
6468
$settingsFile = 'config-' . $username . str_replace(".", "_", str_replace("www.", "", $_SERVER['SERVER_NAME'])) . '.php';
6569

@@ -164,7 +168,7 @@
164168
if (verifyHash($_POST['password'], $ICEcoder["password"]) === $ICEcoder["password"]) {
165169
session_regenerate_id();
166170
if ($ICEcoder["multiUser"]) {
167-
$_SESSION['username'] = $_POST['username'];
171+
$_SESSION['username'] = $postUsername;
168172
}
169173
$_SESSION['loggedIn'] = true;
170174
$extraProcessesClass = new ExtraProcesses();
@@ -235,7 +239,7 @@
235239
}
236240
// Set the session user level
237241
if ($ICEcoder["multiUser"]) {
238-
$_SESSION['username'] = $_POST['username'];
242+
$_SESSION['username'] = $postUsername;
239243
}
240244
$_SESSION['loggedIn'] = true;
241245
$extraProcessesClass = new ExtraProcesses();

0 commit comments

Comments
 (0)