Skip to content

Escape APP_ID for ViewEncapsulation.Emulated #63251

Closed

Description

@dgp1130

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

APP_ID is not escaped despite being included in a CSS selector, meaning if it includes special syntax known to CSS it can confuse the selector.

For example, providing:

{
  provide: APP_ID,
  useValue: 'foo:bar',
}

Generates the following DOM:

<h1 _ngcontent-foo:bar-c2846697727="" class="foo">Hello from Angular!</h1>

Which is actually ok, : is valid in this context. But the CSS is not:

.foo[_ngcontent-foo:bar-c2846697727] { /* ... */ }

This is invalid because : triggers a pseudo-class in the middle of an attribute selector. Ideally, we should escape this with CSS.escape, though a similar problem exists with the attribute name itself. For example, foo bar triggers

InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': '_nghost-foo bar-c2846697727' is not a valid attribute name.

I'm not sure you really can escape a space from an attribute name, so we might just have to ban that (and any other invalid characters such as >) altogether.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-74rnmosz?description=An%20angular-cli%20project%20based%20on%20@angular/animations,%20@angular/common,%20@angular/compiler,%20@angular/core,%20@angular/forms,%20@angular/platform-browser,%20@angular/platform-browser-dynamic,%20@angular/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&file=src%2Fmain.ts&template=node&title=Angular%20Starter

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)


Anything else?

No response

Activity

  1. added a commit that references this issue on Aug 19, 2025
    7abe5e3
  2. JeanMeche commented on Aug 19, 2025

    @JeanMeche
    Member

    CSS.escape is also unsafe to use because of the non-browser environments where the render also runs.
    What alternative do we have ?

    Should we maybe throw an error in devmode so a more "complex" check doesn't impact prod builds ? Or Is APP_ID the kind of token that are often set depending on envs ?

  3. alan-agius4 commented on Aug 20, 2025

    @alan-agius4
    Contributor

    Indeed, CSS.escape and CSS.unescape cannot be used in this context. We could probably just allow alphanumeric characters, _ and -.

    The APP_ID token is used when having multiple applications running on the same page so that CSS is not leaked between an application and another.

  4. added
    area: core
    on Aug 20, 2025
  5. added this to the needsTriage milestone on Aug 20, 2025
  6. added 3 commits that reference this issue on Sep 16, 2025
    bf28d36
    dad815b
    2a2647e
  7. JeanMeche commented on Sep 19, 2025

    @JeanMeche
    Member
  8. angular-automatic-lock-bot commented on Oct 20, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corebugcore: stylesheets

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions