Skip to content

Commit 3036cd0

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

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

front/index.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
require_once $_SERVER['DOCUMENT_ROOT'].'/php/templates/language/lang.php';
88
require_once $_SERVER['DOCUMENT_ROOT'].'/php/templates/security.php';
99

10+
// if (session_status() === PHP_SESSION_NONE) {
11+
// session_start();
12+
// }
13+
1014
session_start();
1115

1216
const COOKIE_NAME = 'NetAlertX_SaveLogin';
@@ -25,10 +29,13 @@ function validate_local_path(?string $encoded): string {
2529
if (!$encoded) return DEFAULT_REDIRECT;
2630

2731
$decoded = base64_decode($encoded, true);
28-
if ($decoded === false) return DEFAULT_REDIRECT;
32+
if ($decoded === false) {
33+
return DEFAULT_REDIRECT;
34+
}
2935

30-
// strict local path check
31-
if (!preg_match('#^/[a-zA-Z0-9_\-/\.]*$#', $decoded)) {
36+
// strict local path check (allow safe query strings + fragments)
37+
// Using ~ as the delimiter instead of #
38+
if (!preg_match('~^(?!//)(?!.*://)/[a-zA-Z0-9_\-./?=&:%#]*$~', $decoded)) {
3239
return DEFAULT_REDIRECT;
3340
}
3441

@@ -74,7 +81,9 @@ function logout_user(): void {
7481
===================================================== */
7582

7683
if ($nax_WebProtection !== 'true') {
77-
login_user();
84+
if (!is_authenticated()) {
85+
login_user();
86+
}
7887
safe_redirect(append_hash($redirectTo));
7988
}
8089

@@ -190,7 +199,11 @@ function logout_user(): void {
190199
<!-- /.login-logo -->
191200
<div class="login-box-body">
192201
<p class="login-box-msg"><?= lang('Login_Box');?></p>
193-
<form action="index.php<?php echo !empty($_GET['next']) ? '?next=' . htmlspecialchars($_GET['next']) : ''; ?>" method="post">
202+
<form action="index.php<?php
203+
echo !empty($_GET['next'])
204+
? '?next=' . htmlspecialchars($_GET['next'], ENT_QUOTES, 'UTF-8')
205+
: '';
206+
?>" method="post">
194207
<div class="form-group has-feedback">
195208
<input type="hidden" name="url_hash" id="url_hash">
196209
<input type="password" class="form-control" placeholder="<?= lang('Login_Psw-box');?>" name="loginpassword">

front/php/templates/security.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
// Start session if not already started
4+
if (session_status() == PHP_SESSION_NONE) {
5+
session_start();
6+
}
7+
38
// Constants
49
$configFolderPath = rtrim(getenv('NETALERTX_CONFIG') ?: '/data/config', '/');
510
$legacyConfigPath = $_SERVER['DOCUMENT_ROOT'] . "/../config/app.conf";
@@ -45,10 +50,6 @@ function redirect($url) {
4550
$authHeader = apache_request_headers()['Authorization'] ?? '';
4651
$sessionLogin = isset($_SESSION['login']) ? $_SESSION['login'] : 0;
4752

48-
// Start session if not already started
49-
if (session_status() == PHP_SESSION_NONE) {
50-
session_start();
51-
}
5253

5354
// Handle logout
5455
if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'logout') {
@@ -86,7 +87,7 @@ function redirect($url) {
8687
// Logged in or stay on this page if we are on the index.php already
8788
} else {
8889
// We need to redirect
89-
$returnUrl = base64_encode($_SERVER['REQUEST_URI']);
90+
$returnUrl = rawurlencode(base64_encode($_SERVER['REQUEST_URI']));
9091
redirect("/index.php?next=" . $returnUrl);
9192
exit; // exit is needed to prevent authentication bypass
9293
}

0 commit comments

Comments
 (0)