Skip to content

Legacy Defensive Checks Role Supplier JSON

Paulo Gomes da Cruz Junior edited this page Nov 12, 2026 · 1 revision

Legacy Defensive Checks: Roles, Authentication Suppliers, and JSON

The legacy hardening work exposed three boundary cases where permissive matching or broad fallback behavior can hide defects. Test the negative cases alongside the intended success path.

Match role boundaries explicitly

JwtRoleChecker.hasRole accepts a concrete role and abstract roles that begin with the exact role plus an underscore. hasConcreteRole checks only the exact role, while hasAbstractRole checks the exact prefix and client identifier.

For a role such as PLANNER, cover the concrete PLANNER value and an intended value such as PLANNER_12345. Also reject unrelated authorities such as SUPER_PLANNER_12345. A substring check would accept that unintended authority.

Relevant code and tests:

  • JwtRoleChecker.java
  • JwtRoleCheckerIntegrationTest.java
  • JwtRoleAuthorizationManagerFactoryTest.java

Resolve authentication once

Spring authorization events expose authentication through a supplier. Read it once into a local variable, then use that same value for classification and logging. Repeated calls can observe different results from a deferred or stateful supplier, and they make exactly-once behavior harder to test.

SecurityEventLoggingListener.onAuthorizationDenied now resolves the supplier once before distinguishing an absent or anonymous authentication (401) from an authenticated denial (403). SecurityEventLoggingListenerTest.authorizationDenialReadsAuthenticationSupplierOnce verifies the call count and classification.

Relevant code and test:

  • SecurityEventLoggingListener.java
  • SecurityEventLoggingListenerTest.java

Catch only expected parse failures

When malformed secondary-mark JSON should produce an empty list, catch Jackson's JacksonException around deserialization. Do not catch every Exception: that can turn unrelated programming errors or unexpected runtime failures into apparently valid empty results.

The mapper tests cover null, blank, valid, empty-array, and malformed JSON. Keeping the catch limited to JacksonException preserves the fallback for invalid input without swallowing unrelated runtime defects.

Relevant code and test:

  • ReportingUnitSearchExpandedMapper.java
  • ReportingUnitSearchExpandedMapperTest.java

Review checklist

  • Does a role matcher enforce the separator and complete identifier boundary?
  • Does code resolve a deferred supplier once before branching on its value?
  • Does a fallback catch only the expected failure type?
  • Do tests prove the accepted path and a nearby rejected or propagated failure?

Related guidance

Waste Plus Wiki

Home

Architecture

Codes

Decisions

Knowledge Base

Development

Changes

Clone this wiki locally