Skip to content

Method security annotations, like PreAuthorize, not working on private methods after upgrade to 6.4 #16967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
betalb opened this issue Apr 18, 2025 · 1 comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: bug A general bug

Comments

@betalb
Copy link

betalb commented Apr 18, 2025

Describe the bug
Method security annotations, like PreAuthorize, not working on private methods after upgrade to spring-security 6.4. This only works in case of AspectJ compile time weaving (probably load time should work too, but I was not able to create workable example).

Those annotations worked in spring-security 6.3 and earlier.
I see that there was some refactoring in annotation processing code, and it seems that linked method is explicitly rejecting private class members:

To Reproduce
Create project that has dependency on spring-security-aspects, aspectjrt and uses aspectj-maven-plugin for compile-time weaving.

Create Rest endpoint that has PreAuthorize annotation on private method

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class Endpoint {

    @GetMapping
    public List<String> get() {
        return doGet();
    }

    @PreAuthorize("denyAll()")
    private List<String> doGet() {
        return List.of("nothing");
    }

}

Expected behavior
User should see 403 error, but instead he sees a response ["nothing"]

Sample

self-invocation.zip

In attached sample if modifier is changed from private to protected on method name.vitalii.selfinvocation.Endpoint#doGet, app starts to work as expected.

@betalb betalb added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Apr 18, 2025
@betalb
Copy link
Author

betalb commented Apr 18, 2025

I tried to briefly check spring-core AnnotationsScanner and it looks like it allowed private methods, but only if method being called is defined on targetClass and not on parent classes. And it seems that sping-security version has check for this case

but it breaks in scenario of aspectj compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant