Skip to content

Java: Uncaught servlet exception#3839

Merged
aschackmull merged 20 commits intogithub:mainfrom
luchua-bc:uncaught-servlet-exception
Dec 2, 2020
Merged

Java: Uncaught servlet exception#3839
aschackmull merged 20 commits intogithub:mainfrom
luchua-bc:uncaught-servlet-exception

Conversation

@luchua-bc
Copy link
Contributor

@luchua-bc luchua-bc commented Jun 29, 2020

This PR is to address CWE-600 "Uncaught Exception in Servlet".

Failure to catch exceptions in a servlet could leave a system in a vulnerable state, possibly resulting in denial-of-service attacks, or the exposure of sensitive information because when a servlet throws an exception, the servlet container typically sends debugging information back to the user. And that information could be very valuable to an attacker.

The CodeQL query in this PR detects method calls within servlet methods that can throw IOException and RuntimeException but those exceptions are not caught then processed.

I've tested the query against some popular open-source software including Apache ActiveMQ. Details and screenshots are attached in the associated all-for-one issue #143.

Please also note I added a stub class GenericServlet, which implements the Servlet interface, and added extends GenericServlet to the stub class HttpServlet, which was missing in the repository. Without this, the check of instanceof ServletClass against the popular javax.servlet.http.HttpServlet class will fail, which should not in my opinion.

Please consider to merge the PR. Thanks.

@intrigus-lgtm
Copy link
Contributor

This PR seems to contain unrelated commits?

@luchua-bc
Copy link
Contributor Author

Thanks @intrigus-lgtm . I've removed those three unrelated commits.

@aschackmull
Copy link
Contributor

I think for this case a force push to clean up the commits is fine - especially since review hasn't started.

@luchua-bc
Copy link
Contributor Author

Thanks @aschackmull for the advice. I will do a force push next time before review starts.

@adityasharad adityasharad changed the base branch from master to main August 14, 2020 18:34
@smowton smowton self-assigned this Oct 19, 2020
@luchua-bc
Copy link
Contributor Author

Improved the query by checking the web.xml file and making sure there is no defined in order to prevent FPs.

@smowton
Copy link
Contributor

smowton commented Oct 30, 2020

Your commit "Add error-page check" contains accidental changes to the Servlet stubs, and also an unrelated (but good) improvement adding // BAD and // GOOD tags to a different test (move those to a different commit)

@smowton
Copy link
Contributor

smowton commented Oct 30, 2020

Ah perhaps those changes were deliberate? In that case my complaint is just that the commit message disagrees with what the commit does -- try to (a) keep one commit doing one thing, and (b) make sure the message matches what is done, otherwise it looks like a mistake

@luchua-bc luchua-bc force-pushed the uncaught-servlet-exception branch from 7d94204 to 67af9b0 Compare October 30, 2020 17:06
@luchua-bc
Copy link
Contributor Author

Those changes were deliberate but are for two loosely coupled things - one is the error-page check and the other is updated test cases although test results don't change since the error-page configuration in web.xml is disabled so that we can see what the query can detect without that configuration.

I've split the commit into two commits.

smowton
smowton previously approved these changes Oct 31, 2020
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, will ping the Java team for final review

@smowton
Copy link
Contributor

smowton commented Oct 31, 2020

As with all PRs, if not done already make sure you've used codeql query format

@luchua-bc
Copy link
Contributor Author

I used the "Format Document" tool of the CodeQL plugin within VS Code to do formatting and both query files (one query and one modified query library) were formatted already.

Thanks @smowton for the quick turn-around and all your help with this PR.

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this query also detect throw statements (whose exceptions are not caught) directly within the servlet methods?
The stack trace would not reveal that much information, but it still reveals implementation details. Though this contradicts with one of the test cases where rethrowing the exception without stack trace is considered good behavior.

@luchua-bc
Copy link
Contributor Author

Hi @Marcono1234,

The purpose of this query is to detect a common programming error that Java EE developers don't handle IOExceptions because the Servlet method signatures declare those. And developers may not be aware that uncaught runtime exceptions and IO exceptions could leak valuable stack traces.

I've added the code to detect a throw statement containing the same exception variable directly under a catch statement e.g. catch (UnknownHostException uhex) {throw new IOException(uhex);}. This helps one of the test cases.

I think this change is probably safe but I don't want to catch every throw statement, which may introduce many false positives. The rational is that developers know what they are doing if they intentionally throw an exception with a message containing only some information e.g. ex.getMessage() or a custom message.

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to also detect calls to initCause(...) and addSuppressed(...)? For both the exception will be displayed in the stack trace as well, e.g.:

catch (Exception e) {
    IOException ioException = new IOException();
    ioException.initCause(e);
    // Or
    ioException.addSuppressed(e);
    throw ioException;
}

@luchua-bc
Copy link
Contributor Author

I've modified the query to handle initCause(...) and addSuppressed(...). As there are only three simple cases, I modelled them without using a data flow.

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should cover all common cases, thanks!

Also feel free to dismiss my review comments. After all only the comments of the maintainers of this repository matter I guess.

@aschackmull aschackmull merged commit 0cc324b into github:main Dec 2, 2020
@luchua-bc
Copy link
Contributor Author

Thanks @aschackmull for all your help with this PR.

@luchua-bc luchua-bc deleted the uncaught-servlet-exception branch December 2, 2020 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments