Skip to content

fix(bundle): deprecate tagging two services with the same alias - #784

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/bundle-tag-alias-collision
Nov 8, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/bundle-tag-alias-collision

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #769.

The defect

AlgorithmCompilerPass, HeaderCheckerCompilerPass and ClaimCheckerCompilerPass add every tagged service to their factory with add($alias, $service). The factory keeps one service per alias, and the passes never checked whether an alias was already taken. The service tagged last replaces the other, and the container compiles without a word.

The application's services are defined before the bundle's. So an application service tagged with an alias that the bundle already uses (exp, iat, nbf, or an algorithm name) is replaced by the bundle's own service. With the configuration of the issue:

services:
    app.protected_exp_header_checker:
        class: Jose\Component\Checker\ExpirationTimeChecker
        arguments: { $clock: '@Psr\Clock\ClockInterface', $protectedHeaderOnly: true }
        tags:
            - { name: 'jose.checker.header', alias: 'exp' }

jose:
    checkers:
        headers:
            api: { headers: ['exp'] }

jose.header_checker.api holds the bundle's ExpirationTimeChecker, whose protectedHeaderOnly() is false. A JWS in a JSON serialization that carries exp only in its unprotected header passes check($jws, 0, ['exp']). I reproduced it with a real kernel on 4.3.x. Under the alias exp, the token is accepted. Under exp_protected, the application's checker is used and the token is refused (The header parameter "exp" must be protected.). Algorithms behave the same way: an algorithm service built with other constructor arguments and tagged with its standard name is replaced by the bundle's default instance.

The fix

As proposed in the issue, for 4.3.0:

  • The three passes trigger a deprecation when two different services claim the same alias. The message names the tag, the alias and both services, and says which one is kept:

    Since web-token/jwt-framework 4.3.0: The services "app.protected_exp_header_checker" and "Jose\Component\Checker\ExpirationTimeChecker" are both tagged "jose.checker.header" with the alias "exp": "Jose\Component\Checker\ExpirationTimeChecker" replaces "app.protected_exp_header_checker". Sharing an alias between two services is deprecated and will not be possible in 5.0.0; give each service an alias of its own.

  • The resolution does not change: the service tagged last is still the one kept.
  • One service tagged twice with the same alias stays silent. One service may still carry several aliases.
  • The deprecation is triggered while the container is compiled. In debug mode, Symfony writes it to the container's deprecation log, which debug:container --deprecations and the profiler show (checked with a debug kernel).
  • The three passes now share this code through an @internal trait, TaggedServiceAliases. It reads the tag attributes with their types, which removes three entries from the PHPStan baseline and adds none.

The 5.0.0 part, an exception instead of the deprecation, is #786 (draft, on 5.0.x).

This is not in 4.2.7: the fix is a deprecation, and the patch branches take none. The issue moves to the 4.3.0 milestone with this PR, as #756 did with #778.

This is the companion of #778 (#756), which covers two checkers of the same claim or header parameter inside one manager. The two PRs touch different files.

Behaviour change

services tagged with an alias before after
one service per alias — unchanged, no notice
one service tagged twice with the same alias, or under several aliases — unchanged, no notice
two services sharing an alias (application and bundle, or two bundles) the last one tagged is kept, silently the last one tagged is kept, with one notice per replaced service

No public or protected signature changes.

Tests

  • tests/Bundle/JoseFramework/DependencyInjection/Compiler/TaggedServiceAliasesTest.php runs each of the three passes on a bare ContainerBuilder:
    • an alias shared by two services is reported, and the factory holds the second service;
    • one service tagged twice with the same alias is not reported, nor are services with aliases of their own;
    • a tag without an alias is still refused with the same message as before.
  • tests/Bundle/JoseFramework/Functional/AliasCollisionTest.php uses a real kernel:
    • the configuration of the issue triggers the notice, and jose.header_checker.api still holds the bundle's checker;
    • the bundle's own services use each alias once. The test covers all the algorithms, claim and header managers, and the typ: option.
  • tests/Bundle/JoseFramework/IsolatedKernel.php is a kernel configured by a closure and compiled in a directory of its own. A deprecation triggered during compilation only shows when the container is actually compiled, and the shared cache of AppKernel skips compilation once it is warm.

Verification

  • On the base branch, the 4 tests that expect the notice fail (no notice) and the 10 others pass.
  • Full suite green:
    • PHP 8.4: 2,270 tests (base: 2,256), 4 skipped and 2 incomplete;
    • PHP 8.2 with --prefer-lowest (Symfony 7.0): 2,270 tests, 113 skipped and 2 incomplete.
  • ECS, Rector, PHPStan and Deptrac are clean with the current phpqa:8.4 image.
  • Mutation testing on the diff: all 14 mutants are killed with a 60-second timeout. The CI job, with its 3-second timeout, will skip them as "required more time than configured", because the kernel tests that compile the container also cover these lines.
  • BC, checked against the base commit:
    • API: a reflection dump of every class, interface, trait and enum of src/ (443 on the base, 444 here) differs only by the new @internal trait. process() and every other public or protected signature are unchanged.
    • Behaviour: a probe boots a kernel compiled from scratch, on the base and on the branch, for 13 scenarios. They cover the header checker of the issue, a claim checker, and an algorithm (PBES2-HS256+A128KW built with other arguments). Two more add a bundle registered after JoseFrameworkBundle that redefines exp, alone and together with the application's service. The rest cover a tag without an alias, a null alias, one service tagged twice, one service under two aliases, integer and empty aliases, the bundle alone, and the full test configuration of AppKernel. The factories, the managers, the outcome of the boot and the exception messages are identical. The only difference is the new notice, in the 6 scenarios where two services share an alias.
    • Only string aliases are tracked. A non-string alias, which the add(string $alias, …) call of the generated container coerces, keeps its current behaviour, without a notice.
    • Deprecations: I ran the full suite with a logging trigger_deprecation(), after clearing var/cache. The new notice is triggered 4 times, all by the new tests. No existing test or configuration triggers it, including the configuration of the test kernel.
    • trigger_deprecation() comes from web-token/jwt-library and symfony/dependency-injection, which the bundle already requires.
  • Merge-up: the commit merges into 5.0.x without conflict.

Found by the Anthropic OSS Scanner (ANT-2026-635NQ9DS).

The algorithm, header checker and claim checker compiler passes add every
tagged service to its factory under the alias of its tag. The factory keeps
one service per alias, so when two services share an alias, the one tagged
last replaces the other without a word. The application's services are
defined before the bundle's: a service of the application tagged with an
alias the bundle already uses ("exp", "iat", "nbf" or an algorithm name) is
replaced by the bundle's own service, and a header checker meant to accept
protected headers only can be silently swapped for one that does not.

The three passes now trigger a deprecation naming both services when an
alias is shared, and keep the current resolution. One service tagged twice
with the same alias stays silent. Sharing an alias will throw an exception
in 5.0.0. The passes share that code through an internal trait, which also
drops three PHPStan baseline entries.

Reported by Anthropic's OSS Scanner (ANT-2026-635NQ9DS).
@Spomky Spomky added this to the 4.3.0 milestone Nov 8, 2026
@Spomky Spomky added the bug label Nov 8, 2026
@Spomky
Spomky merged commit dd1b645 into 4.3.x Nov 8, 2026
18 checks passed
@Spomky
Spomky deleted the fix/bundle-tag-alias-collision branch November 8, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundle: an application service tagged with an alias the bundle already uses is replaced by the bundle's service

1 participant