Skip to content

Psrstan - #63

Open
bmfmancini wants to merge 35 commits into
Cacti:developfrom
bmfmancini:psrstan
Open

Psrstan#63
bmfmancini wants to merge 35 commits into
Cacti:developfrom
bmfmancini:psrstan

Conversation

@bmfmancini

Copy link
Copy Markdown
Member

Full PSR12 and PHPstan level 8 sweep
Typing run completed
Drop support for PHP7 syntax

security: harden audit logging and management

- prevent stored XSS in audit views
- require CSRF-protected POST and management permission for purging
- recursively redact web and CLI credentials
- generate safe, standards-compliant CSV exports
- record hook events explicitly as attempted actions
- harden retention, replication, and external logging
- add security regression checks
- bump plugin version to 1.3
security: complete audit logging hardening

- bound request depth, field counts, string sizes, and JSON parsing
- detect and redact additional secret-shaped values
- finalize request outcomes as completed or failed
- track and retry failed external log delivery
- expose delivery status in the UI and CSV exports
- upgrade existing remote-poller schemas
- add retention, schema, delivery, and security regression tests
- update documentation and translation template
- bump plugin version to 1.4
This will make it easier for some SIEMS like splunk to ingest the log file
feat: add normalized compliance audit event capture

- add event and correlation UUIDs with integrity metadata
- record actor, target, outcome, timing, and event classification
- deliver finalized request outcomes to external log consumers
- audit log views, searches, details, exports, and purges
- capture logout and session-timeout events on Cacti 1.2.x
- finalize CLI audit events and expand CSV exports
- add schema migration, documentation, and tests
…ce-event-capture

# Conflicts:
#	.github/workflows/plugin-ci-workflow.yml
#	CHANGELOG.md
#	README.md
#	audit.php
#	audit_functions.php
#	setup.php
- composer.json with php-cs-fixer ^3.86, phpstan ^2.2, pest ^2
- .php-cs-fixer.php copied verbatim from Cacti develop (tabs, custom ruleset)
- .phpstan.neon matching Cacti shape, paths adapted, level 8
- phpstan/stubs/cacti.stubs.php typed Cacti function signatures
- .editorconfig (tabs for PHP/JS/SH, binary for po/pot/mo)
- .gitignore updated for vendor/, caches, baseline

Phase 1 of code quality sweep. No functionality changes.
- Convert array() to short array syntax []
- Normalize switch/case indentation (PSR-12)
- Convert dirname(__FILE__) to __DIR__
- Normalize else if to elseif
- Apply binary operator alignment (=> = ===)
- Single quotes, concat spaces, trailing whitespace
- Fix controller_security_test assertions for short array syntax
- Fix SetupStructureTest to check INFO file instead of source regex

Phase 2 of code quality sweep. No functionality changes.
- Add declare(strict_types=1) to all in-scope PHP files
- Replace Php74CompatibilityTest with Php81SyntaxTest
- Php81SyntaxTest asserts strict_types and short array syntax
- CS-Fixer normalizes declare spacing to Cacti convention

Phase 3 (partial) of code quality sweep. No functionality changes.
- Remove declare(strict_types=1) from all PHP files per team lead directive
- Update Php81SyntaxTest to not assert strict_types
- strict_types will not be supported until Cacti 1.3 is released

No functionality changes.
- audit_syslog.php: 38 functions typed (params + return types)
- audit_functions.php: 30 functions typed (params + return types)
- setup.php: 19 functions typed (params + return types)
- audit.php: 7 functions typed (params + return types)
- Uses union types (array|false, int|false), mixed, nullable (?T), void
- No strict_types (deferred until Cacti 1.3)
- No functionality changes — types match existing runtime values

Phase 3 of code quality sweep.
- Add @param/@return array value type docblocks (40+ missingType.iterableValue)
- Add is_array() guards for foreach on db_fetch results (7 foreach.nonIterable)
- Add is_array/null-coalescing guards for array|false offset access (6 errors)
- Add null coalescing for string|null arguments (preg_replace, trim, substr)
- Add resource|false guards for fputcsv/fclose
- Fix audit_json_encode fallback to guarantee string return
- Fix audit_syslog_config null coalescing for optional keys (20 offsetAccess.notFound)
- Fix stubs: __() variadic, api_plugin_register_hook 5th param, db_add_index mixed
- Add ignoreErrors for includeOnce.fileNotFound, treatPhpDocTypesAsCertain: false
- Regenerate baseline (210 errors, all in test files / pre-existing debt)
- PHPStan with baseline: 0 errors. Without baseline: 7 source errors (all ignored)

Phase 4 of code quality sweep. No functionality changes.
- .github/workflows/code-quality.yml: PHP 8.1/8.2/8.3/8.4 matrix
- Runs PHP-CS-Fixer (dry-run), PHPStan, Pest, and plain-PHP tests
- Existing plugin-ci-workflow.yml remains unchanged

Phase 5 of code quality sweep. No functionality changes.
- Bump minimum PHP to ^8.2 (pest ^2 requires PHP 8.2+)
- Pin symfony components to ^7.0 (avoid PHP 8.4-only symfony 8.x)
- Set composer platform.php to 8.2 for reproducible lock
- Regenerate composer.lock with 8.2-compatible versions
- Update CI matrix to PHP 8.2/8.3/8.4

Fixes CI composer install failure on PHP 8.1.
- Keep require php ^8.1; pest ^2.0 requires PHP 8.2 so:
  - composer install uses --ignore-platform-reqs (lock resolves on any PHP)
  - platform pinned to 8.2 for reproducible lock
  - symfony pinned to ^7.0
- Pest tests skipped on PHP 8.1 in CI (pest 2.x needs 8.2+)
- PHP-CS-Fixer and PHPStan run on all PHP versions (8.1-8.4)
- Regenerate baseline (210 errors, unchanged)

Fixes CI composer install failure while maintaining PHP 8.1 support.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR modernizes the Audit plugin for a PHP 8.1+ baseline, applying PSR-12-style refactors while introducing PHPStan (level 8) and PHP-CS-Fixer automation to keep the codebase consistently typed and formatted going forward.

Changes:

  • Updated plugin PHP source to add return types, tighter type handling, and short array syntax aligned with PSR-12/strict static analysis.
  • Added code quality tooling (Composer dev deps, PHPStan config/baseline, PHP-CS-Fixer config) plus a GitHub Actions workflow to run linting, formatting checks, PHPStan, and tests.
  • Updated and expanded security/syslog tests to match new syntax/behavior expectations, and replaced the PHP 7.4 compatibility test with a PHP 8.1+ syntax test.

Reviewed changes

Copilot reviewed 22 out of 24 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/syslog_queue_test.php PSR-12 style updates (short arrays / spacing) for syslog queue tests.
tests/syslog_functions_test.php PSR-12 style updates and minor readability tweaks in syslog integration tests.
tests/Security/SetupStructureTest.php Updates assertions to validate plugin metadata is read from INFO via parse_ini_file.
tests/Security/PreparedStatementConsistencyTest.php Converts arrays to short syntax; minor cleanup.
tests/Security/Php81SyntaxTest.php New test enforcing PHP 8.1+ conventions (e.g., short array syntax).
tests/Security/Php74CompatibilityTest.php Removed PHP 7.4 compatibility assertions as part of dropping PHP7-era constraints.
tests/security_functions_test.php PSR-12 style updates and short array syntax conversion.
tests/Pest.php Comment style update to line comment.
tests/controller_security_test.php Short array syntax conversion and minor string normalization in guard checks.
tests/bootstrap.php Short array syntax conversion in test stubs.
setup.php Adds typing/return types, improves defensive handling for DB results, and updates realm/schema/upgrade logic formatting.
phpstan/stubs/cacti.stubs.php New PHPStan stubs describing Cacti core APIs/constants/globals used by the plugin.
phpstan-baseline.neon New PHPStan baseline for known/accepted issues while moving to level 8.
index.php Switches header call to single quotes (style-only change).
composer.json Introduces Composer tooling, requires PHP ^8.1, and adds scripts for fixer, PHPStan, and tests.
audit.php Typing + PSR-12 refactor; adds more defensive checks around DB results/output handles and reorganizes controller actions.
audit_syslog.php Typing + PSR-12 refactor; strengthens config parsing/validation and return shapes for PHPStan.
audit_functions.php Typing + PSR-12 refactor; enhances JSON helpers/redaction; adjusts external/syslog delivery flows.
.phpstan.neon New PHPStan configuration targeting key plugin paths at level 8 with project-specific ignores.
.php-cs-fixer.php New fixer config matching Cacti core’s ruleset, adapted to the plugin layout.
.gitignore Ignores Composer vendor, PHPStan cache, fixer cache, and PHPUnit/Pest caches.
.github/workflows/code-quality.yml New CI workflow running lint, PHP-CS-Fixer (dry-run), PHPStan, and tests across PHP 8.1–8.4.
.editorconfig New editor configuration (tabs for PHP/JS/SH, LF endings, trimming rules).

Comment thread audit_functions.php
Comment on lines +446 to 450
$event = db_fetch_row_prepared('SELECT * FROM audit_log WHERE id = ?', [$id]);

if (is_array($event) && $event['request_status'] == 'started') {
return;
}
Comment thread audit_functions.php Outdated
Comment on lines +56 to +59
}
break;

$objects[] = $result;
}
Comment thread setup.php
Comment on lines 423 to 427
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/audit/INFO', true);
return $info['info'];

return is_array($info) ? $info['info'] : [];
}

@TheWitness TheWitness left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So no composer in plugins and we run our linting, phpcs fixer and stanley inside of the cacti root.

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.

3 participants