Skip to content

Commit 75c7d6c

Browse files
committed
add redirect after log in to support deep links
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
1 parent d434cc5 commit 75c7d6c

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎docs/img/device_details.png‎

31.1 KB
Loading

‎front/index.php‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@
1010
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/templates/language/lang.php';
1111
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/templates/security.php';
1212

13+
// capture the redirect to after log in query string if available
14+
$redirectTo = 'devices.php'; // Default destination
15+
if (!empty($_GET['next'])) {
16+
$decoded = base64_decode($_GET['next']);
17+
// Validate that it's a local path to prevent Open Redirect vulnerabilities
18+
if (strpos($decoded, '/') === 0 && strpos($decoded, '//') !== 0) {
19+
$redirectTo = $decoded;
20+
}
21+
}
22+
1323
$CookieSaveLoginName = 'NetAlertX_SaveLogin';
1424

1525
if ($nax_WebProtection != 'true')
1626
{
17-
header('Location: devices.php');
27+
if (!empty($_POST['url_hash'])) {
28+
$redirectTo .= $_POST['url_hash'];
29+
}
30+
header("Location: $redirectTo");
1831
$_SESSION["login"] = 1;
1932
exit;
2033
}
@@ -31,17 +44,25 @@
3144
// Password without Cookie check -> pass and set initial cookie
3245
if (isset ($_POST["loginpassword"]) && $nax_Password === hash('sha256',$_POST["loginpassword"]))
3346
{
34-
header('Location: devices.php');
47+
if (!empty($_POST['url_hash'])) {
48+
$redirectTo .= $_POST['url_hash'];
49+
}
50+
header("Location: $redirectTo");
3551
$_SESSION["login"] = 1;
3652
if (isset($_POST['PWRemember'])) {setcookie($CookieSaveLoginName, hash('sha256',$_POST["loginpassword"]), time()+604800);}
53+
exit;
3754
}
3855

3956
// active Session or valid cookie (cookie not extends)
4057
if (( isset ($_SESSION["login"]) && ($_SESSION["login"] == 1)) || (isset ($_COOKIE[$CookieSaveLoginName]) && $nax_Password === $_COOKIE[$CookieSaveLoginName]))
4158
{
42-
header('Location: devices.php');
59+
if (!empty($_POST['url_hash'])) {
60+
$redirectTo .= $_POST['url_hash'];
61+
}
62+
header("Location: $redirectTo");
4363
$_SESSION["login"] = 1;
4464
if (isset($_POST['PWRemember'])) {setcookie($CookieSaveLoginName, hash('sha256',$_POST["loginpassword"]), time()+604800);}
65+
exit;
4566
}
4667

4768
$login_headline = lang('Login_Toggle_Info_headline');
@@ -53,15 +74,15 @@
5374
// no active session, cookie not checked
5475
if (isset ($_SESSION["login"]) == FALSE || $_SESSION["login"] != 1)
5576
{
56-
if ($nax_Password === '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92')
77+
if ($nax_Password === '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92')
5778
{
5879
$login_info = lang('Login_Default_PWD');
5980
$login_mode = 'danger';
6081
$login_display_mode = 'display: block;';
6182
$login_headline = lang('Login_Toggle_Alert_headline');
6283
$login_icon = 'fa-ban';
63-
}
64-
else
84+
}
85+
else
6586
{
6687
$login_mode = 'info';
6788
$login_display_mode = 'display: none;';
@@ -109,8 +130,9 @@
109130
<!-- /.login-logo -->
110131
<div class="login-box-body">
111132
<p class="login-box-msg"><?= lang('Login_Box');?></p>
112-
<form action="index.php" method="post">
133+
<form action="index.php<?php echo !empty($_GET['next']) ? '?next=' . htmlspecialchars($_GET['next']) : ''; ?>" method="post">
113134
<div class="form-group has-feedback">
135+
<input type="hidden" name="url_hash" id="url_hash">
114136
<input type="password" class="form-control" placeholder="<?= lang('Login_Psw-box');?>" name="loginpassword">
115137
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
116138
</div>
@@ -119,7 +141,7 @@
119141
<div class="checkbox icheck">
120142
<label>
121143
<input type="checkbox" name="PWRemember">
122-
<div style="margin-left: 10px; display: inline-block; vertical-align: top;">
144+
<div style="margin-left: 10px; display: inline-block; vertical-align: top;">
123145
<?= lang('Login_Remember');?><br><span style="font-size: smaller"><?= lang('Login_Remember_small');?></span>
124146
</div>
125147
</label>
@@ -129,7 +151,7 @@
129151
<div class="col-xs-4" style="padding-top: 10px;">
130152
<button type="submit" class="btn btn-primary btn-block btn-flat"><?= lang('Login_Submit');?></button>
131153
</div>
132-
<!-- /.col -->
154+
<!-- /.col -->
133155
</div>
134156
</form>
135157

@@ -159,6 +181,9 @@
159181
<!-- iCheck -->
160182
<script src="lib/iCheck/icheck.min.js"></script>
161183
<script>
184+
if (window.location.hash) {
185+
document.getElementById('url_hash').value = window.location.hash;
186+
}
162187
$(function () {
163188
$('input').iCheck({
164189
checkboxClass: 'icheckbox_square-blue',
@@ -174,7 +199,7 @@ function Passwordhinfo() {
174199
} else {
175200
x.style.display = "none";
176201
}
177-
}
202+
}
178203

179204
</script>
180205
</body>

‎front/php/templates/security.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function redirect($url) {
8686
// Logged in or stay on this page if we are on the index.php already
8787
} else {
8888
// We need to redirect
89-
redirect('/index.php');
89+
$returnUrl = base64_encode($_SERVER['REQUEST_URI']);
90+
redirect("/index.php?next=" . $returnUrl);
9091
exit; // exit is needed to prevent authentication bypass
9192
}
9293
}

0 commit comments

Comments
 (0)