Fix missing object-level authorization (IDOR) in Java and Spring Boot
=====================================================================

Get the user from Spring Security with @AuthenticationPrincipal, never from
request parameters, and scope every repository query with tenant_id = :tenant
AND (owner_id = :user OR :isBillingAdmin). Detail, list and export then return
only rows the caller may read; map an empty result to 404. In @PostAuthorize
or @PostFilter expressions, compare the tenant before any hasRole() shortcut.

Page: https://softwaresecurity.com/fix/missing-authorization/java-spring/

What is in this archive
-----------------------
missing-authorization/java-spring/   the project: three implementations of the same code and one test suite
  src/main/java/com/example/invoices/impl/VulnerableInvoiceRepository.java   (vulnerable)
  src/main/java/com/example/invoices/impl/FixedInvoiceRepository.java   (fixed)
  src/main/java/com/example/invoices/impl/WrongInvoiceRepository.java   (a plausible fix that still fails)
run-all.mjs   runs the tests in all three modes and checks the results (Node.js only)
LICENSE-CODE.txt

Run it
------
  Tested with OpenJDK 21.0.12.1 (Homebrew), Maven 3.9.16. run-all.mjs needs
  Node.js 22 or later. Supported versions:
  https://softwaresecurity.com/fix/versions/

  From the missing-authorization/java-spring folder, install the dependencies
  once (this needs network access):

    mvn -B -q test

  Then run the tests. IMPL chooses the implementation the tests load:

    # every test passes
    IMPL=fixed mvn -B -o -q test
    # an exploit test fails
    IMPL=vulnerable mvn -B -o -q test
    # a test fails
    IMPL=wrong mvn -B -o -q test

  The JUnit report is written to target/surefire-reports/TEST-*.xml. To run all
  three modes and check the results in one step, run the harness from this top
  folder (it needs only Node.js; --install runs the install step first):

    node run-all.mjs --install missing-authorization/java-spring
    node run-all.mjs missing-authorization/java-spring

Verified
--------
  11 October 2026 on macOS 27.0 (arm64), with java OpenJDK 21.0.12.1, maven
  3.9.16, sqlite 3.53.4. Versions and results hold for that date: run the tests
  with your own versions before relying on the fix.

Licence
-------
  The code is MIT licensed (LICENSE-CODE.txt). The page text is CC BY 4.0.
