-
-
Notifications
You must be signed in to change notification settings - Fork 365
Next release - deep link support after log in and refactor of index.php #1500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d434cc5
75c7d6c
a088f45
3d3abe7
3036cd0
249d12d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,76 +3,185 @@ | |
|
|
||
| <?php | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // check if authenticated | ||
| // Be CAREFUL WHEN INCLUDING NEW PHP FILES | ||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/php/server/db.php'; | ||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/php/templates/language/lang.php'; | ||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/php/templates/security.php'; | ||
|
|
||
| $CookieSaveLoginName = 'NetAlertX_SaveLogin'; | ||
|
|
||
| if ($nax_WebProtection != 'true') | ||
| { | ||
| header('Location: devices.php'); | ||
| $_SESSION["login"] = 1; | ||
| require_once $_SERVER['DOCUMENT_ROOT'].'/php/server/db.php'; | ||
| require_once $_SERVER['DOCUMENT_ROOT'].'/php/templates/language/lang.php'; | ||
| require_once $_SERVER['DOCUMENT_ROOT'].'/php/templates/security.php'; | ||
|
|
||
| // if (session_status() === PHP_SESSION_NONE) { | ||
| // session_start(); | ||
| // } | ||
|
|
||
| session_start(); | ||
|
|
||
| const COOKIE_NAME = 'NetAlertX_SaveLogin'; | ||
| const DEFAULT_REDIRECT = '/devices.php'; | ||
|
|
||
| /* ===================================================== | ||
| Helper Functions | ||
| ===================================================== */ | ||
|
|
||
| function safe_redirect(string $path): void { | ||
| header("Location: {$path}", true, 302); | ||
| exit; | ||
| } | ||
|
|
||
| // Logout | ||
| if (isset ($_GET["action"]) && $_GET["action"] == 'logout') | ||
| { | ||
| setcookie($CookieSaveLoginName, '', time()+1); // reset cookie | ||
| $_SESSION["login"] = 0; | ||
| header('Location: index.php'); | ||
| exit; | ||
| function validate_local_path(?string $encoded): string { | ||
| if (!$encoded) return DEFAULT_REDIRECT; | ||
|
|
||
| $decoded = base64_decode($encoded, true); | ||
| if ($decoded === false) { | ||
| return DEFAULT_REDIRECT; | ||
| } | ||
|
|
||
| // strict local path check (allow safe query strings + fragments) | ||
| // Using ~ as the delimiter instead of # | ||
| if (!preg_match('~^(?!//)(?!.*://)/[a-zA-Z0-9_\-./?=&:%#]*$~', $decoded)) { | ||
| return DEFAULT_REDIRECT; | ||
| } | ||
|
|
||
| return $decoded; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function append_hash(string $url): string { | ||
| if (!empty($_POST['url_hash'])) { | ||
| return $url . preg_replace('/[^#a-zA-Z0-9_\-]/', '', $_POST['url_hash']); | ||
| } | ||
| return $url; | ||
| } | ||
|
Comment on lines
45
to
50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The JS (line 257-259) always provides Proposed fix function append_hash(string $url): string {
if (!empty($_POST['url_hash'])) {
- return $url . preg_replace('/[^#a-zA-Z0-9_\-]/', '', $_POST['url_hash']);
+ $sanitized = preg_replace('/[^#a-zA-Z0-9_\-]/', '', $_POST['url_hash']);
+ if (str_starts_with($sanitized, '#')) {
+ return $url . $sanitized;
+ }
}
return $url;
}🤖 Prompt for AI Agents |
||
|
|
||
| function is_authenticated(): bool { | ||
| return isset($_SESSION['login']) && $_SESSION['login'] === 1; | ||
| } | ||
|
|
||
| function login_user(): void { | ||
| $_SESSION['login'] = 1; | ||
| session_regenerate_id(true); | ||
| } | ||
|
|
||
| function is_https_request(): bool { | ||
|
|
||
| // Direct HTTPS detection | ||
| if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { | ||
| return true; | ||
| } | ||
|
|
||
| // Standard port check | ||
| if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) { | ||
| return true; | ||
| } | ||
|
|
||
| // Trusted proxy headers (only valid if behind a trusted reverse proxy) | ||
| if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && | ||
| strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') { | ||
| return true; | ||
| } | ||
|
|
||
| if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && | ||
| strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) === 'on') { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
||
| function logout_user(): void { | ||
| $_SESSION = []; | ||
| session_destroy(); | ||
|
|
||
| setcookie(COOKIE_NAME,'',[ | ||
| 'expires'=>time()-3600, | ||
| 'path'=>'/', | ||
| 'secure'=>is_https_request(), | ||
| 'httponly'=>true, | ||
| 'samesite'=>'Strict' | ||
| ]); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* ===================================================== | ||
| Redirect Handling | ||
| ===================================================== */ | ||
|
|
||
| $redirectTo = validate_local_path($_GET['next'] ?? null); | ||
|
|
||
| /* ===================================================== | ||
| Web Protection Disabled | ||
| ===================================================== */ | ||
|
|
||
| if ($nax_WebProtection !== 'true') { | ||
| if (!is_authenticated()) { | ||
| login_user(); | ||
| } | ||
| safe_redirect(append_hash($redirectTo)); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Password without Cookie check -> pass and set initial cookie | ||
| if (isset ($_POST["loginpassword"]) && $nax_Password === hash('sha256',$_POST["loginpassword"])) | ||
| { | ||
| header('Location: devices.php'); | ||
| $_SESSION["login"] = 1; | ||
| if (isset($_POST['PWRemember'])) {setcookie($CookieSaveLoginName, hash('sha256',$_POST["loginpassword"]), time()+604800);} | ||
| /* ===================================================== | ||
| Login Attempt | ||
| ===================================================== */ | ||
|
|
||
| if (!empty($_POST['loginpassword'])) { | ||
|
|
||
| $incomingHash = hash('sha256', $_POST['loginpassword']); | ||
|
|
||
| if (hash_equals($nax_Password, $incomingHash)) { | ||
|
|
||
| login_user(); | ||
|
|
||
| if (!empty($_POST['PWRemember'])) { | ||
| $token = bin2hex(random_bytes(32)); | ||
|
|
||
| $_SESSION['remember_token'] = hash('sha256',$token); | ||
|
|
||
| setcookie(COOKIE_NAME,$token,[ | ||
| 'expires'=>time()+604800, | ||
| 'path'=>'/', | ||
| 'secure'=>is_https_request(), | ||
| 'httponly'=>true, | ||
| 'samesite'=>'Strict' | ||
| ]); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| safe_redirect(append_hash($redirectTo)); | ||
| } | ||
| } | ||
|
|
||
| // active Session or valid cookie (cookie not extends) | ||
| if (( isset ($_SESSION["login"]) && ($_SESSION["login"] == 1)) || (isset ($_COOKIE[$CookieSaveLoginName]) && $nax_Password === $_COOKIE[$CookieSaveLoginName])) | ||
| { | ||
| header('Location: devices.php'); | ||
| $_SESSION["login"] = 1; | ||
| if (isset($_POST['PWRemember'])) {setcookie($CookieSaveLoginName, hash('sha256',$_POST["loginpassword"]), time()+604800);} | ||
| /* ===================================================== | ||
| Remember Me Validation | ||
| ===================================================== */ | ||
|
|
||
| if (!is_authenticated() && !empty($_COOKIE[COOKIE_NAME]) && !empty($_SESSION['remember_token'])) { | ||
|
|
||
| if (hash_equals($_SESSION['remember_token'], hash('sha256',$_COOKIE[COOKIE_NAME]))) { | ||
| login_user(); | ||
| safe_redirect(append_hash($redirectTo)); | ||
| } | ||
| } | ||
jokob-sk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* ===================================================== | ||
| Already Logged In | ||
| ===================================================== */ | ||
|
|
||
| if (is_authenticated()) { | ||
| safe_redirect(append_hash($redirectTo)); | ||
| } | ||
|
|
||
| /* ===================================================== | ||
| Login UI Variables | ||
| ===================================================== */ | ||
|
|
||
| $login_headline = lang('Login_Toggle_Info_headline'); | ||
| $login_info = lang('Login_Info'); | ||
| $login_mode = 'danger'; | ||
| $login_display_mode = 'display: block;'; | ||
| $login_icon = 'fa-info'; | ||
|
|
||
| // no active session, cookie not checked | ||
| if (isset ($_SESSION["login"]) == FALSE || $_SESSION["login"] != 1) | ||
| { | ||
| if ($nax_Password === '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92') | ||
| { | ||
| $login_info = lang('Login_Info'); | ||
| $login_mode = 'info'; | ||
| $login_display_mode = 'display:none;'; | ||
| $login_icon = 'fa-info'; | ||
|
|
||
| if ($nax_Password === '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92') { | ||
| $login_info = lang('Login_Default_PWD'); | ||
| $login_mode = 'danger'; | ||
| $login_display_mode = 'display: block;'; | ||
| $login_display_mode = 'display:block;'; | ||
| $login_headline = lang('Login_Toggle_Alert_headline'); | ||
| $login_icon = 'fa-ban'; | ||
| } | ||
| else | ||
| { | ||
| $login_mode = 'info'; | ||
| $login_display_mode = 'display: none;'; | ||
| $login_headline = lang('Login_Toggle_Info_headline'); | ||
| $login_icon = 'fa-info'; | ||
| } | ||
| } | ||
|
|
||
| // ################################################## | ||
| // ## Login Processing end | ||
| // ################################################## | ||
| ?> | ||
|
|
||
| <!DOCTYPE html> | ||
|
|
@@ -109,8 +218,13 @@ | |
| <!-- /.login-logo --> | ||
| <div class="login-box-body"> | ||
| <p class="login-box-msg"><?= lang('Login_Box');?></p> | ||
| <form action="index.php" method="post"> | ||
| <form action="index.php<?php | ||
| echo !empty($_GET['next']) | ||
| ? '?next=' . htmlspecialchars($_GET['next'], ENT_QUOTES, 'UTF-8') | ||
| : ''; | ||
| ?>" method="post"> | ||
| <div class="form-group has-feedback"> | ||
| <input type="hidden" name="url_hash" id="url_hash"> | ||
| <input type="password" class="form-control" placeholder="<?= lang('Login_Psw-box');?>" name="loginpassword"> | ||
| <span class="glyphicon glyphicon-lock form-control-feedback"></span> | ||
| </div> | ||
|
|
@@ -119,7 +233,7 @@ | |
| <div class="checkbox icheck"> | ||
| <label> | ||
| <input type="checkbox" name="PWRemember"> | ||
| <div style="margin-left: 10px; display: inline-block; vertical-align: top;"> | ||
| <div style="margin-left: 10px; display: inline-block; vertical-align: top;"> | ||
| <?= lang('Login_Remember');?><br><span style="font-size: smaller"><?= lang('Login_Remember_small');?></span> | ||
| </div> | ||
| </label> | ||
|
|
@@ -129,7 +243,7 @@ | |
| <div class="col-xs-4" style="padding-top: 10px;"> | ||
| <button type="submit" class="btn btn-primary btn-block btn-flat"><?= lang('Login_Submit');?></button> | ||
| </div> | ||
| <!-- /.col --> | ||
| <!-- /.col --> | ||
| </div> | ||
| </form> | ||
|
|
||
|
|
@@ -159,6 +273,9 @@ | |
| <!-- iCheck --> | ||
| <script src="lib/iCheck/icheck.min.js"></script> | ||
| <script> | ||
| if (window.location.hash) { | ||
| document.getElementById('url_hash').value = window.location.hash; | ||
| } | ||
| $(function () { | ||
| $('input').iCheck({ | ||
| checkboxClass: 'icheckbox_square-blue', | ||
|
|
@@ -174,7 +291,7 @@ function Passwordhinfo() { | |
| } else { | ||
| x.style.display = "none"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| </script> | ||
| </body> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double
session_start()will trigger a PHP warning.security.php(required on line 9) already callssession_start()at its line 49. Calling it again here on line 11 produces a PHP warning in 7.2+. Guard it or remove the duplicate.Proposed fix
🤖 Prompt for AI Agents