Skip to content

fix(core): validate deserialized class types in ServiceOptions.newInstance - #15182

Merged
whowes merged 1 commit into
mainfrom
whowes/service-options-reflect
Dec 1, 2026
Merged

whowes merged 1 commit into
mainfrom
whowes/service-options-reflect

Conversation

@whowes

@whowes whowes commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Hardens ServiceOptions.newInstance by deferring class initialization until assignability to the expected factory interface is verified. Updates ServiceOptions, GrpcTransportOptions, and HttpTransportOptions readObject call sites accordingly and adds unit tests.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enhances security and type safety during deserialization by verifying that dynamically instantiated classes are assignable to expected types before they are initialized. It updates ServiceOptions.newInstance to accept an expected class type, avoids running static initializers prematurely, and replaces deprecated reflection calls. Corresponding serialization round-trip tests are added across transport options. The reviewer suggested improving type safety in ServiceOptions.newInstance by changing the expected parameter type from Class<?> to Class, which allows using expected.cast(...) and removing the @SuppressWarnings("unchecked") annotation.

@whowes
whowes force-pushed the whowes/service-options-reflect branch from add5f1a to 8aed79e Compare November 24, 2026 01:41
@whowes
whowes marked this pull request as ready for review November 24, 2026 01:45
@whowes
whowes requested a review from a team as a code owner November 24, 2026 01:45
@whowes
whowes requested a review from lqiu96 November 24, 2026 01:45
@whowes
whowes force-pushed the whowes/service-options-reflect branch from 8aed79e to 9a9accc Compare November 24, 2026 23:13
@SuppressWarnings("unchecked")
@InternalApi
public static <T> T newInstance(String className) throws IOException, ClassNotFoundException {
public static <T> T newInstance(String className, Class<?> expected)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think we can mark the other newInstance() variant (the one without the expected class) as obsolete and tell any users to use this variant instead for security (I doubt we have any public users of this, but just in case)?

I think this is probably the best practice to reduce any attack vectors.

@whowes whowes Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added @Deprecated to the single-arg newInstance() with a pointer to use the new 2-arg version. IMO it makes more sense than @ObsoleteApi for @InternalApi methods.

After thinking about it some more I also opted to harden the deprecated version by checking that the class is assignable to a small allowlist (the factory interfaces used by the existing internal callers). I think the only reason to keep it around is for possible version skew effects, and after some soak time I think we'll be able to safely remove it.

@whowes
whowes force-pushed the whowes/service-options-reflect branch from 9a9accc to 6966b3e Compare November 30, 2026 20:57
whowes added a commit that referenced this pull request Nov 30, 2026
Formats 4 pre-existing non-complying files in
`sdk-platform-java/java-core` (`google-cloud-core` and
`google-cloud-core-http`) using `com.spotify.fmt:fmt-maven-plugin:2.29`
(following #15028) to fix presubmit lint failures on e.g. #15182
@whowes
whowes force-pushed the whowes/service-options-reflect branch from 6966b3e to 5164ea1 Compare November 30, 2026 23:33
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'gapic-generator-java-root'

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@whowes
whowes merged commit 35a7890 into main Dec 1, 2026
407 of 412 checks passed
@whowes
whowes deleted the whowes/service-options-reflect branch December 1, 2026 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants