JDK-8306819 : Consider disabling the compiler's default active annotation processing
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 20
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2023-04-21
  • Updated: 2024-10-27
  • Resolved: 2023-10-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22 b19Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
JSR 269 introduced annotation processing into Java.
However it was implemented that this is enabled by default inside the compiler and can be disabled with -proc:none flag. Annotation processors are also loaded via ServiceLoader.

The implementation happened nearly 20 years ago, in a time when build tools like Ant, Maven and Gradle had no widespread usage or didn't even exist yet.
Based on my observations, nowadays annotation processing is hardly used anymore and if it's used it mostly occurs after the actual compilation process in a build tool - with a specific Annotation Processor only.

Furthermore - because it's enabled by default - it may expose the user to a risk of unintentional executing code that should only be compiled.
Consider the following scenario:
* A new version of a library - used inside one of your programs maybe only transitively - get's released. However it is infected with a malicious annotation processor.
* The new version is picked up by your automated dependency update tool (e.g. https://github.com/renovatebot/renovate or https://github.com/dependabot) and a PullRequest is created
* The PR is picked up by a CI server which compiles the code to check if everything works
* When compiling the code the malicious annotation processor is executed and it may compromise your CI server

The above scenario can easily be mitigated with the -proc:none Flag, however as annotation processing usually occurs without any log outputs or additional hints, it's likely that most users don't even know that it's enabled by default.

So maybe it would be a good idea to disable annotation processing by default and enable it only when e.g. the -proc (without Arguments) Flag is set. This would be a breaking change, however it may be acceptable considering that the security improvements for most users outweigh the effort of some users to add a simple flag.



Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/14432 Date: 2023-06-12 23:49:51 +0000
27-10-2024

Change reverted under JDK-8321073; no release note needed for JDK 22.
31-01-2024

I think the change like this requires a much broader discussion. More expanded comment here: https://github.com/openjdk/jdk/pull/14432#issuecomment-1834517085
30-11-2023

Changeset: dc4bc4f0 Author: Joe Darcy <darcy@openjdk.org> Date: 2023-10-06 21:45:58 +0000 URL: https://git.openjdk.org/jdk/commit/dc4bc4f0844b768e83406f44f2a9ee50686b1d9d
06-10-2023

RFE to disable annotation processing by default and enable it only when e.g. the -proc (without Arguments) Flag is set.
25-04-2023