Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ Document all changes in `CHANGELOG.md`; use descriptive commit messages referenc
## Internationalization (i18n)

- Translatable strings are managed with GNU gettext via `locales/build_gettext.sh`. `locales/po/cacti.pot` is the source template; Weblate owns syncing the per-language `.po`/`.mo` files from it.
- When a pull request adds or changes a string wrapped in `__()`/`__n()`/`__esc()`/`__x()`/`__xn()`/`__gettext()`, run `locales/build_gettext.sh` before pushing and add the resulting change to `locales/po/cacti.pot` only. Do not commit the regenerated per-language `.po`/`.mo` files in the same PR — Weblate takes care of the rest.
- **Never commit the per-language `.po` or compiled `.mo` files** (`locales/po/*.po`, `locales/LC_MESSAGES/*.mo`) in a plugin PR. Weblate is the sole owner of those catalogs, and regenerating them here produces spurious diffs and merge conflicts. `locales/po/cacti.pot` is the ONLY translation artifact a PR may add or modify.
- When a pull request adds or changes a string wrapped in `__()`/`__n()`/`__esc()`/`__x()`/`__xn()`/`__gettext()`, run `locales/build_gettext.sh` before pushing and stage `locales/po/cacti.pot` only. `build_gettext.sh` also rewrites the `.po`/`.mo` files as a side effect; revert those before committing (`git checkout -- locales/po/*.po locales/LC_MESSAGES`), or run only the `xgettext` step that targets `cacti.pot`.

## References

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

--- develop ---

* security: Add a version-safe CSP nonce (`plugin_quicktree_csp_nonce()`) to every inline `<script>` tag so pages stay compatible with Cacti's Content-Security-Policy nonce enforcement, while falling back cleanly on older Cacti releases that lack the `CactiSecureHeaders` class
- issue: Quicktree continually registers a realm
- issue: Fix quicktree_show_tab() building an invalid `?location=tabtab`/`?location=tabconsole` URL, an undefined-variable text-domain typo in the QuickTree page's "no graph specified" message, and a misspelled `qucktree` text domain
- security: Migrate quicktree SQL helpers to prepared statements, normalize location handling, and ensure redirects terminate execution
Expand Down
28 changes: 14 additions & 14 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Cacti \n"
"Report-Msgid-Bugs-To: developers@cacti.net\n"
"POT-Creation-Date: 2026-09-25 02:59-0400\n"
"POT-Creation-Date: 2026-09-27 11:16-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Howard Jones <howie@thingy.com>>\n"
"Language-Team: Howard Jones <howie@thingy.com>\n"
Expand Down Expand Up @@ -71,8 +71,8 @@ msgstr ""
msgid "Branch Name"
msgstr ""

#: quicktree.php:126 quicktree.php:178 quicktree.php:335 setup.php:269
#: setup.php:276 setup.php:283 setup.php:290 setup.php:297 setup.php:304
#: quicktree.php:126 quicktree.php:178 quicktree.php:335 setup.php:285
#: setup.php:292 setup.php:299 setup.php:306 setup.php:313 setup.php:320
msgid "QuickTree"
msgstr ""

Expand Down Expand Up @@ -173,46 +173,46 @@ msgstr ""
msgid "No Graphs Added Yet"
msgstr ""

#: setup.php:45
#: setup.php:61
msgid "QuickTree Management"
msgstr ""

#: setup.php:90
#: setup.php:106
msgid "Misc"
msgstr ""

#: setup.php:94 setup.php:175
#: setup.php:110 setup.php:191
msgid "Quicktree"
msgstr ""

#: setup.php:98
#: setup.php:114
msgid "Page Style"
msgstr ""

#: setup.php:99
#: setup.php:115
msgid "Where to display the QuickTree page"
msgstr ""

#: setup.php:102
#: setup.php:118
msgid "Tab"
msgstr ""

#: setup.php:103
#: setup.php:119
msgid "Console Menu"
msgstr ""

#: setup.php:104
#: setup.php:120
msgid "Both Tab and Console Menu"
msgstr ""

#: setup.php:203
#: setup.php:219
msgid "Add this graph to QuickTree"
msgstr ""

#: setup.php:226
#: setup.php:242
msgid "Management"
msgstr ""

#: setup.php:226
#: setup.php:242
msgid "QuickTree Trees"
msgstr ""
2 changes: 1 addition & 1 deletion quicktree.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
}

