Files
cacti/tests/Unit/Security
b23ad25036 security(script-server): add path containment + preserve Windows script paths (forum t=64910) (#8654)
* security(script-server): add path containment and preserve Windows script paths

Develop's script server loaded the include file after only a file_exists()
check and dispatched the named function with no containment, and
get_script_query_path() returned the raw script path. This forward-ports the
script-server hardening from 1.2.x and adapts it to the revised,
non-mangling path method so it works on Windows.

script_server.php:
  - Build the allowed script roots from CACTI_PATH_BASE and CACTI_PATH_SCRIPTS.
  - Validate realpath($include_file) is within the allowed roots before
    require_once().
  - After loading, reject PHP internal functions and any function whose
    resolved source file lives outside the allowed roots (ReflectionFunction).
  - Add the script_server_path_is_allowed() containment helper.

lib/data_query.php (get_script_query_path):
  - Reject '..' path traversal.
  - Sanitize the script path without corrupting Windows separators: strip
    shell metacharacters on Windows while preserving the backslash separator
    (cacti_escapeshellcmd() would turn 'C:\path\script.php' into
    'C: path script.php', which the containment check then rejects as 'C:');
    non-Windows platforms use escapeshellcmd() on their forward-slash paths.

Adds tests/Unit/Security/PathTraversal/ScriptServerContainmentHardeningTest.php
covering the containment guard and the Windows-safe sanitization, with both
source-scan and runtime suites.

Companion fixes: #8652 (1.2.x) and #8653 (1.2.31).
Forum: https://forums.cacti.net/viewtopic.php?t=64910

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(script-server): narrow realpath() result to satisfy PHPStan level 6

realpath() returns string|false; assigning it to \ propagated
string|false across the command loop, so basename(), realpath(),
file_exists(), cacti_strtolower() and script_server_log_request() were all
flagged as receiving string|false. Reject a false realpath() explicitly in
the containment guard (behaviour-preserving: script_server_path_is_allowed()
already returns false for a false path), which narrows \ to
string before it is assigned back to \.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: TheWitness <copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-09 10:03:55 -05:00
..