-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix possible XSS attack in development through SSR. (#40152)
Escape the content of the strings so that it can be safely inserted into a comment node. The issue is that HTML does not specify any way to escape comment end text inside the comment. `<!-- The way you close a comment is with "-->". -->`. Above the `"-->"` is meant to be text not an end to the comment. This can be created programmatically through DOM APIs. ``` div.innerHTML = div.innerHTML ``` One would expect that the above code would be safe to do, but it turns out that because comment text is not escaped, the comment may contain text which will prematurely close the comment opening up the application for XSS attack. (In SSR we programmatically create comment nodes which may contain such text and expect them to be safe.) This function escapes the comment text by looking for the closing char sequence `-->` and replace it with `-_-_>` where the `_` is a zero width space `\u200B`. The result is that if a comment contains `-->` text it will render normally but it will not cause the HTML parser to close the comment. PR Close #40152
- Loading branch information
Showing
5 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
const END_COMMENT = /-->/g; | ||
const END_COMMENT_ESCAPED = '-\u200B-\u200B>'; | ||
|
||
/** | ||
* Escape the content of the strings so that it can be safely inserted into a comment node. | ||
* | ||
* The issue is that HTML does not specify any way to escape comment end text inside the comment. | ||
* `<!-- The way you close a comment is with "-->". -->`. Above the `"-->"` is meant to be text not | ||
* an end to the comment. This can be created programmatically through DOM APIs. | ||
* | ||
* ``` | ||
* div.innerHTML = div.innerHTML | ||
* ``` | ||
* | ||
* One would expect that the above code would be safe to do, but it turns out that because comment | ||
* text is not escaped, the comment may contain text which will prematurely close the comment | ||
* opening up the application for XSS attack. (In SSR we programmatically create comment nodes which | ||
* may contain such text and expect them to be safe.) | ||
* | ||
* This function escapes the comment text by looking for the closing char sequence `-->` and replace | ||
* it with `-_-_>` where the `_` is a zero width space `\u200B`. The result is that if a comment | ||
* contains `-->` text it will render normally but it will not cause the HTML parser to close the | ||
* comment. | ||
* | ||
* @param value text to make safe for comment node by escaping the comment close character sequence | ||
*/ | ||
export function escapeCommentText(value: string): string { | ||
return value.replace(END_COMMENT, END_COMMENT_ESCAPED); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Component} from '@angular/core'; | ||
import {TestBed} from '@angular/core/testing'; | ||
|
||
|
||
describe('comment node text escaping', () => { | ||
it('should not be possible to do XSS through comment reflect data', () => { | ||
@Component({template: `<div><span *ngIf="xssValue"></span><div>`}) | ||
class XSSComp { | ||
xssValue: string = '--> --><script>"evil"</script>'; | ||
} | ||
|
||
TestBed.configureTestingModule({declarations: [XSSComp]}); | ||
const fixture = TestBed.createComponent(XSSComp); | ||
fixture.detectChanges(); | ||
const div = fixture.nativeElement.querySelector('div') as HTMLElement; | ||
// Serialize into a string to mimic SSR serialization. | ||
const html = div.innerHTML; | ||
// This must be escaped or we have XSS. | ||
expect(html).not.toContain('--><script'); | ||
// Now parse it back into DOM (from string) | ||
div.innerHTML = html; | ||
// Verify that we did not accidentally deserialize the `<script>` | ||
const script = div.querySelector('script'); | ||
expect(script).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {escapeCommentText} from '@angular/core/src/util/dom'; | ||
|
||
describe('comment node text escaping', () => { | ||
describe('escapeCommentText', () => { | ||
it('should not change anything on basic text', () => { | ||
expect(escapeCommentText('text')).toEqual('text'); | ||
}); | ||
|
||
it('should escape end marker', () => { | ||
expect(escapeCommentText('before-->after')).toEqual('before-\u200b-\u200b>after'); | ||
}); | ||
|
||
it('should escape multiple markers', () => { | ||
expect(escapeCommentText('before-->inline-->after')) | ||
.toEqual('before-\u200b-\u200b>inline-\u200b-\u200b>after'); | ||
}); | ||
}); | ||
}); |