Skip to content

Detect alias circles when resolving alias targets - #37585

Merged
sbrannen merged 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/resolve-aliases-circle-check
Oct 26, 2026
Merged

sbrannen merged 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/resolve-aliases-circle-check

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

SimpleAliasRegistry rejects circular aliases when they are registered, but resolveAliases() skips that check when only the target name of an alias changes. A placeholder in the target name can therefore introduce a circle that is registered silently, and the next lookup of either alias loops forever in canonicalName(). This PR adds the missing checkForAliasCircle() call.

Problem

resolveAliases() handles three cases for each alias:

  1. The alias or name resolves to null, or both resolve to the same value: the entry is removed.
  2. The alias itself changes: checkForAliasCircle(resolvedName, resolvedAlias) is invoked before the new entry is stored.
  3. Only the target name changes: the entry is updated with aliasMap.put(alias, resolvedName), without a circle check.

For example:

<alias name="${target}" alias="alias1"/>
<alias name="alias1" alias="alias2"/>

With target=alias2, case 3 turns alias1 -> ${target} into alias1 -> alias2 while alias2 -> alias1 already exists. No exception is thrown, and getBean("alias1") then spins in canonicalName(). Registering the same two aliases directly fails immediately with Circular reference - 'alias2' is a direct or indirect alias for 'alias1' already.

The early detection in registerAlias() was introduced for exactly this kind of endless loop in SPR-7274 (gh-11933).

Fix

checkForAliasCircle(resolvedName, alias) is now invoked in case 3 before the entry is updated, in the same way as in case 2. Resolution of a circle introduced through a placeholder now fails with the same IllegalStateException as a direct registration, and the alias map is left unchanged for that alias.

A new test in SimpleAliasRegistryTests reproduces the scenario above. It fails before the change (no exception is thrown) and passes after it. The spring-core and spring-beans test suites pass.

SimpleAliasRegistry checks for circular references when an alias is
registered and when resolveAliases() resolves an alias to a new name,
but not when only the target name of an alias changes. A placeholder
in the target name of an alias, for example
<alias name="${target}" alias="alias1"/>, could therefore resolve to
an alias that already points back to alias1. The circle was then
registered silently and canonicalName() looped forever on the next
lookup of either alias.

Invoke checkForAliasCircle() in that branch as well, so that such a
configuration fails with the same IllegalStateException as a direct
registration of the circle.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 24, 2026
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Oct 26, 2026
@sbrannen sbrannen self-assigned this Oct 26, 2026
@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 26, 2026
@sbrannen sbrannen added this to the 7.1.0-RC2 milestone Oct 26, 2026
@sbrannen
sbrannen merged commit 62c0f6f into spring-projects:main Oct 26, 2026
5 checks passed
@sbrannen

Copy link
Copy Markdown
Member

This has been merged into main.

Thanks

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

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants