Skip to content

Exceptions for Authorized Objects should propagate when returned from a Controller #16058

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
Tracked by #16057
jzheaux opened this issue Nov 11, 2024 · 1 comment · May be fixed by #17074
Open
Tracked by #16057

Exceptions for Authorized Objects should propagate when returned from a Controller #16058

jzheaux opened this issue Nov 11, 2024 · 1 comment · May be fixed by #17074
Assignees
Milestone

Comments

@jzheaux
Copy link
Contributor

jzheaux commented Nov 11, 2024

When using @AuthorizeReturnObject on a controller method like so:

@AuthorizeReturnObject
@GetMapping(...)
public MyObject getObject() {
    return ...
}

Any unhandled authorization failures are not propagated to the ExceptionTranslationFilter and instead result in a 500.

Security currently supports using @HandleAuthorizationDenied to provide behavior like ignoring that field when the response is serialized. However, it would be helpful to be able to leverage the logic in ExceptionTranslationFilter, including calling any configured AccessDeniedHandler.

One way to do this may be to provide an implementation of AbstractView that is used by a default exception handler. It would ideally have a similar effect to:

@ExceptionHandler(HttpMessageNotWritableException.class)
View handleWrite(HttpMessageNotWritableException ex) {
    if (ex.getRootCause() instanceof AuthorizationDeniedException denied) {
       return new AbstractView() {
          @Override
          protected void renderMergedOutputModel(Map<String, Object> model,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
             throw ex;
          }
       };
    }
    throw ex;
}

but without applications needing to register it.

@evgeniycheban
Copy link
Contributor

Hi, @jzheaux can you assign it to me?

@jzheaux jzheaux added this to the 7.0.x milestone May 1, 2025
evgeniycheban added a commit to evgeniycheban/spring-security that referenced this issue May 8, 2025
… a Controller

Closes spring-projectsgh-16058

Signed-off-by: Evgeniy Cheban <mister.cheban@gmail.com>
evgeniycheban added a commit to evgeniycheban/spring-security that referenced this issue May 8, 2025
… a Controller

Closes spring-projectsgh-16058

Signed-off-by: Evgeniy Cheban <mister.cheban@gmail.com>
evgeniycheban added a commit to evgeniycheban/spring-security that referenced this issue May 8, 2025
… a Controller

Closes spring-projectsgh-16058

Signed-off-by: Evgeniy Cheban <mister.cheban@gmail.com>
evgeniycheban added a commit to evgeniycheban/spring-security that referenced this issue May 8, 2025
… a Controller

Closes spring-projectsgh-16058

Signed-off-by: Evgeniy Cheban <mister.cheban@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants