Skip to content

Use of RewriteHandler.LastRuleHandler without a child handler should produce a clear error message. #12300

Description

@joakime

Jetty version(s)
12.0.13

Jetty Environment
Any

Java version/vendor (use: java -version)
Any

OS type/version
Any

Description
If an instance of a Handler.Wrapper without a child is allowed to start and begin handling requests, the error message is unclear.

java.lang.NullPointerException: Cannot invoke "org.eclipse.jetty.server.Request$Handler.handle(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response, org.eclipse.jetty.util.Callback)" because "this._handler" is null

We should make the start of this kind of handler fail the start and throw a meaningful message that the Handler.Wrapper is missing a child.

Activity

  1. added
    BugFor general bugs on Jetty side
    on Sep 23, 2024
  2. janbartel commented on Sep 24, 2024

    @janbartel
    Contributor

    @joakime actually the issue is not coming from Handler.Wrapper, this is a problem purely with the RewriteHandler.LastRuleHandler, which while it might be a Handler.Wrapper isn't using the inherited handler, but rather actually defines it's own _handler here: https://github.com/jetty/jetty.project/blob/jetty-12.0.x/jetty-core/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java#L148.

    So we need a more meaningful message in RewriteHandler.LastRuleHandler if it starts with a null _handler.

    It's possible to also put an IllegalStateException into Handler.Wrapper.doStart if the _handler is null but first we would have to check that the Handler.Wrapper instance is !dynamic (as if dynamic is true, starting without a handler set is allowed).

  3. gregw commented on Sep 24, 2024

    @gregw
    Contributor

    There is a use-case for optional handlers in Handler.Wrapper. The classic example is ServletHandler, which is a Wrapper, but typically does not have a nested Handler. This is only important for deployments without a default servlet.

  4. added this to Jetty 12.0.15 FROZEN and removed this from Jetty 12.0.14on Sep 25, 2024
  5. changed the title [-]Use of Handler.Wrapper without a child handler should produce a clear error message.[/-] [+]Use of RewriteHandler.LastRuleHandler without a child handler should produce a clear error message.[/+] on Sep 30, 2024
  6. sbordet commented on Oct 21, 2024

    @sbordet
    Contributor

    @janbartel LastRuleHandler is not a oej.server.Handler.Wrapper, but rather just a Request.Wrapper.

    I agree that we might need a better error message, but it should not impact oej.server.Handler classes.

  7. self-assigned this
    on Oct 21, 2024
  8. self-assigned this
    on Nov 27, 2024
  9. 1 remaining item

  10. codecrafter2017 commented on Jun 25, 2025

    @codecrafter2017

    Hi,
    First I'd like to thank you for all your work on Jetty.

    I'm prepared do some work on this. I'd like to clarify an approach to the solution for this issue.

    Starting off - Rule.Handler extends Request.Wrapper, and we can throw with a reasonable message from within LastRuleHandler or RewriteHandler.

    LastRuleHandler is instantiated within RewriteHandler, and any exception can be handled along the same path as other Exceptions from handle().

    We can add a throw(IllegalArgument) within the constructor for LastRuleHandler, or before LastRuleHandler _handler.handle(IllegalState or NullPointer). If we are ok with preventing the current rewrites if getHandler returns null, we can also perform the check within RewriteHandler's handler or by overriding its doStart().

    Is the state legal if there's no downstream handler for a rewriteHandler ?

    Can we add an exception within handler.wrapper.doStart() ? It may impact doStart() performance due to the exception check.

  11. sbordet commented on Jul 4, 2025

    @sbordet
    Contributor

    @codecrafter2017 I think the best approach is to override RewriteHandler.doStart() and there add a check about the child Handler: if null throw IllegalStateException.

    The reason is that RewriteHandler is a non-dynamic Handler.Wrapper, so a child Handler cannot be added after it is started.

    Please do this work in the jetty-12.1.x branch.

    Thanks!

  12. added 2 commits that reference this issue on Jul 16, 2025
    5b897e2
    bad3dc6
  13. added a commit that references this issue on Jul 21, 2025
    99886f5
  14. sanjerai commented on Aug 19, 2025

    @sanjerai
    Contributor

    @sbordet i think this can be marked closed now as it has been merged.

  15. sbordet commented on Aug 19, 2025

    @sbordet
    Contributor

    Fixed by #13391.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFor general bugs on Jetty sideHelp Wanted

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions