Security rules
NGB053: Runtime template compilation from dynamic input
Reports Angular components whose template is not a compile-time constant: @Component({ template }) decorators and Component({ template })(class {}) calls evaluated at runtime, and ɵcompileComponent(type, { template }).
| Property | Value |
|---|---|
| Rule ID | NGB053 |
| Category | Angular security & XSS |
| Default severity | high |
| CWE | CWE-1336, CWE-79, CWE-94 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports Angular components whose template is not a compile-time constant: @Component({ template }) decorators and Component({ template })(class {}) calls evaluated at runtime, and ɵcompileComponent(type, { template }). Templates traced to an untrusted source are reported with the trace (high / critical); other dynamic templates with low confidence.
Why it matters
A component template is code, not data. With the JIT compiler in the bundle, a template built from user input lets the attacker write Angular template syntax (event bindings that call component methods, structural directives, interpolation into attributes) and raw HTML that Angular never sanitizes because it trusts its own templates. Angular's documentation is explicit: never generate templates from user data.
Detection behavior and supported patterns
@Component({ template: value })/Component({ template: value })(…)wherevalueis not a constant string (literals,constvariables and constant template literals are constant)ɵcompileComponent(type, { template: value })- Untrusted origin traced through the data-flow engine (URL, messages, HTTP responses, storage, user input) → high, critical for URL / message sources
Example
const Dynamic = Component({ selector: 'app-dyn', template: this.route.snapshot.queryParams['layout'] })(class {});Safer alternative
@Component({ selector: 'app-dyn', template: '<p>{{ layout }}</p>' })export class Dynamic { layout = this.route.snapshot.queryParams['layout']; }Remediation
Keep templates static. Render user-provided content through data bindings ({{ text }}, [innerHTML] with Angular's sanitizer) or choose among a fixed set of precompiled components (NgComponentOutlet, ViewContainerRef.createComponent).
False-positive considerations
- Templates assembled at runtime from constant fragments that the engine cannot prove constant (e.g. selected by a lookup) are reported with low confidence.
Known limitations
- Templates compiled by third-party dynamic-component libraries are not visible.
- Dynamic templates are only exploitable when the JIT compiler is shipped; the rule does not check the build configuration.
References
Run it
ngbastion explain NGB053