[Java] Add generateInsecureTlsHook option to omit the trust-all-certificates hook (jersey2/jersey3) - #24786
[Java] Add generateInsecureTlsHook option to omit the trust-all-certificates hook (jersey2/jersey3)#24786K3vChan wants to merge 2 commits into
Conversation
The jersey2 and jersey3 ApiClient templates always emit disableCertificateValidation(), which installs an X509TrustManager with empty checkClientTrusted/checkServerTrusted bodies. CodeQL reports it as java/insecure-trustmanager at high severity, which fails the code scanning check on any PR touching the generated client. The method is a protected opt-in hook that nothing in the generated client calls, but analysers flag code as written rather than as reached, so projects carry a high-severity alert for a method they never use. There is currently no way to opt out: no option gates the block, suppression comments do not survive regeneration, and .openapi-generator-ignore cannot skip ApiClient because it is the class you configure. Add a generateInsecureTlsHook option gating the method, the javadoc that points at it, and the seven imports that become unused without it. It defaults to true, so generated output is unchanged and existing subclasses that call the hook keep compiling. Regenerating every java* sample produces no diff. Scoped to jersey2 and jersey3. okhttp-gson contains a similar trust-all block, but there it sits inside applySslSettings() behind the runtime verifyingSsl field, which has a public setVerifyingSsl setter. That is a live feature rather than a dead hook, so removing it would be a breaking change and is left alone.
There was a problem hiding this comment.
1 issue found across 6 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="docs/generators/java-microprofile.md">
<violation number="1" location="docs/generators/java-microprofile.md:59">
P2: This option is documented for java-microprofile but is a no-op there. java-microprofile (JavaMicroprofileServerCodegen) extends JavaClientCodegen, so the unconditionally-registered CliOption at JavaClientCodegen.java:287 propagates here and to every other Java generator, yet only the jersey2/jersey3 ApiClient mustache templates consult generateInsecureTlsHook. Users of this generator see an option that does nothing, contradicting the PR's stated jersey2/jersey3 scope. Add the CliOption (and its additionalProperties defaulting) only for the jersey2/jersey3 libraries, or otherwise filter it from generators whose templates never read it.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| |generateBuilders|Whether to generate builders for models| |false| | ||
| |generateClientAsBean|For resttemplate, restclient and webclient, configure whether to create `ApiClient.java` and Apis clients as bean (with `@Component` annotation).| |false| | ||
| |generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false| | ||
| |generateInsecureTlsHook|Generate the ApiClient.disableCertificateValidation hook, which trusts all TLS certificates (default to true). Set to false to omit it, e.g. when static analysis flags the trust-all TrustManager it contains. Available on `jersey2`, `jersey3` libraries.| |false| |
There was a problem hiding this comment.
P2: This option is documented for java-microprofile but is a no-op there. java-microprofile (JavaMicroprofileServerCodegen) extends JavaClientCodegen, so the unconditionally-registered CliOption at JavaClientCodegen.java:287 propagates here and to every other Java generator, yet only the jersey2/jersey3 ApiClient mustache templates consult generateInsecureTlsHook. Users of this generator see an option that does nothing, contradicting the PR's stated jersey2/jersey3 scope. Add the CliOption (and its additionalProperties defaulting) only for the jersey2/jersey3 libraries, or otherwise filter it from generators whose templates never read it.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At docs/generators/java-microprofile.md, line 59:
<comment>This option is documented for java-microprofile but is a no-op there. java-microprofile (JavaMicroprofileServerCodegen) extends JavaClientCodegen, so the unconditionally-registered CliOption at JavaClientCodegen.java:287 propagates here and to every other Java generator, yet only the jersey2/jersey3 ApiClient mustache templates consult generateInsecureTlsHook. Users of this generator see an option that does nothing, contradicting the PR's stated jersey2/jersey3 scope. Add the CliOption (and its additionalProperties defaulting) only for the jersey2/jersey3 libraries, or otherwise filter it from generators whose templates never read it.</comment>
<file context>
@@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|generateBuilders|Whether to generate builders for models| |false|
|generateClientAsBean|For resttemplate, restclient and webclient, configure whether to create `ApiClient.java` and Apis clients as bean (with `@Component` annotation).| |false|
|generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false|
+|generateInsecureTlsHook|Generate the ApiClient.disableCertificateValidation hook, which trusts all TLS certificates (default to true). Set to false to omit it, e.g. when static analysis flags the trust-all TrustManager it contains. Available on `jersey2`, `jersey3` libraries.| |false|
|gradleProperties|Append additional Gradle properties to the gradle.properties file| |null|
|groupId|groupId in generated pom.xml| |org.openapitools|
</file context>
CliOption.newBoolean(opt, description) records false, so the generated docs tables and config-help reported a default of false while processOpts treats an absent property as true. For an option controlling whether a trust-all TrustManager is emitted, advertising the wrong default is worse than for most, so pass the default explicitly.
Fixes #24785
The
jersey2andjersey3ApiClienttemplates unconditionally emitdisableCertificateValidation(), which builds anX509TrustManagerwith emptycheckClientTrusted/checkServerTrustedbodies and installs it into theSSLContext. CodeQL reports this asjava/insecure-trustmanagerat highseverity, which fails the code scanning check on any PR that adds or touches the
generated client. Sonar's
java:S4830flags the same pattern.The method is a
protectedopt-in hook — nothing in the generated client callsit, and the javadoc on
customizeClientBuildertells you to override and invokeit if you want it. But analysers flag code as written, not as reached, so
projects that commit their generated client (or scan
target/) carry ahigh-severity alert for a method they never use, with no way to opt out today:
configOptiongates the block — it is unconditional template text;.openapi-generator-ignoreexcludes whole files, andApiClientis the classyou configure, so it cannot be skipped.
What this changes
Adds a
generateInsecureTlsHookoption that gates the method, the javadoc linesthat point at it, and the seven imports that become unused without it
(
SSLContext,TrustManager,X509TrustManager,X509Certificate,SecureRandom,KeyManagementException,NoSuchAlgorithmException).It defaults to
true, so generated output is unchanged. Anyone whooverrides
customizeClientBuilderand calls the hook keeps compiling. Optingout is explicit:
Scope
Limited to
jersey2andjersey3.okhttp-gsoncontains a similar trust-allX509TrustManager, but there it sits insideapplySslSettings()behind theruntime
verifyingSslfield, which has a publicsetVerifyingSsl(boolean)setter and is a documented feature. Gating that would be a breaking change, so
it is left alone.
native,apache-httpclientandresttemplatedo not emitthe block at all.
Testing
testInsecureTlsHookGeneratedByDefaultandtestInsecureTlsHookOmittedWhenDisabledtoJavaClientCodegenTest, both runagainst
jersey2andjersey3via a data provider (4 cases). The secondasserts the method, the
X509TrustManagerand each of the seven imports areabsent.
JavaClientCodegenTest: 278 tests, 0 failures.bin/configs/java*.yamlsample: no diff, confirmingthe default preserves current output.
jersey2andjersey3clients with the option off and confirmed noresidue of the method or its imports, and that spacing around the removal
stays correct (one blank line between the neighbouring members).
PR checklist
were regenerated and are included.
masteras a non-breaking change.(Java Jersey2), and @bbdouglas @wing328 as Java maintainers, in case you would
prefer a different option name or a different default.
Summary by cubic
Adds a
generateInsecureTlsHookoption forjersey2/jersey3so the generatedApiClientno longer has to include the trust-alldisableCertificateValidationhook that static analyzers flag asjava/insecure-trustmanager(fixes #24785).The option defaults to
true, so generated output is unchanged; docs previously advertised afalsedefault because the CLI option wasn't explicitly registered with one, which is now fixed. Setting it tofalseremoves the hook, its javadoc, and the seven imports that become unused.okhttp-gsonis left untouched because its equivalent is a live runtime feature behindsetVerifyingSsl, not a dead method.New Features
generateInsecureTlsHookis a newconfigOptionsboolean forjersey2/jersey3, defaulting totrue.Written for commit 8d3bace. Summary will update on new commits.