?>
<script type="text/javascript"> $(function() { document.location = "<?php print $url; ?>"; }); </script>
<script type="text/javascript" <?php print plugin_quicktree_csp_nonce(); ?>> $(function() { document.location = "<?php print $url; ?>"; }); </script>
<?php

exit;
Expand Down
20 changes: 18 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
+-------------------------------------------------------------------------+
*/

/**
* Return the CSP nonce attribute for inline <script> tags, safely across
* Cacti versions. Newer Cacti releases enforce a Content-Security-Policy that
* requires a per-request nonce on parser-inserted scripts; older releases lack
* the CactiSecureHeaders class, so this returns an empty string there.
*
* @return string The nonce attribute when supported, otherwise empty string.
*/
function plugin_quicktree_csp_nonce(): string {
Comment thread
TheWitness marked this conversation as resolved.
if (class_exists('CactiSecureHeaders')) {
return CactiSecureHeaders::getNonceAttribute();
}

return '';
}

/**
* Installs the quicktree plugin: registers its Cacti hooks (top_header_tabs,
* top_graph_header_tabs, config_arrays, config_settings,
Expand Down Expand Up @@ -243,10 +259,10 @@ function quicktree_page_head(): void {

$page = get_current_page();

print '<script type="text/javascript" src="' . $config['url_path'] . 'plugins/quicktree/js/quicktree.js"></script>';
print get_md5_include_js('plugins/quicktree/js/quicktree.js');

if (strstr($page, 'quicktree.php') !== false) {
print '<link rel="stylesheet" href="' . $config['url_path'] . 'plugins/quicktree/css/quicktree.css"></link>';
print get_md5_include_css('plugins/quicktree/css/quicktree.css');
}
}

Expand Down
102 changes: 102 additions & 0 deletions tests/Unit/CspNonceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for plugin_quicktree_csp_nonce().
*
* The helper has two branches that hinge on whether Cacti's global
* CactiSecureHeaders class is present. A single PHP process can only ever
* see the class as present or absent, never both, so the class-present
* branch is exercised in an isolated child process that defines a
* controlled double, rather than by mutating global state in this process
* or asserting on the source text.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

test('csp nonce returns an empty string when CactiSecureHeaders is unavailable', function () {
if (class_exists('CactiSecureHeaders')) {
$this->markTestSkipped('CactiSecureHeaders is present in this runtime; the delegation path is covered by the isolated-process test.');
}

expect(plugin_quicktree_csp_nonce())->toBe('');
});

test('csp nonce helper is declared to return a string', function () {
$ref = new ReflectionFunction('plugin_quicktree_csp_nonce');

expect((string) $ref->getReturnType())->toBe('string');
});

test('csp nonce delegates to CactiSecureHeaders when the class is available', function () {
$wrapper = realpath(__DIR__ . '/../../setup.php');
expect($wrapper)->not->toBeFalse();

// The wrapper's file may not be loadable in isolation (some plugins
// include translised arrays at file scope), so hand the child the unit
// bootstrap too; it defines the Cacti stubs the file needs at load time.
$bootstrap = realpath(__DIR__ . '/../bootstrap-unit.php');

// Run in a clean child process so the double never leaks into the rest
// of the suite and the delegation branch is genuinely invoked.
$script = <<<'CHILD'
<?php
error_reporting(0);
ini_set('display_errors', '0');

class CactiSecureHeaders {
public static function getNonceAttribute(): string {
return ' nonce="pest-controlled-double"';
}
}

if (isset($argv[2]) && $argv[2] !== '' && is_file($argv[2])) {
try { require $argv[2]; } catch (\Throwable $e) { /* no Cacti host locally */ }
}

require $argv[1];

echo "<<<" . plugin_quicktree_csp_nonce() . ">>>";
CHILD;

$tmp = tempnam(sys_get_temp_dir(), 'csp_');
expect($tmp)->not->toBeFalse();

try {
file_put_contents($tmp, $script);

$command = escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($tmp) . ' ' . escapeshellarg($wrapper) . ' ' . escapeshellarg($bootstrap === false ? '' : $bootstrap);
$output = shell_exec($command);

expect($output)->not->toBeNull();

preg_match('/<<<(.*)>>>/s', (string) $output, $matches);
expect($matches)->toHaveKey(1);
expect($matches[1])->toBe(' nonce="pest-controlled-double"');
} finally {
@unlink($tmp);
}
});
Loading