Skip to content

Commit 05101af

Browse files
committed
Merge branch 'fix/group-description-escaped-2954' into 'master'
Fix escaping in group descriptions #2954 See merge request minds/front!1736
2 parents 48ea441 + 00e3109 commit 05101af

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { DecodeHtmlStringPipe } from './decode-html-string';
3+
4+
describe('DecodeHtmlStringPipe', () => {
5+
beforeEach(() => {
6+
TestBed.configureTestingModule({
7+
declarations: [DecodeHtmlStringPipe],
8+
});
9+
});
10+
11+
it('it should initialize', () => {
12+
expect(true).toBeTruthy();
13+
});
14+
15+
it('should transform html entity quotes into raw quotes', () => {
16+
const pipe = new DecodeHtmlStringPipe();
17+
expect(pipe.transform('"hello"')).toEqual('"hello"');
18+
});
19+
20+
it('should transform html entity ampersand into its symbol', () => {
21+
const pipe = new DecodeHtmlStringPipe();
22+
expect(pipe.transform('&hello')).toEqual('&hello');
23+
});
24+
25+
it('should transform html entity less than into its symbol', () => {
26+
const pipe = new DecodeHtmlStringPipe();
27+
expect(pipe.transform('&lt;hello')).toEqual('<hello');
28+
});
29+
30+
it('should transform html entity greater than into its symbol', () => {
31+
const pipe = new DecodeHtmlStringPipe();
32+
expect(pipe.transform('&gt;hello')).toEqual('>hello');
33+
});
34+
35+
it('should remove HTML tags', () => {
36+
const pipe = new DecodeHtmlStringPipe();
37+
expect(pipe.transform('<img><div>')).toEqual('');
38+
expect(pipe.transform('<IMG SRC="javascript:alert(\'XSS\');">')).toEqual(
39+
''
40+
);
41+
});
42+
43+
it('should transform many html entities into raw quotes', () => {
44+
const pipe = new DecodeHtmlStringPipe();
45+
expect(pipe.transform('&amp;hello&gt;&lt;this&quot;')).toEqual(
46+
'&hello><this"'
47+
);
48+
});
49+
50+
it('should not transform text with no html entities', () => {
51+
const pipe = new DecodeHtmlStringPipe();
52+
const str = '& this is "text"';
53+
expect(pipe.transform(str)).toEqual(str);
54+
});
55+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
/**
4+
* Decodes a string with HTML entities.
5+
* Idea taken from https://stackoverflow.com/a/34064434
6+
*/
7+
@Pipe({ name: 'decodeHtmlString' })
8+
export class DecodeHtmlStringPipe implements PipeTransform {
9+
/**
10+
* Transforms a string such that HTML entities are decoded.
11+
* @param { string } value - string to decode.
12+
* @returns { string } - decoded string.
13+
*/
14+
public transform(value: string): string {
15+
const doc = new DOMParser().parseFromString(value, 'text/html');
16+
return doc.documentElement.textContent;
17+
}
18+
}

src/app/common/pipes/pipes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TimediffPipe } from './timediff.pipe';
1212
import { FriendlyDateDiffPipe } from './friendlydatediff';
1313
import { AsyncStatePipe } from './async-state.pipe';
1414
import { FileSizePipe } from './filesize';
15+
import { DecodeHtmlStringPipe } from './decode-html-string';
1516

1617
export const MINDS_PIPES = [
1718
AbbrPipe,
@@ -30,4 +31,5 @@ export const MINDS_PIPES = [
3031
SafeStylePipe,
3132
AsyncStatePipe,
3233
FileSizePipe,
34+
DecodeHtmlStringPipe,
3335
];

src/app/modules/groups/profile/profile.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ <h1>
135135
<textarea
136136
name="briefdescription"
137137
[autoGrow]
138-
[(ngModel)]="group.briefdescription"
138+
[ngModel]="group.briefdescription | decodeHtmlString"
139+
(ngModelChange)="group.briefdescription = $event"
139140
placeholder="Enter a brief description"
140141
i18n-placeholder="@@GROUPS__DESCRIPTION_PLACEHOLDER"
141142
mTextInputAutocomplete

0 commit comments

Comments
 (0)