Skip to content

Commit ff26061

Browse files
committed
check for special characters in login shell
1 parent 5a92fdb commit ff26061

File tree

2 files changed

+74
-58
lines changed

2 files changed

+74
-58
lines changed

resources/lib/UnityUser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,10 @@ public function getSSHKeys($ignorecache = false)
446446
*/
447447
public function setLoginShell($shell, $operator = null, $send_mail = true)
448448
{
449-
// FIXME throw error if shell is not ascii
450449
// ldap schema syntax is "IA5 String (1.3.6.1.4.1.1466.115.121.1.26)"
450+
if (!mb_check_encoding($shell, 'ASCII')) {
451+
throw new Exception("non ascii characters are not allowed in a login shell!");
452+
}
451453
$ldapUser = $this->getLDAPUser();
452454
if ($ldapUser->exists()) {
453455
$ldapUser->setAttribute("loginshell", $shell);

webroot/panel/account.php

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -186,49 +186,28 @@
186186
<hr>
187187

188188
<form action="" method="POST">
189-
190-
<input type="hidden" name="form_type" value="loginshell">
191-
192-
<select id="loginSelector" name= "shellSelect">
193-
194-
<option value="" disabled hidden>Select Login Shell...</option>
195-
196-
<?php
197-
$cur_shell = $USER->getLoginShell();
198-
$found_selector = false;
199-
foreach ($CONFIG["loginshell"]["shell"] as $shell) {
200-
if ($cur_shell == $shell) {
201-
echo "<option selected>$shell</option>";
202-
$found_selector = true;
203-
} else {
204-
echo "<option>$shell</option>";
205-
}
206-
}
207-
208-
if ($found_selector) {
209-
echo "<option value='custom'>Custom</option>";
210-
} else {
211-
echo "<option value='custom' selected>Custom</option>";
212-
}
213-
?>
214-
</select>
215-
216-
<?php
217-
218-
if ($found_selector) {
219-
echo "<input id='customLoginBox' type='text'
220-
placeholder='Enter login shell path (ie. /bin/bash)' name='shell'>";
221-
} else {
222-
echo "<input id='customLoginBox' type='text'
223-
placeholder='Enter login shell path (ie. /bin/bash)' name='shell' value='$cur_shell'>";
224-
}
225-
226-
?>
227-
<br>
228-
<input type='submit' value='Set Login Shell'>
229-
189+
<input type="hidden" name="form_type" value="loginshell">
190+
<select id="loginSelector" name= "shellSelect">
191+
<?php
192+
foreach ($CONFIG["loginshell"]["shell"] as $shell) {
193+
echo "<option>$shell</option>";
194+
}
195+
echo "<option>Custom</option>";
196+
?>
197+
</select>
198+
<?php
199+
echo "
200+
<input
201+
id='customLoginBox'
202+
type='text'
203+
placeholder='Enter login shell path (ie. /bin/bash)'
204+
name='shell'
205+
>
206+
";
207+
?>
208+
<br>
209+
<input id='submitLoginShell' type='submit' value='Set Login Shell'>
230210
</form>
231-
232211
<hr>
233212

234213
<h5>Account Deletion</h5>
@@ -255,32 +234,67 @@
255234

256235
<hr>
257236

258-
259237
<script>
260-
$("button.btnAddKey").click(function() {
261-
openModal("Add New Key", "<?php echo $CONFIG["site"]["prefix"]; ?>/panel/modal/new_key.php");
238+
const sitePrefix = '<?php echo $CONFIG["site"]["prefix"]; ?>';
239+
const ldapLoginShell = '<?php echo $USER->getLoginShell(); ?>';
240+
241+
$("#loginSelector option").each(function(i, e) {
242+
if ($(this).val() == ldapLoginShell) {
243+
$(this).attr("selected", true);
244+
}
262245
});
263246

264-
var customLoginBox = $("#customLoginBox");
265-
if (customLoginBox.val() == "") {
266-
// login box is empty, so we hide it by default
267-
// if the login box had a value, that means it would be a custom shell
268-
// and should not hide by default
269-
customLoginBox.hide();
270-
}
247+
$("button.btnAddKey").click(function() {
248+
openModal("Add New Key", `${sitePrefix}/panel/modal/new_key.php`);
249+
});
271250

272-
$("#loginSelector").change(function() {
251+
function showOrHideCustomLoginBox() {
273252
var customBox = $("#customLoginBox");
274-
if($(this).val() == "custom") {
253+
if($("#loginSelector").val() == "Custom") {
275254
customBox.show();
276255
} else {
277256
customBox.hide();
278257
}
279-
});
258+
}
259+
$("#loginSelector").change(showOrHideCustomLoginBox);
260+
showOrHideCustomLoginBox();
280261

281-
if ($("#loginSelector").val() == "custom") {
282-
$("#customLoginBox").show();
262+
function getNewLoginShell() {
263+
var loginSelectorVal = $("#loginSelector").val();
264+
if (loginSelectorVal != "Custom") {
265+
return loginSelectorVal;
266+
}
267+
return $("#customLoginBox").val();
268+
}
269+
function isLoginShellValid(x) {
270+
if (x.trim().length === 0) {
271+
return false;
272+
}
273+
// only ascii characters allowed
274+
if (!(/^[\x00-\x7F]*$/.test(x))) {
275+
return false;
276+
}
277+
return true;
278+
}
279+
function enableOrDisableSubmitLoginShell() {
280+
var submitLoginShell = $("#submitLoginShell");
281+
var newLoginShell = getNewLoginShell();
282+
if (!isLoginShellValid(newLoginShell)) {
283+
submitLoginShell.attr("disabled", true);
284+
$("#customLoginBox").css("box-shadow", "0 0 0 0.3rem rgba(220, 53, 69, 0.25)");
285+
return;
286+
} else {
287+
$("#customLoginBox").css("box-shadow", "none");
288+
}
289+
if (newLoginShell == ldapLoginShell) {
290+
submitLoginShell.attr("disabled", true);
291+
return;
292+
}
293+
submitLoginShell.attr("disabled", false);
283294
}
295+
$("#customLoginBox").on("input", enableOrDisableSubmitLoginShell);
296+
$("#loginSelector").change(enableOrDisableSubmitLoginShell);
297+
enableOrDisableSubmitLoginShell()
284298
</script>
285299

286300
<style>

0 commit comments

Comments
 (0)