10000 Revert "fix(core): fix possible XSS attack in development through SSR… · angular/angular@c64a56f · GitHub
[go: up one dir, main page]

Skip to content

Commit c64a56f

Browse files
Revert "fix(core): fix possible XSS attack in development through SSR (#40525)" (#40533)
This reverts commit bb3b315. Reason for Revert: Issues with Google3 TAP Failures PR Close #40533
1 parent bb3b315 commit c64a56f

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

packages/core/src/util/dom.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
const END_COMMENT = /(<|>)/g;
10-
const END_COMMENT_ESCAPED = '\u200B$1\u200B';
9+
const END_COMMENT = /-->/g;
10+
const END_COMMENT_ESCAPED = '-\u200B-\u200B>';
1111

1212
/**
1313
* Escape the content of the strings so that it can be safely inserted into a comment node.
1414
*
1515
* The issue is that HTML does not specify any way to escape comment end text inside the comment.
16-
* Consider: `<!-- The way you close a comment is with ">", and "->" at the beginning or by "-->" or
17-
* "--!>" at the end. -->`. Above the `"-->"` is meant to be text not an end to the comment. This
18-
* can be created programmatically through DOM APIs. (`<!--` are also disallowed.)
19-
*
20-
* see: https://html.spec.whatwg.org/multipage/syntax.html#comments
16+
* `<!-- The way you close a comment is with "-->". -->`. Above the `"-->"` is meant to be text not
17+
* an end to the comment. This can be created programmatically through DOM APIs.
2118
*
2219
* ```
2320
* div.innerHTML = div.innerHTML
@@ -29,7 +26,7 @@ const END_COMMENT_ESCAPED = '\u200B$1\u200B';
2926
* may contain such text and expect them to be safe.)
3027
*
3128
* This function escapes the comment text by looking for the closing char sequence `-->` and replace
32-
* it with `--_>_` where the `_` is a zero width space `\u200B`. The result is that if a comment
29+
* it with `-_-_>` where the `_` is a zero width space `\u200B`. The result is that if a comment
3330
* contains `-->` text it will render normally but it will not cause the HTML parser to close the
3431
* comment.
3532
*

packages/core/test/acceptance/security_spec.ts

+18-27
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,24 @@ import {TestBed} from '@angular/core/testing';
1111

1212

1313
describe('comment node text escaping', () => {
14-
// see: https://html.spec.whatwg.org/multipage/syntax.html#comments
15-
['>', // self closing
16-
'-->', // standard closing
17-
'--!>', // alternate closing
18-
'<!-- -->', // embedded comment.
19-
].forEach((xssValue) => {
20-
it('should not be possible to do XSS through comment reflect data when writing: ' + xssValue,
21-
() => {
22-
@Component({template: `<div><span *ngIf="xssValue"></span><div>`})
23-
class XSSComp {
24-
// ngIf serializes the `xssValue` into a comment for debugging purposes.
25-
xssValue: string = xssValue + '<script>"evil"</script>';
26-
}
14+
it('should not be possible to do XSS through comment reflect data', () => {
15+
@Component({template: `<div><span *ngIf="xssValue"></span><div>`})
16+
class XSSComp {
17+
xssValue: string = '--> --><script>"evil"</script>';
18+
}
2719

28-
TestBed.configureTestingModule({declarations: [XSSComp]});
29-
const fixture = TestBed.createComponent(XSSComp);
30-
fixture.detectChanges();
31-
const div = fixture.nativeElement.querySelector('div') as HTMLElement;
32-
// Serialize into a string to mimic SSR serialization.
33-
const html = div.innerHTML;
34-
// This must be escaped or we have XSS.
35-
expect(html).not.toContain('--><script');
36-
// Now parse it back into DOM (from string)
37-
div.innerHTML = html;
38-
// Verify that we did not accidentally deserialize the `<script>`
39-
const script = div.querySelector('script');
40-
expect(script).toBeFalsy();
41-
});
20+
TestBed.configureTestingModule({declarations: [XSSComp]});
21+
const fixture = TestBed.createComponent(XSSComp);
22+
fixture.detectChanges();
23+
const div = fixture.nativeElement.querySelector('div') as HTMLElement;
24+
// Serialize into a string to mimic SSR serialization.
25+
const html = div.innerHTML;
26+
// This must be escaped or we have XSS.
27+
expect(html).not.toContain('--><script');
28+
// Now parse it back into DOM (from string)
29+
div.innerHTML = html;
30+
// Verify that we did not accidentally deserialize the `<script>`
31+
const script = div.querySelector('script');
32+
expect(script).toBeFalsy();
4233
});
4334
});

packages/core/test/util/dom_spec.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@ describe('comment node text escaping', () => {
1414
expect(escapeCommentText('text')).toEqual('text');
1515
});
1616

17-
it('should escape "<" or ">"', () => {
18-
expect(escapeCommentText('<')).toEqual('\u200b<\u200b');
19-
expect(escapeCommentText('<<')).toEqual('\u200b<\u200b\u200b<\u200b');
20-
expect(escapeCommentText('>')).toEqual('\u200b>\u200b');
21-
expect(escapeCommentText('>>')).toEqual('\u200b>\u200b\u200b>\u200b');
22-
});
23-
2417
it('should escape end marker', () => {
25-
expect(escapeCommentText('before-->after')).toEqual('before--\u200b>\u200bafter');
18+
expect(escapeCommentText('before-->after')).toEqual('before-\u200b-\u200b>after');
2619
});
2720

2821
it('should escape multiple markers', () => {
2922
expect(escapeCommentText('before-->inline-->after'))
30-
.toEqual('before--\u200b>\u200binline--\u200b>\u200bafter');
23+
.toEqual('before-\u200b-\u200b>inline-\u200b-\u200b>after');
3124
});
3225
});
3326
});

0 commit comments

Comments
 (0)
0