-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Xss fix #40136
Closed
Closed
Xss fix #40136
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
AndrewKushnir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OOC, why use two
\u200B
s? Wouldn't--\u200B>
also work?