Mitigation Strategy: Enforce Strict Contextual Escaping (SCE)
-
Description:
- Verify SCE is Enabled: In your AngularJS application configuration (typically in your main app module definition), ensure that SCE is enabled by default. AngularJS enables it by default, but explicitly check for any code that might disable it. Look for configurations like
$sceProvider.enabled(false);
and ensure they are not present or are justified and understood. - Use Safe Contexts: Primarily use AngularJS directives like
ng-bind
,{{ }}
(with SCE enabled),ng-src
,ng-href
, etc., which automatically apply contextual escaping based on the context where data is being rendered (HTML, URL, JavaScript, CSS). Developers should consistently choose these directives over manual DOM manipulation. - Review
$sce.trustAs*
Usage: Conduct a code review specifically targeting AngularJS components to identify all instances where$sce.trustAsHtml
,$sce.trustAsJs
,$sce.trustAsUrl
,$sce.trustAsResourceUrl
or similar$sce.trustAs*
functions are used within AngularJS code. - Justify and Secure
$sce.trustAs*
Usage: For each instance of$sce.trustAs*
usage within AngularJS components, rigorously justify its necessity. If justified, ensure the data being passed to these functions is meticulously validated and sanitized before being trusted, ideally within the AngularJS component or a closely related service. - Minimize
$sce.trustAs*
Usage: Refactor AngularJS components to minimize or eliminate the need for$sce.trustAs*
functions wherever possible. Explore alternative approaches that leverage AngularJS's built-in safe contexts and data binding mechanisms within the AngularJS framework.
- Verify SCE is Enabled: In your AngularJS application configuration (typically in your main app module definition), ensure that SCE is enabled by default. AngularJS enables it by default, but explicitly check for any code that might disable it. Look for configurations like
-
Threats Mitigated:
- Client-Side Template Injection (CSTI) - High Severity: SCE directly prevents the browser from executing arbitrary code injected into AngularJS templates by treating user-provided data as plain text unless explicitly marked as trusted in a specific context by AngularJS.
- Cross-Site Scripting (XSS) - High Severity: By escaping HTML entities within AngularJS templates, SCE prevents malicious scripts embedded in data from being executed in the user's browser within the AngularJS application context.
-
Impact:
- CSTI - Significantly Reduces Risk: Enforcing SCE is the primary AngularJS-specific defense against CSTI. When properly implemented within AngularJS components, it effectively neutralizes this threat within the client-side framework.
- XSS - Significantly Reduces Risk: SCE provides a strong layer of defense against many common XSS vectors arising from AngularJS template rendering and data binding.
-
Currently Implemented:
- Partially Implemented: SCE is enabled by default in the application configuration. Directives like
ng-bind
and{{ }}
are generally used in newer AngularJS templates.
- Partially Implemented: SCE is enabled by default in the application configuration. Directives like
-
Missing Implementation:
- Complete Review of AngularJS
$sce.trustAs*
Usage: A comprehensive code review is needed specifically within AngularJS components to identify and justify all uses of$sce.trustAs*
functions. Older AngularJS components might be using$sce.trustAsHtml
without proper justification or sanitization within the AngularJS codebase. - AngularJS Specific Documentation and Developer Training: Developers need to be explicitly trained on the importance of SCE within the AngularJS context and best practices for using it in AngularJS components, including avoiding unnecessary
$sce.trustAs*
usage within AngularJS code.
- Complete Review of AngularJS
Mitigation Strategy: Utilize AngularJS's Built-in Security Features
-
Description:
- Prioritize Safe Directives: Developers should consistently prioritize using AngularJS's built-in directives like
ng-bind
,{{ }}
(with SCE),ng-src
,ng-href
,ng-style
,ng-class
, etc., for data binding and rendering within AngularJS templates. These directives are designed to automatically handle contextual escaping and prevent common XSS vulnerabilities within the AngularJS framework. - Avoid Manual DOM Manipulation in AngularJS: Discourage or strictly control the use of manual DOM manipulation methods within AngularJS controllers and directives, such as
element.innerHTML
,document.write
, or jQuery's.html()
when dealing with user-provided data or dynamic content. These methods can easily bypass AngularJS's built-in security features and introduce vulnerabilities. - Leverage AngularJS Form Controls: Utilize AngularJS form controls (e.g.,
<input ng-model>
,<textarea ng-model>
) for handling user input within forms. AngularJS form controls, when used with data binding, benefit from SCE and are generally safer than manually constructing form elements and handling input values. - Secure AngularJS Routing: When implementing routing in AngularJS applications, ensure that route parameters and URL segments are handled securely and are not directly interpolated into templates without proper escaping. Use AngularJS's routing mechanisms and avoid constructing URLs manually with user-controlled data within AngularJS components.
- Prioritize Safe Directives: Developers should consistently prioritize using AngularJS's built-in directives like
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - High Severity: Utilizing AngularJS's built-in security features, especially safe directives, directly mitigates XSS vulnerabilities that can arise from improper data rendering within AngularJS templates.
- Client-Side Template Injection (CSTI) - Medium Severity: While SCE is the primary defense against CSTI, using safe directives reinforces this defense by ensuring data is rendered in the intended context and not interpreted as executable code by AngularJS.
-
Impact:
- XSS - Significantly Reduces Risk: Consistently using AngularJS's safe directives and avoiding manual DOM manipulation within AngularJS components significantly reduces the risk of XSS vulnerabilities within the client-side application.
- CSTI - Partially Reduces Risk: Using safe directives contributes to a more secure AngularJS application and reduces the attack surface for CSTI, working in conjunction with SCE.
-
Currently Implemented:
- Partially Implemented: Newer AngularJS components generally utilize safe directives. However, older components or quick fixes might still rely on manual DOM manipulation in some areas.
-
Missing Implementation:
- Code Review for Manual DOM Manipulation in AngularJS: Conduct a targeted code review of AngularJS components to identify and refactor instances of manual DOM manipulation, replacing them with AngularJS's safe directives and data binding mechanisms.
- AngularJS Secure Coding Guidelines: Establish and enforce AngularJS-specific secure coding guidelines that emphasize the use of safe directives and discourage manual DOM manipulation within AngularJS components.
- Developer Training on AngularJS Security Best Practices: Provide focused training to developers on AngularJS-specific security best practices, highlighting the importance of utilizing built-in security features and avoiding insecure patterns within the AngularJS framework.
Mitigation Strategy: Avoid $sce.trustAsHtml
and Similar Functions with User-Controlled Data (AngularJS Context)
-
Description:
- AngularJS Code Policy: Establish a strict coding policy specifically within the AngularJS development team that explicitly forbids passing user-controlled data directly to
$sce.trustAsHtml
,$sce.trustAsJs
,$sce.trustAsUrl
,$sce.trustAsResourceUrl
, or any similar$sce.trustAs*
functions within AngularJS components and services. - AngularJS Code Review for Violations: Conduct regular code reviews of AngularJS code specifically to actively identify and eliminate any instances where user input is being directly passed to these
$sce.trustAs*
functions within AngularJS components. - Refactor AngularJS Code to Safe Alternatives: When
$sce.trustAs*
is currently used with user-controlled data within AngularJS components, refactor the AngularJS code to use safer alternatives within the framework. This might involve:- Using data binding with directives like
ng-bind
and{{ }}
(with SCE) within AngularJS templates. - Sanitizing user input outside of AngularJS and then using
$sce.trustAsHtml
only on the sanitized output within AngularJS components (with extreme caution and strong justification). - Restructuring AngularJS component logic to avoid the need to trust user-controlled HTML or JavaScript within the AngularJS framework.
- Using data binding with directives like
- AngularJS Developer Training: Educate AngularJS developers specifically on the security risks of using
$sce.trustAs*
with user-controlled data within AngularJS and emphasize the importance of safer AngularJS-specific alternatives and data binding practices.
- AngularJS Code Policy: Establish a strict coding policy specifically within the AngularJS development team that explicitly forbids passing user-controlled data directly to
-
Threats Mitigated:
- Client-Side Template Injection (CSTI) - High Severity: Directly using
$sce.trustAsHtml
within AngularJS components with user input completely bypasses SCE within the AngularJS framework and opens the door to CSTI vulnerabilities within the client-side application. - Cross-Site Scripting (XSS) - High Severity: Similar to CSTI, bypassing SCE through
$sce.trustAsHtml
within AngularJS allows for easy XSS exploitation within the AngularJS application context.
- Client-Side Template Injection (CSTI) - High Severity: Directly using
-
Impact:
- CSTI - Significantly Reduces Risk: Eliminating direct user input to
$sce.trustAs*
functions within AngularJS components is crucial for preventing CSTI vulnerabilities related to SCE bypass within the AngularJS framework. - XSS - Significantly Reduces Risk: This mitigation directly addresses a major avenue for XSS attacks within AngularJS applications by preventing SCE bypass through
$sce.trustAs*
in AngularJS code.
- CSTI - Significantly Reduces Risk: Eliminating direct user input to
-
Currently Implemented:
- Partially Implemented: General awareness among senior AngularJS developers about the risks of
$sce.trustAsHtml
within AngularJS components. However, no formal AngularJS-specific policy or systematic AngularJS code review process is in place.
- Partially Implemented: General awareness among senior AngularJS developers about the risks of
-
Missing Implementation:
- Formal AngularJS Policy and Guidelines: Establish a clear policy specifically for AngularJS development prohibiting direct user input to
$sce.trustAs*
functions within AngularJS code and document AngularJS-specific secure coding guidelines for developers. - Automated AngularJS Code Analysis: Integrate static code analysis tools specifically configured for AngularJS code into the development pipeline to automatically detect potential violations of this policy within AngularJS components (e.g., using linters or security-focused code scanners that understand AngularJS patterns).
- Retroactive AngularJS Code Review and Refactoring: Conduct a dedicated code review of existing AngularJS code to identify and refactor existing instances where
$sce.trustAs*
is used with user-controlled data within AngularJS components.
- Formal AngularJS Policy and Guidelines: Establish a clear policy specifically for AngularJS development prohibiting direct user input to