Skip to content

Commit ff34cbd

Browse files
committed
fix: share name validation
1 parent 8e1347c commit ff34cbd

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

emhttp/languages/en_US/helptext.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ Select the desired destinations and press **Write** to copy the settings to the
540540
:share_edit_name_help:
541541
The share name can be up to 40 characters, and is case-sensitive with these restrictions:
542542

543-
* cannot contain a double-quote character (") or the following characters: / \ * < > |
544-
* cannot be one of the reserved share names: flash, cache, cache2, .., disk1, disk2, ..
545-
546-
We highly recommend to make your life easier and avoid special characters.
543+
* must start with a letter
544+
* subsequent characters can be a letter, digit, space, period, underscore, or dash, except the last character may not be a space or a period
545+
* there may not be consecutive spaces
546+
* cannot be one of the reserved names: flash, cache, cache2, .., disk1, disk2, ..
547547
:end
548548

549549
:share_edit_comments_help:

emhttp/plugins/dynamix/ShareEdit.page

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ _(Write settings to)_ <i class="fa fa-arrow-right fa-fw"></i>
380380

381381
<div markdown="1" class="shade">
382382
_(Share name)_:
383-
: <input type="text" id="shareName" name="shareName" maxlength="40" autocomplete="off" spellcheck="false" value="<?=htmlspecialchars($name)?>" oninput="checkName(this.value)" title="_(Hidden share names are not allowed)_" pattern="^[^\.].*"><span id="zfs-name" class="orange-text"><i class="fa fa-warning"></i> _(Share name contains invalid characters for ZFS use)_</span>
383+
: <input type="text" id="shareName" name="shareName" maxlength="40" autocomplete="off" spellcheck="false" value="<?=htmlspecialchars($name)?>" oninput="checkName(this.value)" title="_(Hidden share names are not allowed)_" pattern="^[^\.].*"><span id="zfs-name" class="orange-text"><i class="fa fa-warning"></i> _(Share name contains invalid character(s))_</span>
384384

385385
:share_edit_name_help:
386386

@@ -1016,8 +1016,68 @@ function prepareEdit() {
10161016
return false;
10171017
}
10181018

1019+
// Length check
1020+
if (share.length > 40) {
1021+
swal({
1022+
title: "Invalid share name",
1023+
text: "Share name must be 40 characters or less",
1024+
type: 'error',
1025+
html: true,
1026+
confirmButtonText: "Ok"
1027+
});
1028+
return false;
1029+
1030+
}// Must start with a letter
1031+
if (!/^[A-Za-z]/.test(share)) {
1032+
swal({
1033+
title: "Invalid share name",
1034+
text: "Share name must begin with a letter",
1035+
type: 'error',
1036+
html: true,
1037+
confirmButtonText: "Ok"
1038+
});
1039+
return false;
1040+
}
1041+
1042+
// Allowed characters check
1043+
if (!/^[A-Za-z0-9 ._-]+$/.test(share)) {
1044+
swal({
1045+
title: "Invalid share name",
1046+
text: "Share name may only contain letters, digits, spaces, '.', '_', or '-'",
1047+
type: 'error',
1048+
html: true,
1049+
confirmButtonText: "Ok"
1050+
});
1051+
return false;
1052+
}
1053+
1054+
// Must not end with '.' or space
1055+
if (/[. ]$/.test(share)) {
1056+
swal({
1057+
title: "Invalid share name",
1058+
text: "Share name cannot end with '.' or a space",
1059+
type: 'error',
1060+
html: true,
1061+
confirmButtonText: "Ok"
1062+
});
1063+
return false;
1064+
}
1065+
1066+
// Must not contain consecutive spaces
1067+
if (/ +/.test(share)) {
1068+
swal({
1069+
title: "Invalid share name",
1070+
text: "Share name cannot contain consecutive spaces",
1071+
type: 'error',
1072+
html: true,
1073+
confirmButtonText: "Ok"
1074+
});
1075+
return false;
1076+
}
1077+
1078+
// Must not be one of our reserved strings
10191079
reserved = [<?=implode(',',array_map('escapestring',explode(',',$var['reservedNames'])))?>];
1020-
if (reserved.includes(share)) {
1080+
if (reserved.some(name => name.toLowerCase() === share.toLowerCase())) {
10211081
swal({
10221082
title: "Invalid share name",
10231083
text: "Do not use reserved names",
@@ -1027,8 +1087,9 @@ function prepareEdit() {
10271087
return false;
10281088
}
10291089

1090+
// Must not be same name as a pool
10301091
pools = [<?=implode(',',array_map('escapestring',$pools))?>];
1031-
if (pools.includes(share)) {
1092+
if (pools.some(name => name.toLowerCase() === share.toLowerCase())) {
10321093
swal({
10331094
title: "Invalid share name",
10341095
text: "Do not use pool names",
@@ -1038,9 +1099,6 @@ function prepareEdit() {
10381099
return false;
10391100
}
10401101

1041-
/* Clean up the share name. */
1042-
share = safeName(share);
1043-
10441102
/* Update settings. */
10451103
form.shareName.value = share;
10461104
form.shareUseCache.value = z(4);
@@ -1309,38 +1367,12 @@ document.addEventListener('DOMContentLoaded', function() {
13091367
programmaticChange = false;
13101368
});
13111369

1312-
/* Clean up the share name by removing invalid characters. */
1313-
function safeName(name) {
1314-
/* Declare variables at the function scope */
1315-
const validChars = /^[A-Za-z0-9-_.: ]*$/;
1316-
let isValidName, sanitizedString, i;
1317-
1318-
/* Check if the name contains only valid characters */
1319-
isValidName = validChars.test(name);
1320-
1321-
/* If valid, return the name as it is */
1322-
if (isValidName) {
1323-
return name;
1324-
}
1325-
1326-
/* If not valid, sanitize the name by removing invalid characters */
1327-
sanitizedString = '';
1328-
for (i = 0; i < name.length; i++) {
1329-
if (validChars.test(name[i])) {
1330-
sanitizedString += name[i];
1331-
}
1332-
}
1333-
1334-
/* Return the sanitized string */
1335-
return sanitizedString;
1336-
}
1337-
1338-
/* Remove characters not allowed in share name. */
1370+
/* Check for characters not allowed in share name. */
13391371
function checkName(name) {
13401372
/* Declare variables at the function scope */
13411373
var isValidName;
13421374

1343-
isValidName = /^[A-Za-z0-9-_.: ]*$/.test(name);
1375+
isValidName = /^[A-Za-z0-9-_. ]*$/.test(name);
13441376

13451377
if (isValidName) {
13461378
$('#zfs-name').hide();

0 commit comments

Comments
 (0)