Mitigation Strategy: Strict Contextual Escaping (SCE) and Sanitization (AngularJS-Specific)
-
Description:
- Identify all AngularJS interpolation points: Locate all instances in your templates using double curly braces
{{ }}
or AngularJS directives likeng-bind
,ng-bind-html
,ng-include
,ng-src
,ng-href
, etc. - Categorize data sources: Determine if the data is user input, API data, or hardcoded.
- Apply appropriate
$sce
methods:- Use
$sce.trustAsHtml()
,$sce.trustAsUrl()
,$sce.trustAsJs()
only when absolutely necessary and with thoroughly validated, trusted input. Prioritize built-in directives. - Prefer
ng-bind
(for plain text) orng-bind-html
(for HTML, with sanitization) over direct interpolation or DOM manipulation.
- Use
- Implement a sanitization library (DOMPurify) within an AngularJS service or filter:
- Create an AngularJS filter:
angular.module('myApp').filter('sanitizeHtml', ['$sce', function($sce) { return function(input) { return $sce.trustAsHtml(DOMPurify.sanitize(input)); }; }]);
- Use the filter with
ng-bind-html
:<div ng-bind-html="myUntrustedHtml | sanitizeHtml"></div>
- Create an AngularJS filter:
- Configure a whitelist within the AngularJS filter: Configure DOMPurify to allow only specific HTML tags and attributes. Example:
DOMPurify.sanitize(input, { ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p', 'br'], ALLOWED_ATTR: ['href', 'title'] });
- Regularly review AngularJS code: Periodically review all uses of
$sce
and sanitization within your AngularJS components and services. - Avoid
ng-bind-html-unsafe
: Remove or refactor any instances of this directive within your AngularJS application.
- Identify all AngularJS interpolation points: Locate all instances in your templates using double curly braces
-
Threats Mitigated:
- Cross-Site Scripting (XSS) via AngularJS Expressions (CSTI): Severity: High. Exploits AngularJS's expression evaluation to inject malicious scripts.
- AngularJS Sandbox Escapes (related to XSS): Severity: High. Bypasses the AngularJS sandbox to execute arbitrary code.
-
Impact:
- XSS: Reduces XSS risk significantly (High to Low/Medium).
- Sandbox Escapes: Reduces impact of escapes (High to Medium).
-
Currently Implemented: [Example: Implemented in the AngularJS
productDetail
component using thesanitizeHtml
filter.$sce
is used correctly in the AngularJSuserProfile
service.] -
Missing Implementation: [Example: Missing in the AngularJS
blogComments
component. Also missing in the AngularJSadminDashboard
where API data is rendered without sanitization.]
Mitigation Strategy: Avoid Dynamic Template Compilation with User Input (AngularJS $compile
)
-
Description:
- Identify uses of
$compile
in AngularJS code: Search your AngularJS codebase for all instances of$compile
. - Analyze the template source: Determine where the template string passed to
$compile
originates. - Refactor if necessary (AngularJS-specific):
- If the template string includes any user input, refactor the AngularJS code to avoid
$compile
. - Use AngularJS directives, components, or
ng-include
with static templates instead. - If dynamic templates are essential, ensure the template structure is hardcoded and only data within the template is dynamic (and properly sanitized using AngularJS's
$sce
and a sanitizer).
- If the template string includes any user input, refactor the AngularJS code to avoid
- Identify uses of
-
Threats Mitigated:
- Cross-Site Scripting (XSS) via AngularJS Expressions (CSTI): Severity: High. Dynamic template compilation with user input is extremely dangerous.
- AngularJS Sandbox Escapes: Severity: High. Increases the attack surface.
-
Impact:
- XSS: Eliminates risk from this vector (High to None).
- Sandbox Escapes: Reduces likelihood (High to Medium).
-
Currently Implemented: [Example: Removed all uses of
$compile
with user input in AngularJS code.$compile
is only used for a trusted internal AngularJS template.] -
Missing Implementation: [Example: None. Policy against using
$compile
with user input in AngularJS.]
Mitigation Strategy: Prototype Pollution Prevention (AngularJS-Specific Considerations)
-
Description:
- Identify Object Modification in AngularJS: Locate areas where objects are created, modified, or merged, especially involving user input or data binding in AngularJS.
- Freeze/Seal Critical AngularJS Objects: Use
Object.freeze()
orObject.seal()
on objects that should not be tampered with, particularly after they are passed to AngularJS's data binding system. - Validate and Sanitize Input (AngularJS Context): Before using user input to create or modify objects that will be used with AngularJS's data binding, validate and sanitize.
- Safe Object Merging (AngularJS-Specific):
- Avoid using
angular.extend()
orangular.copy()
directly with untrusted data that will be used in AngularJS's data binding. - If merging is necessary within AngularJS, use a safe deep-merge function or implement a custom deep-copy function that explicitly avoids modifying the prototype.
- Avoid using
- Review Third-Party AngularJS Libraries: Examine any third-party AngularJS libraries for potential prototype pollution vulnerabilities.
-
Threats Mitigated:
- Prototype Pollution: Severity: Medium to High. Can lead to DoS, unexpected behavior, or potentially RCE.
-
Impact:
- Prototype Pollution: Significantly reduces risk (Medium/High to Low).
-
Currently Implemented: [Example:
Object.freeze()
is used on the AngularJS application configuration object. We use a custom deep-copy function.] -
Missing Implementation: [Example: Review the AngularJS
userData
service, where user profile data is merged. Implement a safe merge function.]
Mitigation Strategy: Denial of Service (DoS) Prevention via Digest Cycle Optimization (AngularJS-Specific)
-
Description:
- Profile your AngularJS application: Use Batarang or browser tools to identify digest cycle bottlenecks.
- Minimize AngularJS watchers:
- Use one-time binding (
::
) for data that doesn't change. - Avoid watchers on
$rootScope
whenever possible. - Use
track by
withng-repeat
in AngularJS templates. - Consolidate AngularJS watchers.
- Use one-time binding (
- Debounce/Throttle user input (AngularJS context):
- Use Lodash/Underscore (or similar) to debounce/throttle functions triggered by frequent user events that interact with AngularJS's scope.
- Optimize
ng-repeat
(AngularJS-Specific):- Use
track by
to help AngularJS efficiently identify changes. - Avoid complex expressions within
ng-repeat
. - Consider pagination/infinite scrolling for large lists managed by AngularJS.
- Use
-
Threats Mitigated:
- Denial of Service (DoS) via Digest Cycle Manipulation: Severity: Medium. Attackers can trigger excessive AngularJS digest cycles.
-
Impact:
- DoS: Reduces likelihood of DoS (Medium to Low).
-
Currently Implemented: [Example: We use one-time binding.
track by
is used in all AngularJSng-repeat
directives.] -
Missing Implementation: [Example: Implement debouncing on the search input in the AngularJS
productSearch
component. Review watchers in the AngularJSorderHistory
component.]
Mitigation Strategy: Secure use of $http
and $resource
(AngularJS-Specific)
-
Description:
- Input Validation (AngularJS Context): Validate all data received from the server before using it within AngularJS's scope or templates.
- Sanitization (AngularJS Context): Sanitize any HTML or JavaScript received from the server before rendering it, using AngularJS's
$sce
and a sanitizer like DOMPurify (as described in the first mitigation strategy). - Correct HTTP Methods: Use appropriate HTTP methods (GET, POST, PUT, DELETE) as intended.
- CSRF Protection (AngularJS-Specific):
- Ensure AngularJS's built-in CSRF protection is enabled. Configure
$httpProvider.defaults.xsrfCookieName
and$httpProvider.defaults.xsrfHeaderName
. - The server should generate a CSRF token and include it in a cookie.
- AngularJS will automatically include this token in request headers.
- The server should validate the token.
- Ensure AngularJS's built-in CSRF protection is enabled. Configure
- Avoid JSONP (AngularJS Context): Avoid using JSONP with AngularJS if possible. If necessary, ensure the source is absolutely trusted and validate the response carefully. Consider CORS instead.
-
Threats Mitigated:
- Cross-Site Scripting (XSS): Severity: High.
- Cross-Site Request Forgery (CSRF): Severity: High.
-
Impact:
- XSS: Reduces XSS risk (High to Low/Medium).
- CSRF: Eliminates CSRF risk (High to None).
-
Currently Implemented: [Example: CSRF protection is enabled and configured in AngularJS. We validate data types on the server.]
-
Missing Implementation: [Example: Implement client-side sanitization of server responses in the AngularJS
blogComments
component. Review JSONP use in the AngularJSlegacyData
service.]