In C#, assertions can be stripped out by the compiler by switching from debug mode to release mode. From a security (and UX) standpoint, this is a good thing in order to avoid exposing a stacktrace to the end-user, as a stacktrace may lower user-confidence in the stability of the app, and a stacktrace could expose vulnerabilities which can be exploited. Further, a stacktrace could give some information that can help hackers reverse-engineer the software.
After looking at Java assertions, it appears that no such capability exists. That is, assertions remain in the compiled bytecode, and there are no command-line options to the Java compiler to strip them away. Rather, we see that the Java runtime permits the enabling of assertions, something which could be done by a malicious user attempting to glean stacktrace information. Also, I've even seen some encourage the idea that assertions should fail dramatically to spit out an AssertionError
and a stacktrace. Or simply, people are neglecting to talk about the security aspects of letting an application fail like this.
My initial assessment is that the assertions in Java pose a security risk, even if they provide advantages for debugging or maintenance.
Should we stay away from using assertions in Java for security-critical applications; or if there is a proper, and secure, way include assertions, how should it be done?
Edit for Clarity: I'm assuming here that the Java application is being deployed to the end-user as a desktop application. That is, the user downloads/installs the application. That being said, it is my understanding that the user is able to run an executable jar while enabling assertions with something to the effect of:
java -ea -jar "MyJar.jar"