Skip to content

security(csp): version-safe inline-script nonce wrapper + strengthen PO/MO instructions - #249

Open
TheWitness wants to merge 1 commit into
developfrom
feature/csp-nonce-inline-js
Open

TheWitness wants to merge 1 commit into
developfrom
feature/csp-nonce-inline-js

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Summary

Part of the fleet-wide CSP-nonce rollout (mirrors the pilot Cacti/plugin_mactrack#374).

  1. Version-safe CSP nonce on inline JavaScript. Adds a wrapper
    plugin_<name>_csp_nonce() and applies it to every inline <script> block, so
    pages stay compatible with Cacti's Content-Security-Policy nonce enforcement.
  2. Strengthened i18n instruction in .github/copilot-instructions.md — the
    .po/.mo files are never committed (Weblate owns them); only cacti.pot.

Wrapper (cross-version safe)

function plugin_<name>_csp_nonce(): string {
	if (class_exists('CactiSecureHeaders')) {
		return CactiSecureHeaders::getNonceAttribute();
	}

	return '';
}

Emits the per-request nonce="..." on Cacti releases that ship CactiSecureHeaders
(present on both develop and 1.2.x), and '' on older releases — so the tag
stays valid either way. The wrapper lives in the plugin's functions library where one
exists, otherwise in setup.php (loaded on every Cacti page by the plugin loader).

Also

  • tests/Unit/CspNonceTest.php covers the empty-string fallback, the string
    return type, and delegation to CactiSecureHeaders.
  • Where the plugin emits its own external <script src> / CSS <link>, those go
    through Cacti's get_md5_include_js() / get_md5_include_css() helpers (automatic
    nonce + md5 cache-buster).
  • locales/po/cacti.pot regenerated for shifted source line references; the
    per-language .po/.mo catalogs are intentionally not committed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Add coverage for the class-present nonce delegation path.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

Adds version-safe CSP nonce support for inline/external scripts and strengthens translation-catalog guidance.

Changes:

  • Adds nonce helpers and applies them to inline scripts.
  • Uses Cacti asset helpers for external JavaScript.
  • Updates tests, changelog, POT references, and i18n instructions.
File Description
tests/​Unit/​CspNonceTest.php Tests CSP nonce helper behavior.
tests/​bootstrap-unit.php Adds asset-helper test stubs.
setup.php Adds nonce helper and secures inline scripts.
monitor_controller.php Adds nonce support and helper-based JavaScript loading.
locales/​po/​cacti.pot Updates generated source references.
CHANGELOG.md Documents CSP changes.
.github/​copilot-instructions.md Strengthens PO/MO handling guidance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +25 to +29
test('csp nonce delegates to CactiSecureHeaders when the class is available', function () {
$source = file_get_contents(__DIR__ . '/../../setup.php');

expect($source)->toContain("class_exists('CactiSecureHeaders')");
expect($source)->toContain('CactiSecureHeaders::getNonceAttribute()');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants