Skip to content

Commit c3cd386

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

File tree

2 files changed

+80
-58
lines changed

2 files changed

+80
-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: 77 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 id='customLoginSelectorOption' value='custom'>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,73 @@
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+
var defaultShellSelected = false;
242+
$("#loginSelector option").each(function(i, e) {
243+
if ($(this).val() == ldapLoginShell) {
244+
$(this).attr("selected", true);
245+
defaultShellSelected = true;
246+
}
262247
});
263-
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();
248+
if (!defaultShellSelected) {
249+
$("#customLoginBox").val(ldapLoginShell);
250+
$("#customLoginSelectorOption").attr("selected", true);
270251
}
271252

272-
$("#loginSelector").change(function() {
253+
$("button.btnAddKey").click(function() {
254+
openModal("Add New Key", `${sitePrefix}/panel/modal/new_key.php`);
255+
});
256+
257+
function showOrHideCustomLoginBox() {
273258
var customBox = $("#customLoginBox");
274-
if($(this).val() == "custom") {
259+
if($("#loginSelector").val() == "custom") {
275260
customBox.show();
276261
} else {
277262
customBox.hide();
278263
}
279-
});
264+
}
265+
$("#loginSelector").change(showOrHideCustomLoginBox);
266+
showOrHideCustomLoginBox();
280267

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

286306
<style>

0 commit comments

Comments
 (0)