Skip to content

Avoid spring-core in password encoder validation - #19341

Merged
jzheaux merged 1 commit into
spring-projects:7.1.xfrom
seonwooj0810:gh-19317
Sep 10, 2026
Merged

jzheaux merged 1 commit into
spring-projects:7.1.xfrom
seonwooj0810:gh-19317

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Closes gh-19317

Root cause

spring-security-crypto declares org.springframework:spring-core as an optional dependency in crypto/spring-security-crypto.gradle, so it is not propagated through the published POM. The module is intentionally usable standalone — historically the runtime code paths only relied on JDK types.

In 7.1.0 the validation block in AbstractValidatingPasswordEncoder#matches and #upgradeEncoding was refactored to use org.springframework.util.StringUtils#hasLength (commit 9323775, "Update javadoc and apply StringUtils#hasLength"). That introduced a hard reference from a hot path to a spring-core class. Standalone consumers of spring-security-crypto (e.g. anyone calling BCryptPasswordEncoder#matches) now hit:

java.lang.NoClassDefFoundError: org/springframework/util/StringUtils

The invariant being violated is the module-level one expressed by the gradle file: the runtime hot path of spring-security-crypto must not depend on spring-core. @org.springframework.lang.Contract is also referenced in this class but has RetentionPolicy.CLASS, so it is not loaded at runtime and is not affected.

Change

Replace the two StringUtils.hasLength(...) calls with the inline == null || isEmpty() checks that were in place before 9323775, and drop the now-unused import. The semantics are identical (StringUtils.hasLength(cs) is defined as cs != null && cs.length() > 0).

KeyStoreKeyFactory also uses StringUtils.getFilenameExtension, but its constructor already takes org.springframework.core.io.Resource, so users of that class necessarily have spring-core on the classpath and it is not a regression. This PR leaves it untouched.

Test evidence

The existing AbstractPasswordEncoderValidationTests (extended by AbstractValidatingPasswordEncoderTests, BCryptPasswordEncoderTests, etc.) already exercises the null/empty rawPassword and encodedPassword cases on the affected branch, and continues to pass:

./gradlew :spring-security-crypto:compileJava :spring-security-crypto:test --rerun-tasks \
  --tests "*AbstractValidatingPasswordEncoderTests" \
  --tests "*BCryptPasswordEncoderTests" \
  --tests "*AbstractPasswordEncoderTests"
=> BUILD SUCCESSFUL

./gradlew :spring-security-crypto:format :spring-security-crypto:check -x test
=> BUILD SUCCESSFUL  (checkstyleMain, checkstyleTest, checkFormat all pass)

I considered adding a dedicated regression test that asserts the class can load without spring-core, but driving an isolated classloader from a test that itself runs under the project's full classpath is more invasive than the fix and harder to maintain — happy to add one if maintainers prefer.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 14, 2026
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Gentle ping — open since mid-June.

spring-security-crypto declares spring-core as optional, so it is not propagated through the published POM and the module is meant to work standalone. The 7.1.0 refactor of AbstractValidatingPasswordEncoder#matches/#upgradeEncoding to org.springframework.util.StringUtils#hasLength (9323775) introduced a hard requirement on it (gh-19317). The PR goes back to JDK types.

Is dropping the StringUtils usage the fix you'd prefer, or would you rather make spring-core a real dependency of the crypto module? Still merges cleanly, checks green.

@alexsuter

Copy link
Copy Markdown

merge please :)

@alexsuter

alexsuter commented Sep 9, 2026 •

Copy link
Copy Markdown

Hi guys
Any chance that this gets merged? I think this is straightforward. It blocks us from upgrading the library.
Or what can I do for it?

@alexsuter

Copy link
Copy Markdown

Hello @jzheaux

Do you have time to have a look at this PR?

@jzheaux jzheaux self-assigned this Sep 10, 2026
@jzheaux jzheaux added in: crypto An issue in spring-security-crypto type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 10, 2026
@jzheaux jzheaux added this to the 7.0.8 milestone Sep 10, 2026
`spring-core` is an `optional` dependency of `spring-security-crypto`,
so it is not published in the POM and is not guaranteed to be on the
classpath when the module is used standalone.

7.1.0 refactored `AbstractValidatingPasswordEncoder` to use
`org.springframework.util.StringUtils#hasLength`, which causes
`NoClassDefFoundError` on `BCryptPasswordEncoder#matches` (and other
encoders extending it) for standalone users.

Restore the inline null/empty checks the encoder used prior to that
refactor so that the runtime hot path no longer reaches into
`spring-core`.

Closes spring-projectsgh-19317

Signed-off-by: seonwoo_jung <[email protected]>
@jzheaux
jzheaux changed the base branch from main to 7.1.x September 10, 2026 21:32
@jzheaux jzheaux modified the milestones: 7.0.8, 7.1.2 Sep 10, 2026
@jzheaux
jzheaux merged commit 7cc2077 into spring-projects:7.1.x Sep 10, 2026
7 checks passed
@jzheaux

jzheaux commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, @seonwooj0810, for this PR and for your patience as I got around to it. Thank you, @alexsuter for your patience as well. This is now merged into 7.1.x and forward-ported to main.

@alexsuter

Copy link
Copy Markdown

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: crypto An issue in spring-security-crypto type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoClassDefFound with version 7.1.0

4 participants